Suppose I have some per-class data: (AandB.h)
class A { public: static Persister* getPersister(); } class B { public: static Persister* getPersister(); }
… and lots and lots more classes. And I want to do something like:
persistenceSystem::registerPersistableType( A::getPersister() ); persistenceSystem::registerPersistableType( B::getPersister() ); ... persistenceSystem::registerPersistableType( Z::getPersister() );
… for each class.
My question is: is there a way to automate building a list of per-type data so that I don’t have to enumerate each type in a big chunk (as in the above example)?
For example, one way you might do this is: (AutoRegister.h)
struct AutoRegisterBase { virtual ~AutoRegisterBase() {} virtual void registerPersist() = 0; static AutoRegisterBase*& getHead() { static AutoRegisterBase* head= NULL; return head; } AutoRegisterBase* next; }; template <typename T> struct AutoRegister : public AutoRegisterBase { AutoRegister() { next = getHead(); getHead() = this; } virtual void registerPersist() { persistenceSystem::registerPersistableType( T::getPersister() ); } };
and use this as follows: (AandB.cxx: )
static AutoRegister<A> auto_a; static AutoRegister<B> auto_b;
Now, after my program starts, I can safely do: (main.cxx)
int main( int, char ** ) { AutoRegisterBase* p = getHead(); while ( p ) { p->registerPersist(); p = p->next; } ... }
to collect each piece of per-type data and register them all in a big list somewhere for devious later uses.
The problem with this approach is that requires me to add an AutoRegister object somewhere per type. (i.e. its not very automatic and is easy to forget to do). And what about template classes? What I’d really like is for the instantiation of a template class to somehow cause that class to get automatically registered in the list. If I could do this I would avoid having to have the user of the class (rather than the author) to remember to create a:
static AutoRegister< SomeClass<X1> > auto_X1; static AutoRegister< SomeClass<X2> > auto_X2; ... etc....
for each template class instantiation.
For FIW, I suspect there’s no solution to this.
You can execute something before main once if a instantiation of a template is made. The trick is to put a static data member into a class template, and reference that from outside. The side effect that static data member triggers can be used to call the register function:
Derive the class you want to be auto-registered from
automatic_register<YourClass>. The register function will be called before main, when the declaration ofreferreris instantiated (which happens when that class is derived from, which will implicitly instantiate that class from the template).Having some test program (instead of the register function, a function do_it is called):
Yields this output (as expected):