在运行时可以访问类信息的可扩展方法

时间:2012-02-27 19:15:16

标签: c++ factory-pattern

我正在制作一个简单的消息传递实体系统。我有一个实体描述符表连接到工厂,用于在运行时创建实体子类,我希望它可以通过字符串创建它们:

EntityManager manager; //managers have all of the entity table information (See below)

//Counter is a sample class that inherits from Entity
Counter* counter = manager.makeEntity("Counter"); //the string doesn't have to match the class name.

现在,我知道我可以使用简单的switch语句,但我希望系统也可以扩展 - 也就是说,当我(或我的系统的其他用户)想要创建一个新的Entity子类时,我不会不必转到开关块并添加它。目前,我使用宏来创建辅助类,我静态地实例化,以便构造函数将条目添加到实体表。这些类还初始化实体并从构造函数中删除大量样板文件。

//EHandle is a wrapper for Entity*. Currently std::shared_ptr<Entity>

class GenericDesc
{
public:
virtual ~GenericDesc() {}
    virtual EHandle makeEntity() const =0;
};

namespace Descriptor
{
    //Adds a descriptor to an internal map<string, GenericDesc*>
    void addEntityDescriptor(const std::string& type, GenericDesc& desc);
    EHandle newEntity(const std::string& type); //Factory method
}

//Add this to every entity class definition
#define DECLARE_ENTITY_CLASS(CLASS_NAME) \
    friend class CLASS_NAME##Descriptor;


//Use these after a class definition to add the entity class to the descriptor table
#define BEGIN_ENTITY_TYPE(ENTITY_NAME, CLASS_NAME, BASE_NAME) \
    BEGIN_ENTITY_TYPE_GUTS(ENTITY_NAME, CLASS_NAME) \
        BASE_NAME##Descriptor::prepareEntity(ent);

#define BEGIN_ENTITY_TYPE_BASELESS(ENTITY_NAME, CLASS_NAME) \
    BEGIN_ENTITY_TYPE_GUTS(ENTITY_NAME, CLASS_NAME) \
        ent->self = ent;

#define BEGIN_ENTITY_TYPE_GUTS(ENTITY_NAME, CLASS_NAME) \
class CLASS_NAME##Descriptor : public GenericDesc \
{ \
private: \
    typedef CLASS_NAME ClassName; \
public: \
    CLASS_NAME##Descriptor() \
    { \
        Descriptor::addEntityDescriptor(ENTITY_NAME, *this); \
    } \
    virtual ~CLASS_NAME##Descriptor() {} \
    virtual EHandle makeEntity() const\
    { \
        auto ent = std::shared_ptr<CLASS_NAME>(new CLASS_NAME); \
        prepareEntity(ent); \
        ent->type = ENTITY_NAME; \
        return ent; \
    } \
    static void prepareEntity(std::shared_ptr<ClassName> ent) \
    {

//These functions are caled between BEGIN_ENTITY_TYPE and END_ENTITY_TYPE
//ADD_ENTITY_INPUT binds a function to a string
#define ADD_ENTITY_INPUT(INPUT_NAME, INPUT_FUNC) \
        ent->addInput(INPUT_NAME, std::bind(&ClassName::INPUT_FUNC, ent, std::placeholders::_1));
//ADD_ENTITY_OUTPUT binds an Output object to a string
#define ADD_ENTITY_OUTPUT(OUTPUT_NAME, OUTPUT_OBJECT) \
        ent->addOutput(OUTPUT_NAME, ent->OUTPUT_OBJECT);

#define END_ENTITY_TYPE(CLASS_NAME) \
    } \
}; \
static CLASS_NAME##Descriptor CLASS_NAME##Desc; //TODO: find a way to fix the multiple-static-allocation issue

这个想法是你创建了一个BEGIN_ENTITY_TYPE(...)END_ENTITY_TYPE(...)子句,中间有ADD_ENTITY_x位。我的问题是,是否有一个较少的宏方式来做到这一点,仍然最小化样板,并且不需要修改定义Entity子类的文件之外的任何文件。模板类可以工作,但我不知道如何用模板类做ADD_ENTITY_INPUT / OUTPUT。

1 个答案:

答案 0 :(得分:0)

这可能不完全是您所追求的,但请考虑以下内容:

class Factory
{
public:
    virtual void InitialiseFactory() = 0;
    Entity* CreateEntity( string type )
    {
        // (obviously, you must handle the string not being found.)
        return m_MapEntities[ type ]->Clone();
    }
};

class MyFactory : public Factory
{
public:
    void InitialiseFactory()
    {
        m_MapEntities["typea"] = new MyEntity();
    } 
};

这里的想法是查找在基类中是保密的,可以保持不变,但提供实体的细节是在派生类的实现中。

另一种方法是在每个实体中使用静态方法调用工厂,但仍需要从某个地方调用该方法。