index a vector of properties

Discussion in 'C++' started by eric, Aug 9, 2005.

  1. eric

    eric Guest

    i need a class whose interface allows the class to be interrogated at
    runtime by a client with no prior knowledge of the implementation.
    i've accomplished this with an abstract base class Object which
    contains a vector of properties, the Property class is declared as

    template < class Name, class Type > class Property {...}

    which the Object class uses like this

    std::vector < Property < std::string, boost::any > > properties_;

    there is a library of classes descended from Object. Each derived
    class always writes the same names and types to its property vector,
    for example an instance of class ObjectCar might say

    properties_.push_back(Property < std::string, boost::any >
    ("color", "blue"));
    properties_.push_back(Property < std::string, boost::any >
    ("numWheels", 4));

    suppose you have a client process which knows about Objects but not
    about ObjectCars. that process could receive an Object* pointing to an
    ObjectCar and the process could interrogate the property vector to find
    the names, types, and values it contains.

    That's the design and it does what I need. The issue is, for a given
    class such as ObjectCar, the 0th element of the vector is always a
    property with name "color", what's an elegant way to fix this
    relationship, given the completely abstract implementation of the base
    class?

    at the moment i say

    #define INDEX_COLOR 0
    #define NAME_COLOR "color"

    So code within ObjectCar, or within a client process which happens to
    know the implementation of ObjectCar, might say

    cout << "name = " << NAME_COLOR << "
    value = " << boost::any_cast< std::string >(properties_[INDEX_COLOR]())
    << endl;

    is there a better way to handle the case where the index and name of an
    item in the property vector are known at compile time?

    thanks,
    eric
     
    eric, Aug 9, 2005
    #1
    1. Advertising

  2. eric

    hacker++ Guest

    Maybe you could use a map instead of the vector, with name as key?
     
    hacker++, Aug 10, 2005
    #2
    1. Advertising

  3. eric

    eric Guest

    thanks very much for getting back to me.

    i agree that
    map < std::string, boost::any >
    represents the problem more directly. thanks for that.

    but i still have the problem that every ObjectCar has
    propertyMap_["color"] = xxx
    so i still have that magic string "color" kicking around. i'd like to
    find a way to formalize the keys in ObjectCar's property map while
    still leaving the base Object class totally abstract.

    any feedback gratefully received.

    regards,
    eric
     
    eric, Aug 10, 2005
    #3
    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. pmatos
    Replies:
    6
    Views:
    24,134
  2. Replies:
    8
    Views:
    2,005
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    627
    James Kanze
    Sep 4, 2007
  4. Rushikesh Joshi
    Replies:
    0
    Views:
    399
    Rushikesh Joshi
    Jul 10, 2004
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    360
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page