1 // Boost.Signals library
3 // Copyright (C) 2001-2002 Doug Gregor (gregod@cs.rpi.edu)
5 // Permission to copy, use, sell and distribute this software is granted
6 // provided this copyright notice appears in all copies.
7 // Permission to modify the code and to distribute modified code is granted
8 // provided this copyright notice appears in all copies, and a notice
9 // that the code was modified is included with the copyright notice.
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
14 // For more information, see http://www.boost.org
16 #ifndef BOOST_SIGNALS_TRACKABLE_HPP
17 #define BOOST_SIGNALS_TRACKABLE_HPP
19 #include <boost/type_traits.hpp>
20 #include <boost/signals/connection.hpp>
21 #include <boost/pending/ct_if.hpp>
22 #include <boost/ref.hpp>
23 #include <boost/utility/addressof.hpp>
29 namespace BOOST_SIGNALS_NAMESPACE {
30 // Base class for "trackable" objects that can be tracked when they are
31 // bound in slot target functions. When a trackable object is destroyed,
32 // the signal/slot connections are disconnected automatically.
35 static void signal_disconnected(void* obj, void* data);
37 friend class detail::signal_base_impl;
38 friend class detail::slot_base;
39 void signal_connected(connection, BOOST_SIGNALS_NAMESPACE::detail::bound_object&) const;
42 trackable() : connected_signals(), dying(false) {}
43 trackable(const trackable&) : connected_signals(), dying(false) {}
46 trackable& operator=(const trackable&)
48 connected_signals.clear();
53 typedef std::list<connection> connection_list;
54 typedef connection_list::iterator connection_iterator;
56 // List of connections that this object is part of
57 mutable connection_list connected_signals;
59 // True when the object is being destroyed
64 template<bool Cond> struct truth {};
66 // A visitor that adds each trackable object to a vector
67 class bound_objects_visitor {
69 bound_objects_visitor(std::vector<const trackable*>& v) :
75 void operator()(const T& t) const
81 // decode() decides between a reference wrapper and anything else
83 void decode(const reference_wrapper<T>& t, int) const
85 add_if_trackable(t.get_pointer());
89 void decode(const T& t, long) const
91 typedef truth<(is_pointer<T>::value)> is_a_pointer;
92 maybe_get_pointer(t, is_a_pointer());
95 // maybe_get_pointer() decides between a pointer and a non-pointer
97 void maybe_get_pointer(const T& t, truth<true>) const
103 void maybe_get_pointer(const T& t, truth<false>) const
105 // Take the address of this object, because the object itself may be
107 add_if_trackable(addressof(t));
110 // add_if_trackable() adds trackable objects to the list of bound objects
111 inline void add_if_trackable(const trackable* b) const
114 bound_objects.push_back(b);
118 inline void add_if_trackable(const void*) const
123 inline void add_if_trackable(R (*)()) const
127 template<typename R, typename T1>
128 inline void add_if_trackable(R (*)(T1)) const
132 template<typename R, typename T1, typename T2>
133 inline void add_if_trackable(R (*)(T1, T2)) const
137 template<typename R, typename T1, typename T2, typename T3>
138 inline void add_if_trackable(R (*)(T1, T2, T3)) const
142 template<typename R, typename T1, typename T2, typename T3, typename T4>
143 inline void add_if_trackable(R (*)(T1, T2, T3, T4)) const
147 template<typename R, typename T1, typename T2, typename T3, typename T4,
149 inline void add_if_trackable(R (*)(T1, T2, T3, T4, T5)) const
153 template<typename R, typename T1, typename T2, typename T3, typename T4,
154 typename T5, typename T6>
155 inline void add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6)) const
159 template<typename R, typename T1, typename T2, typename T3, typename T4,
160 typename T5, typename T6, typename T7>
161 inline void add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6, T7)) const
165 template<typename R, typename T1, typename T2, typename T3, typename T4,
166 typename T5, typename T6, typename T7, typename T8>
167 inline void add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6, T7, T8)) const
171 template<typename R, typename T1, typename T2, typename T3, typename T4,
172 typename T5, typename T6, typename T7, typename T8, typename T9>
174 add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6, T7, T8, T9)) const
178 template<typename R, typename T1, typename T2, typename T3, typename T4,
179 typename T5, typename T6, typename T7, typename T8, typename T9,
182 add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)) const
186 mutable std::vector<const trackable*>& bound_objects;
188 } // end namespace detail
189 } // end namespace BOOST_SIGNALS_NAMESPACE
191 } // end namespace boost
193 #endif // BOOST_SIGNALS_TRACKABLE_HPP