I have an application that requires the packing of heterogeneous data in to a single structure. For example, a single structure might contain three floats, two integers and a string. I don't know which fields I'll have until runtime, and the key requirement is that the process be extremely fast. I was planning to use an array of void*, which I can cast to the appropriate type when the message reaches its destination, but is there a better way of doing this? Perhaps using Boost?
Perhaps boost_variant will satisfy your needs?
http://www.boost.org/doc/html/variant.html
Could you use the plain old union?
I had the same problem. My solution was to define a interface called Data. This Interface did not provide anything except a virtual destructor. All my data-types now inherit from the Data interface. This allows me to define a vector of Data pointers. When I need them I cast them to the actual type, so that I can use them.
This solution avoids the use of void Pointers, by using a marker class instead.
// Marker interface
class Data {
public:
virtual ~Data()=0;
}
// Own Datatype
class MyDataType: public Data {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With