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