Library of Assembled Shared Sources
callback_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/** @class lass::util::Callback0
44 * @ingroup Callback
45 * @brief callback with 0 parameter(s) and without returnvalue.
46 * @date 2002
47 * @author Bram de Greve [Bramz] (contact: bram@cocamware.com)
48 *
49 * It's a single object that can hold a reference to a free function or an
50 * object/(const) method pair. Once the callback is constructed, it works
51 * completely transparent. All it shows to the client is a function that
52 * takes one parameter but gives no returnvalue.
53 */
54
55
56
57#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_0_H
58#define LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_0_H
59
60
61
62// --- OLD INTERFACES ----------------------------------------------------------
63
64#include "util_common.h"
65#include "callback_common.h"
66#include "shared_ptr.h"
67#include "impl/dispatcher_0.h"
68#include "../meta/is_derived.h"
69#include "../meta/wrap.h"
70
71
72
73// --- NEW INTERFACES ----------------------------------------------------------
74
75namespace lass
76{
77namespace util
78{
79
80// THE CALLBACK:
81// This is the actuall class you use to hold callbacks:
82
84{
85public:
86
87 typedef Callback0 TSelf;
88 typedef SharedPtr<impl::Dispatcher0> TDispatcherPtr;
89
90 // STRUCTORS
91
92 /** Default constructor, construct empty callback.
93 */
95 {
96 }
97
98 /** callback to function or other callable entity.
99 */
100 template <typename Function>
101 Callback0(Function iFunction):
102 dispatcher_(make(iFunction, meta::Wrap<typename meta::IsDerived<Function, impl::Dispatcher0>::Type>()))
103 {
104 }
105
106 /** callback to method of object
107 */
108 template <typename ObjectPtr, typename Method>
109 Callback0(ObjectPtr iObject, Method iMethod):
110 dispatcher_(new impl::Dispatcher0Method<ObjectPtr, Method>(iObject, iMethod))
111 {
112 }
113
114 /** copy constructor
115 */
116 Callback0(const Callback0& iOther):
117 dispatcher_(iOther.dispatcher_)
118 {
119 }
120
121 /** move constructor
122 */
123 Callback0(Callback0&& iOther) noexcept:
124 dispatcher_(std::move(iOther.dispatcher_))
125 {
126 }
127
128
129 // OPERATORS
130
131 /** assignment operator
132 */
133 TSelf& operator=(const TSelf& iOther)
134 {
135 TSelf temp(iOther);
136 swap(temp);
137 return *this;
138 }
139
140 /** move assignment operator
141 */
142 TSelf& operator=(TSelf&& iOther) noexcept
143 {
144 dispatcher_ = std::move(iOther.dispatcher_);
145 return *this;
146 }
147
148 /** THE operator. Executes the callback.
149 */
150 void operator()() const
151 {
152 if (!isEmpty())
153 {
154 dispatcher_->call();
155 }
156 }
157
158
159 // METHODS
160
161 /** Reset to empty callback.
162 */
163 void reset()
164 {
165 dispatcher_.reset();
166 }
167
168 /** Returns true if no callback dispatcher is assigned to this object.
169 */
170 bool isEmpty() const
171 {
172 return dispatcher_.isEmpty();
173 }
174
175 /** return this->isEmpty()
176 */
177 bool operator!() const
178 {
179 return dispatcher_.isEmpty();
180 }
181
182 /** return !this->isEmpty())
183 */
184 explicit operator bool() const
185 {
186 return !dispatcher_.isEmpty();
187 }
188
189 /** Swaps the dispatcher of this callback with the dispatcher of another.
190 */
191 void swap(TSelf& iOther)
192 {
193 dispatcher_.swap(iOther.dispatcher_);
194 }
195
196 /** return true if two callbacks call the same function/method,
197 * NEEDS RTTI!
198 */
199 bool operator==(const TSelf& iOther) const
200 {
201 if (dispatcher_ == iOther.dispatcher_)
202 {
203 return true;
204 }
205 return dispatcher_ && dispatcher_->isEquivalent(iOther.dispatcher_.get());
206 }
207
208 const TDispatcherPtr& dispatcher() const
209 {
210 return dispatcher_;
211 }
212
213private:
214
215 template <typename Function>
216 static TDispatcherPtr make(Function iFunction, meta::Wrap<meta::False>)
217 {
218 return TDispatcherPtr(new impl::Dispatcher0Function<Function>(iFunction));
219 }
220
221 template <typename Dispatcher>
222 static TDispatcherPtr make(Dispatcher iDispatcher, meta::Wrap<meta::True>)
223 {
224 return TDispatcherPtr(new Dispatcher(iDispatcher));
225 }
226
227 TDispatcherPtr dispatcher_;
228};
229
230
231
232/** return true if two callbacks are different
233 * @relates Callback0
234 */
235inline bool operator!=(const Callback0& iA, const Callback0& iB)
236{
237 return !(iA == iB);
238}
239
240
241
242/** make a Callback0 from a function
243 * @relates Callback0
244 */
245inline Callback0 makeCallback(void (*iFunction)())
246{
247 return Callback0(iFunction);
248}
249
250
251
252/** convencie function, make callback from callback
253 */
254inline const Callback0& makeCallback(const Callback0& iCallback)
255{
256 return iCallback;
257}
258
259
260/** make a Callback0 from a object and method
261 * @relates Callback0
262 */
263template <typename ObjectPtr, typename Object> inline
264Callback0 makeCallback(ObjectPtr iObject, void (Object::*iMethod)())
265{
266 return Callback0(iObject, iMethod);
267}
268
269
270
271/** make a Callback0 from a object and const method
272 * @relates Callback0
273 */
274template <typename ObjectPtr, typename Object> inline
275Callback0 makeCallback(ObjectPtr iObject, void (Object::*iMethod)() const)
276{
277 return Callback0(iObject, iMethod);
278}
279
280
281
282}
283
284}
285
286#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_CALLBACK_0
287#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_CALLBACK_PYTHON_H
289#endif
290
291#endif
292
293// EOF
callback with 0 parameter(s) and without returnvalue.
Definition callback_0.h:84
Callback0 makeCallback(void(*iFunction)())
make a Callback0 from a function
Definition callback_0.h:245
bool isEmpty() const
Returns true if no callback dispatcher is assigned to this object.
Definition callback_0.h:170
Callback0 makeCallback(ObjectPtr iObject, void(Object::*iMethod)())
make a Callback0 from a object and method
Definition callback_0.h:264
TSelf & operator=(TSelf &&iOther) noexcept
move assignment operator
Definition callback_0.h:142
Callback0(const Callback0 &iOther)
copy constructor
Definition callback_0.h:116
TSelf & operator=(const TSelf &iOther)
assignment operator
Definition callback_0.h:133
Callback0(Function iFunction)
callback to function or other callable entity.
Definition callback_0.h:101
bool operator==(const TSelf &iOther) const
return true if two callbacks call the same function/method, NEEDS RTTI!
Definition callback_0.h:199
bool operator!=(const Callback0 &iA, const Callback0 &iB)
return true if two callbacks are different
Definition callback_0.h:235
Callback0 makeCallback(ObjectPtr iObject, void(Object::*iMethod)() const)
make a Callback0 from a object and const method
Definition callback_0.h:275
Callback0()
Default constructor, construct empty callback.
Definition callback_0.h:94
Callback0(ObjectPtr iObject, Method iMethod)
callback to method of object
Definition callback_0.h:109
Callback0(Callback0 &&iOther) noexcept
move constructor
Definition callback_0.h:123
void operator()() const
THE operator.
Definition callback_0.h:150
bool operator!() const
return this->isEmpty()
Definition callback_0.h:177
void swap(TSelf &iOther)
Swaps the dispatcher of this callback with the dispatcher of another.
Definition callback_0.h:191
void reset()
Reset to empty callback.
Definition callback_0.h:163
library for template meta programming
Definition bool.h:76
general utility, debug facilities, ...
const Callback0 & makeCallback(const Callback0 &iCallback)
convencie function, make callback from callback
Definition callback_0.h:254
Library for Assembled Shared Sources.
Definition config.h:53