C ++中的工厂模式:生成显式createInstance() - 方法自动 [英] Factory Pattern in C++: generating explicit createInstance()-Method automatically

查看:676
本文介绍了C ++中的工厂模式:生成显式createInstance() - 方法自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个C ++框架时遇到问题,用户应该有比使用它更少的开销。用户可以通过创建一个包含一个类的共享库来发布他们的工作,该类由一个框架的BaseClass派生,并实现一个externCcreateInstance() - 方法,以便返回一个实例的派生类。因此,框架可以通过使用dlsym()的共享库调用createInstance-Method来访问用户类。

  class BaseClass { } 
class UserClass:public BaseClass {}

externC{
BaseClass * UserXcreateInstance(){
return new UserClass();
}
}

在框架中:

  typedef BaseClass *(* CreateInstance)(); 
void * handle;
CreateInstance createInstance;
handle = dlopen(libUserLibrary.so,RTLD_LAZY | RTLD_GLOBAL);
createInstance = reinterpret_cast< CreateInstance *> dlsym(handle,UserXcreateInstance);
BaseClass * userX = createInstance();

我的问题:是否可以生成UserXcreateInstance() - 方法,每个用户都是冗余的图书馆,所以用户不必考虑它?



我以为可以使用模板+宏,但我还没有找到一种方式要做到这一点...



另一种方法,我在想,直接通过dlsym和适当的名称来调用任何用户类的构造函数。 (我从配置文件中知道任何命名空间+类名)但是我不认为这是一个正确的解决方案,特别是构造函数调用与常规函数调用不一样,但非常有趣...

解决方案

我无法想像有一种方法可以自动创建,而不需要对用户的每个部分进行任何编码。我可以想像如何简化它,也许使用宏:

  #define OBJECT_CREATOR(X)\ 
extern C{\
BaseClass * UserXCreateInstance(){\
return new X(); \
} \
}

而用户只需要放在他的cpp文件中:

  OBJECT_CREATOR(UserClass); 


i have the problem in writing a C++ framework, that users should have less overhead than possible to use it. Users can publish their work to the frameworks by creating a shared library that contains a class, which is derived by a frameworks' BaseClass and implementing an extern "C" createInstance()-method in order to return an instance its' derived class. So the framework can access the user class by calling the createInstance-Method through the shared library with dlsym().

class BaseClass{}
class UserClass : public BaseClass{}

extern "C"{  
   BaseClass* UserXcreateInstance(){
    return new UserClass();
   }                        
}

In framework:

typedef BaseClass* (*CreateInstance) ();
void* handle;
CreateInstance createInstance;
handle = dlopen( "libUserLibrary.so", RTLD_LAZY | RTLD_GLOBAL );
createInstance = reinterpret_cast <CreateInstance*> dlsym( handle, "UserXcreateInstance" );
BaseClass* userX = createInstance();

My Question: Is it possible to generate the UserXcreateInstance()-method, which is redundant in each user library, so that the user don`t have to think about it?

I thought it would be possible with templates+macros, but I haven't yet found a way to do this...

Another approach, I was thinking is directly call the constructor of any user class via dlsym and appropiate name mangling. (I know any namespace + class name from a config file) But I don`t think this a proper solution, especially a constructor call is not the same as a regular function call... but very interesting...

解决方案

I can't imagine a way to create this automatically without any coding per part of the user. I can imagine ways to simplify it, perhaps using a macro:

#define OBJECT_CREATOR(X) \
    extern "C" {          \
         BaseClass *UserXCreateInstance() {\
             return new X(); \
         }\
    }

And the user just need to put on his cpp file:

OBJECT_CREATOR(UserClass);

这篇关于C ++中的工厂模式:生成显式createInstance() - 方法自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆