1 // Boost.Signals2 library
3 // Copyright Frank Mori Hess 2007-2008.
4 // Copyright Timmo Stange 2007.
5 // Copyright Douglas Gregor 2001-2004. Use, modification and
6 // distribution is subject to the Boost Software License, Version
7 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see http://www.boost.org
12 // This file is included iteratively, and should not be protected from multiple inclusion
14 #ifdef BOOST_NO_VARIADIC_TEMPLATES
15 #define BOOST_SIGNALS2_NUM_ARGS BOOST_PP_ITERATION()
17 #define BOOST_SIGNALS2_NUM_ARGS 1
25 template<typename Signature, typename SlotFunction> class slot;
27 // slot class template.
28 template<BOOST_SIGNALS2_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS),
29 typename SlotFunction = BOOST_SIGNALS2_FUNCTION_N_DECL(BOOST_SIGNALS2_NUM_ARGS)>
30 class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
32 template<BOOST_SIGNALS2_SLOT_TEMPLATE_SPECIALIZATION_DECL(BOOST_SIGNALS2_NUM_ARGS)>
33 class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) BOOST_SIGNALS2_SLOT_TEMPLATE_SPECIALIZATION
34 : public slot_base, public detail::BOOST_SIGNALS2_STD_FUNCTIONAL_BASE(R)
38 template<BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS, Other), typename OtherSlotFunction>
39 friend class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
41 typedef SlotFunction slot_function_type;
42 typedef R result_type;
43 typedef typename mpl::identity<BOOST_SIGNALS2_SIGNATURE_FUNCTION_TYPE(BOOST_SIGNALS2_NUM_ARGS)>::type signature_type;
45 #ifdef BOOST_NO_VARIADIC_TEMPLATES
47 // typedef Tn argn_type;
48 #define BOOST_SIGNALS2_MISC_STATEMENT(z, n, data) \
49 typedef BOOST_PP_CAT(T, BOOST_PP_INC(n)) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type);
50 BOOST_PP_REPEAT(BOOST_SIGNALS2_NUM_ARGS, BOOST_SIGNALS2_MISC_STATEMENT, ~)
51 #undef BOOST_SIGNALS2_MISC_STATEMENT
52 #if BOOST_SIGNALS2_NUM_ARGS == 1
53 typedef arg1_type argument_type;
54 #elif BOOST_SIGNALS2_NUM_ARGS == 2
55 typedef arg1_type first_argument_type;
56 typedef arg2_type second_argument_type;
59 template<unsigned n> class arg : public
60 detail::BOOST_SIGNALS2_PREPROCESSED_ARG_N_TYPE_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
61 <n BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
62 BOOST_SIGNALS2_ARGS_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS)>
65 BOOST_STATIC_CONSTANT(int, arity = BOOST_SIGNALS2_NUM_ARGS);
67 #else // BOOST_NO_VARIADIC_TEMPLATES
69 template<unsigned n> class arg
72 typedef typename detail::variadic_arg_type<n, Args...>::type type;
74 BOOST_STATIC_CONSTANT(int, arity = sizeof...(Args));
76 #endif // BOOST_NO_VARIADIC_TEMPLATES
79 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const F& f)
81 init_slot_function(f);
84 #ifdef BOOST_NO_VARIADIC_TEMPLATES
85 template<BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS, Other), typename OtherSlotFunction>
86 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
87 <BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS, Other), OtherSlotFunction> &other_slot):
88 slot_base(other_slot), _slot_function(other_slot._slot_function)
92 template<typename Signature, typename OtherSlotFunction>
93 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const slot<Signature, OtherSlotFunction> &other_slot):
94 slot_base(other_slot), _slot_function(other_slot._slot_function)
97 // bind syntactic sugar
98 BOOST_SIGNALS2_SLOT_N_BINDING_CONSTRUCTORS
100 R operator()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
102 locked_container_type locked_objects = lock();
103 return _slot_function(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
105 R operator()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
107 locked_container_type locked_objects = lock();
108 return _slot_function(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
111 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const weak_ptr<void> &tracked) {
112 _tracked_objects.push_back(tracked);
115 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const signal_base &signal)
117 track_signal(signal);
120 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const slot_base &slot)
122 tracked_container_type::const_iterator it;
123 for(it = slot.tracked_objects().begin(); it != slot.tracked_objects().end(); ++it)
125 _tracked_objects.push_back(*it);
129 template<typename ForeignWeakPtr>
130 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track_foreign(const ForeignWeakPtr &tracked,
131 typename weak_ptr_traits<ForeignWeakPtr>::shared_type * /*SFINAE*/ = 0)
133 _tracked_objects.push_back(detail::foreign_void_weak_ptr(tracked));
136 template<typename ForeignSharedPtr>
137 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track_foreign(const ForeignSharedPtr &tracked,
138 typename shared_ptr_traits<ForeignSharedPtr>::weak_type * /*SFINAE*/ = 0)
140 _tracked_objects.push_back
142 detail::foreign_void_weak_ptr
144 typename shared_ptr_traits<ForeignSharedPtr>::weak_type(tracked)
150 const slot_function_type& slot_function() const {return _slot_function;}
151 slot_function_type& slot_function() {return _slot_function;}
154 void init_slot_function(const F& f)
156 _slot_function = detail::get_invocable_slot(f, detail::tag_type(f));
157 signals2::detail::tracked_objects_visitor visitor(this);
158 boost::visit_each(visitor, f);
161 SlotFunction _slot_function;
164 #ifdef BOOST_NO_VARIADIC_TEMPLATES
167 template<unsigned arity, typename Signature, typename SlotFunction>
169 // partial template specialization
170 template<typename Signature, typename SlotFunction>
171 class slotN<BOOST_SIGNALS2_NUM_ARGS, Signature, SlotFunction>
174 typedef BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)<
175 BOOST_SIGNALS2_PORTABLE_SIGNATURE(BOOST_SIGNALS2_NUM_ARGS, Signature),
180 } // end namespace signals2
181 } // end namespace boost
183 #undef BOOST_SIGNALS2_NUM_ARGS