Automatic static invocation of derived types
Asked Answered
L

4

13

Does anyone know of a way to make derived classes automatically instantiate a static variable with a template type (this either has to require nothing from the writer of the derived class, or force him to call this static method in order to make the derived class definition valid).

This is probably impossible to understand, I'll try and define it better.

Basically I have a global factory class with a templated function called registerType. For every class derived from Entity, I need this function to be called with the template parameter of the derived type. At the moment, I have to manually do it in some init function, which results in a large block of calls to this function, which kind of goes against the principle of templates for me.

So I have this:

class Factory
{
  template <typename EntityType>
  registerEntityType();
};

void someInitFunction()
{
   /// All of these are derived from Entity
  gFactory.registerEntityType<EntityType1>();
  gFactory.registerEntityType<EntityType2>();
  gFactory.registerEntityType<EntityType3>();
  /// and so on
}

whereas I would rather have this:

class Factory
{
  template <typename EntityType>
  registerEntityType();
};

class Entity // Abstract
{
    /// This function should be called automatically with the derived 
    /// type as a parameter
    SomeStaticConstructor<MDerivedType>() 
    {
      gFactory.registerEntityType<MDerivedType>();
    }
};

EDIT: This is the static recurring template code that isn't working:

This is my base class, and the class for automatically registering stuff

template <typename DerivedType>
class Registrar
{
    public:
        Registrar();
        void check();
};
template <typename Product, typename DerivedType>
class AbstractFactory: public AbstractFactoryBase<Product>
{
    public:
        AbstractFactory();
        ~AbstractFactory();
    private:
        static Registrar<DerivedType> registrar;
};

The registrar's constructor

template <typename DerivedType>
Registrar<DerivedType>::Registrar()
{
    std::cout << DerivedType::name() << " initialisation" << std::endl;
    g_AbstractFactories.registerFactoryType<DerivedType>(DerivedType::name());
}

And a derived type

class CrateFactory : public AbstractFactory<Entity, CrateFactory>
{
    public:
        CrateFactory(FactoryLoader* loader);
        virtual ~CrateFactory();
        Entity* useFactory(FactoryParameters* parameters);
        static std::string name()
        {
            return "CrateFactory";
        }
Limitation answered 19/6, 2011 at 0:34 Comment(2)
The first path I'd tread down is making Entity a template class, so it knows the derived type. The problem there is derived types would either have to themselves be templates (and thus be abstract), or never used as base classes. I've also seen macros used for this in win32 wrapper libraries. And, this question is somewhat related - #139100Vaccine
Nm, it seems it is called CRTP, and the answers captured what I was getting at :)Vaccine
L
2

If anyone is still interested, I figured it out. Static template member variables are not automatically instantiated unless they are used. I needed it to be instantiated before the constructor was called, so I couldn't make it a static local. The solution is to make it a static template member variable, and then use it (just call an empty function on it if you want) in a member function (I use the constructor). This forces the compiler to instantiate the static for every template parameter ever declared, because the instantiated constructor code uses it, for example:

My registry class, with its blank function for calling

template <typename DerivedType>
class Registrar
{
    public:
        Registrar();
        void check(){}
};

My class I want registered.

template <typename Product, typename DerivedType>
class AbstractFactory: public AbstractFactoryBase<Product>
{
    public:
        AbstractFactory();
        ~AbstractFactory();
    private:
        static Registrar<DerivedType> registrar;
};

The registrar's constructor

template <typename DerivedType>
Registrar<DerivedType>::Registrar()
{
    std::cout << DerivedType::name() << " initialisation" << std::endl;
    g_AbstractFactories.registerFactoryType<DerivedType>(DerivedType::name());
}

And my classes constructor

template <typename Product, typename DerivedType>
AbstractFactory::AbstractFactory()
{
    registrar.check();
}
Limitation answered 24/6, 2011 at 0:24 Comment(0)
S
10

I'd recommend a CTRP-backed approach:

// Entity.h
class EntityBase
{ // abstract
};

template<class Derived>
class Entity
  : public EntityBase
{ // also abstract thanks to the base
  static char _enforce_registration; // will be instantiated upon program start
};

// your actual types in other headers
class EntityType1
  : public Entity<EntityType1>
{ // automatic registration thanks to the _enforce_registration of the base
  // ...
};

// Entity.cpp
#include "Entity.h"

template<class T>
char RegisterType(){
  GetGlobalFactory().registerEntityType<T>();
  return 0; // doesn't matter, never used.
}

template<class Derived>
char Entity<Derived>::_enforce_registration = RegisterType<Derived>();

Though, as seen, you now need to get your factory through a GetGlobalFactory function, which lazy initializes the factory to ensure that it has been initialized before the enforced registration happens:

Factory& GetGlobalFactory(){
  static Factory _factory;
  return _factory;
}
Sheen answered 19/6, 2011 at 0:56 Comment(2)
@Merlyn: right, I knew I wanted to link to it, but somehow forgot it.Sheen
+1. This is what I was trying to get at in my comment on the OP, but I forgot it was a full-blown idiom. Not doing much C++ these days :)Vaccine
Z
8

You might be able to get what you want using a mix-in and the CRTP.

But first, you need to take care of the "order of initialization" problem. To ensure the gFactory exists before you try to use it, you really need to make it a proper "singleton" class, like this:

class Factory {
public:
    static Factory &getFactory() { static Factory f; return f; }
    template <typename EntityType>
    void registerEntityType() { ... }
};

Then the "mix-in" would look like this:

template <typename T>
class EntityMixin {
private:
    struct RegisterMe {
        RegisterMe() { Factory::getFactory().registerEntityType<T>(); }
    };
    EntityMixin() {
        static RegisterMe r;
    }
};

And you would use it like this:

class EntityType1 : public Entity, EntityMixin<EntityType1> { ... };
class EntityType2 : public Entity, EntityMixin<EntityType2> { ... };
class EntityType3 : public Entity, EntityMixin<EntityType3> { ... };

[Update]

You can also take the Xeo/Merlyn idea of creating an EntityBase, rename EntityMixin to Entity, and avoid the need to inherit from two places. I actually think my original proposal is more clear; you could even call the mixin FactoryMixin and tack it on to any class you want to register.

But the Xeo/Merlyn version would look like so:

class Factory {
    public:
    static Factory &getFactory() { static Factory f; return f; }
    template <typename EntityType>
    void registerEntityType() { ... }
};

class EntityBase { ... } ;

template <typename T>
class Entity : public EntityBase {
private:
    struct RegisterMe {
        RegisterMe() { Factory::getFactory().registerEntityType<T>(); }
    };
    Entity() {
        static RegisterMe r;
    }
};

class EntityType1 : public Entity<EntityType1> { ... };
class EntityType2 : public Entity<EntityType2> { ... };
class EntityType3 : public Entity<EntityType3> { ... };

The keys to any solution are the CRTP and careful use of static local variables to avoid the order-of-initialization problem.

Zygote answered 19/6, 2011 at 0:51 Comment(13)
I'd suggest combining the mix-in and the entity class, if possible.Vaccine
So I'm separating the mixin and the entity class just for semantics right? (because having both in one class is ugly and conflicting) or is there some other reason?Limitation
The mixin is a template class. The common base Entity class is not (at least, I assumed that is what you want...). Therefore they cannot be the same.Zygote
Ok cool. I don't quite understand statics, is there a way for the registration function to be called when the program loads, as opposed to when the first instance of the object is allocated?Limitation
@Alasdair: Yes, see my answer with the static char that gets initialized upon program start.Sheen
@Xeo: That doesn't appear to be working :( I've placed both a registration class and a char initialised the way you did as static member variables, but they are never constructed. I put the same variables as static locals in the constructor and they get invoked during the first construction of each derived typeLimitation
@Alasdair: Put a print in the RegisterType function, it should work. Also, did you really put the initialization of the static char in some .cpp?Sheen
I had done, and tried to hit it in the debugger, its not being initialised. I just edited the OP with the code that doesn't work, can you see anything wrong with it?Limitation
@Alasdair: You can life the static RegisterMe r out of the constructor and just make it a private member variable of the class. You definitely want to use Factory::getFactory() instead of gFactory, though, because there is no way to ensure the order of construction of these sorts of top-level forms. I have updated my last code example to work this way... Let me know if it is still unclearZygote
@Alasdair: Actually, my suggestion does not work... The problem with static member variables is that they need to have a definition (in a .cc file) in addition to a declaration (in the header file). Hm...Zygote
To get them to register when the program loads, you just need to have globals (or variables at the top of main) of type Entity1, Entity2, etc.Zygote
Nemo: gFactory is actually a macro for a static get, I just don't like how long the code is for calling a function like that. Is there no way to execute code for every derived type, without explicity doing so? (Aside from instantiating the curiously recurring template)Limitation
The only way I can see is to lift the static RegisterMe r out of the constructor and make it a static variable member of the template class. But then you have to define it somewhere, so in some source file you would need to write EntityMixin<Entity1>::RegisterMe EntityMixin<Entity1>::r, and do that for Entity1, Entity2, etc. I do not think you can do what you ask without putting some definition at the top-level for each EntityN class.Zygote
L
2

If anyone is still interested, I figured it out. Static template member variables are not automatically instantiated unless they are used. I needed it to be instantiated before the constructor was called, so I couldn't make it a static local. The solution is to make it a static template member variable, and then use it (just call an empty function on it if you want) in a member function (I use the constructor). This forces the compiler to instantiate the static for every template parameter ever declared, because the instantiated constructor code uses it, for example:

My registry class, with its blank function for calling

template <typename DerivedType>
class Registrar
{
    public:
        Registrar();
        void check(){}
};

My class I want registered.

template <typename Product, typename DerivedType>
class AbstractFactory: public AbstractFactoryBase<Product>
{
    public:
        AbstractFactory();
        ~AbstractFactory();
    private:
        static Registrar<DerivedType> registrar;
};

The registrar's constructor

template <typename DerivedType>
Registrar<DerivedType>::Registrar()
{
    std::cout << DerivedType::name() << " initialisation" << std::endl;
    g_AbstractFactories.registerFactoryType<DerivedType>(DerivedType::name());
}

And my classes constructor

template <typename Product, typename DerivedType>
AbstractFactory::AbstractFactory()
{
    registrar.check();
}
Limitation answered 24/6, 2011 at 0:24 Comment(0)
M
0

Sorry to bump this old thread again, but I found it first hit, and a valuable option is missing in my eyes: the unforgettable factory with automatic registry, published by Nir Friedmann: http://www.nirfriedman.com/2018/04/29/unforgettable-factory/

Basic idea - as well as the other solutions:

  1. CRTP to inject functionality into derived classes automatically.
  2. Use initialization of a static member to force code to be executed before main.

Additional ideas:

  1. The template takes a list of types, which is interpreted as Constructor Parameter List. Thus each derived class must provide a CTor with exactly this parameter List (in the example below: QGraphicsItem*).
  2. Using the PassKey-idiom it is made sure that direct derrivations from the Base class (in the example: SnapHelper) are not possible. One has to use the Base's Registrar for deriving (like: class Wurst: public SnapHelper::Registrar<Wurst>)
  3. Use as little as possible memory for the factory - like only the "list" itself.

Factory Boilerplate:

template < class Base, class... Args >
class Factory
{
    using FuncType = Base*  ( * ) ( Args... );

  public:
    // Simplest possible factory access:
    static const QList< QPair< FuncType, QString > >& Classes() { return classes(); }
    // convenience factory function:
    template < class... T >
    static Base* newT( int id, T&&... args )
    {   return classes().at( id ).first( std::forward< T >( args )... );    }

    template < class T >
    struct Registrar : Base
    {
        friend T;
        static bool registerT()
        {
            Factory::classes().append( { []( Args... args ) -> Base*
                                         { return new T( std::forward< Args >( args )... ); },
                                         typeid( T ).name() } );
            return true;
        }
        static bool registered;

      private:
        Registrar()
            : Base( Key{} )
        {
            ( void ) registered;
        }
    };

    friend Base;

    static QList< QPair< FuncType, QString > >& classes()
    {
        static QList< QPair< FuncType, QString > > s;
        return s;
    }

  private:
    class Key
    {
        Key() {}
        template < class T >
        friend struct Registrar;
    };
    Factory() = default;
};

template < class Base, class... Args >
template < class T >
bool Factory< Base, Args... >::Registrar< T >::registered =
    Factory< Base, Args... >::Registrar< T >::registerT();

After having this relatively light boilerplate, we need a base class:

struct SnapHelper : Factory< SnapHelper, QGraphicsItem* >
{
    SnapHelper( Key ) {}
    virtual ~SnapHelper()                                          = default;
    virtual void       moved( const QList< QGraphicsItem* >&,
                              const QRectF& newMoveItemSceneRect ) = 0;
    virtual SnapResult doSnap()                                    = 0;
};

Now, deriving a "SnapHelper" could look like this:

class SnapRange
    : public SnapHelper::Registrar< SnapRange >
{
    QSizeF myRange;

  public:
    SnapRange( QGraphicsItem* itemBeingMoved );
    // a Snaprange is only moved, nothing else
    void   moved( const QList< QGraphicsItem* >& neighbours, const QRectF& nmisr ) override {}
    // The Range display item does not snap at all, but it should vanish after movement
    SnapResult doSnap() override { return SnapResult(); }
};

Using this factory works well. You might want to help yourself typing less ...

typedef Factory< SnapHelper, QGraphicsItem* > SnapFactory;

... with above type definition. Using the factory is pretty straight forward then:

int id=0;
for ( const auto &it: SnapFactory::Classes() )
{
    qDebug() << "creating type:" << it.second << "id=" << id++;
    SnapHelper* helper = it.first( std::forward<params...> );
    // the convenient way is with using newT, though ...
    //SnapHelper* helper = SnapFactory::newT( id, <params...> );
}

P.S.: I'm using Qt, but that's just my way ... ;)

Myosin answered 4/11, 2023 at 21:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.