Mapping of constants to data members of a class?

V

Vikas

Hi y'all,

I have an existing GUI application where various windows has columns
which are mapped to data members of various classes. Now, I have to
implement a new functionality to all the classes and I was wondering
if I can somehow map the data member fields to the column ids.

Following code illustrates the question.

// Existing code

// Constants for window columns
const int COL_A 1;
const int COL_B 2;
const int COL_C 3;
....
<const for other columns>

// classes whose member fields holds the data of the columns
class foo
{
public:
string a;
string b;
int c;
...
<other data member corresponding to columns>
};


now at run_time I need to do something like the following:

// New functionality

void bar(int col_id, int val)
{
int field_val = mapping_to_get_value_based_on_col_id();
if (field_val == val)
new_functionality();
}

I am having a hard time to find a generic wasy to do the mapping. I
know, I can have a switch statement for all the classes where based on
col number I get the field value but it will be nice to have a simple,
generic way.

Thanks for your replies.
Vikas
 
S

Stephan Br?nnimann

Hi y'all,

I have an existing GUI application where various windows has columns
which are mapped to data members of various classes. Now, I have to
implement a new functionality to all the classes and I was wondering
if I can somehow map the data member fields to the column ids.

What is the use behind the column ideas?
Something like "virtual QString text ( int column ) const", see
http://doc.trolltech.com/3.3/qlistviewitem.html.
Following code illustrates the question.

// Existing code

// Constants for window columns
const int COL_A 1;
const int COL_B 2;
const int COL_C 3;
...
<const for other columns>

// classes whose member fields holds the data of the columns
class foo
{
public:
string a;
string b;
int c;
...
<other data member corresponding to columns>
};


now at run_time I need to do something like the following:

// New functionality

void bar(int col_id, int val)
{
int field_val = mapping_to_get_value_based_on_col_id();
if (field_val == val)
new_functionality();
}

How to you make use of the argument `col_id'?
What about:

class Base {
public:
// returns true if `col' can be mapped to an integer value,
// `val' is then set to this value, else left unchanged.
virtual bool intval(int col, int& val) const = 0;
};

void bar(const Base& base, int col, int val)
{
int retval;
if (base.intval(col, retval)) {
if (retval == val) new_functionality();
}
}

In this design bar(...) dominates the concrete classes derived from Base.
Eventually you want to give the leaf classes more flexibility, this
however depends on when bar(...) is called.
I am having a hard time to find a generic wasy to do the mapping. I
know, I can have a switch statement for all the classes where based on
col number I get the field value but it will be nice to have a simple,
generic way.

Thanks for your replies.
Vikas

Stephan Brönnimann
(e-mail address removed)
Open source rating and billing engine for communication networks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top