Chris@16
|
1 // Boost.Signals2 library
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Frank Mori Hess 2007-2008.
|
Chris@16
|
4 // Copyright Timmo Stange 2007.
|
Chris@16
|
5 // Copyright Douglas Gregor 2001-2004. Use, modification and
|
Chris@16
|
6 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
7 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9
|
Chris@16
|
10 // For more information, see http://www.boost.org
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_SIGNALS2_SLOT_BASE_HPP
|
Chris@16
|
13 #define BOOST_SIGNALS2_SLOT_BASE_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/shared_ptr.hpp>
|
Chris@16
|
16 #include <boost/weak_ptr.hpp>
|
Chris@16
|
17 #include <boost/signals2/detail/foreign_ptr.hpp>
|
Chris@16
|
18 #include <boost/signals2/expired_slot.hpp>
|
Chris@16
|
19 #include <boost/signals2/signal_base.hpp>
|
Chris@16
|
20 #include <boost/throw_exception.hpp>
|
Chris@16
|
21 #include <boost/variant/apply_visitor.hpp>
|
Chris@16
|
22 #include <boost/variant/variant.hpp>
|
Chris@16
|
23 #include <vector>
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost
|
Chris@16
|
26 {
|
Chris@16
|
27 namespace signals2
|
Chris@16
|
28 {
|
Chris@16
|
29 namespace detail
|
Chris@16
|
30 {
|
Chris@16
|
31 class tracked_objects_visitor;
|
Chris@16
|
32
|
Chris@16
|
33 typedef boost::variant<boost::weak_ptr<void>, detail::foreign_void_weak_ptr > void_weak_ptr_variant;
|
Chris@16
|
34 typedef boost::variant<boost::shared_ptr<void>, detail::foreign_void_shared_ptr > void_shared_ptr_variant;
|
Chris@16
|
35 class lock_weak_ptr_visitor
|
Chris@16
|
36 {
|
Chris@16
|
37 public:
|
Chris@16
|
38 typedef void_shared_ptr_variant result_type;
|
Chris@16
|
39 template<typename WeakPtr>
|
Chris@16
|
40 result_type operator()(const WeakPtr &wp) const
|
Chris@16
|
41 {
|
Chris@16
|
42 return wp.lock();
|
Chris@16
|
43 }
|
Chris@16
|
44 };
|
Chris@16
|
45 class expired_weak_ptr_visitor
|
Chris@16
|
46 {
|
Chris@16
|
47 public:
|
Chris@16
|
48 typedef bool result_type;
|
Chris@16
|
49 template<typename WeakPtr>
|
Chris@16
|
50 bool operator()(const WeakPtr &wp) const
|
Chris@16
|
51 {
|
Chris@16
|
52 return wp.expired();
|
Chris@16
|
53 }
|
Chris@16
|
54 };
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 class slot_base
|
Chris@16
|
58 {
|
Chris@16
|
59 public:
|
Chris@16
|
60 typedef std::vector<detail::void_weak_ptr_variant> tracked_container_type;
|
Chris@16
|
61 typedef std::vector<detail::void_shared_ptr_variant> locked_container_type;
|
Chris@16
|
62
|
Chris@16
|
63 const tracked_container_type& tracked_objects() const {return _tracked_objects;}
|
Chris@16
|
64 locked_container_type lock() const
|
Chris@16
|
65 {
|
Chris@16
|
66 locked_container_type locked_objects;
|
Chris@16
|
67 tracked_container_type::const_iterator it;
|
Chris@16
|
68 for(it = tracked_objects().begin(); it != tracked_objects().end(); ++it)
|
Chris@16
|
69 {
|
Chris@16
|
70 locked_objects.push_back(apply_visitor(detail::lock_weak_ptr_visitor(), *it));
|
Chris@16
|
71 if(apply_visitor(detail::expired_weak_ptr_visitor(), *it))
|
Chris@16
|
72 {
|
Chris@16
|
73 boost::throw_exception(expired_slot());
|
Chris@16
|
74 }
|
Chris@16
|
75 }
|
Chris@16
|
76 return locked_objects;
|
Chris@16
|
77 }
|
Chris@16
|
78 bool expired() const
|
Chris@16
|
79 {
|
Chris@16
|
80 tracked_container_type::const_iterator it;
|
Chris@16
|
81 for(it = tracked_objects().begin(); it != tracked_objects().end(); ++it)
|
Chris@16
|
82 {
|
Chris@16
|
83 if(apply_visitor(detail::expired_weak_ptr_visitor(), *it)) return true;
|
Chris@16
|
84 }
|
Chris@16
|
85 return false;
|
Chris@16
|
86 }
|
Chris@16
|
87 protected:
|
Chris@16
|
88 friend class detail::tracked_objects_visitor;
|
Chris@16
|
89
|
Chris@16
|
90 void track_signal(const signal_base &signal)
|
Chris@16
|
91 {
|
Chris@16
|
92 _tracked_objects.push_back(signal.lock_pimpl());
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 tracked_container_type _tracked_objects;
|
Chris@16
|
96 };
|
Chris@16
|
97 }
|
Chris@16
|
98 } // end namespace boost
|
Chris@16
|
99
|
Chris@16
|
100 #endif // BOOST_SIGNALS2_SLOT_BASE_HPP
|