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