
On Wed, Aug 26, 2009 at 8:32 PM, Ryan McConnehey
I doubt Boost.Variant is the best choice for this. You say a template function to enforce types, I would say Boost.Enable_if combined with Boost.Type_traits and Boost.MPL would make it real easy.
I'm all for making this easy. So, let me give you some background of what I'm trying to do. I'm reading an xml file that contains a binary format description (lets call the class BFD). This will allow me to specify a customers binary data in such a way as to place the binary data into an object. Each parameter has a type associated with it. This can be a int, short, string, long etc. The class reading the xml file is storing the type information based on the parameter name. Any class trying to retrieve this binary format description will need to specify the parameter name and expected type. If the parameter name exists but the type is incorrect then an empty description is returned.
I need to limit the types that can be requested from the BFD. That's where the mpl::vector of known types comes into play. The function returning the binary description information will check (at compile time) to make sure the type requested is part of the known types. Since the BFD is comparing types I need to convert the string names of these types, in the xml file, into real types. This has posed a problem. I come up with nothing better than a case statement. Once the type is known I then create a binary format description object (this is a template structure). The only reason the structure is a template is to associate a type. This is why I though the boost::variant was necessary. A variant composed of each of the types in the mpl::vector was defined as the second parameter of a map with the key as the parameter name from the xml file. The function returning the binary description information will check the type of the data stored in the map and if the types match then return the structure.
So with this information in mind how would you use the boost::enable_if, boost::type_traits and boost:mpl.
Ah, I did not have enough information, in that case I would use Boost.Variant as the container with something like Spirit2.1's string table to do a faster type (string->type and parsing) lookup, that would give you excellent speed for that purpose. Something like Boost.Fusion's map would also work as well, but which is better depends on how much of the work is done at compile-time and how much is done at run-time, I am not fully clear about that, I do not suppose you could show some pseudo-code of what you want?