Chris@16
|
1 // Boost.Signals library
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Douglas Gregor 2001-2004. Use, modification and
|
Chris@16
|
4 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7
|
Chris@16
|
8 // For more information, see http://www.boost.org
|
Chris@16
|
9
|
Chris@16
|
10 #ifndef BOOST_SIGNALS_SLOT_HEADER
|
Chris@16
|
11 #define BOOST_SIGNALS_SLOT_HEADER
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/signals/detail/signals_common.hpp>
|
Chris@16
|
14 #include <boost/signals/connection.hpp>
|
Chris@16
|
15 #include <boost/signals/trackable.hpp>
|
Chris@16
|
16 #include <boost/visit_each.hpp>
|
Chris@16
|
17 #include <boost/shared_ptr.hpp>
|
Chris@16
|
18 #include <cassert>
|
Chris@16
|
19
|
Chris@16
|
20 #ifdef BOOST_HAS_ABI_HEADERS
|
Chris@16
|
21 # include BOOST_ABI_PREFIX
|
Chris@16
|
22 #endif
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost {
|
Chris@16
|
25 namespace BOOST_SIGNALS_NAMESPACE {
|
Chris@16
|
26 namespace detail {
|
Chris@16
|
27 class BOOST_SIGNALS_DECL slot_base {
|
Chris@16
|
28 // We would have to enumerate all of the signalN classes here as
|
Chris@16
|
29 // friends to make this private (as it otherwise should be). We can't
|
Chris@16
|
30 // name all of them because we don't know how many there are.
|
Chris@16
|
31 public:
|
Chris@16
|
32 struct data_t {
|
Chris@16
|
33 std::vector<const trackable*> bound_objects;
|
Chris@16
|
34 connection watch_bound_objects;
|
Chris@16
|
35 };
|
Chris@16
|
36 shared_ptr<data_t> get_data() const { return data; }
|
Chris@16
|
37
|
Chris@16
|
38 // Get the set of bound objects
|
Chris@16
|
39 std::vector<const trackable*>& get_bound_objects() const
|
Chris@16
|
40 { return data->bound_objects; }
|
Chris@16
|
41
|
Chris@16
|
42 // Determine if this slot is still "active", i.e., all of the bound
|
Chris@16
|
43 // objects still exist
|
Chris@16
|
44 bool is_active() const
|
Chris@16
|
45 { return data->watch_bound_objects.connected(); }
|
Chris@16
|
46
|
Chris@16
|
47 protected:
|
Chris@16
|
48 // Create a connection for this slot
|
Chris@16
|
49 void create_connection();
|
Chris@16
|
50
|
Chris@16
|
51 shared_ptr<data_t> data;
|
Chris@16
|
52
|
Chris@16
|
53 private:
|
Chris@16
|
54 static void bound_object_destructed(void*, void*) {}
|
Chris@16
|
55 };
|
Chris@16
|
56 } // end namespace detail
|
Chris@16
|
57
|
Chris@16
|
58 // Get the slot so that it can be copied
|
Chris@16
|
59 template<typename F>
|
Chris@16
|
60 reference_wrapper<const F>
|
Chris@16
|
61 get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
|
Chris@16
|
62 { return reference_wrapper<const F>(f); }
|
Chris@16
|
63
|
Chris@16
|
64 template<typename F>
|
Chris@16
|
65 const F&
|
Chris@16
|
66 get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
|
Chris@16
|
67 { return f; }
|
Chris@16
|
68
|
Chris@16
|
69 template<typename F>
|
Chris@16
|
70 const F&
|
Chris@16
|
71 get_invocable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
|
Chris@16
|
72 { return f; }
|
Chris@16
|
73
|
Chris@16
|
74 // Get the slot so that it can be inspected for trackable objects
|
Chris@16
|
75 template<typename F>
|
Chris@16
|
76 const F&
|
Chris@16
|
77 get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::signal_tag)
|
Chris@16
|
78 { return f; }
|
Chris@16
|
79
|
Chris@16
|
80 template<typename F>
|
Chris@16
|
81 const F&
|
Chris@16
|
82 get_inspectable_slot(const reference_wrapper<F>& f, BOOST_SIGNALS_NAMESPACE::detail::reference_tag)
|
Chris@16
|
83 { return f.get(); }
|
Chris@16
|
84
|
Chris@16
|
85 template<typename F>
|
Chris@16
|
86 const F&
|
Chris@16
|
87 get_inspectable_slot(const F& f, BOOST_SIGNALS_NAMESPACE::detail::value_tag)
|
Chris@16
|
88 { return f; }
|
Chris@16
|
89
|
Chris@16
|
90 // Determines the type of the slot - is it a signal, a reference to a
|
Chris@16
|
91 // slot or just a normal slot.
|
Chris@16
|
92 template<typename F>
|
Chris@16
|
93 typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
|
Chris@16
|
94 tag_type(const F&)
|
Chris@16
|
95 {
|
Chris@16
|
96 typedef typename BOOST_SIGNALS_NAMESPACE::detail::get_slot_tag<F>::type
|
Chris@16
|
97 the_tag_type;
|
Chris@16
|
98 the_tag_type tag = the_tag_type();
|
Chris@16
|
99 return tag;
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 } // end namespace BOOST_SIGNALS_NAMESPACE
|
Chris@16
|
103
|
Chris@16
|
104 template<typename SlotFunction>
|
Chris@16
|
105 class slot : public BOOST_SIGNALS_NAMESPACE::detail::slot_base {
|
Chris@16
|
106 typedef BOOST_SIGNALS_NAMESPACE::detail::slot_base inherited;
|
Chris@16
|
107 typedef typename inherited::data_t data_t;
|
Chris@16
|
108
|
Chris@16
|
109 public:
|
Chris@16
|
110 template<typename F>
|
Chris@16
|
111 slot(const F& f) : slot_function(BOOST_SIGNALS_NAMESPACE::get_invocable_slot(f, BOOST_SIGNALS_NAMESPACE::tag_type(f)))
|
Chris@16
|
112 {
|
Chris@16
|
113 this->data.reset(new data_t);
|
Chris@16
|
114
|
Chris@16
|
115 // Visit each of the bound objects and store them for later use
|
Chris@16
|
116 // An exception thrown here will allow the basic_connection to be
|
Chris@16
|
117 // destroyed when this goes out of scope, and no other connections
|
Chris@16
|
118 // have been made.
|
Chris@16
|
119 BOOST_SIGNALS_NAMESPACE::detail::bound_objects_visitor
|
Chris@16
|
120 do_bind(this->data->bound_objects);
|
Chris@16
|
121 visit_each(do_bind,
|
Chris@16
|
122 BOOST_SIGNALS_NAMESPACE::get_inspectable_slot
|
Chris@16
|
123 (f, BOOST_SIGNALS_NAMESPACE::tag_type(f)));
|
Chris@16
|
124 create_connection();
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 #ifdef __BORLANDC__
|
Chris@16
|
128 template<typename F>
|
Chris@16
|
129 slot(F* f) : slot_function(f)
|
Chris@16
|
130 {
|
Chris@16
|
131 this->data.reset(new data_t);
|
Chris@16
|
132 create_connection();
|
Chris@16
|
133 }
|
Chris@16
|
134 #endif // __BORLANDC__
|
Chris@16
|
135
|
Chris@16
|
136 // We would have to enumerate all of the signalN classes here as friends
|
Chris@16
|
137 // to make this private (as it otherwise should be). We can't name all of
|
Chris@16
|
138 // them because we don't know how many there are.
|
Chris@16
|
139 public:
|
Chris@16
|
140 // Get the slot function to call the actual slot
|
Chris@16
|
141 const SlotFunction& get_slot_function() const { return slot_function; }
|
Chris@16
|
142
|
Chris@16
|
143 void release() const { data->watch_bound_objects.set_controlling(false); }
|
Chris@16
|
144
|
Chris@16
|
145 private:
|
Chris@16
|
146 slot(); // no default constructor
|
Chris@16
|
147 slot& operator=(const slot&); // no assignment operator
|
Chris@16
|
148
|
Chris@16
|
149 SlotFunction slot_function;
|
Chris@16
|
150 };
|
Chris@16
|
151 } // end namespace boost
|
Chris@16
|
152
|
Chris@16
|
153 #ifdef BOOST_HAS_ABI_HEADERS
|
Chris@16
|
154 # include BOOST_ABI_SUFFIX
|
Chris@16
|
155 #endif
|
Chris@16
|
156
|
Chris@16
|
157 #endif // BOOST_SIGNALS_SLOT_HEADER
|