Register an object creator in object factory

两盒软妹~` 提交于 2019-11-27 22:37:36

So you want to put variables definitions in header file? There is a portable way: static variables of template classes. So we get:

template <typename T, typename I>
struct AutoRegister: public I
{
    // a constructor which use ourRegisterer so that it is instantiated; 
    // problem catched by bocco
    AutoRegister() { &ourRegisterer; } 
private:
    static RegisterClass<T, I> ourRegisterer;
};

template <typename T, typename I>
RegisterClass<T, I> AutoRegister<T, I>::ourRegisterer;

class Foo: AutoRegister<Foo, IFoo>
{
public:
    virtual void Do();         
};

Note that I've kept the inheritance of IFoo non virtual. If there is any risk of inheriting from that class multiple times, it should be virtual.

Cygon's answer is probably what you need, but you also might get away with lazy registration, depending what exacly is registration for.

Move registration into special base class and attach that besides interface in the macro.

Inside constructor use a local static flag, so that each class registers only once on first instanse creation.

Probably won't compile, but you should get what I meant:

template<className, interfaceName>
class RegistratorBase
{
public:
     RegistratorBase()
     {
          static bool registered = false;
          if(!registered)
                ObjectFactory<interfaceName>::GetInstance().Register<className>();
     }
};

#define REGISTER_CLASS(className, interfaceName) \
  class className : public interfaceName, private RegistratorBase<className, interfaceName>

Why not change the macro so REGISTER_CLASS only registers the class without declaring it?

It would allow you to derive from another class while also implementing the interface, you could put the registration into your .cpp file where it's only in one compilation unit and it would minimize header dependencies (you no longer have to include the object factory header in all your public headers).

There is a VC++ extension that should cause the linker to pick just one of the globals and discard the unused ones. The variable in question needs to be globally visible (= no static, would cause linker errors if the compiler doesn't support the extension):

__declspec( selectany )

Usage would be like this:

#define REGISTER_CLASS(className, interfaceName) \
  class className; \
  __declspec( selectany ) \
    RegisterClass<className, interfaceName> regInFactory##className; \
  class className : public interfaceName

Obviously you just need to move the class registrar declaration into a .cpp file. Perhaps it should be the .cpp file that contains the implementation of the class you register with that registrar.

You don't really need the registrar class, you only need one instance of it. By putting it into a .cpp file you hide it from all the other source completely but still have a global object initialized on startup.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!