如何创建一个通用的基于模板的工厂? [英] How to create a generic template based factory?

查看:202
本文介绍了如何创建一个通用的基于模板的工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++ 98。我想创建一个基于模板的通用工厂,创建者可以采取任何参数创建目标对象,或一个参数。

I am using c++98. I want to create a generic factory which is template based, and the creator can take no parameter to create the target object, or one parameter.

/*<class.h> begins

#ifndef INCLUDED_CLASS
#define INCLUDED_CLASS

#include <iostream>
#include <boost/shared_ptr.hpp>

class A
{
public:
    A() { _a = 27; }
    void print() const { std::cout << "A is " << _a << "." << std::endl; }
protected:
    int _a;
};
typedef boost::shared_ptr<A> APtr;

class AA : public A
{
public:
    void print() const { std::cout << "AA is " << _a << "!" << std::endl; }
};

class B
{
public:
    B(double b) { _b = b; }
    void print() const { std::cout << "B is " << _b << "." << std::endl; }
protected:
    double _b;
};
typedef boost::shared_ptr<B> BPtr;

class BB : public B
{
public:
    BB(double b) : B(b) {};
    void print() const { std::cout << "BB is " << _b << "!" << std::endl; }
};

#endif

/*<class.h> ends



/*<factory.h> begins

#ifndef INCLUDED_FACTORY
#define INCLUDED_FACTORY

#include <map>
#include <string>
#include <boost/shared_ptr.hpp>

template<class bT, class pT=void>
class Creator
{
public:
    virtual bT* create() = 0;
    virtual bT* create(const pT* param) = 0;
};

template <class bT, class pT>
struct CreatorPtr
{
    typedef boost::shared_ptr< Creator<bT> > type;
};


template <class bT, class cT, class pT>
class CreatorImpl : public Creator<bT, pT>
{
public:
   virtual bT* create() { return new cT; }
   virtual bT* create(const pT* param) { return new cT(param); }
};


template<class bT, class pT=void>
class Factory
{
public:
    virtual bT* create(const std::string& name) const = 0;
    virtual bT* create(const std::string& name, const pT* param) const = 0;
protected:
    Factory() {}
    Factory(const Factory&) {}
    Factory &operator=(const Factory&) { return *this; }
    void registerCreator(const std::string& name, typename CreatorPtr<bT, pT>::type creator)
        { _table_creator[name] = creator; }
    typedef std::map<std::string, typename CreatorPtr<bT, pT>::type> tableCreator;
    typedef typename tableCreator::const_iterator citerTc;
    citerTc begin() const { return _table_creator.begin(); }
    citerTc end() const { return _table_creator.end(); }
    citerTc find(const std::string& name) const { return _table_creator.find(name); }
protected:
    tableCreator _table_creator;
};

class A;
class EngineA : public Factory<A>
{
public:
    virtual A* create(const std::string& name) const
    {
        citerTc it = find(name);
        if (it != end() && it->second)
        {
            return it->second->create();
        }
        else
            return (A*)NULL;
    }

    static Factory<A>& get()
    {
        static EngineA instance;
        instance.registerEngine();
        return instance;
    }
private:
    virtual A* create(const std::string& name, const void* param) const { return (A*)NULL; }
private:
    virtual void registerEngine();
};

void EngineA::registerEngine()
{
    CreatorPtr<A, void>::type AACreator(new CreatorImpl<A, AA, void>);
    registerCreator("AA", AACreator);
}

class B;
class EngineB : public Factory<B, double>
{
public:
    virtual B* create(const std::string& name, const double* value) const
    {
        citerTc it = find(name);
        if (it != end() && it->second && value)
        {
            return it->second->create(value);
        }
        else
            return (B*)NULL;
    }

            static Factory<B, double>& get()
    {
        static EngineB instance;
        instance.registerEngine();
        return instance;
    }
private:
    virtual B* create(const std::string& name) const { return (B*)NULL; }
private:
    virtual void registerEngine();
};

void EngineB::registerEngine()
{
    CreatorPtr<B, double>::type BBCreator(new CreatorImpl<B, BB, double>);
    registerCreator("BB", BBCreator);
}

#endif

/*<factory.h> ends



/*<main.cpp> begins

#include <class.h>
#include <factory.h>

int main(void)
{
    APtr a(EngineA::get().create("AA"));
    if (a)
        a->print();

    double value = 35.7;
    BPtr b(EngineB::get().create("BB",&value));
    if (b)
        b->print();

    return 0;
}

/*<main.cpp> ends

编译错误是:

....../boost//boost_1_46_1/include/boost/smart_ptr/shared_ptr.hpp: In constructor ‘boost::shared_ptr<T>::shared_ptr(Y*) [with Y = CreatorImpl<B, BB, double>, T = Creator<B, void>]’:
./factory.h:116:   instantiated from here
....../boost//boost_1_46_1/include/boost/smart_ptr/shared_ptr.hpp:187: error: cannot convert ‘CreatorImpl<B, BB, double>*’ to ‘Creator<B, void>*’ in initialization
./factory.h: In member function ‘bT* CreatorImpl<bT, cT, pT>::create() [with bT = B, cT = BB, pT = double]’:
main.cpp:15:   instantiated from here
./factory.h:27: error: no matching function for call to ‘BB::BB()’
./class.h:36: note: candidates are: BB::BB(double)
./class.h:34: note:                 BB::BB(const BB&)
./factory.h: In member function ‘bT* CreatorImpl<bT, cT, pT>::create(const pT*) [with bT = B, cT = BB, pT = double]’:
main.cpp:15:   instantiated from here
./factory.h:28: error: no matching function for call to ‘BB::BB(const double*&)’
./class.h:36: note: candidates are: BB::BB(double)
./class.h:34: note:                 BB::BB(const BB&)
./factory.h: In member function ‘bT* CreatorImpl<bT, cT, pT>::create(const pT*) [with bT = A, cT = AA, pT = void]’:
main.cpp:15:   instantiated from here
./factory.h:28: error: no matching function for call to ‘AA::AA(const void*&)’
./class.h:18: note: candidates are: AA::AA()
./class.h:18: note:                 AA::AA(const AA&)
make: *** [test] Error 1

任何人都可以用错误的帮助?这是创建一个通用的基于模板的工厂的好办法?

Can anyone help with the errors? Is this a good way to create a generic template based factory?

推荐答案

下面看看我的回答类似的东西:<一href=\"http://stackoverflow.com/a/18253631/2288854\">http://stackoverflow.com/a/18253631/2288854

Check out my answer here for something similar: http://stackoverflow.com/a/18253631/2288854

在code使用宏来产生一个通用的工厂模板,需要长达N个参数。在C ++ 98它不会工作,但也许可以给你一些想法。

The code uses macros to produce a generic factory template which takes up to N parameters. It wont work in c++98, but perhaps it can give you some ideas.

这篇关于如何创建一个通用的基于模板的工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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