Chris@16
|
1 // Boost.Signals2 library
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Frank Mori Hess 2007-2008.
|
Chris@16
|
4 // Copyright Timmo Stange 2007.
|
Chris@16
|
5 // Copyright Douglas Gregor 2001-2004. Use, modification and
|
Chris@16
|
6 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
7 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9
|
Chris@16
|
10 // For more information, see http://www.boost.org
|
Chris@16
|
11
|
Chris@16
|
12 // This file is included iteratively, and should not be protected from multiple inclusion
|
Chris@16
|
13
|
Chris@16
|
14 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
15 #define BOOST_SIGNALS2_NUM_ARGS BOOST_PP_ITERATION()
|
Chris@16
|
16 #else
|
Chris@16
|
17 #define BOOST_SIGNALS2_NUM_ARGS 1
|
Chris@16
|
18 #endif
|
Chris@16
|
19
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost
|
Chris@16
|
22 {
|
Chris@16
|
23 namespace signals2
|
Chris@16
|
24 {
|
Chris@16
|
25 template<typename Signature, typename SlotFunction> class slot;
|
Chris@16
|
26
|
Chris@16
|
27 // slot class template.
|
Chris@16
|
28 template<BOOST_SIGNALS2_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS),
|
Chris@16
|
29 typename SlotFunction = BOOST_SIGNALS2_FUNCTION_N_DECL(BOOST_SIGNALS2_NUM_ARGS)>
|
Chris@16
|
30 class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
|
Chris@16
|
31
|
Chris@16
|
32 template<BOOST_SIGNALS2_SLOT_TEMPLATE_SPECIALIZATION_DECL(BOOST_SIGNALS2_NUM_ARGS)>
|
Chris@16
|
33 class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) BOOST_SIGNALS2_SLOT_TEMPLATE_SPECIALIZATION
|
Chris@16
|
34 : public slot_base, public detail::BOOST_SIGNALS2_STD_FUNCTIONAL_BASE(R)
|
Chris@16
|
35
|
Chris@16
|
36 {
|
Chris@16
|
37 public:
|
Chris@16
|
38 template<BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS, Other), typename OtherSlotFunction>
|
Chris@16
|
39 friend class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
|
Chris@16
|
40
|
Chris@16
|
41 typedef SlotFunction slot_function_type;
|
Chris@16
|
42 typedef R result_type;
|
Chris@16
|
43 typedef typename mpl::identity<BOOST_SIGNALS2_SIGNATURE_FUNCTION_TYPE(BOOST_SIGNALS2_NUM_ARGS)>::type signature_type;
|
Chris@16
|
44
|
Chris@16
|
45 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
46
|
Chris@16
|
47 // typedef Tn argn_type;
|
Chris@16
|
48 #define BOOST_SIGNALS2_MISC_STATEMENT(z, n, data) \
|
Chris@16
|
49 typedef BOOST_PP_CAT(T, BOOST_PP_INC(n)) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type);
|
Chris@16
|
50 BOOST_PP_REPEAT(BOOST_SIGNALS2_NUM_ARGS, BOOST_SIGNALS2_MISC_STATEMENT, ~)
|
Chris@16
|
51 #undef BOOST_SIGNALS2_MISC_STATEMENT
|
Chris@16
|
52 #if BOOST_SIGNALS2_NUM_ARGS == 1
|
Chris@16
|
53 typedef arg1_type argument_type;
|
Chris@16
|
54 #elif BOOST_SIGNALS2_NUM_ARGS == 2
|
Chris@16
|
55 typedef arg1_type first_argument_type;
|
Chris@16
|
56 typedef arg2_type second_argument_type;
|
Chris@16
|
57 #endif
|
Chris@16
|
58
|
Chris@16
|
59 template<unsigned n> class arg : public
|
Chris@16
|
60 detail::BOOST_SIGNALS2_PREPROCESSED_ARG_N_TYPE_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
|
Chris@16
|
61 <n BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
|
Chris@16
|
62 BOOST_SIGNALS2_ARGS_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS)>
|
Chris@16
|
63 {};
|
Chris@16
|
64
|
Chris@16
|
65 BOOST_STATIC_CONSTANT(int, arity = BOOST_SIGNALS2_NUM_ARGS);
|
Chris@16
|
66
|
Chris@16
|
67 #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
68
|
Chris@16
|
69 template<unsigned n> class arg
|
Chris@16
|
70 {
|
Chris@16
|
71 public:
|
Chris@16
|
72 typedef typename detail::variadic_arg_type<n, Args...>::type type;
|
Chris@16
|
73 };
|
Chris@16
|
74 BOOST_STATIC_CONSTANT(int, arity = sizeof...(Args));
|
Chris@16
|
75
|
Chris@16
|
76 #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
77
|
Chris@16
|
78 template<typename F>
|
Chris@16
|
79 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const F& f)
|
Chris@16
|
80 {
|
Chris@16
|
81 init_slot_function(f);
|
Chris@16
|
82 }
|
Chris@16
|
83 // copy constructors
|
Chris@16
|
84 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
85 template<BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS, Other), typename OtherSlotFunction>
|
Chris@16
|
86 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
|
Chris@16
|
87 <BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS, Other), OtherSlotFunction> &other_slot):
|
Chris@16
|
88 slot_base(other_slot), _slot_function(other_slot._slot_function)
|
Chris@16
|
89 {
|
Chris@16
|
90 }
|
Chris@16
|
91 #endif
|
Chris@16
|
92 template<typename Signature, typename OtherSlotFunction>
|
Chris@16
|
93 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const slot<Signature, OtherSlotFunction> &other_slot):
|
Chris@16
|
94 slot_base(other_slot), _slot_function(other_slot._slot_function)
|
Chris@16
|
95 {
|
Chris@16
|
96 }
|
Chris@16
|
97 // bind syntactic sugar
|
Chris@16
|
98 BOOST_SIGNALS2_SLOT_N_BINDING_CONSTRUCTORS
|
Chris@16
|
99 // invocation
|
Chris@16
|
100 R operator()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
|
Chris@16
|
101 {
|
Chris@16
|
102 locked_container_type locked_objects = lock();
|
Chris@16
|
103 return _slot_function(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
|
Chris@16
|
104 }
|
Chris@16
|
105 R operator()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
|
Chris@16
|
106 {
|
Chris@16
|
107 locked_container_type locked_objects = lock();
|
Chris@16
|
108 return _slot_function(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
|
Chris@16
|
109 }
|
Chris@16
|
110 // tracking
|
Chris@16
|
111 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const weak_ptr<void> &tracked) {
|
Chris@16
|
112 _tracked_objects.push_back(tracked);
|
Chris@16
|
113 return *this;
|
Chris@16
|
114 }
|
Chris@16
|
115 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const signal_base &signal)
|
Chris@16
|
116 {
|
Chris@16
|
117 track_signal(signal);
|
Chris@16
|
118 return *this;
|
Chris@16
|
119 }
|
Chris@16
|
120 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const slot_base &slot)
|
Chris@16
|
121 {
|
Chris@16
|
122 tracked_container_type::const_iterator it;
|
Chris@16
|
123 for(it = slot.tracked_objects().begin(); it != slot.tracked_objects().end(); ++it)
|
Chris@16
|
124 {
|
Chris@16
|
125 _tracked_objects.push_back(*it);
|
Chris@16
|
126 }
|
Chris@16
|
127 return *this;
|
Chris@16
|
128 }
|
Chris@16
|
129 template<typename ForeignWeakPtr>
|
Chris@16
|
130 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track_foreign(const ForeignWeakPtr &tracked,
|
Chris@16
|
131 typename weak_ptr_traits<ForeignWeakPtr>::shared_type * /*SFINAE*/ = 0)
|
Chris@16
|
132 {
|
Chris@16
|
133 _tracked_objects.push_back(detail::foreign_void_weak_ptr(tracked));
|
Chris@16
|
134 return *this;
|
Chris@16
|
135 }
|
Chris@16
|
136 template<typename ForeignSharedPtr>
|
Chris@16
|
137 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track_foreign(const ForeignSharedPtr &tracked,
|
Chris@16
|
138 typename shared_ptr_traits<ForeignSharedPtr>::weak_type * /*SFINAE*/ = 0)
|
Chris@16
|
139 {
|
Chris@16
|
140 _tracked_objects.push_back
|
Chris@16
|
141 (
|
Chris@16
|
142 detail::foreign_void_weak_ptr
|
Chris@16
|
143 (
|
Chris@16
|
144 typename shared_ptr_traits<ForeignSharedPtr>::weak_type(tracked)
|
Chris@16
|
145 )
|
Chris@16
|
146 );
|
Chris@16
|
147 return *this;
|
Chris@16
|
148 }
|
Chris@16
|
149
|
Chris@16
|
150 const slot_function_type& slot_function() const {return _slot_function;}
|
Chris@16
|
151 slot_function_type& slot_function() {return _slot_function;}
|
Chris@16
|
152 private:
|
Chris@16
|
153 template<typename F>
|
Chris@16
|
154 void init_slot_function(const F& f)
|
Chris@16
|
155 {
|
Chris@16
|
156 _slot_function = detail::get_invocable_slot(f, detail::tag_type(f));
|
Chris@16
|
157 signals2::detail::tracked_objects_visitor visitor(this);
|
Chris@16
|
158 boost::visit_each(visitor, f);
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 SlotFunction _slot_function;
|
Chris@16
|
162 };
|
Chris@16
|
163
|
Chris@16
|
164 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
165 namespace detail
|
Chris@16
|
166 {
|
Chris@16
|
167 template<unsigned arity, typename Signature, typename SlotFunction>
|
Chris@16
|
168 class slotN;
|
Chris@16
|
169 // partial template specialization
|
Chris@16
|
170 template<typename Signature, typename SlotFunction>
|
Chris@16
|
171 class slotN<BOOST_SIGNALS2_NUM_ARGS, Signature, SlotFunction>
|
Chris@16
|
172 {
|
Chris@16
|
173 public:
|
Chris@16
|
174 typedef BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)<
|
Chris@16
|
175 BOOST_SIGNALS2_PORTABLE_SIGNATURE(BOOST_SIGNALS2_NUM_ARGS, Signature),
|
Chris@16
|
176 SlotFunction> type;
|
Chris@16
|
177 };
|
Chris@16
|
178 }
|
Chris@16
|
179 #endif
|
Chris@16
|
180 } // end namespace signals2
|
Chris@16
|
181 } // end namespace boost
|
Chris@16
|
182
|
Chris@16
|
183 #undef BOOST_SIGNALS2_NUM_ARGS
|