Embedded Template Library 1.0
Loading...
Searching...
No Matches
functional.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) 2014 John Wellbelove
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_FUNCTIONAL_INCLUDED
32#define ETL_FUNCTIONAL_INCLUDED
33
34#include "platform.h"
35#include "utility.h"
36
39
42
43namespace etl
44{
45 //***************************************************************************
48 //***************************************************************************
49 template <typename T>
51 {
52 public:
53
54 typedef T type;
55
56 ETL_CONSTEXPR20 explicit reference_wrapper(T& t_) ETL_NOEXCEPT
57 : t(&t_)
58 {
59 }
60
61 ETL_CONSTEXPR20 reference_wrapper(const reference_wrapper& rhs) ETL_NOEXCEPT
62 : t(rhs.t)
63 {
64 }
65
66 ETL_CONSTEXPR20 reference_wrapper<T>& operator = (const reference_wrapper& rhs) ETL_NOEXCEPT
67 {
68 t = rhs.t;
69 return *this;
70 }
71
72 ETL_CONSTEXPR20 T& get() const ETL_NOEXCEPT
73 {
74 return *t;
75 }
76
77 ETL_CONSTEXPR20 operator T&() const ETL_NOEXCEPT
78 {
79 return *t;
80 }
81
82 private:
83
84 T* t;
85 };
86
87 //***************************************************************************
88 template <typename T>
90 {
91 return reference_wrapper<T>(t);
92 }
93
94 //***************************************************************************
95 template <typename T>
96 reference_wrapper<T> ref(reference_wrapper<T> t)
97 {
98 return reference_wrapper<T>(t.get());
99 }
100
101 //***************************************************************************
102 template <typename T>
103 reference_wrapper<const T> cref(const T& t)
104 {
105 return reference_wrapper<const T>(t);
106 }
107
108 //***************************************************************************
109 template <typename T>
110 reference_wrapper<const T> cref(reference_wrapper<T> t)
111 {
112 return reference_wrapper<const T>(t.get());
113 }
114
115 //***************************************************************************
116 template <typename TArgumentType, typename TResultType>
118 {
120 typedef TResultType result_type;
121 };
122
123 //***************************************************************************
124 template <typename TFirstArgumentType, typename TSecondArgumentType, typename TResultType>
131
132 //***************************************************************************
133 template <typename T = void>
134 struct less : public etl::binary_function<T, T, bool>
135 {
136 typedef T value_type;
137
138 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
139 {
140 return (lhs < rhs);
141 }
142 };
143
144#if ETL_USING_CPP11
145 template <>
146 struct less<void> : public etl::binary_function<void, void, bool>
147 {
148 typedef int is_transparent;
149
150 template <typename T1, typename T2>
151 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
152 {
153 return static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs);
154 }
155 };
156#endif
157
158 //***************************************************************************
159 template <typename T = void>
160 struct less_equal : public etl::binary_function<T, T, bool>
161 {
162 typedef T value_type;
163
164 ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const
165 {
166 return !(rhs < lhs);
167 }
168 };
169
170#if ETL_USING_CPP11
171 template <>
172 struct less_equal<void> : public etl::binary_function<void, void, bool>
173 {
174 typedef int is_transparent;
175
176 template <typename T1, typename T2>
177 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
178 {
179 return !(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs));
180 }
181 };
182#endif
183
184 //***************************************************************************
185 template <typename T = void>
186 struct greater : public etl::binary_function<T, T, bool>
187 {
188 typedef T value_type;
189
190 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
191 {
192 return (rhs < lhs);
193 }
194 };
195
196#if ETL_USING_CPP11
197 template <>
198 struct greater<void> : public etl::binary_function<void, void, bool>
199 {
200 typedef int is_transparent;
201
202 template <typename T1, typename T2>
203 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
204 {
205 return static_cast<T1&&>(rhs) < static_cast<T2&&>(lhs);
206 }
207 };
208#endif
209
210 //***************************************************************************
211 template <typename T = void>
212 struct greater_equal : public etl::binary_function<T, T, bool>
213 {
214 typedef T value_type;
215
216 ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const
217 {
218 return !(lhs < rhs);
219 }
220 };
221
222#if ETL_USING_CPP11
223 template <>
224 struct greater_equal<void> : public etl::binary_function<void, void, bool>
225 {
226 typedef int is_transparent;
227
228 template <typename T1, typename T2>
229 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
230 {
231 return static_cast<T1&&>(rhs) < static_cast<T2&&>(lhs);
232 }
233 };
234#endif
235
236 //***************************************************************************
237 template <typename T = void>
238 struct equal_to : public etl::binary_function<T, T, bool>
239 {
240 typedef T value_type;
241
242 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
243 {
244 return lhs == rhs;
245 }
246 };
247
248#if ETL_USING_CPP11
249 template <>
250 struct equal_to<void> : public etl::binary_function<void, void, bool>
251 {
252 typedef void value_type;
253 typedef int is_transparent;
254
255 template <typename T1, typename T2>
256 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
257 {
258 return static_cast<T1&&>(lhs) == static_cast<T2&&>(rhs);
259 }
260 };
261#endif
262
263 //***************************************************************************
264 template <typename T = void>
265 struct not_equal_to : public etl::binary_function<T, T, bool>
266 {
267 typedef T value_type;
268
269 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
270 {
271 return !(lhs == rhs);
272 }
273 };
274
275#if ETL_USING_CPP11
276 template <>
277 struct not_equal_to<void> : public etl::binary_function<void, void, bool>
278 {
279 typedef int is_transparent;
280
281 template <typename T1, typename T2>
282 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
283 {
284 return !(static_cast<T1&&>(lhs) == static_cast<T2&&>(rhs));
285 }
286 };
287#endif
288
289 //***************************************************************************
290 template <typename TFunction>
291 class binder1st : public etl::unary_function<typename TFunction::second_argument_type, typename TFunction::result_type>
292 {
293 protected:
294
295 TFunction operation;
296 typename TFunction::first_argument_type value;
297
298 public:
299
300 binder1st(const TFunction& f, const typename TFunction::first_argument_type& v)
301 : operation(f), value(v)
302 {
303 }
304
305 typename TFunction::result_type operator()(typename TFunction::second_argument_type& x) const
306 {
307 return operation(value, x);
308 }
309
310 typename TFunction::result_type operator()(const typename TFunction::second_argument_type& x) const
311 {
312 return operation(value, x);
313 }
314 };
315
316 template <typename F, typename T>
317 binder1st<F> bind1st(const F& f, const T& x)
318 {
319 return binder1st<F>(f, x);
320 }
321
322 //***************************************************************************
323 template <typename TFunction >
324 class binder2nd : public etl::unary_function<typename TFunction::first_argument_type, typename TFunction::result_type>
325 {
326 protected:
327 TFunction operation;
328 typename TFunction::second_argument_type value;
329 public:
330 binder2nd(const TFunction& f, const typename TFunction::second_argument_type& v)
331 : operation(f), value(v)
332 {
333 }
334
335 typename TFunction::result_type operator()(typename TFunction::first_argument_type& x) const
336 {
337 return operation(x, value);
338 }
339
340 typename TFunction::result_type operator()(const typename TFunction::first_argument_type& x) const
341 {
342 return operation(x, value);
343 }
344 };
345
346 template <typename F, typename T>
347 binder2nd<F> bind2nd(const F& f, const T& x)
348 {
349 return binder2nd<F>(f, x);
350 }
351
352 //***************************************************************************
353 template <typename T = void>
354 struct plus
355 {
356 typedef T first_argument_type;
357 typedef T second_argument_type;
358 typedef T result_type;
359
360 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
361 {
362 return lhs + rhs;
363 }
364 };
365
366 //***************************************************************************
367 template <typename T = void>
368 struct minus
369 {
370 typedef T first_argument_type;
371 typedef T second_argument_type;
372 typedef T result_type;
373
374 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
375 {
376 return lhs - rhs;
377 }
378 };
379
380 //***************************************************************************
381 template <typename T = void>
382 struct negate
383 {
384 typedef T argument_type;
385 typedef T result_type;
386
387 ETL_CONSTEXPR T operator()(const T& lhs) const
388 {
389 return -lhs;
390 }
391 };
392
393 //***************************************************************************
394 template <typename T = void>
396 {
397 typedef T first_argument_type;
398 typedef T second_argument_type;
399 typedef T result_type;
400
401 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
402 {
403 return lhs * rhs;
404 }
405 };
406
407 //***************************************************************************
408 template <typename T = void>
409 struct divides
410 {
411 typedef T first_argument_type;
412 typedef T second_argument_type;
413 typedef T result_type;
414
415 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
416 {
417 return lhs / rhs;
418 }
419 };
420
421 //***************************************************************************
422 template <typename T = void>
423 struct modulus
424 {
425 typedef T first_argument_type;
426 typedef T second_argument_type;
427 typedef T result_type;
428
429 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
430 {
431 return lhs % rhs;
432 }
433 };
434
435 //***************************************************************************
436 template <typename T = void>
438 {
439 typedef T first_argument_type;
440 typedef T second_argument_type;
441 typedef T result_type;
442
443 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
444 {
445 return lhs && rhs;
446 }
447 };
448
449 //***************************************************************************
450 template <typename T = void>
452 {
453 typedef T first_argument_type;
454 typedef T second_argument_type;
455 typedef T result_type;
456
457 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
458 {
459 return lhs || rhs;
460 }
461 };
462
463 //***************************************************************************
464 template <typename T = void>
466 {
467 typedef T first_argument_type;
468 typedef T second_argument_type;
469 typedef T result_type;
470
471 ETL_CONSTEXPR T operator()(const T& lhs) const
472 {
473 return !lhs;
474 }
475 };
476
477 //***************************************************************************
478 template <typename T = void>
479 struct bit_and
480 {
481 typedef T first_argument_type;
482 typedef T second_argument_type;
483 typedef T result_type;
484
485 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
486 {
487 return lhs & rhs;
488 }
489 };
490
491 //***************************************************************************
492 template <typename T = void>
493 struct bit_or
494 {
495 typedef T first_argument_type;
496 typedef T second_argument_type;
497 typedef T result_type;
498
499 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
500 {
501 return lhs | rhs;
502 }
503 };
504
505 //***************************************************************************
506 template <typename T = void>
507 struct bit_xor
508 {
509 typedef T first_argument_type;
510 typedef T second_argument_type;
511 typedef T result_type;
512
513 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
514 {
515 return lhs ^ rhs;
516 }
517 };
518
519 //***************************************************************************
520 template <typename T = void>
521 struct bit_not
522 {
523 typedef T first_argument_type;
524 typedef T second_argument_type;
525 typedef T result_type;
526
527 ETL_CONSTEXPR T operator()(const T& lhs) const
528 {
529 return ~lhs;
530 }
531 };
532
533#if ETL_USING_CPP11
534 namespace private_functional
535 {
536 //***************************************************************************
537 template<typename TReturnType, typename TClassType, typename... TArgs>
538 class mem_fn_impl
539 {
540 public:
541
543
546 {
547 }
548
549 ETL_CONSTEXPR TReturnType operator()(TClassType& instance, TArgs... args) const
550 {
551 return (instance.*member_function)(etl::forward<TArgs>(args)...);
552 }
553
554 private:
555
556 MemberFunctionType member_function;
557 };
558
559 //***************************************************************************
560 template<typename TReturnType, typename TClassType, typename... TArgs>
562 {
563 public:
564
565 typedef TReturnType(TClassType::* MemberFunctionType)(TArgs...) const;
566
567 ETL_CONSTEXPR const_mem_fn_impl(MemberFunctionType member_function_)
568 : member_function(member_function_)
569 {
570 }
571
572 ETL_CONSTEXPR TReturnType operator()(const TClassType& instance, TArgs... args) const
573 {
574 return (instance.*member_function)(etl::forward<TArgs>(args)...);
575 }
576
577 private:
578
579 MemberFunctionType member_function;
580 };
581 }
582
583 //***************************************************************************
584 template<typename TReturnType, typename TClassType, typename... TArgs>
585 ETL_CONSTEXPR
586 private_functional::mem_fn_impl<TReturnType, TClassType, TArgs...> mem_fn(TReturnType(TClassType::* member_function)(TArgs...))
587 {
588 return private_functional::mem_fn_impl<TReturnType, TClassType, TArgs...>(member_function);
589 }
590
591 //***************************************************************************
592 template<typename TReturnType, typename TClassType, typename... TArgs>
593 ETL_CONSTEXPR
594 private_functional::const_mem_fn_impl<TReturnType, TClassType, TArgs...> mem_fn(TReturnType(TClassType::* member_function)(TArgs...) const)
595 {
596 return private_functional::const_mem_fn_impl<TReturnType, TClassType, TArgs...>(member_function);
597 }
598#endif
599}
600
601#endif
602
Definition functional.h:292
Definition functional.h:325
Definition functional.h:51
bitset_ext
Definition absolute.h:38
Definition functional.h:126
Definition functional.h:480
Definition functional.h:522
Definition functional.h:494
Definition functional.h:508
Definition functional.h:410
Definition functional.h:239
Definition functional.h:213
Definition functional.h:187
Definition functional.h:161
Definition functional.h:135
Definition functional.h:438
Definition functional.h:466
Definition functional.h:452
Definition functional.h:369
Definition functional.h:424
Definition functional.h:396
Definition functional.h:383
Definition functional.h:266
pair holds two objects of arbitrary type
Definition utility.h:164
ETL_CONSTEXPR pair()
Default constructor.
Definition utility.h:176
Definition functional.h:355
Definition functional.h:118