Boost之事件處理Boost.Signals

來源:互聯網
上載者:User

20130811 wcdj

很多開發人員在聽到術語'事件處理'時就會想到GUI:點擊一下某個按鈕,相關聯的功能就會被執行。 點擊本身就是事件,而功能就是相對應的事件處理器。這一模式的使用當然不僅限於GUI。 一般情況下,任意對象都可以調用基於特定事件的專門函數。 本章所介紹的
Boost.Signals 庫提供了一個簡單的方法在 C++ 中應用這一模式。

嚴格來說,Boost.Function 庫也可以用於事件處理(http://blog.csdn.net/delphiwcdj/article/details/9564659)。 不過,Boost.Function 和 Boost.Signals 之間的一個主要區別在於,Boost.Signals 能夠將一個以上的事件處理器關聯至單個事件。
因此,Boost.Signals 可以更好地支援事件驅動的開發,當需要進行事件處理時,應作為第一選擇。

boost_1_54_0/boost/signals.hpp標頭檔說明:

#ifndef BOOST_SIGNAL_HPP#define BOOST_SIGNAL_HPP#ifndef BOOST_SIGNALS_NO_DEPRECATION_WARNING# if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__DMC__)#  pragma message ("Warning: Boost.Signals is no longer being maintained and is now deprecated. Please switch to Boost.Signals2. To disable this warning message, define BOOST_SIGNALS_NO_DEPRECATION_WARNING.")# elif defined(__GNUC__) || defined(__HP_aCC) || defined(__SUNPRO_CC) || defined(__IBMCPP__)#  warning                  "Boost.Signals is no longer being maintained and is now deprecated. Please switch to Boost.Signals2. To disable this warning message, define BOOST_SIGNALS_NO_DEPRECATION_WARNING."# endif#endif

測試代碼:

#include <boost/signal.hpp> //#include <boost/signals2.hpp> #include <iostream> void func() { std::cout << "Hello, world!" << std::endl; } int main() { boost::signal<void ()> s; s.connect(func); s(); } 

以上代碼在x86_64使用boost_1_54_0編譯連結時報錯:

mba:signals gerryyang$ g++ singals.cpp -o singalsIn file included from singals.cpp:1:/usr/local/include/boost/signal.hpp:17:4: warning: #warning "Boost.Signals is no longer being maintained and is now deprecated. Please switch to Boost.Signals2. To disable this warning message, define BOOST_SIGNALS_NO_DEPRECATION_WARNING."Undefined symbols for architecture x86_64:  "boost::signals::connection::~connection()", referenced from:      _main in ccrwHjrA.o      boost::signals::detail::slot_base::data_t::~data_t()in ccrwHjrA.o  "boost::signals::detail::signal_base::signal_base(boost::function2<bool, boost::signals::detail::stored_group, boost::signals::detail::stored_group> const&, boost::any const&)", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::signal0(boost::last_value<void> const&, std::less<int> const&)in ccrwHjrA.o  "boost::signals::detail::signal_base::~signal_base()", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::signal0(boost::last_value<void> const&, std::less<int> const&)in ccrwHjrA.o      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::~signal0()in ccrwHjrA.o  "boost::signals::detail::named_slot_map::end()", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::operator()()in ccrwHjrA.o  "boost::signals::detail::named_slot_map::begin()", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::operator()()in ccrwHjrA.o  "boost::signals::detail::signal_base_impl::connect_slot(boost::any const&, boost::signals::detail::stored_group const&, boost::shared_ptr<boost::signals::detail::slot_base::data_t>, boost::signals::connect_position)", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::connect(boost::slot<boost::function<void ()()> > const&, boost::signals::connect_position)in ccrwHjrA.o  "boost::signals::detail::call_notification::call_notification(boost::shared_ptr<boost::signals::detail::signal_base_impl> const&)", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::operator()()in ccrwHjrA.o  "boost::signals::detail::call_notification::~call_notification()", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::operator()()in ccrwHjrA.o  "boost::signals::detail::slot_base::create_connection()", referenced from:      boost::slot<boost::function<void ()()> >::slot<void ()()>(void ( const&)())in ccrwHjrA.o  "boost::signals::trackable::~trackable()", referenced from:      boost::signal0<void, boost::last_value<void>, int, std::less<int>, boost::function<void ()()> >::~signal0()in ccrwHjrA.old: symbol(s) not found for architecture x86_64collect2: ld returned 1 exit status

參考:

[1] http://zh.highscore.de/cpp/boost/  第四章 事件處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.