1 // Boost.Signals library
3 // Copyright Douglas Gregor 2001-2004. 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)
8 // For more information, see http://www.boost.org
10 #ifndef BOOST_SIGNALS_TRACKABLE_HPP
11 #define BOOST_SIGNALS_TRACKABLE_HPP
13 #include <boost/type_traits.hpp>
14 #include <boost/signals/connection.hpp>
15 #include <boost/ref.hpp>
16 #include <boost/utility/addressof.hpp>
20 #ifdef BOOST_HAS_ABI_HEADERS
21 # include BOOST_ABI_PREFIX
26 namespace BOOST_SIGNALS_NAMESPACE
{
27 // Base class for "trackable" objects that can be tracked when they are
28 // bound in slot target functions. When a trackable object is destroyed,
29 // the signal/slot connections are disconnected automatically.
30 class BOOST_SIGNALS_DECL trackable
{
32 static void signal_disconnected(void* obj
, void* data
);
34 friend class detail::signal_base_impl
;
35 friend class detail::slot_base
;
36 void signal_connected(connection
, BOOST_SIGNALS_NAMESPACE::detail::bound_object
&) const;
39 trackable() : connected_signals(), dying(false) {}
40 trackable(const trackable
&) : connected_signals(), dying(false) {}
43 trackable
& operator=(const trackable
&)
46 connected_signals
.clear();
52 typedef std::list
<connection
> connection_list
;
53 typedef connection_list::iterator connection_iterator
;
55 // List of connections that this object is part of
56 mutable connection_list connected_signals
;
58 // True when the object is being destroyed
63 template<bool Cond
> struct truth
{};
65 // A visitor that adds each trackable object to a vector
66 class bound_objects_visitor
{
68 bound_objects_visitor(std::vector
<const trackable
*>& v
) :
74 void operator()(const T
& t
) const
80 // decode() decides between a reference wrapper and anything else
82 void decode(const reference_wrapper
<T
>& t
, int) const
84 add_if_trackable(t
.get_pointer());
88 void decode(const T
& t
, long) const
90 typedef truth
<(is_pointer
<T
>::value
)> is_a_pointer
;
91 maybe_get_pointer(t
, is_a_pointer());
94 // maybe_get_pointer() decides between a pointer and a non-pointer
96 void maybe_get_pointer(const T
& t
, truth
<true>) const
102 void maybe_get_pointer(const T
& t
, truth
<false>) const
104 // Take the address of this object, because the object itself may be
106 add_if_trackable(boost::addressof(t
));
109 // add_if_trackable() adds trackable objects to the list of bound objects
110 inline void add_if_trackable(const trackable
* b
) const
113 bound_objects
.push_back(b
);
117 inline void add_if_trackable(const void*) const { }
120 inline void add_if_trackable(R (*)()) const { }
122 template<typename R
, typename T1
>
123 inline void add_if_trackable(R (*)(T1
)) const { }
125 template<typename R
, typename T1
, typename T2
>
126 inline void add_if_trackable(R (*)(T1
, T2
)) const { }
128 template<typename R
, typename T1
, typename T2
, typename T3
>
129 inline void add_if_trackable(R (*)(T1
, T2
, T3
)) const { }
131 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
>
132 inline void add_if_trackable(R (*)(T1
, T2
, T3
, T4
)) const { }
134 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
,
136 inline void add_if_trackable(R (*)(T1
, T2
, T3
, T4
, T5
)) const { }
138 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
,
139 typename T5
, typename T6
>
140 inline void add_if_trackable(R (*)(T1
, T2
, T3
, T4
, T5
, T6
)) const { }
142 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
,
143 typename T5
, typename T6
, typename T7
>
144 inline void add_if_trackable(R (*)(T1
, T2
, T3
, T4
, T5
, T6
, T7
)) const { }
146 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
,
147 typename T5
, typename T6
, typename T7
, typename T8
>
149 add_if_trackable(R (*)(T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
)) const { }
151 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
,
152 typename T5
, typename T6
, typename T7
, typename T8
, typename T9
>
154 add_if_trackable(R (*)(T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
, T9
)) const { }
156 template<typename R
, typename T1
, typename T2
, typename T3
, typename T4
,
157 typename T5
, typename T6
, typename T7
, typename T8
, typename T9
,
160 add_if_trackable(R (*)(T1
, T2
, T3
, T4
, T5
, T6
, T7
, T8
, T9
, T10
)) const { }
162 std::vector
<const trackable
*>& bound_objects
;
164 } // end namespace detail
165 } // end namespace BOOST_SIGNALS_NAMESPACE
167 } // end namespace boost
169 #ifdef BOOST_HAS_ABI_HEADERS
170 # include BOOST_ABI_SUFFIX
173 #endif // BOOST_SIGNALS_TRACKABLE_HPP