Generic x-dimensional name values

C

Christopher

Let's see if I can explain my goal....

I need to devise a data structure that can contain a name and a data
object of any type (at least built in types)

For example:
name - "Car1" value - "WGNZYH"
name - "Latitude" value - 80.1

I created an class that represents both members as a std::string and
use boost's lexiconal cast along with template params for this class
and it works great for 1 dimension. My co-worker seems to think using
strings is inefficient memory wise, but I don't see an alternative, at
least not without tracking types and using unions, which I do not want
to do. We'll worry about efficiency later...

My problem is on how to represent multidimensional constructs of this
object where the dimension could be any number. At first, we assumed
we would only have 3 at most, but it doesn't seem very robust to me.

I went and made another class that wraps a map of the first. We'll
call that MyObject2D.
I went and made another class that wraps a map of the second. We'll
call that MyObject3D.
You see the problem..


For explanation, here is some sample data:

"car", "WGNYTH"
"wheel", "0901929AB"
"size", 18
"tread", 2
"psi", 35.001
"color", "black"

etc.

I want the user to be able to look up and retrieve values, groups of
values, or groups of groups, etc, by name.
The user should already know what types to expect.

usage like:
MyObject2D wheel = MyObject3D.Get2D("wheel");

int size;
wheel.GetMyObject1D(size);

I don't like having a seperate class for every dimension though. Any
Ideas on how to do something like this another way?
 
T

Thomas J. Gritzan

Christopher said:
Let's see if I can explain my goal....

I need to devise a data structure that can contain a name and a data
object of any type (at least built in types)

For example:
name - "Car1" value - "WGNZYH"
name - "Latitude" value - 80.1
[...]
My problem is on how to represent multidimensional constructs of this
object where the dimension could be any number. At first, we assumed
we would only have 3 at most, but it doesn't seem very robust to me.

I went and made another class that wraps a map of the first. We'll
call that MyObject2D.
I went and made another class that wraps a map of the second. We'll
call that MyObject3D.
You see the problem..

Do you want a multidimensional data structure, or a hierarchical /
recursive data structure?

For the latter, you could make a std::map, where the value type is a
boost::variant (google for it), which can be recursive.
 
C

Christopher

Christopher said:
Let's see if I can explain my goal....
I need to devise a data structure that can contain a name and a data
object of any type (at least built in types)
[..]

Like std::map<std::string, boost::any> ?

V

Almost, except
"Discriminated types that contain values of different types but do not
attempt conversion between them"

I want to be able to convert from any built in type to any other built
in type during sets and gets.
I have that part down though. Storing as strings and using lexical
casting seems to work pretty well.
Its the grouping part I can't wrap my head around.

I need name value pairs
I need named groups of name value pairs
I need named groups, of named groups, of name value pairs
and on and on.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top