0

Whats the best approach to a fast generic data type in C++. I have an event system that needs to be able to pass event information around the program.

sendEvent(123, "a String", aFloat, aVec3, etc); // sendEvent(EventID, EventArg, EventArg ...);

eventData.at(0).asFloat();
eventData.at(1).asInt(); // etc.

Until now I only needed float/int/ptr, and I handled this inside a union. but now I've got a few most structures and this is becoming a little more complex, so I'm revisiting it.

I had a dig through boost::any but this is too slow, there could be alot of data flying around, but this idea seems like the right direction.

I had a rather naive shot with using void* data hold but that got very ugly quickly.

1 Answers1

1

If you have a limited set of supported types, you can try boost::variant. It's more or less a better union. It avoids the heap allocations that boost::any is using and should give maximum performance.

http://www.boost.org/doc/libs/1_56_0/doc/html/variant.html

Example:

// create a variant capable of storing int, float and string
boost::variant<int, float, std::string> eventData;
eventData = 42.f; // store a float
float f = boost::get<float>(eventData); // get the float
Horstling
  • 2,131
  • 12
  • 14