Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to for C++ types to self register in a list?

Tags:

c++

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.

like image 532
user48956 Avatar asked Dec 30 '08 21:12

user48956


2 Answers

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:

template<typename D>
struct automatic_register {
private:
    struct exec_register {
        exec_register() {
            persistenceSystem::registerPersistableType(
                D::getPersister()
            );
        }
    };
    // will force instantiation of definition of static member
    template<exec_register&> struct ref_it { };

    static exec_register register_object;
    static ref_it<register_object> referrer;
};

template<typename D> typename automatic_register<D>::exec_register 
    automatic_register<D>::register_object;

Derive the class you want to be auto-registered from automatic_register<YourClass> . The register function will be called before main, when the declaration of referrer is 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):

struct foo : automatic_register<foo> {    
    static void do_it() {
        std::cout << " doit "; 
    } 
}; 

int main() { 
    std::cout << " main "; 
}

Yields this output (as expected):

doit main
like image 105
Johannes Schaub - litb Avatar answered Nov 12 '22 00:11

Johannes Schaub - litb


Register each template at run-time in the constructor. Use a static variable per template to check if the type has already been registered. The following is a quickly hacked together example:

#include <iostream>
#include <vector>

using namespace std;

class Registerable {
    static vector<Registerable *> registry_;

public:
    static void registerFoo(Registerable *p)
    {
        registry_.push_back(p);
    }

    static void printAll()
    {
        for (vector<Registerable *>::iterator it = registry_.begin();
             it != registry_.end(); ++it)
            (*it)->print();
    }

    virtual void print() = 0;
};

vector<Registerable *> Registerable::registry_;

template <typename T>
class Foo : public Registerable {
    static bool registered_;

public:
    Foo()
    {
        if (!registered_) {
            registerFoo(this);
            registered_ = true;
        }
    }

    void print()
    {
        cout << sizeof (T) << endl;
    }
};

template <typename T> bool Foo<T>::registered_ = false;

int
main(int argc, char *argv[])
{
    Foo<char> fooChar;
    Foo<short> fooShort;
    Foo<int> fooInt;

    Registerable::printAll();

    return 0;
}

It should output the size of each template parameter in the order the classes were instantiated:

1
2
4

This version removes the registration code from each constructor and puts it in a base class.

#include <iostream>
#include <vector>

using namespace std;

class Registerable {
    static vector<Registerable *> registry_;

public:
    static void registerFoo(Registerable *p)
    {
        registry_.push_back(p);
    }

    static void printAll()
    {
        for (vector<Registerable *>::iterator it = registry_.begin();
             it != registry_.end(); ++it)
            (*it)->print();
    }

    virtual void print() = 0;
};

vector<Registerable *> Registerable::registry_;

template <typename T>
class Registerer : public Registerable {
    static bool registered_;

public:
    Registerer(T *self)
    {
        if (!registered_) {
            registerFoo(self);
            registered_ = true;
        }
    }
};

template <typename T> bool Registerer<T>::registered_ = false;

template <typename T>
class Foo : public Registerer<Foo<T> > {
public:
    Foo() : Registerer<Foo<T> >(this) { }

    void print()
    {
        cout << sizeof (T) << endl;
    }
};

int
main(int argc, char *argv[])
{
    Foo<char> fooChar;
    Foo<short> fooShort;
    Foo<int> fooInt;

    Registerable::printAll();

    return 0;
}

I added an example of another non-template class using the registry. So, the final output would be:

foo: 1
foo: 2
foo: 4
bar
like image 29
Judge Maygarden Avatar answered Nov 11 '22 22:11

Judge Maygarden