Mapping of constants to data members of a class?

Discussion in 'C++' started by Vikas, Sep 9, 2004.

  1. Vikas

    Vikas Guest

    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
     
    Vikas, Sep 9, 2004
    #1
    1. Advertising

  2. (Vikas) wrote in message news:<>...
    > 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

    Open source rating and billing engine for communication networks.
     
    Stephan Br?nnimann, Sep 10, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    8
    Views:
    994
    Jeff Schwab
    Dec 14, 2003
  2. Bret Pehrson
    Replies:
    4
    Views:
    505
    Bret Pehrson
    Dec 9, 2004
  3. hdixon
    Replies:
    3
    Views:
    651
    hdixon
    Jul 9, 2006
  4. mars
    Replies:
    2
    Views:
    289
    Gabriel
    Sep 9, 2005
  5. Jason
    Replies:
    2
    Views:
    523
    Jonathan Mcdougall
    May 13, 2006
Loading...

Share This Page