Embedded Template Library 1.0
Loading...
Searching...
No Matches
closure.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2025 BMW AG
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_CLOSURE_INCLUDED
32#define ETL_CLOSURE_INCLUDED
33
34#include "platform.h"
35#include "delegate.h"
36#include "tuple.h"
37#include "utility.h"
38#include "type_list.h"
39
40namespace etl
41{
42#if ETL_USING_CPP11 && !defined(ETL_CLOSURE_FORCE_CPP03_IMPLEMENTATION)
43 //*************************************************************************
45 //*************************************************************************
46 template <typename>
47 class closure;
48
49 //*************************************************************************
60 //*************************************************************************
61 template <typename TReturn, typename... TArgs>
62 class closure<TReturn(TArgs...)>
63 {
64 public:
65
66 using delegate_type = etl::delegate<TReturn(TArgs...)>;
68
69 //*********************************************************************
73 //*********************************************************************
74 ETL_CONSTEXPR14 closure(const delegate_type& f, const TArgs... args) ETL_NOEXCEPT
75 : m_f(f)
76 , m_args(args...)
77 {
78 }
79
80 //*********************************************************************
83 //*********************************************************************
84 ETL_CONSTEXPR14 TReturn operator()() const ETL_NOEXCEPT
85 {
87 }
88
89 //*********************************************************************
95 //*********************************************************************
96 template <size_t Index, typename UArg>
97 ETL_CONSTEXPR14 void bind(UArg arg) ETL_NOEXCEPT
98 {
99 static_assert(etl::is_convertible<UArg, etl::type_list_type_at_index_t<argument_types, Index>>::value, "Argument is not convertible");
100 static_assert(!etl::is_reference<UArg>::value, "Cannot bind reference arguments");
101
103 }
104
105 //*********************************************************************
110 template <typename... UArgs>
111 ETL_CONSTEXPR14 void bind(UArgs&&... args) ETL_NOEXCEPT
112 {
113 static_assert(sizeof...(UArgs) == sizeof...(TArgs), "Argument count mismatch");
115 }
116
117 private:
118
119 //*********************************************************************
123 template <size_t... Indexes, typename... UArgs>
124 ETL_CONSTEXPR14 void bind_impl(etl::index_sequence<Indexes...>, UArgs&&... args) ETL_NOEXCEPT
125 {
126 // Expand the pack and call bind<Index>(arg) for each argument
127 int dummy[] = {0, (bind<Indexes>(etl::forward<UArgs>(args)), 0)...};
128 (void)dummy; // Suppress unused variable warning
129 }
130
131 //*********************************************************************
135 //*********************************************************************
136 template<size_t... Indexes>
137 ETL_CONSTEXPR14 TReturn execute(etl::index_sequence<Indexes...>) const ETL_NOEXCEPT
138 {
139 return m_f(etl::get<Indexes>(m_args)...);
140 }
141
142 delegate_type m_f;
144 };
145#else
146 //*************************************************************************
148 //*************************************************************************
149 template <typename>
150 class closure;
151
152 //*************************************************************************
156 //*************************************************************************
157 template<typename TReturn, typename TArg0>
159 {
160 public:
161
164
165 //*********************************************************************
169 //*********************************************************************
171 : m_f(f)
172 , m_arg0(arg0)
173 {
174 }
175
176 //*********************************************************************
179 //*********************************************************************
181 {
182 return m_f(m_arg0);
183 }
184
185 private:
186
187 delegate_type m_f;
188 TArg0 m_arg0;
189 };
190
191 //*************************************************************************
196 //*************************************************************************
197 template<typename TReturn, typename TArg0, typename TArg1>
199 {
200 public:
201
203
204 //*********************************************************************
209 //*********************************************************************
210 closure(const delegate_type& f, const TArg0 arg0, const TArg1 arg1)
211 : m_f(f)
212 , m_arg0(arg0)
213 , m_arg1(arg1)
214 {
215 }
216
217 //*********************************************************************
220 //*********************************************************************
222 {
223 return m_f(m_arg0, m_arg1);
224 }
225
226 private:
227
228 delegate_type m_f;
229 TArg0 m_arg0;
230 TArg1 m_arg1;
231 };
232
233 //*************************************************************************
239 //*************************************************************************
240 template<typename TReturn, typename TArg0, typename TArg1, typename TArg2>
242 {
243 public:
244
246
247 //*********************************************************************
253 //*********************************************************************
254 closure(const delegate_type& f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2)
255 : m_f(f)
256 , m_arg0(arg0)
257 , m_arg1(arg1)
258 , m_arg2(arg2)
259 {
260 }
261
262 //*********************************************************************
265 //*********************************************************************
267 {
268 return m_f(m_arg0, m_arg1, m_arg2);
269 }
270
271 private:
272
273 delegate_type m_f;
274 TArg0 m_arg0;
275 TArg1 m_arg1;
276 TArg2 m_arg2;
277 };
278
279 //*************************************************************************
286 //*************************************************************************
287 template<typename TReturn, typename TArg0, typename TArg1, typename TArg2, typename TArg3>
289 {
290 public:
291
293
294 //*********************************************************************
301 //*********************************************************************
302 closure(const delegate_type& f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3)
303 : m_f(f)
304 , m_arg0(arg0)
305 , m_arg1(arg1)
306 , m_arg2(arg2)
307 , m_arg3(arg3)
308 {
309 }
310
311 //*********************************************************************
314 //*********************************************************************
316 {
317 return m_f(m_arg0, m_arg1, m_arg2, m_arg3);
318 }
319
320 private:
321
322 delegate_type m_f;
323 TArg0 m_arg0;
324 TArg1 m_arg1;
325 TArg2 m_arg2;
326 TArg3 m_arg3;
327 };
328
329 //*************************************************************************
337 //*************************************************************************
338 template<typename TReturn, typename TArg0, typename TArg1, typename TArg2, typename TArg3, typename TArg4>
340 {
341 public:
342
344
345 //*********************************************************************
353 //*********************************************************************
354 closure(const delegate_type& f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3, const TArg4 arg4)
355 : m_f(f)
356 , m_arg0(arg0)
357 , m_arg1(arg1)
358 , m_arg2(arg2)
359 , m_arg3(arg3)
360 , m_arg4(arg4)
361 {
362 }
363
364 //*********************************************************************
367 //*********************************************************************
369 {
370 return m_f(m_arg0, m_arg1, m_arg2, m_arg3, m_arg4);
371 }
372
373 private:
374
375 delegate_type m_f;
376 TArg0 m_arg0;
377 TArg1 m_arg1;
378 TArg2 m_arg2;
379 TArg3 m_arg3;
380 TArg4 m_arg4;
381 };
382#endif
383}
384
385#endif
TReturn operator()() const
Definition closure.h:368
closure(const delegate_type &f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3, const TArg4 arg4)
Definition closure.h:354
closure(const delegate_type &f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2, const TArg3 arg3)
Definition closure.h:302
TReturn operator()() const
Definition closure.h:315
closure(const delegate_type &f, const TArg0 arg0, const TArg1 arg1, const TArg2 arg2)
Definition closure.h:254
TReturn operator()() const
Definition closure.h:266
TReturn operator()() const
Definition closure.h:221
closure(const delegate_type &f, const TArg0 arg0, const TArg1 arg1)
Definition closure.h:210
TReturn operator()() const
Definition closure.h:180
etl::delegate< TReturn(TArg0)> delegate_type
The delegate type to be invoked.
Definition closure.h:163
closure(const delegate_type &f, const TArg0 arg0)
Definition closure.h:170
Base template for closure.
Definition closure.h:150
Declaration.
Definition delegate_cpp03.h:191
is_reference
Definition type_traits_generator.h:1150
bitset_ext
Definition absolute.h:38
ETL_NODISCARD ETL_CONSTEXPR14 T round_half_even_unscaled(T value) ETL_NOEXCEPT
Definition scaled_rounding.h:314