index a vector of properties

E

eric

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
 
E

eric

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
 

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top