shared_connection_block.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Boost.Signals2 library
  2. // Copyright Frank Mori Hess 2007-2008.
  3. // Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // For more information, see http://www.boost.org
  8. #ifndef BOOST_SIGNALS2_SHARED_CONNECTION_BLOCK_HPP
  9. #define BOOST_SIGNALS2_SHARED_CONNECTION_BLOCK_HPP
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/signals2/connection.hpp>
  12. #include <boost/weak_ptr.hpp>
  13. namespace boost
  14. {
  15. namespace signals2
  16. {
  17. class shared_connection_block
  18. {
  19. public:
  20. shared_connection_block(const signals2::connection &conn = signals2::connection(),
  21. bool initially_blocked = true):
  22. _weak_connection_body(conn._weak_connection_body)
  23. {
  24. if(initially_blocked) block();
  25. }
  26. void block()
  27. {
  28. if(blocking()) return;
  29. boost::shared_ptr<detail::connection_body_base> connection_body(_weak_connection_body.lock());
  30. if(connection_body == 0)
  31. {
  32. // Make _blocker non-empty so the blocking() method still returns the correct value
  33. // after the connection has expired.
  34. _blocker.reset(static_cast<int*>(0));
  35. return;
  36. }
  37. _blocker = connection_body->get_blocker();
  38. }
  39. void unblock()
  40. {
  41. _blocker.reset();
  42. }
  43. bool blocking() const
  44. {
  45. shared_ptr<void> empty;
  46. return _blocker < empty || empty < _blocker;
  47. }
  48. signals2::connection connection() const
  49. {
  50. return signals2::connection(_weak_connection_body);
  51. }
  52. private:
  53. boost::weak_ptr<detail::connection_body_base> _weak_connection_body;
  54. shared_ptr<void> _blocker;
  55. };
  56. }
  57. } // end namespace boost
  58. #endif // BOOST_SIGNALS2_SHARED_CONNECTION_BLOCK_HPP