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";
}
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With