自动注册类厂 代码实现

来源:互联网 发布:烟台淘宝客服招聘 编辑:程序博客网 时间:2024/04/28 17:26
struct CellCreateKey
{
    INT64 ddwColFlag;


    CellCreateKey(INT64 ddwColFlag)
    {
        this->ddwColFlag = ddwColFlag;
    }
    CellCreateKey(const CellCreateKey& oParam)
    {
        this->ddwColFlag = oParam.ddwColFlag;
    }
    bool operator<(const CellCreateKey& oParam) const
    {
        return ddwColFlag < oParam.ddwColFlag;
    }
};


class Factory
{
public:
    static Factory& Get();
    Type* CreateCPD(const CellCreateKey& stParam);


public:
    template<class T>
    struct Register_Helper
    {
        Register_Helper(const CellCreateKey& stKey)
        {
            Factory::Get().s_mapCPD.emplace(stKey, [&]{return T(stKey.ddwColFlag)});
        }


        Register_Helper(const CellCreateKey& stkey, std::function<Type*()> createFun)
        {
            Factory::Get().s_mapCPD.emplace(stkey, createFun);
        }
    };


protected:
    Factory(){};
protected:
    static map<CellCreateKey, std::function<Type*()>> s_mapCPD;
};


#define Register_CPD(ddwColFlag, CPD) static Factory::Register_Helper<CPD> oRegister_Helper_##ddwColFlag##(CellCreateKey(ddwColFlag))
#define Register_CPD_EX(ddwColFlag, createFunc) static Factory::Register_Helper<int> oRegister_Helper_##ddwColFlag##(CellCreateKey(ddwColFlag), createFunc)
原创粉丝点击