Library of Assembled Shared Sources
dispatcher_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#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_IMPL_DISPATCHER_R_0_H
44#define LASS_GUARDIAN_OF_INCLUSION_UTIL_IMPL_DISPATCHER_R_0_H
45
47#include "../callback_common.h"
48
49// --- NEW INTERFACES ----------------------------------------------------------
50
51namespace lass
52{
53namespace util
54{
55namespace impl
56{
57
58/** abstract base class of all dispatchers for lass::util::CallbackR0.
59 * @internal
60 * @sa CallbackR0
61 * @author Bram de Greve [Bramz]
62 */
63template
64<
65 typename R
66>
67class DispatcherR0: public TDispatcherAllocatorBase
68{
69public:
70
71 DispatcherR0() {}
72 virtual ~DispatcherR0() {}
73 R call() const
74 {
75 return doCall();
76 }
77 bool isEquivalent(const DispatcherR0<R>* iOther) const
78 {
79 return doIsEquivalent(iOther);
80 }
81
82protected:
83
84 DispatcherR0(const DispatcherR0<R>& /*iOther*/) {}
85
86private:
87
88 virtual R doCall() const = 0;
89 virtual bool doIsEquivalent(const DispatcherR0<R>* iOther) const = 0;
90
91 DispatcherR0& operator=(const DispatcherR0<R>& iOther);
92};
93
94
95
96/** Dispatcher for lass::util::CallbackR0 to a free function:
97 * @internal
98 * @sa CallbackR0
99 * @author Bram de Greve [Bramz]
100 */
101template
102<
103 typename R,
104 typename FunctionType,
105 typename Enable = void
106>
107class DispatcherR0Function: public DispatcherR0<R>
108{
109public:
110
111 typedef DispatcherR0Function<R, FunctionType, Enable> TSelf;
112 typedef FunctionType TFunction;
113
114 DispatcherR0Function(typename CallTraits<TFunction>::TParam iFunction):
115 function_(iFunction)
116 {
117 }
118
119 const TFunction& function() const
120 {
121 return function_;
122 }
123
124private:
125
126 R doCall() const override
127 {
128 if (!function_)
129 {
130 LASS_THROW_EX(EmptyCallback, "You've tried to call an empty CallbackR0. Can't return a value.");
131 }
132 return function_();
133 }
134 bool doIsEquivalent(const DispatcherR0<R>* other) const override
135 {
136 if constexpr (impl::IsEqualityComparable<TFunction>::value)
137 {
138 return other && typeid( *other ) == typeid( TSelf )
139 && static_cast<const TSelf*>(other)->function_ == function_;
140 }
141 else
142 {
143 return false;
144 }
145 }
146
147 TFunction function_;
148};
149
150
151
152#if LASS_HAVE_CPP_STD_11 && !LASS_HAVE_LAMBDA_OPERATOR_NOT
153
154/** Dispatcher for lass::util::CallbackR0 to a callable that does not support operator!
155 * @internal
156 * @sa CallbackR0
157 * @author Bram de Greve [Bramz]
158 *
159 * With C++11, lambdas can also be used as callables. But the MSVC compiler did not support
160 * the unary ! operator. This was fixed in VS 2019 version 16.2.
161 * https://twitter.com/lunasorcery/status/1092870113374687232
162 */
163template
164<
165 typename R,
166 typename FunctionType
167>
169 <
170 R,
171 FunctionType,
172 typename meta::EnableIf<!HasOperatorNot<FunctionType>::value>::Type
173 >
174 : public DispatcherR0<R>
175{
176public:
177
178 typedef FunctionType TFunction;
179
180 DispatcherR0Function(typename CallTraits<TFunction>::TParam iFunction):
181 function_(iFunction)
182 {
183 }
184
185 const TFunction& function() const
186 {
187 return function_;
188 }
189
190private:
191
192 R doCall() const override
193 {
194 return function_();
195 }
196 bool doIsEquivalent(const DispatcherR0<R>* /*iOther*/) const override
197 {
198 return false;
199 }
200
201 TFunction function_;
202};
203
204#endif
205
206
207
208/** Dispatcher for lass::util::CallbackR0 to an object/method pair.
209 * @internal
210 * @sa CallbackR0
211 * @author Bram de Greve [Bramz]
212 */
213template
214<
215 typename R,
216 typename ObjectPtr,
217 typename Method
218>
219class DispatcherR0Method: public DispatcherR0<R>
220{
221public:
222
223 typedef DispatcherR0Method<R, ObjectPtr, Method> TSelf;
224
225 DispatcherR0Method(typename CallTraits<ObjectPtr>::TParam iObject,
226 typename CallTraits<Method>::TParam iMethod):
227 object_(iObject),
228 method_(iMethod)
229 {
230 }
231
232private:
233
234 R doCall() const override
235 {
236 if (!object_ || !method_)
237 {
238 LASS_THROW_EX(EmptyCallback, "You've tried to call an empty CallbackR0. Can't return a value.");
239 }
240 return ((*object_).*method_)();
241 }
242 bool doIsEquivalent(const DispatcherR0<R>* iOther) const override
243 {
244 const TSelf* other = dynamic_cast<const TSelf*>(iOther);
245 return other && object_ == other->object_ && method_ == other->method_;
246 }
247
248 ObjectPtr object_;
249 Method method_;
250};
251
252}
253
254}
255
256}
257
258#endif // Guardian of Inclusion
259
260
261// EOF
Dispatcher for lass::util::CallbackR0 to a free function:
abstract base class of all dispatchers for lass::util::CallbackR0.
library for template meta programming
Definition bool.h:76
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Definition config.h:53