boost::factory 创建 boost::enable_shared_from_this 类
我有多个从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在文档中,参数传递给
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 caseboost::factory<Aa::pointer>
.如果有人犯同样的错误,这里有答案。
它不需要
boost::factory
来工作,直接使用boost::function
就足够了。所以
效果很好!!!
If anyone goes into the same mistake, Here the answer.
It does not need
boost::factory
to work, usingboost::function
directly is enough.so
Works great !!!