1

Issue about Storeing unknown Data in a Class

How can I provide a class in C++ which is able to store any number of variables each of any type? From inside and outside of the class I would like to read, add, modify and delete these data.

So what I am looking for is maybe a list where the datatypes can vary.

Example how the Usage could look like

Class Object(); Object->Storage->Create("position", "float"); // create Object->Storage->Write("position", 42.0f); // write int Result = Object->Storage->Read("position"); // read Object->Storage->Delete("position"); // delete 

How to do that or why to do it in another Way

My question is if there is a common approach for this problem and what a good solution is. If my thoughts were not correct and there is another technique or place to store data for my aim (next paragraph) please tell me.

Optional Background Information and Context

The reason behind this need is that I am writing a kind of component based game engine. There are Components and Systems. A System could be the title menu or a world level and I can add as many Components to it as I wish. Every game tick, the System calls a specified method over all of its Components. Components are e.g. Physics, Terrain, Draw, Characters, Sound, ... The mentioned unknown data structure is what a Component should be able to store in its System. The Component is nearly static and its state depends on the storage it uses. This Storage system is (together with the event manager) used to communicate between Components.

Edit:

By researches I figured out that what I want is called Data Centered or Data Oriented in software design. Now I have a starting point to do more research on this topic. Thank you Michael Anderson for your answer, probably I will use that.

10
  • Hash Map is the one you want. Commented Oct 17, 2012 at 5:08
  • the problem might be different data types have different length. You'll have to use pointers. Does void* do the trick for you? Commented Oct 17, 2012 at 5:10
  • I am not aware of the void* practice but I will google that later this day. But do you think of my idea as a good approach? Commented Oct 17, 2012 at 5:14
  • 2
    Why not have a parent class of all your components that has an OnTick() method, and then have all of your components inherit from it? Why do you need classes that can hold a bunch of types defined like that when you can just have real child classes with "float position;" and whatnot in them? Commented Oct 17, 2012 at 5:16
  • 1
    There must be some code that understands the data that you stored, knowing it's type, meaning and usage. So there must be some meaning of the property, why don't you can just add a member in some derived class to store them? Why use dynamic. Just because in the end of the day you have to do something on that data, which implies that they have the same interface for you to operate on. This is what you need to think before implement the classes, and just abstract the interface, then derive it for the component specific state. Commented Oct 17, 2012 at 5:27

2 Answers 2

2

You probably want to use boost::any for this kind of thing.

Your code will look like this:

std::map<std::string,boost::any> object; object["position"] = 42.0f; //This will throw if the type is wrong int Result = boost::any_cast<float>(object["position"]); object.erase("position"); 
Sign up to request clarification or add additional context in comments.

Comments

1

Does the Standard Template Library (STL) - more specifically, the use of a map - not work for your needs?

http://en.cppreference.com/w/cpp/container/map

You can use the map to hold values which are either:

1) a "CustomObject", which is a union of all possible variable / object types.

2) a pointer to the actual objects / variables.

4 Comments

I really like maps, but I don't know how I could save data of every type to a map. For example there are eight Components bound to a System and everyone of them stores other data in the System. That could be a mesh, a list of meshes, a pointer to shader, just a bool flag, ... or all of these at the same time.
Here's a quick tutorial on unions: cplusplus.com/forum/beginner/15843, but it looks like a void pointer is easier.
as long as they're all objects, you should be able to distinguish them with instanceof. In all other cases, you'll have to hold the data type separately. This is a limitation of c++
They could be objects of my own classes, integers, strings, lists of anything, and so on.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.