存储数组中的对象

时间:2010-06-16 20:40:59

标签: c++ boost signals boost-any

我想在地图中保存增强信号对象(关联:信号名称→信号对象)。信号签名是不同的,因此第二种类型的地图应该是 boost :: any

map<string, any> mSignalAssociation;

问题是如何在不定义新信号签名类型的情况下存储对象?

typedef boost::signals2::signal<void (int KeyCode)> sigKeyPressed;
mSignalAssociation.insert(make_pair("KeyPressed", sigKeyPressed()));
// This is what I need: passing object without type definition
mSignalAssociation["KeyPressed"] = (typename boost::signals2::signal<void (int KeyCode)>());
// One more trying which won't work. And I don't want use this
sigKeyPressed mKeyPressed;
mSignalAssociation["KeyPressed"] = mKeyPressed;

所有这些尝试都会抛出错误:

/usr/include/boost/noncopyable.hpp: In copy constructor ‘boost::signals2::signal_base::signal_base(const boost::signals2::signal_base&)’: 
In file included from /usr/include/boost/signals2/detail/signals_common.hpp:17:0, 
/usr/include/boost/noncopyable.hpp:27:7: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private 
/usr/include/boost/signals2/signal_base.hpp:22:5: error: within this context 
---------- 
/usr/include/boost/signals2/detail/signal_template.hpp: In copy constructor ‘boost::signals2::signal1<void, int&, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>::signal1(const boost::signals2::signal1<void, int, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>&)’: 
In file included from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:52:0, 
/usr/include/boost/signals2/detail/signal_template.hpp:578:5: note: synthesized method ‘boost::signals2::signal_base::signal_base(const boost::signals2::signal_base&)’ first required here 
                from /usr/include/boost/signals2.hpp:16, 
--------- 
/usr/include/boost/signals2/preprocessed_signal.hpp: In copy constructor ‘boost::signals2::signal<void(int)>::signal(const boost::signals2::signal<void(int)>&)’: 
In file included from /usr/include/boost/signals2/signal.hpp:36:0, 
/usr/include/boost/signals2/preprocessed_signal.hpp:42:5: note: synthesized method ‘boost::signals2::signal1<void, int, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>::signal1(const boost::signals2::signal1<void, int, boost::signals2::optional_last_value<void>, int, std::less<int>, boost::function<void(int)>, boost::function<void(const boost::signals2::connection&, int)>, boost::signals2::mutex>&)’ first required here 
                from /home/ockonal/Workspace/Projects/Pseudoform-2/include/Core/Systems.hpp:6,

1 个答案:

答案 0 :(得分:4)

这与anymap无关。升压信号根本不可复制。你可以将它们包装在一个智能指针中,例如shared_ptr,如果你想要一些可复制的东西并在它自己之后清理。