boost::factory 创建 boost::enable_shared_from_this 类

发布于 2024-11-06 12:33:15 字数 2134 浏览 4 评论 0原文

我有多个从 A 派生的类

class A : public boost::enable_shared_from_this<A> {
    public:
        typedef boost::shared_ptr<A>    pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new A(socket));
        }
    private:
        A(&tcp::socket socket) : m_socket(socket) {}

        tcp::socket&    m_socket;
}

Class Aa : public A {
    public:
        typedef boost::shared_ptr<Aa>   pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new Aa(socket));
        }

    private:
        Aa(&tcp::socket socket) : A(socket) {}
}

Class Ab : public A {
    public:
        typedef boost::shared_ptr<Ab>   pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new Ab(socket));
        }

    private:
        Ab(&tcp::socket socket) : A(socket) {}
}

[...]

Class Ax : public A {
    public:
        typedef boost::shared_ptr<Ax>   pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new Ax(socket));
        }

    private:
        Ax(&tcp::socket socket) : A(socket) {}
}

我想使用 boost::factory 创建一个工厂模式 我写过类似的东西:

class MyFactory {
    public:
        static MyFactory* getInstance();

        typedef boost::function< A(tcp::socket) > a_fact;

    private:
        MyFactory() {
            map["Aa"] = boost::bind(boost::factory< Aa::create >, _1);
            map["Ab"] = boost::bind(boost::factory< Ab::create >, _1);
            […]
            map["Ax"] = boost::bind(boost::factory< Ax::create >, _1);
        }

        std::map<std::string, a_fact>   map;
    };

当然,它不会编译...... 给我错误:

MyFactory.cc:13: error: type/value mismatch at argument 1 in template parameter list for ‘template<class Pointer, class Allocator, boost::factory_alloc_propagation AP> class boost::factory’
MyFactory.cc:13: error:   expected a type, got ‘Aa::create’

任何想法,我应该如何实现它?

I have multiple class derivated from A

class A : public boost::enable_shared_from_this<A> {
    public:
        typedef boost::shared_ptr<A>    pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new A(socket));
        }
    private:
        A(&tcp::socket socket) : m_socket(socket) {}

        tcp::socket&    m_socket;
}

Class Aa : public A {
    public:
        typedef boost::shared_ptr<Aa>   pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new Aa(socket));
        }

    private:
        Aa(&tcp::socket socket) : A(socket) {}
}

Class Ab : public A {
    public:
        typedef boost::shared_ptr<Ab>   pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new Ab(socket));
        }

    private:
        Ab(&tcp::socket socket) : A(socket) {}
}

[...]

Class Ax : public A {
    public:
        typedef boost::shared_ptr<Ax>   pointer;

        static pointer create(tcp::socket& socket) {
            return pointer(new Ax(socket));
        }

    private:
        Ax(&tcp::socket socket) : A(socket) {}
}

I would like to create a factory pattern using boost::factory
I have writted something like that :

class MyFactory {
    public:
        static MyFactory* getInstance();

        typedef boost::function< A(tcp::socket) > a_fact;

    private:
        MyFactory() {
            map["Aa"] = boost::bind(boost::factory< Aa::create >, _1);
            map["Ab"] = boost::bind(boost::factory< Ab::create >, _1);
            […]
            map["Ax"] = boost::bind(boost::factory< Ax::create >, _1);
        }

        std::map<std::string, a_fact>   map;
    };

And of course, it does not compil...
giving me the error :

MyFactory.cc:13: error: type/value mismatch at argument 1 in template parameter list for ‘template<class Pointer, class Allocator, boost::factory_alloc_propagation AP> class boost::factory’
MyFactory.cc:13: error:   expected a type, got ‘Aa::create’

Any idea, how should I implement it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的心境我的脸 2024-11-13 12:33:15

文档中,参数传递给factory-template 是您期望结果的指针类型。因此,在您的情况下 boost::factory

In the documentation the argument passed to the factory-template is the pointer type you expect as a result. So in your case boost::factory<Aa::pointer>.

想念有你 2024-11-13 12:33:15

如果有人犯同样的错误,这里有答案。
它不需要boost::factory来工作,直接使用boost::function就足够了。

所以

class MyFactory {
    public:
        static MyFactory* getInstance();

        typedef boost::function< A(tcp::socket) > a_fact;

    private:
        MyFactory() {
            map["Aa"] = boost::bind(&Aa::create, _1);
            map["Ab"] = boost::bind(&Ab::create, _1);
            […]
            map["Ax"] = boost::bind(&Ax::create, _1);
        }

        std::map<std::string, a_fact>   map;
    };

效果很好!!!

If anyone goes into the same mistake, Here the answer.
It does not need boost::factory to work, using boost::function directly is enough.

so

class MyFactory {
    public:
        static MyFactory* getInstance();

        typedef boost::function< A(tcp::socket) > a_fact;

    private:
        MyFactory() {
            map["Aa"] = boost::bind(&Aa::create, _1);
            map["Ab"] = boost::bind(&Ab::create, _1);
            […]
            map["Ax"] = boost::bind(&Ax::create, _1);
        }

        std::map<std::string, a_fact>   map;
    };

Works great !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文