connection.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. boost::signals2::connection provides a handle to a signal/slot connection.
  3. Author: Frank Mori Hess <fmhess@users.sourceforge.net>
  4. Begin: 2007-01-23
  5. */
  6. // Copyright Frank Mori Hess 2007-2008.
  7. // Distributed under the Boost Software License, Version
  8. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // See http://www.boost.org/libs/signals2 for library home page.
  11. #ifndef BOOST_SIGNALS2_CONNECTION_HPP
  12. #define BOOST_SIGNALS2_CONNECTION_HPP
  13. #include <boost/function.hpp>
  14. #include <boost/mpl/bool.hpp>
  15. #include <boost/noncopyable.hpp>
  16. #include <boost/shared_ptr.hpp>
  17. #include <boost/signals2/detail/null_output_iterator.hpp>
  18. #include <boost/signals2/detail/unique_lock.hpp>
  19. #include <boost/signals2/slot.hpp>
  20. #include <boost/weak_ptr.hpp>
  21. namespace boost
  22. {
  23. namespace signals2
  24. {
  25. extern inline void null_deleter(const void*) {}
  26. namespace detail
  27. {
  28. class connection_body_base
  29. {
  30. public:
  31. connection_body_base():
  32. _connected(true)
  33. {
  34. }
  35. virtual ~connection_body_base() {}
  36. void disconnect()
  37. {
  38. unique_lock<connection_body_base> local_lock(*this);
  39. nolock_disconnect();
  40. }
  41. void nolock_disconnect()
  42. {
  43. _connected = false;
  44. }
  45. virtual bool connected() const = 0;
  46. shared_ptr<void> get_blocker()
  47. {
  48. unique_lock<connection_body_base> local_lock(*this);
  49. shared_ptr<void> blocker = _weak_blocker.lock();
  50. if(blocker == shared_ptr<void>())
  51. {
  52. blocker.reset(this, &null_deleter);
  53. _weak_blocker = blocker;
  54. }
  55. return blocker;
  56. }
  57. bool blocked() const
  58. {
  59. return !_weak_blocker.expired();
  60. }
  61. bool nolock_nograb_blocked() const
  62. {
  63. return nolock_nograb_connected() == false || blocked();
  64. }
  65. bool nolock_nograb_connected() const {return _connected;}
  66. // expose part of Lockable concept of mutex
  67. virtual void lock() = 0;
  68. virtual void unlock() = 0;
  69. protected:
  70. mutable bool _connected;
  71. weak_ptr<void> _weak_blocker;
  72. };
  73. template<typename GroupKey, typename SlotType, typename Mutex>
  74. class connection_body: public connection_body_base
  75. {
  76. public:
  77. typedef Mutex mutex_type;
  78. connection_body(const SlotType &slot_in):
  79. slot(slot_in)
  80. {
  81. }
  82. virtual ~connection_body() {}
  83. virtual bool connected() const
  84. {
  85. unique_lock<mutex_type> local_lock(_mutex);
  86. nolock_grab_tracked_objects(detail::null_output_iterator());
  87. return nolock_nograb_connected();
  88. }
  89. const GroupKey& group_key() const {return _group_key;}
  90. void set_group_key(const GroupKey &key) {_group_key = key;}
  91. bool nolock_slot_expired() const
  92. {
  93. bool expired = slot.expired();
  94. if(expired == true)
  95. {
  96. _connected = false;
  97. }
  98. return expired;
  99. }
  100. template<typename OutputIterator>
  101. void nolock_grab_tracked_objects(OutputIterator inserter) const
  102. {
  103. slot_base::tracked_container_type::const_iterator it;
  104. for(it = slot.tracked_objects().begin();
  105. it != slot.tracked_objects().end();
  106. ++it)
  107. {
  108. void_shared_ptr_variant locked_object
  109. (
  110. apply_visitor
  111. (
  112. detail::lock_weak_ptr_visitor(),
  113. *it
  114. )
  115. );
  116. if(apply_visitor(detail::expired_weak_ptr_visitor(), *it))
  117. {
  118. _connected = false;
  119. return;
  120. }
  121. *inserter++ = locked_object;
  122. }
  123. }
  124. // expose Lockable concept of mutex
  125. virtual void lock()
  126. {
  127. _mutex.lock();
  128. }
  129. virtual void unlock()
  130. {
  131. _mutex.unlock();
  132. }
  133. SlotType slot;
  134. private:
  135. mutable mutex_type _mutex;
  136. GroupKey _group_key;
  137. };
  138. }
  139. class shared_connection_block;
  140. class connection
  141. {
  142. public:
  143. friend class shared_connection_block;
  144. connection() {}
  145. connection(const connection &other): _weak_connection_body(other._weak_connection_body)
  146. {}
  147. connection(const boost::weak_ptr<detail::connection_body_base> &connectionBody):
  148. _weak_connection_body(connectionBody)
  149. {}
  150. ~connection() {}
  151. void disconnect() const
  152. {
  153. boost::shared_ptr<detail::connection_body_base> connectionBody(_weak_connection_body.lock());
  154. if(connectionBody == 0) return;
  155. connectionBody->disconnect();
  156. }
  157. bool connected() const
  158. {
  159. boost::shared_ptr<detail::connection_body_base> connectionBody(_weak_connection_body.lock());
  160. if(connectionBody == 0) return false;
  161. return connectionBody->connected();
  162. }
  163. bool blocked() const
  164. {
  165. boost::shared_ptr<detail::connection_body_base> connectionBody(_weak_connection_body.lock());
  166. if(connectionBody == 0) return true;
  167. return connectionBody->blocked();
  168. }
  169. bool operator==(const connection& other) const
  170. {
  171. boost::shared_ptr<detail::connection_body_base> connectionBody(_weak_connection_body.lock());
  172. boost::shared_ptr<detail::connection_body_base> otherConnectionBody(other._weak_connection_body.lock());
  173. return connectionBody == otherConnectionBody;
  174. }
  175. bool operator!=(const connection& other) const
  176. {
  177. return !(*this == other);
  178. }
  179. bool operator<(const connection& other) const
  180. {
  181. boost::shared_ptr<detail::connection_body_base> connectionBody(_weak_connection_body.lock());
  182. boost::shared_ptr<detail::connection_body_base> otherConnectionBody(other._weak_connection_body.lock());
  183. return connectionBody < otherConnectionBody;
  184. }
  185. void swap(connection &other)
  186. {
  187. using std::swap;
  188. swap(_weak_connection_body, other._weak_connection_body);
  189. }
  190. protected:
  191. boost::weak_ptr<detail::connection_body_base> _weak_connection_body;
  192. };
  193. inline void swap(connection &conn1, connection &conn2)
  194. {
  195. conn1.swap(conn2);
  196. }
  197. class scoped_connection: public connection
  198. {
  199. public:
  200. scoped_connection() {}
  201. scoped_connection(const connection &other):
  202. connection(other)
  203. {}
  204. ~scoped_connection()
  205. {
  206. disconnect();
  207. }
  208. scoped_connection& operator=(const connection &rhs)
  209. {
  210. disconnect();
  211. connection::operator=(rhs);
  212. return *this;
  213. }
  214. connection release()
  215. {
  216. connection conn(_weak_connection_body);
  217. _weak_connection_body.reset();
  218. return conn;
  219. }
  220. private:
  221. scoped_connection(const scoped_connection &other);
  222. scoped_connection& operator=(const scoped_connection &rhs);
  223. };
  224. // Sun 5.9 compiler doesn't find the swap for base connection class when
  225. // arguments are scoped_connection, so we provide this explicitly.
  226. inline void swap(scoped_connection &conn1, scoped_connection &conn2)
  227. {
  228. conn1.swap(conn2);
  229. }
  230. }
  231. }
  232. #endif // BOOST_SIGNALS2_CONNECTION_HPP