473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

index a vector of properties

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

Aug 9 '05 #1
2 1582
Maybe you could use a map instead of the vector, with name as key?

Aug 9 '05 #2
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

Aug 10 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C++ code available to...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
6
by: Joe | last post by:
I have a: vector<string> which contains a few dozen elements. I want to find the index of the element containing a certain string. for example: vector<string> strings;...
0
by: hypernihl | last post by:
Hello, I have two vectors "vector<int> x,y;" I want to sort vector x "sort(x.begin(),x.end());" I want to sort vector y with respect to x. I need the new index of vector x after it has been...
8
by: ALiX | last post by:
Hi all, In my code I use different vectors of the same size to hold some data. At some point I need to iterate through all vectors at the same time. Now, the question is what type should the...
7
by: Plissken.s | last post by:
Hi, How can I find out the index of a vector which has the min element? I know how to find out the * value * of a vector which has the min element. But how can I find out its index? for...
4
by: boheman | last post by:
Hi, I am wondering if there is a simple and quick way to return the indices of sorted vector. for example, I have a vector<intx containing {5, 2, 3, 0, 2}. I can use sort(x.begin(), x.end(),...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
1
by: OriginalCopy | last post by:
This is a demonstrative code which could be used for debugging purposes. And yet I don't know how to insert the necessary data on line 63, purely syntactically speaking ? I'm a beginner with STL, and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.