I have to write a library exporting data to various formats. Every format is a type of a given boost::variant
, e.g.
typdef boost::variant<csv, xml, hdf5> TFormat
with formats csv, xml and hdf5. This approach worked quite good for me. The library should be quite versatile and open for possible extensions from the client side. Hence, it would be nice if clients could add user-defined formats in a unified manner.
I would prefer that a I client can add several macros like
REGISTER_FORMAT(binary)
REGISTER_FORMAT(mpeg)
and afterwards my typedef
magically changes to
typedef boost::variant<csv, xml, hdf, binary> TFormat.
I already figured out how to construct a type dynamic boost::variant
using the following mpl-code that luckily compiles with MSVC2010.
#include <boost/variant/variant.hpp>
#include <boost/mpl/vector.hpp>
int main() {
// Adding the type int, to a vector containing the types double and std::string
using namespace boost;
typedef mpl::vector< double, std::string > DefaultTypes;
typedef mpl::push_front< DefaultTypes, int >::type ExtendedTypes;
// typedef TFormat of type boost::variant<double, std::string, int>
typedef boost::make_variant_over< ExtendedTypes >::type TFormat;
return 0;
}
Still I'm not able to implemented the mentioned macro, since it is not clear beforehand how often a client would call REGISTER_FORMAT
or if he would use the macro at all.
Does anyone has an idea how to implement such a macro or something similar?