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 // This file intentionally does not have include guards, because it is meant
11 // to be included multiple times (one for each signalN class). The
12 // BOOST_SIGNALS_SIGNAL_TEMPLATE_HEADER_INCLUDED macro merely serves to
13 // suppress reinclusion of the files that this header depends on.
15 #ifndef BOOST_SIGNALS_SIGNAL_TEMPLATE_HEADER_INCLUDED
16 #define BOOST_SIGNALS_SIGNAL_TEMPLATE_HEADER_INCLUDED
17 # include <boost/config.hpp>
18 # include <boost/signals/connection.hpp>
19 # include <boost/utility.hpp>
20 # include <boost/ref.hpp>
21 # include <boost/signals/slot.hpp>
22 # include <boost/last_value.hpp>
23 # include <boost/signals/detail/signal_base.hpp>
24 # include <boost/signals/detail/slot_call_iterator.hpp>
25 # include <boost/mpl/bool.hpp>
26 # include <boost/type_traits/is_convertible.hpp>
28 # include <functional>
30 #endif // !BOOST_SIGNALS_SIGNAL_TEMPLATE_HEADER_INCLUDED
32 #ifdef BOOST_HAS_ABI_HEADERS
33 # include BOOST_ABI_PREFIX
36 // Include the appropriate functionN header
37 #define BOOST_SIGNAL_FUNCTION_N_HEADER BOOST_JOIN(<boost/function/function,BOOST_SIGNALS_NUM_ARGS.hpp>)
38 #include BOOST_SIGNAL_FUNCTION_N_HEADER
40 // Determine if a comma should follow a listing of the arguments/parameters
41 #if BOOST_SIGNALS_NUM_ARGS == 0
42 # define BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
44 # define BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS ,
45 #endif // BOOST_SIGNALS_NUM_ARGS > 0
47 // Define class names used
48 #define BOOST_SIGNALS_SIGNAL BOOST_JOIN(signal,BOOST_SIGNALS_NUM_ARGS)
49 #define BOOST_SIGNALS_FUNCTION BOOST_JOIN(function,BOOST_SIGNALS_NUM_ARGS)
50 #define BOOST_SIGNALS_ARGS_STRUCT BOOST_JOIN(args,BOOST_SIGNALS_NUM_ARGS)
51 #define BOOST_SIGNALS_CALL_BOUND BOOST_JOIN(call_bound,BOOST_SIGNALS_NUM_ARGS)
53 // Define commonly-used instantiations
54 #define BOOST_SIGNALS_ARGS_STRUCT_INST \
55 BOOST_SIGNALS_NAMESPACE::detail::BOOST_SIGNALS_ARGS_STRUCT<BOOST_SIGNALS_TEMPLATE_ARGS>
58 namespace BOOST_SIGNALS_NAMESPACE {
60 // Holds the arguments for a bound slot call in a single place
61 template<BOOST_SIGNALS_TEMPLATE_PARMS
62 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
64 struct BOOST_SIGNALS_ARGS_STRUCT {
65 BOOST_SIGNALS_ARGS_STRUCT(BOOST_SIGNALS_COPY_PARMS)
66 BOOST_SIGNALS_INIT_ARGS
70 BOOST_SIGNALS_ARGS_AS_MEMBERS
73 // Function object that calls the function object given to it, passing
74 // the bound arguments along to that underlying function object
76 struct BOOST_SIGNALS_CALL_BOUND {
77 template<BOOST_SIGNALS_TEMPLATE_PARMS
78 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
81 typedef BOOST_SIGNALS_ARGS_STRUCT<BOOST_SIGNALS_TEMPLATE_ARGS>*
86 typedef R result_type;
89 caller(args_type a) : args(a) {}
91 template<typename Pair>
92 R operator()(const Pair& slot) const
94 F* target = const_cast<F*>(any_cast<F>(&slot.second));
95 return (*target)(BOOST_SIGNALS_BOUND_ARGS);
101 struct BOOST_SIGNALS_CALL_BOUND<void> {
102 template<BOOST_SIGNALS_TEMPLATE_PARMS
103 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
106 typedef BOOST_SIGNALS_ARGS_STRUCT<BOOST_SIGNALS_TEMPLATE_ARGS>*
111 typedef unusable result_type;
113 caller(args_type a) : args(a) {}
115 template<typename Pair>
116 unusable operator()(const Pair& slot) const
118 F* target = const_cast<F*>(any_cast<F>(&slot.second));
119 (*target)(BOOST_SIGNALS_BOUND_ARGS);
124 } // namespace detail
125 } // namespace BOOST_SIGNALS_NAMESPACE
127 // The actual signalN class
130 BOOST_SIGNALS_TEMPLATE_PARMS
131 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
132 typename Combiner = last_value<R>,
133 typename Group = int,
134 typename GroupCompare = std::less<Group>,
135 typename SlotFunction = BOOST_SIGNALS_FUNCTION<
136 R BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
137 BOOST_SIGNALS_TEMPLATE_ARGS>
139 class BOOST_SIGNALS_SIGNAL :
140 public BOOST_SIGNALS_NAMESPACE::detail::signal_base, // management of slot list
141 public BOOST_SIGNALS_NAMESPACE::trackable // signals are trackable
144 // The slot function type
145 typedef SlotFunction slot_function_type;
147 // Result type of a slot
148 typedef typename BOOST_SIGNALS_NAMESPACE::detail::slot_result_type<R>::type
152 BOOST_SIGNALS_ARG_TYPES
154 #if BOOST_SIGNALS_NUM_ARGS == 1
155 typedef T1 argument_type;
156 #elif BOOST_SIGNALS_NUM_ARGS == 2
157 typedef T1 first_argument_type;
158 typedef T2 second_argument_type;
162 // The real slot name comparison object type
163 typedef BOOST_SIGNALS_NAMESPACE::detail::any_bridge_compare<GroupCompare, Group>
164 real_group_compare_type;
166 // The function object passed to the slot call iterator that will call
167 // the underlying slot function with its arguments bound
168 typedef BOOST_SIGNALS_NAMESPACE::detail::BOOST_SIGNALS_CALL_BOUND<R>
169 outer_bound_slot_caller;
170 typedef typename outer_bound_slot_caller::template
171 caller<BOOST_SIGNALS_TEMPLATE_ARGS
172 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
177 // Combiner's result type
178 typedef typename Combiner::result_type result_type;
181 typedef Combiner combiner_type;
184 typedef slot<slot_function_type> slot_type;
186 // Slot name type and comparison
187 typedef Group group_type;
188 typedef GroupCompare group_compare_type;
190 typedef BOOST_SIGNALS_NAMESPACE::detail::slot_call_iterator<
191 call_bound_slot, iterator> slot_call_iterator;
194 BOOST_SIGNALS_SIGNAL(const Combiner& c = Combiner(),
195 const GroupCompare& comp = GroupCompare()) :
196 BOOST_SIGNALS_NAMESPACE::detail::signal_base(real_group_compare_type(comp),
201 // Connect a slot to this signal
202 BOOST_SIGNALS_NAMESPACE::connection
203 connect(const slot_type&,
204 BOOST_SIGNALS_NAMESPACE::connect_position at
205 = BOOST_SIGNALS_NAMESPACE::at_back);
208 BOOST_SIGNALS_NAMESPACE::connection
209 connect(const group_type&, const slot_type&,
210 BOOST_SIGNALS_NAMESPACE::connect_position at
211 = BOOST_SIGNALS_NAMESPACE::at_back);
213 #if BOOST_WORKAROUND(BOOST_MSVC, <= 0x1700)
214 // MSVC 6.0 and 7.0 don't handle the is_convertible test well
215 void disconnect(const group_type& group)
217 impl->disconnect(group);
221 void disconnect(const T& t)
223 typedef mpl::bool_<(is_convertible<T, group_type>::value)> is_group;
224 this->do_disconnect(t, is_group());
228 // Disconnect a named slot
229 void do_disconnect(const group_type& group, mpl::bool_<true>)
231 impl->disconnect(group);
234 template<typename Function>
235 void do_disconnect(const Function& f, mpl::bool_<false>)
237 // Notify the slot handling code that we are iterating through the slots
238 BOOST_SIGNALS_NAMESPACE::detail::call_notification notification(this->impl);
240 for (iterator i = impl->slots_.begin(); i != impl->slots_.end(); ++i) {
241 slot_function_type& s = *any_cast<slot_function_type>(&i->second);
242 if (s == f) i->first.disconnect();
250 result_type operator()(BOOST_SIGNALS_PARMS);
251 result_type operator()(BOOST_SIGNALS_PARMS) const;
254 { return *any_cast<Combiner>(&impl->combiner_); }
256 const Combiner& combiner() const
257 { return *any_cast<const Combiner>(&impl->combiner_); }
262 BOOST_SIGNALS_TEMPLATE_PARMS
263 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
266 typename GroupCompare,
267 typename SlotFunction
269 BOOST_SIGNALS_NAMESPACE::connection
270 BOOST_SIGNALS_SIGNAL<
271 R, BOOST_SIGNALS_TEMPLATE_ARGS
272 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
273 Combiner, Group, GroupCompare, SlotFunction
274 >::connect(const slot_type& in_slot,
275 BOOST_SIGNALS_NAMESPACE::connect_position at)
277 // If the slot has been disconnected, just return a disconnected
279 if (!in_slot.is_active()) {
280 return BOOST_SIGNALS_NAMESPACE::connection();
283 return impl->connect_slot(in_slot.get_slot_function(), any(),
284 in_slot.get_data(), at);
289 BOOST_SIGNALS_TEMPLATE_PARMS
290 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
293 typename GroupCompare,
294 typename SlotFunction
296 BOOST_SIGNALS_NAMESPACE::connection
297 BOOST_SIGNALS_SIGNAL<
298 R, BOOST_SIGNALS_TEMPLATE_ARGS
299 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
300 Combiner, Group, GroupCompare, SlotFunction
301 >::connect(const group_type& group,
302 const slot_type& in_slot,
303 BOOST_SIGNALS_NAMESPACE::connect_position at)
305 // If the slot has been disconnected, just return a disconnected
307 if (!in_slot.is_active()) {
308 return BOOST_SIGNALS_NAMESPACE::connection();
311 return impl->connect_slot(in_slot.get_slot_function(), group,
312 in_slot.get_data(), at);
317 BOOST_SIGNALS_TEMPLATE_PARMS
318 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
321 typename GroupCompare,
322 typename SlotFunction
324 typename BOOST_SIGNALS_SIGNAL<
325 R, BOOST_SIGNALS_TEMPLATE_ARGS
326 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
327 Combiner, Group, GroupCompare, SlotFunction>::result_type
328 BOOST_SIGNALS_SIGNAL<
329 R, BOOST_SIGNALS_TEMPLATE_ARGS
330 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
331 Combiner, Group, GroupCompare, SlotFunction
332 >::operator()(BOOST_SIGNALS_PARMS)
334 // Notify the slot handling code that we are making a call
335 BOOST_SIGNALS_NAMESPACE::detail::call_notification notification(this->impl);
337 // Construct a function object that will call the underlying slots
338 // with the given arguments.
339 #if BOOST_SIGNALS_NUM_ARGS == 0
340 BOOST_SIGNALS_ARGS_STRUCT_INST args;
342 BOOST_SIGNALS_ARGS_STRUCT_INST args(BOOST_SIGNALS_ARGS);
343 #endif // BOOST_SIGNALS_NUM_ARGS > 0
344 call_bound_slot f(&args);
346 // Let the combiner call the slots via a pair of input iterators
347 return combiner()(slot_call_iterator(notification.impl->slots_.begin(),
348 impl->slots_.end(), f),
349 slot_call_iterator(notification.impl->slots_.end(),
350 impl->slots_.end(), f));
355 BOOST_SIGNALS_TEMPLATE_PARMS
356 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
359 typename GroupCompare,
360 typename SlotFunction
362 typename BOOST_SIGNALS_SIGNAL<
363 R, BOOST_SIGNALS_TEMPLATE_ARGS
364 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
365 Combiner, Group, GroupCompare, SlotFunction>::result_type
366 BOOST_SIGNALS_SIGNAL<
367 R, BOOST_SIGNALS_TEMPLATE_ARGS
368 BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
369 Combiner, Group, GroupCompare, SlotFunction
370 >::operator()(BOOST_SIGNALS_PARMS) const
372 // Notify the slot handling code that we are making a call
373 BOOST_SIGNALS_NAMESPACE::detail::call_notification notification(this->impl);
375 // Construct a function object that will call the underlying slots
376 // with the given arguments.
377 #if BOOST_SIGNALS_NUM_ARGS == 0
378 BOOST_SIGNALS_ARGS_STRUCT_INST args;
380 BOOST_SIGNALS_ARGS_STRUCT_INST args(BOOST_SIGNALS_ARGS);
381 #endif // BOOST_SIGNALS_NUM_ARGS > 0
383 call_bound_slot f(&args);
385 // Let the combiner call the slots via a pair of input iterators
386 return combiner()(slot_call_iterator(notification.impl->slots_.begin(),
387 impl->slots_.end(), f),
388 slot_call_iterator(notification.impl->slots_.end(),
389 impl->slots_.end(), f));
393 #undef BOOST_SIGNAL_FUNCTION_N_HEADER
394 #undef BOOST_SIGNALS_ARGS_STRUCT_INST
395 #undef BOOST_SIGNALS_CALL_BOUND
396 #undef BOOST_SIGNALS_ARGS_STRUCT
397 #undef BOOST_SIGNALS_FUNCTION
398 #undef BOOST_SIGNALS_SIGNAL
399 #undef BOOST_SIGNALS_COMMA_IF_NONZERO_ARGS
401 #ifdef BOOST_HAS_ABI_HEADERS
402 # include BOOST_ABI_SUFFIX