473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

questions about data strcuture

Hi,

I am currently developping a software where items that will be inserted
into a graphical widget ListCtrl are first defined in static array as
shown below :

enum TUiContext
{
EUiContextMain = 0,
EUiContextSchedule,
EUiContextSettings,
EUiContextSubscription,
EUiContextCount
};

typedef struct
{
int TextId;
int ImgId;
int Param;
int nOptInfo;
TCHAR szImgName[MAX_PATH];
} ListInfo_t, *LPListInfo_t;
ListInfo_t CMainView::ms_listInfo_Main[]=
{
// String ID, Img, Param = CmdBarId Enabled ImgName
{ IDS_MENU_BACKUP, 0, IDM_MENU_CMDBAR_BACKUP, TRUE, _T( "" ) },
{ IDS_MENU_SCHEDULE, 2, IDM_MENU_CMDBAR_OPTIONS, TRUE, _T( "" ) },
{ IDS_MENU_RESTORE, 1, IDM_MENU_CMDBAR_RESTORE, TRUE, _T( "" ) },
{ IDS_MENU_MANAGE_SUBSCRIPTION, -1, IDM_MENU_MANAGE_SUBSCRIPTION,
TRUE, _T( "Menu_Account_Manage.png" ) },
};

ListInfo_t CMainView::ms_listInfo_Options[]=
{
// String ID, Img, Param = CmdBarId
{ IDS_MENU_FREQUENCY, 0, IDM_MENU_SCHEDULER, TRUE, _T( "" ) },
{ IDS_MENU_CONTENT, 1, IDM_MENU_SELECTDB, TRUE, _T( "" ) },
};
....
Actually in functions of some parameters, some items won't be inserted
and the field nOptInfo is used for this purpose. If this field equals 1
it will be inserted into the List.

So I have a method called InitResources that check the config parameters
and update nOptInfo for each array.
Once this has been done, I build a vector as shown below :

void CMainView::InitResources()
{
std::vector<ListInfo_tvecListInfo;
std::map<TUiContext, std::vector<ListInfo_t listMap;

// Test config parameters and update nOptInfo
...

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Now build vector from ms_listInfo_Main
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
vecListInfo.clear()
for (int i = 0; i < _countof(ms_listInfo_Main); i++)
{
if (ms_listInfo_Main[i].nOptInfo == TRUE){
vecListInfo.push_back(ms_listInfo_Main[i]);
}
}
listMap[ EUiContextMain ] = vecListInfo;

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Now build vector from ms_listInfo_Options
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
vecListInfo.clear()
for (int i = 0; i < _countof(ms_listInfo_Options); i++)
{
if (ms_listInfo_Options[i].nOptInfo == TRUE){
vecListInfo.push_back(ms_listInfo_Options[i]);
}
}
listMap[ EUiContextSchedule] = vecListInfo;
...

}

I find all this code very ugly and I would like to suggestion to improve
it. I am doing all this because I am switching between different
graphical context and before to do it I save the index of current
selected item in my ListCtrl.


Sep 19 '08 #1
2 1228
In article <48***********************@news.free.fr>,
mo****@anonymous.org says...
typedef struct
{
int TextId;
int ImgId;
int Param;
int nOptInfo;
TCHAR szImgName[MAX_PATH];
} ListInfo_t, *LPListInfo_t;
Since you're apparently using nOptInfo as a boolean, I'd advise defining
it as a boolean. Although it's unrelated to any of the questions at
hand, I'd look carefully at whether szImgName couldn't be an std::string
as well.
vecListInfo.clear()
Rather than defining well ahead of time and clearing when used, I'd
define this immediately before use, in which case it'll definitely be
empty.
for (int i = 0; i < _countof(ms_listInfo_Main); i++)
{
if (ms_listInfo_Main[i].nOptInfo == TRUE){
vecListInfo.push_back(ms_listInfo_Main[i]);
}
}
Then I'd replace this with std::remove_copy_if:

std::remove_copy_if(ms_listInfo_Main, end(ms_listInfo_Main), isOpt());

where isOpt looks something like:

struct isOpt {
bool operator()(ListInfo_t const &li) { return !li.nOptInfo; }
};

At least if I understand your code correctly, I'd then create a single
vector containing (pointers to?) the data you currently have in your
ms_ListInfo_*. I'd then turn the bit of code above (the part that uses
std::remove_copy_if) into a small function (which, for now, I'll call
build_list) that could be called in a loop:

for (int i=0; i<EUiContextCount; i++)
listMap[i] = build_list(i, ms_ListInfo);

then I'd note that this is really equivalent to std::transform:

std::transform(ms_ListInfo.begin(), ms_ListInfo.end(),
listMap, build_list());

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '08 #2
John Doe <mo****@anonymous.orgwrites:
Hi,

I am currently developping a software where items that will be
inserted into a graphical widget ListCtrl are first defined in static
array as shown below :

enum TUiContext
{
EUiContextMain = 0,
EUiContextSchedule,
EUiContextSettings,
EUiContextSubscription,
EUiContextCount
};

typedef struct
{
int TextId;
int ImgId;
int Param;
int nOptInfo;
TCHAR szImgName[MAX_PATH];
} ListInfo_t, *LPListInfo_t;
ListInfo_t CMainView::ms_listInfo_Main[]=
{
// String ID, Img, Param = CmdBarId Enabled ImgName
{ IDS_MENU_BACKUP, 0, IDM_MENU_CMDBAR_BACKUP, TRUE, _T( "" ) },
{ IDS_MENU_SCHEDULE, 2, IDM_MENU_CMDBAR_OPTIONS, TRUE, _T( "" ) },
{ IDS_MENU_RESTORE, 1, IDM_MENU_CMDBAR_RESTORE, TRUE, _T( "" ) },
{ IDS_MENU_MANAGE_SUBSCRIPTION, -1,
IDM_MENU_MANAGE_SUBSCRIPTION, TRUE, _T( "Menu_Account_Manage.png"
) },
};

ListInfo_t CMainView::ms_listInfo_Options[]=
{
// String ID, Img, Param = CmdBarId
{ IDS_MENU_FREQUENCY, 0, IDM_MENU_SCHEDULER, TRUE, _T( "" ) },
{ IDS_MENU_CONTENT, 1, IDM_MENU_SELECTDB, TRUE, _T( "" ) },
};
...
Actually in functions of some parameters, some items won't be inserted
and the field nOptInfo is used for this purpose. If this field equals
1 it will be inserted into the List.

So I have a method called InitResources that check the config
parameters and update nOptInfo for each array.
Once this has been done, I build a vector as shown below :

void CMainView::InitResources()
{
std::vector<ListInfo_tvecListInfo;
std::map<TUiContext, std::vector<ListInfo_t listMap;

// Test config parameters and update nOptInfo
...

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Now build vector from ms_listInfo_Main
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
vecListInfo.clear()
for (int i = 0; i < _countof(ms_listInfo_Main); i++)
{
if (ms_listInfo_Main[i].nOptInfo == TRUE){
vecListInfo.push_back(ms_listInfo_Main[i]);
}
}
listMap[ EUiContextMain ] = vecListInfo;

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// Now build vector from ms_listInfo_Options
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
vecListInfo.clear()
for (int i = 0; i < _countof(ms_listInfo_Options); i++)
{
if (ms_listInfo_Options[i].nOptInfo == TRUE){
vecListInfo.push_back(ms_listInfo_Options[i]);
}
}
listMap[ EUiContextSchedule] = vecListInfo;
...

}

I find all this code very ugly
Yes, it is.
and I would like to suggestion to
improve it. I am doing all this because I am switching between
different graphical context and before to do it I save the index of
current selected item in my ListCtrl.
You need more abstraction. Define classes to hold your data, instead
of storing it in POD (plain old data) structures. When you build
these objects, they will be able to implement any consistency check
you need, so you won't have to check the POD and you won't have to
convert.
With smart use of operators, such as operator<< or operator, you can
make it nice enough. Optional attributes can be fed optionnaly, after
the construction.

Since you seem to have here a hierarchical structure of contexts, be
sure to make them have reference semantics, not value semantics,
otherwise putting them in a std container would project them and lose
data.

Instead of using #define for constants or enums, you should use true
const or enum declarations, with the help of namespaces to have nice
names and avoid collisions:

namespace ids {
namespace menu {
enum menu { backup, schedule, restore, manage_subscription }; }}

Notice the duplication of namespace X { enum X which allows to use
the namespace to qualify the enum constants, without having to
reinvent the wheel by prefixing the constants with the enum name to
avoid collisions.

void CMainView::InitResources()
{
MainContext mctxt;
mctxt<<(MInfo(ids::menu::backup, idm::menu::cmdbar::backup) <<Image(0))
<<(MInfo(ids::menu::schedule,idm::menu::cmdbar::op tions) <<Image(2))
<<(MInfo(ids::menu::restore, idm::menu::cmdbar::restore) <<Image(1))
<<(MInfo(ids::menu::manage_subscription,idm::menu: :manage::subscription)
<<ImageName ("Menu_Account_Manage.png"))
<<(MInfo(ids::menu::wild, idm::menu::cmdbar::broken) <<Disabled()
<<Color(ids::color::red)
<<Etc("you can define whatever attribute you want"));

ScheduleContext sctxt;
sctxt<<(SInfo(ids::menu::frequency,idm::menu::sche duler) <<Image (0))
<<(SInfo(ids::menu::content, idm::menu::selectdb) <<Image (1));

std::map<TUiContext,ContextlistMap;

// Test config parameters and update nOptInfo
// ...
listMap[EUiContextMain ]=nctxt;
listMap[EUiContextSchedule]=sctxt;
// ...
}
--
__Pascal Bourguignon__
Sep 19 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
2
by: juvchan | last post by:
Hi all,i am a newbie in C#. I need to use pointer to a class and strcuture that i have defined in C# so that i call pass their memory address to some function called from Win32 dll (Platform Invoke)?...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
1
by: jason | last post by:
Hello everyone, I have some general questions about the DataTable object, and how it works. Moderately new to C#, I have plenty of texts describing the language, but not so much to reference...
0
by: Ivan Demkovitch | last post by:
Hi! This is a long one :-) Please correct me if I *did* something wrong and help me with my questions (at the end) I'm learning C# and .NET but good at SQL. I'm working on portal and...
3
by: Tony | last post by:
Hi, I hope I can find answers to my questions here. 1. How to execute a Private Sub in VB, from a Javascript function event. 2. How to get a JavaScript Var value from the html page and return...
1
by: E.T. Grey | last post by:
Hi All, Despite spending the past six to seven hours perusing the docs on the mySQl site, I still have questions unanswered, and have been unable to get any help. I am familiar with Sybase, some...
16
by: marc_r_bertrand | last post by:
To all asp/db pros: The quiz code below works. But there is a problem when too many questions are answered (radio buttons clicked). I am not an asp pro. So, is there a pro out there or an...
1
by: robertmeyer1 | last post by:
Hi, I have 3 tables set up. tblQuestion, tblAnswer, tblClient. I have them linked together and have a sbf and mainform set up for data entry. The sbf links the questions and answers together. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.