Library of Assembled Shared Sources
callback_r_0.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2025 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @class lass::util::CallbackR0
46 * @ingroup Callback
47 * @brief callback with 0 parameters but with returnvalue.
48 * @date 2002
49 * @author Bram de Greve [Bramz] (contact: bram@cocamware.com)
50 *
51 * It's a single object that can hold a reference to a free function or an
52 * object/(const) method pair. Once the callback is constructed, it works
53 * completely transparent. All it shows to the client is a function that
54 * takes 0 parameter(s) and gives a returnvalue.
55 */
56
57
58
59#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_R_0_H
60#define LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_R_0_H
61
62
63
64// --- OLD INTERFACES ----------------------------------------------------------
65
66#include "util_common.h"
67#include "shared_ptr.h"
68#include "callback_common.h"
69#include "impl/dispatcher_r_0.h"
70#include "../meta/is_derived.h"
71#include "../meta/wrap.h"
72
73
74
75// --- NEW INTERFACES ----------------------------------------------------------
76
77namespace lass
78{
79namespace util
80{
81
82
83// THE CALLBACK:
84// This is the actuall class you use to hold callbacks:
85
86template
87<
88 typename R
89>
91{
92public:
93
94 typedef CallbackR0<R> TSelf;
95 typedef SharedPtr< impl::DispatcherR0<R> > TDispatcherPtr;
96
97 // STRUCTORS
98
99 /** Default constructor, construct empty callback.
100 */
102 {
103 }
104
105 /** Construct function callback
106 */
107 template <typename Function>
108 CallbackR0(Function iFunction):
109 dispatcher_(make(iFunction, meta::Wrap<typename meta::IsDerived<Function, impl::DispatcherR0<R> >::Type>()))
110 {
111 }
112
113 /** Construct object/method callback.
114 */
115 template <typename ObjectPtr, typename Method>
116 CallbackR0(ObjectPtr iObject, Method iMethod):
117 dispatcher_(new impl::DispatcherR0Method<R, ObjectPtr, Method>(iObject, iMethod))
118 {
119 }
120
121 /** copy constructor
122 */
123 CallbackR0(const TSelf& iOther):
124 dispatcher_(iOther.dispatcher_)
125 {
126 }
127
128 /** move constructor
129 */
130 CallbackR0(TSelf&& iOther) noexcept:
131 dispatcher_(std::move(iOther.dispatcher_))
132 {
133 }
134
135
136 // OPERATORS
137
138 /** assignment operator
139 */
140 TSelf& operator=(const TSelf& iOther)
141 {
142 TSelf temp(iOther);
143 swap(temp);
144 return *this;
145 }
146
147 /** move assignment operator
148 */
149 TSelf& operator=(TSelf&& iOther) noexcept
150 {
151 dispatcher_ = std::move(iOther.dispatcher_);
152 return *this;
153 }
154
155 /** THE operator. Executes the callback.
156 */
157 R operator()() const
158 {
159 if (isEmpty())
160 {
161 LASS_THROW_EX(EmptyCallback, "You've tried to call an empty CallbackR0. Can't return a value.");
162 }
163 return dispatcher_->call();
164 }
165
166
167 // METHODS
168
169 /** Reset to empty callback.
170 */
171 void reset()
172 {
173 dispatcher_.reset();
174 }
175
176 /** Returns true if no callback dispatcher is assigned to this object.
177 */
178 bool isEmpty() const
179 {
180 return dispatcher_.isEmpty();
181 }
182
183 /** return this->isEmpty()
184 */
185 bool operator!() const
186 {
187 return dispatcher_.isEmpty();
188 }
189
190 /** return !this->isEmpty())
191 */
192 explicit operator bool() const
193 {
194 return !dispatcher_.isEmpty();
195 }
196
197 /** Swaps the dispatcher of this callback with the dispatcher of another.
198 */
199 void swap(TSelf& iOther)
200 {
201 dispatcher_.swap(iOther.dispatcher_);
202 }
203
204 /** return true if two callbacks call the same function/method,
205 * NEEDS RTTI!
206 */
207 bool operator==(const TSelf& iOther) const
208 {
209 if (dispatcher_ == iOther.dispatcher_)
210 {
211 return true;
212 }
213 return dispatcher_ && dispatcher_->isEquivalent(iOther.dispatcher_.get());
214 }
215
216 const TDispatcherPtr& dispatcher() const
217 {
218 return dispatcher_;
219 }
220
221private:
222
223 template <typename Function>
224 static TDispatcherPtr make(Function iFunction, meta::Wrap<meta::False>)
225 {
226 return TDispatcherPtr(new impl::DispatcherR0Function<R, Function>(iFunction));
227 }
228
229 template <typename Dispatcher>
230 static TDispatcherPtr make(Dispatcher iDispatcher, meta::Wrap<meta::True>)
231 {
232 return TDispatcherPtr(new Dispatcher(iDispatcher));
233 }
234
235 TDispatcherPtr dispatcher_;
236};
237
238
239
240/** return true if two callbacks are different
241 * @relates CallbackR0
242 */
243template <typename R>
244bool operator!=(const CallbackR0<R>& iA, const CallbackR0<R>& iB)
245{
246 return !(iA == iB);
247}
248
249
250
251/** make a CallbackR0 from a function
252 * @relates CallbackR0
253 */
254template <typename R> inline
256{
257 return Callback0(iFunction);
258}
259
260
261
262/** make a CallbackR0 from a callback
263 * @relates CallbackR0
264 */
265template <typename R> inline
267{
268 return iCallback;
269}
270
271
272
273/** make a CallbackR0 from a object and method
274 * @relates CallbackR0
275 */
276template <typename ObjectPtr, typename Object, typename R> inline
277CallbackR0<R> makeCallback(ObjectPtr iObject, R (Object::*iMethod)())
278{
279 return Callback0(iObject, iMethod);
280}
281
282
283
284/** make a CallbackR0 from a object and const method
285 * @relates CallbackR0
286 */
287template <typename ObjectPtr, typename Object, typename R> inline
288CallbackR0<R> makeCallback(ObjectPtr iObject, R (Object::*iMethod)() const)
289{
290 return Callback0(iObject, iMethod);
291}
292
293
294
295}
296
297}
298
299#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_CALLBACK_R0
300#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_PYTHON_H
302#endif
303
304#endif // Guardian of Inclusion
305
306// EOF
callback with 0 parameter(s) and without returnvalue.
Definition callback_0.h:84
CallbackR0(Function iFunction)
Construct function callback.
bool isEmpty() const
Returns true if no callback dispatcher is assigned to this object.
CallbackR0(TSelf &&iOther) noexcept
move constructor
bool operator!=(const CallbackR0< R > &iA, const CallbackR0< R > &iB)
return true if two callbacks are different
R operator()() const
THE operator.
CallbackR0(const TSelf &iOther)
copy constructor
TSelf & operator=(TSelf &&iOther) noexcept
move assignment operator
CallbackR0()
Default constructor, construct empty callback.
void reset()
Reset to empty callback.
CallbackR0< R > makeCallback(R(*iFunction)())
make a CallbackR0 from a function
CallbackR0< R > makeCallback(ObjectPtr iObject, R(Object::*iMethod)() const)
make a CallbackR0 from a object and const method
bool operator==(const TSelf &iOther) const
return true if two callbacks call the same function/method, NEEDS RTTI!
bool operator!() const
return this->isEmpty()
const CallbackR0< R > & makeCallback(const CallbackR0< R > &iCallback)
make a CallbackR0 from a callback
TSelf & operator=(const TSelf &iOther)
assignment operator
CallbackR0(ObjectPtr iObject, Method iMethod)
Construct object/method callback.
CallbackR0< R > makeCallback(ObjectPtr iObject, R(Object::*iMethod)())
make a CallbackR0 from a object and method
void swap(TSelf &iOther)
Swaps the dispatcher of this callback with the dispatcher of another.
library for template meta programming
Definition bool.h:76
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53