从名称实例化类? [英] Instantiate class from name?

查看:144
本文介绍了从名称实例化类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象我有一堆C ++相关的类(所有扩展相同的基类和提供相同的构造函数),我在一个公共头文件(我包括)中声明,并在一些其他文件(我编译并静态链接为我的程序的构建的一部分)。



我想能够实例化其中一个传递的名称,这是一个参数,必须传递给我的程序(作为命令行或作为编译宏)。



我看到的唯一可能的解决方案是使用一个宏:

 code> #ifndef CLASS_NAME 
#define CLASS_NAME MyDefaultClassToUse
#endif

BaseClass * o = new CLASS_NAME(param1,param2,..);

这是唯一有价值的方法吗?

解决方案

这是一个常见问题,使用注册表模式


这是
注册表模式描述的情况: / p>


对象需要联系另一个
对象,只知道对象的名称
或服务名称
提供,但不是如何联系它。
提供一个服务,取一个对象,服务或角色的名称

返回一个远程代理
封装了如何知道
联系命名对象。


这是相同的基本发布/查找模型
形成服务的基础
Oriented Architecture SOA)和
OSGi中的服务层。


通常使用单例对象实现注册表,单例对象在编译时或在启动时通知对象的名称,以及构建它们的方式。



例如:

 <$> c $ c> template< class T> 
class Registry
{
typedef boost :: function0< T *>创造者;
typedef std :: map< std :: string,Creator>创作者;
Creators _creators;

public:
void register(const std :: string& className,const Creator& creator);
T * create(const std :: string& className);
}

您注册对象的名称和创建函数, p>

 注册表< I>注册表; 
registry.register(MyClass,& MyClass :: Creator);

std :: auto_ptr< T> myT(registry.create(MyClass));

然后我们可以使用聪明的宏来简化它,以便在编译时完成。 ATL 使用可在运行时通过名称创建的CoClasses的注册表模式 - 注册为简单的使用像下面的代码:

  OBJECT_ENTRY_AUTO(someClassID,SomeClassName); 

这个宏放在头文件中的某个地方,魔法使它在单COM服务器启动的时间。


imagine I have a bunch of C++ related classes (all extending the same base class and providing the same constructor) that I declared in a common header file (which I include), and their implementations in some other files (which I compile and link statically as part of the build of my program).

I would like to be able to instantiate one of them passing the name, which is a parameter that has to be passed to my program (either as command line or as a compilation macro).

The only possible solution I see is to use a macro:

#ifndef CLASS_NAME
#define CLASS_NAME MyDefaultClassToUse
#endif

BaseClass* o = new CLASS_NAME(param1, param2, ..);

Is it the only valuable approach?

解决方案

This is a problem which is commonly solved using the Registry Pattern:

This is the situation that the Registry Pattern describes:

Objects need to contact another object, knowing only the object’s name or the name of the service it provides, but not how to contact it. Provide a service that takes the name of an object, service or role and returns a remote proxy that encapsulates the knowledge of how to contact the named object.

It’s the same basic publish/find model that forms the basis of a Service Oriented Architecture (SOA) and for the services layer in OSGi.

You implement a registry normally using a singleton object, the singleton object is informed at compile time or at startup time the names of the objects, and the way to construct them. Then you can use it to create the object on demand.

For example:

template<class T>
class Registry
{
    typedef boost::function0<T *> Creator;
    typedef std::map<std::string, Creator> Creators;
    Creators _creators;

  public:
    void register(const std::string &className, const Creator &creator);
    T *create(const std::string &className);
}

You register the names of the objects and the creation functions like so:

Registry<I> registry;
registry.register("MyClass", &MyClass::Creator);

std::auto_ptr<T> myT(registry.create("MyClass"));

We might then simplify this with clever macros to enable it to be done at compile time. ATL uses the Registry Pattern for CoClasses which can be created at runtime by name - the registration is as simple as using something like the following code:

OBJECT_ENTRY_AUTO(someClassID, SomeClassName);

This macro is placed in your header file somewhere, magic causes it to be registered with the singleton at the time the COM server is started.

这篇关于从名称实例化类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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