I am trying to create a class that can map strings to some arbitrary objects or even primitive types. To clarify, I am not talking of one single generic type, but different types, so the map could look something like this:
["speed": 3, "useNormal": false, "normal": Vec3()]
One approach I have attempted was to create a BaseObject
struct and derive other class like IntObject
and FloatObject
from it. This way I was able to create a std::map<std::string, BaseObject*>
. But this approach feels clunky and getters and setters for the map need a switch case for every single type.
Someone suggested to use bytes. Basically this means to serialize and deserialize the object into bytes arrays and therefore the map would look something like this std::map<std::string, byte*>
. But with this approach the big hurdle to overcome is to somehow generate an object of a certain type from a byte array, which I don't know how to do.
Another suggestion was also to use c++17 variants. But that is too new to be widely supported.
I would appreciate any kind of input!