I generated some types with an external tool. I include some of them depending on the application. I tried to play with both boost preprocessor and boost mpl to tag my generated types and build an mpl vector of these tagged types. Unfortunately, my trick only works when I am including the headers I need in a single file with my TypeRegistry.
// TypeRegistry.h
typedef ::boost::mpl::vector<> TypeRegistryRegisteredTypes0;
# define TYPE_REGISTRY_REGISTER_TYPE(T) \
typedef ::boost::mpl::push_back< \
BOOST_PP_CAT( \
TypeRegistryRegisteredTypes, \
BOOST_PP_COUNTER \
), \
##T \
>::type \
BOOST_PP_CAT(TypeRegistryRegisteredTypes, BOOST_PP_ADD(BOOST_PP_COUNTER,1))
# define TYPE_REGISTRY_SYNC_REGISTERED_TYPES() \
typedef BOOST_PP_CAT( \
TypeRegistryRegisteredTypes, \
BOOST_PP_COUNTER \
) TypeRegistryRegisteredTypes
template
<
class RegisteredTypesBase,
class RegisteredTypesVector
>
class TypeRegistry { ... };
This is the macro I use to register a type.
// A.h
class A : public IBase { ... };
TYPE_REGISTRY_REGISTER_TYPE(A);
#include BOOST_PP_UPDATE_COUNTER()
Here we are, in a source file, say main.h
:
#include "TypeRegistry.h"
#include "A.h"
#include "B.h"
#include "C.h"
...
TYPE_REGISTRY_SYNC_REGISTERED_TYPES();
TypeRegistry< IBase, TypeRegistryRegisteredTypes > typeRegistry;
auto a = typeRegistry.instanceFromName("A");
auto b = typeRegistry.instanceFromName("B");
auto c = typeRegistry.instanceFromName("C");
As I said, the trick only works because I am including all my interesting types --so to say-- at the same time. How would you perform an automatic type recording without having to include them all in a single file (that is, compiling a source file which is including its header file should be sufficient)?