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@101
|
25 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
26 template<typename Signature, typename SlotFunction> class slot;
|
Chris@101
|
27 #else
|
Chris@101
|
28 template<typename Signature, typename SlotFunction = boost::function<Signature> >
|
Chris@101
|
29 class slot;
|
Chris@16
|
30
|
Chris@101
|
31 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1900)
|
Chris@101
|
32 template<typename Signature, typename SlotFunction> class slot{};
|
Chris@101
|
33 #endif
|
Chris@101
|
34 #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
35
|
Chris@16
|
36 template<BOOST_SIGNALS2_SLOT_TEMPLATE_SPECIALIZATION_DECL(BOOST_SIGNALS2_NUM_ARGS)>
|
Chris@16
|
37 class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS) BOOST_SIGNALS2_SLOT_TEMPLATE_SPECIALIZATION
|
Chris@16
|
38 : public slot_base, public detail::BOOST_SIGNALS2_STD_FUNCTIONAL_BASE(R)
|
Chris@16
|
39
|
Chris@16
|
40 {
|
Chris@16
|
41 public:
|
Chris@16
|
42 template<BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS, Other), typename OtherSlotFunction>
|
Chris@16
|
43 friend class BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS);
|
Chris@16
|
44
|
Chris@16
|
45 typedef SlotFunction slot_function_type;
|
Chris@16
|
46 typedef R result_type;
|
Chris@16
|
47 typedef typename mpl::identity<BOOST_SIGNALS2_SIGNATURE_FUNCTION_TYPE(BOOST_SIGNALS2_NUM_ARGS)>::type signature_type;
|
Chris@16
|
48
|
Chris@16
|
49 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
50
|
Chris@16
|
51 // typedef Tn argn_type;
|
Chris@16
|
52 #define BOOST_SIGNALS2_MISC_STATEMENT(z, n, data) \
|
Chris@16
|
53 typedef BOOST_PP_CAT(T, BOOST_PP_INC(n)) BOOST_PP_CAT(BOOST_PP_CAT(arg, BOOST_PP_INC(n)), _type);
|
Chris@16
|
54 BOOST_PP_REPEAT(BOOST_SIGNALS2_NUM_ARGS, BOOST_SIGNALS2_MISC_STATEMENT, ~)
|
Chris@16
|
55 #undef BOOST_SIGNALS2_MISC_STATEMENT
|
Chris@16
|
56 #if BOOST_SIGNALS2_NUM_ARGS == 1
|
Chris@16
|
57 typedef arg1_type argument_type;
|
Chris@16
|
58 #elif BOOST_SIGNALS2_NUM_ARGS == 2
|
Chris@16
|
59 typedef arg1_type first_argument_type;
|
Chris@16
|
60 typedef arg2_type second_argument_type;
|
Chris@16
|
61 #endif
|
Chris@16
|
62
|
Chris@16
|
63 template<unsigned n> class arg : public
|
Chris@16
|
64 detail::BOOST_SIGNALS2_PREPROCESSED_ARG_N_TYPE_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
|
Chris@16
|
65 <n BOOST_SIGNALS2_PP_COMMA_IF(BOOST_SIGNALS2_NUM_ARGS)
|
Chris@16
|
66 BOOST_SIGNALS2_ARGS_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS)>
|
Chris@16
|
67 {};
|
Chris@16
|
68
|
Chris@16
|
69 BOOST_STATIC_CONSTANT(int, arity = BOOST_SIGNALS2_NUM_ARGS);
|
Chris@16
|
70
|
Chris@16
|
71 #else // BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
72
|
Chris@16
|
73 template<unsigned n> class arg
|
Chris@16
|
74 {
|
Chris@16
|
75 public:
|
Chris@16
|
76 typedef typename detail::variadic_arg_type<n, Args...>::type type;
|
Chris@16
|
77 };
|
Chris@16
|
78 BOOST_STATIC_CONSTANT(int, arity = sizeof...(Args));
|
Chris@16
|
79
|
Chris@16
|
80 #endif // BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
81
|
Chris@16
|
82 template<typename F>
|
Chris@16
|
83 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const F& f)
|
Chris@16
|
84 {
|
Chris@16
|
85 init_slot_function(f);
|
Chris@16
|
86 }
|
Chris@16
|
87 // copy constructors
|
Chris@16
|
88 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
89 template<BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_DECL(BOOST_SIGNALS2_NUM_ARGS, Other), typename OtherSlotFunction>
|
Chris@16
|
90 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)
|
Chris@16
|
91 <BOOST_SIGNALS2_PREFIXED_SIGNATURE_TEMPLATE_INSTANTIATION(BOOST_SIGNALS2_NUM_ARGS, Other), OtherSlotFunction> &other_slot):
|
Chris@16
|
92 slot_base(other_slot), _slot_function(other_slot._slot_function)
|
Chris@16
|
93 {
|
Chris@16
|
94 }
|
Chris@16
|
95 #endif
|
Chris@16
|
96 template<typename Signature, typename OtherSlotFunction>
|
Chris@16
|
97 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)(const slot<Signature, OtherSlotFunction> &other_slot):
|
Chris@16
|
98 slot_base(other_slot), _slot_function(other_slot._slot_function)
|
Chris@16
|
99 {
|
Chris@16
|
100 }
|
Chris@16
|
101 // bind syntactic sugar
|
Chris@16
|
102 BOOST_SIGNALS2_SLOT_N_BINDING_CONSTRUCTORS
|
Chris@16
|
103 // invocation
|
Chris@16
|
104 R operator()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS))
|
Chris@16
|
105 {
|
Chris@16
|
106 locked_container_type locked_objects = lock();
|
Chris@16
|
107 return _slot_function(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
|
Chris@16
|
108 }
|
Chris@16
|
109 R operator()(BOOST_SIGNALS2_SIGNATURE_FULL_ARGS(BOOST_SIGNALS2_NUM_ARGS)) const
|
Chris@16
|
110 {
|
Chris@16
|
111 locked_container_type locked_objects = lock();
|
Chris@16
|
112 return _slot_function(BOOST_SIGNALS2_SIGNATURE_ARG_NAMES(BOOST_SIGNALS2_NUM_ARGS));
|
Chris@16
|
113 }
|
Chris@16
|
114 // tracking
|
Chris@16
|
115 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const weak_ptr<void> &tracked) {
|
Chris@16
|
116 _tracked_objects.push_back(tracked);
|
Chris@16
|
117 return *this;
|
Chris@16
|
118 }
|
Chris@16
|
119 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const signal_base &signal)
|
Chris@16
|
120 {
|
Chris@16
|
121 track_signal(signal);
|
Chris@16
|
122 return *this;
|
Chris@16
|
123 }
|
Chris@16
|
124 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track(const slot_base &slot)
|
Chris@16
|
125 {
|
Chris@16
|
126 tracked_container_type::const_iterator it;
|
Chris@16
|
127 for(it = slot.tracked_objects().begin(); it != slot.tracked_objects().end(); ++it)
|
Chris@16
|
128 {
|
Chris@16
|
129 _tracked_objects.push_back(*it);
|
Chris@16
|
130 }
|
Chris@16
|
131 return *this;
|
Chris@16
|
132 }
|
Chris@16
|
133 template<typename ForeignWeakPtr>
|
Chris@16
|
134 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track_foreign(const ForeignWeakPtr &tracked,
|
Chris@16
|
135 typename weak_ptr_traits<ForeignWeakPtr>::shared_type * /*SFINAE*/ = 0)
|
Chris@16
|
136 {
|
Chris@16
|
137 _tracked_objects.push_back(detail::foreign_void_weak_ptr(tracked));
|
Chris@16
|
138 return *this;
|
Chris@16
|
139 }
|
Chris@16
|
140 template<typename ForeignSharedPtr>
|
Chris@16
|
141 BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)& track_foreign(const ForeignSharedPtr &tracked,
|
Chris@16
|
142 typename shared_ptr_traits<ForeignSharedPtr>::weak_type * /*SFINAE*/ = 0)
|
Chris@16
|
143 {
|
Chris@16
|
144 _tracked_objects.push_back
|
Chris@16
|
145 (
|
Chris@16
|
146 detail::foreign_void_weak_ptr
|
Chris@16
|
147 (
|
Chris@16
|
148 typename shared_ptr_traits<ForeignSharedPtr>::weak_type(tracked)
|
Chris@16
|
149 )
|
Chris@16
|
150 );
|
Chris@16
|
151 return *this;
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 const slot_function_type& slot_function() const {return _slot_function;}
|
Chris@16
|
155 slot_function_type& slot_function() {return _slot_function;}
|
Chris@16
|
156 private:
|
Chris@16
|
157 template<typename F>
|
Chris@16
|
158 void init_slot_function(const F& f)
|
Chris@16
|
159 {
|
Chris@16
|
160 _slot_function = detail::get_invocable_slot(f, detail::tag_type(f));
|
Chris@16
|
161 signals2::detail::tracked_objects_visitor visitor(this);
|
Chris@16
|
162 boost::visit_each(visitor, f);
|
Chris@16
|
163 }
|
Chris@16
|
164
|
Chris@16
|
165 SlotFunction _slot_function;
|
Chris@16
|
166 };
|
Chris@16
|
167
|
Chris@16
|
168 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
Chris@16
|
169 namespace detail
|
Chris@16
|
170 {
|
Chris@16
|
171 template<unsigned arity, typename Signature, typename SlotFunction>
|
Chris@16
|
172 class slotN;
|
Chris@16
|
173 // partial template specialization
|
Chris@16
|
174 template<typename Signature, typename SlotFunction>
|
Chris@16
|
175 class slotN<BOOST_SIGNALS2_NUM_ARGS, Signature, SlotFunction>
|
Chris@16
|
176 {
|
Chris@16
|
177 public:
|
Chris@16
|
178 typedef BOOST_SIGNALS2_SLOT_CLASS_NAME(BOOST_SIGNALS2_NUM_ARGS)<
|
Chris@16
|
179 BOOST_SIGNALS2_PORTABLE_SIGNATURE(BOOST_SIGNALS2_NUM_ARGS, Signature),
|
Chris@16
|
180 SlotFunction> type;
|
Chris@16
|
181 };
|
Chris@16
|
182 }
|
Chris@16
|
183 #endif
|
Chris@16
|
184 } // end namespace signals2
|
Chris@16
|
185 } // end namespace boost
|
Chris@16
|
186
|
Chris@16
|
187 #undef BOOST_SIGNALS2_NUM_ARGS
|