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
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