]> git.lyx.org Git - lyx.git/blob - boost/boost/signals/trackable.hpp
update boost to version 1.29.0
[lyx.git] / boost / boost / signals / trackable.hpp
1 // Boost.Signals library
2 //
3 // Copyright (C) 2001-2002 Doug Gregor (gregod@cs.rpi.edu)
4 //
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.
10 //
11 // This software is provided "as is" without express or implied warranty,
12 // and with no claim as to its suitability for any purpose.
13  
14 // For more information, see http://www.boost.org
15
16 #ifndef BOOST_SIGNALS_TRACKABLE_HPP
17 #define BOOST_SIGNALS_TRACKABLE_HPP
18
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>
24 #include <list>
25 #include <vector>
26
27 namespace boost {
28
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.
33   class trackable {
34   private:
35     static void signal_disconnected(void* obj, void* data);
36
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;
40     
41   protected:
42     trackable() : connected_signals(), dying(false) {}
43     trackable(const trackable&) : connected_signals(), dying(false) {}
44     ~trackable();
45     
46     trackable& operator=(const trackable&)
47     {
48       connected_signals.clear();
49       return *this;
50     }
51     
52   private:
53     typedef std::list<connection> connection_list;
54     typedef connection_list::iterator connection_iterator;
55     
56     // List of connections that this object is part of
57     mutable connection_list connected_signals;
58     
59     // True when the object is being destroyed
60     mutable bool dying;
61   };
62
63   namespace detail {
64     template<bool Cond> struct truth {};
65
66     // A visitor that adds each trackable object to a vector
67     class bound_objects_visitor {
68     public:
69       bound_objects_visitor(std::vector<const trackable*>& v) : 
70         bound_objects(v) 
71       {
72       }
73
74       template<typename T>
75       void operator()(const T& t) const
76       {
77         decode(t, 0);
78       }
79
80     private:
81       // decode() decides between a reference wrapper and anything else
82       template<typename T>
83       void decode(const reference_wrapper<T>& t, int) const
84       {
85         add_if_trackable(t.get_pointer());
86       }
87
88       template<typename T>
89       void decode(const T& t, long) const
90       {
91         typedef truth<(is_pointer<T>::value)> is_a_pointer;
92         maybe_get_pointer(t, is_a_pointer());
93       }
94
95       // maybe_get_pointer() decides between a pointer and a non-pointer
96       template<typename T>
97       void maybe_get_pointer(const T& t, truth<true>) const
98       {
99         add_if_trackable(t);
100       }
101
102       template<typename T>
103       void maybe_get_pointer(const T& t, truth<false>) const
104       {
105         // Take the address of this object, because the object itself may be
106         // trackable
107         add_if_trackable(addressof(t));
108       }
109
110       // add_if_trackable() adds trackable objects to the list of bound objects
111       inline void add_if_trackable(const trackable* b) const
112       {
113         if (b) {
114           bound_objects.push_back(b);
115         }
116       }
117
118       inline void add_if_trackable(const void*) const
119       {
120       }
121
122       template<typename R>
123       inline void add_if_trackable(R (*)()) const
124       {
125       }
126
127       template<typename R, typename T1>
128       inline void add_if_trackable(R (*)(T1)) const
129       {
130       }
131
132       template<typename R, typename T1, typename T2>
133       inline void add_if_trackable(R (*)(T1, T2)) const
134       {
135       }
136
137       template<typename R, typename T1, typename T2, typename T3>
138       inline void add_if_trackable(R (*)(T1, T2, T3)) const
139       {
140       }
141
142       template<typename R, typename T1, typename T2, typename T3, typename T4>
143       inline void add_if_trackable(R (*)(T1, T2, T3, T4)) const
144       {
145       }
146
147       template<typename R, typename T1, typename T2, typename T3, typename T4,
148                typename T5>
149       inline void add_if_trackable(R (*)(T1, T2, T3, T4, T5)) const
150       {
151       }
152
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
156       {
157       }
158
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
162       {
163       }
164
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
168       {
169       }
170
171       template<typename R, typename T1, typename T2, typename T3, typename T4,
172                typename T5, typename T6, typename T7, typename T8, typename T9>
173       inline void 
174       add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6, T7, T8, T9)) const
175       {
176       }
177
178       template<typename R, typename T1, typename T2, typename T3, typename T4,
179                typename T5, typename T6, typename T7, typename T8, typename T9,
180                typename T10>
181       inline void 
182       add_if_trackable(R (*)(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)) const
183       {
184       }
185
186       mutable std::vector<const trackable*>& bound_objects;
187     };
188   } // end namespace detail
189 } // end namespace BOOST_SIGNALS_NAMESPACE
190
191 } // end namespace boost
192
193 #endif // BOOST_SIGNALS_TRACKABLE_HPP