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>
50 class reference_wrapper
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>
89 reference_wrapper<T> ref(T& 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 {
106 }
107
108 //***************************************************************************
109 template <typename T>
111 {
112 return reference_wrapper<const T>(t.get());
113 }
114
115 //***************************************************************************
117 //***************************************************************************
118 template <class T>
120 {
121 typedef T type;
122 };
123
124 template <typename T>
126 {
127 typedef T& type;
128 };
129
130#if ETL_USING_CPP11
131 template <typename T>
132 using unwrap_reference_t = typename unwrap_reference<T>::type;
133#endif
134
135 //***************************************************************************
137 //***************************************************************************
138 template <typename T>
139 struct unwrap_ref_decay : etl::unwrap_reference<typename etl::decay<T>::type> {};
140
141#if ETL_USING_CPP11
142 template <typename T>
143 using unwrap_ref_decay_t = typename unwrap_ref_decay<T>::type;
144#endif
145
146 //***************************************************************************
148 //***************************************************************************
149 template <typename TArgumentType, typename TResultType>
151 {
152 typedef TArgumentType argument_type;
153 typedef TResultType result_type;
154 };
155
156 //***************************************************************************
158 //***************************************************************************
159 template <typename TFirstArgumentType, typename TSecondArgumentType, typename TResultType>
161 {
162 typedef TFirstArgumentType first_argument_type;
163 typedef TSecondArgumentType second_argument_type;
164 typedef TResultType result_type;
165 };
166
167 //***************************************************************************
168 template <typename T = void>
169 struct less : public etl::binary_function<T, T, bool>
170 {
171 typedef T value_type;
172
173 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
174 {
175 return (lhs < rhs);
176 }
177 };
178
179#if ETL_USING_CPP11
180 template <>
181 struct less<void> : public etl::binary_function<void, void, bool>
182 {
183 typedef int is_transparent;
184
185 template <typename T1, typename T2>
186 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
187 {
188 return static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs);
189 }
190 };
191#endif
192
193 //***************************************************************************
194 template <typename T = void>
195 struct less_equal : public etl::binary_function<T, T, bool>
196 {
197 typedef T value_type;
198
199 ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const
200 {
201 return !(rhs < lhs);
202 }
203 };
204
205#if ETL_USING_CPP11
206 template <>
207 struct less_equal<void> : public etl::binary_function<void, void, bool>
208 {
209 typedef int is_transparent;
210
211 template <typename T1, typename T2>
212 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
213 {
214 return !(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs));
215 }
216 };
217#endif
218
219 //***************************************************************************
220 template <typename T = void>
221 struct greater : public etl::binary_function<T, T, bool>
222 {
223 typedef T value_type;
224
225 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
226 {
227 return (rhs < lhs);
228 }
229 };
230
231#if ETL_USING_CPP11
232 template <>
233 struct greater<void> : public etl::binary_function<void, void, bool>
234 {
235 typedef int is_transparent;
236
237 template <typename T1, typename T2>
238 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
239 {
240 return static_cast<T2&&>(rhs) < static_cast<T1&&>(lhs);
241 }
242 };
243#endif
244
245 //***************************************************************************
246 template <typename T = void>
247 struct greater_equal : public etl::binary_function<T, T, bool>
248 {
249 typedef T value_type;
250
251 ETL_CONSTEXPR bool operator()(const T& lhs, const T& rhs) const
252 {
253 return !(lhs < rhs);
254 }
255 };
256
257#if ETL_USING_CPP11
258 template <>
259 struct greater_equal<void> : public etl::binary_function<void, void, bool>
260 {
261 typedef int is_transparent;
262
263 template <typename T1, typename T2>
264 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
265 {
266 return static_cast<T1&&>(rhs) < static_cast<T2&&>(lhs);
267 }
268 };
269#endif
270
271 //***************************************************************************
272 template <typename T = void>
273 struct equal_to : public etl::binary_function<T, T, bool>
274 {
275 typedef T value_type;
276
277 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
278 {
279 return lhs == rhs;
280 }
281 };
282
283#if ETL_USING_CPP11
284 template <>
285 struct equal_to<void> : public etl::binary_function<void, void, bool>
286 {
287 typedef void value_type;
288 typedef int is_transparent;
289
290 template <typename T1, typename T2>
291 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
292 {
293 return static_cast<T1&&>(lhs) == static_cast<T2&&>(rhs);
294 }
295 };
296#endif
297
298 //***************************************************************************
299 template <typename T = void>
300 struct not_equal_to : public etl::binary_function<T, T, bool>
301 {
302 typedef T value_type;
303
304 ETL_CONSTEXPR bool operator()(const T &lhs, const T &rhs) const
305 {
306 return !(lhs == rhs);
307 }
308 };
309
310#if ETL_USING_CPP11
311 template <>
312 struct not_equal_to<void> : public etl::binary_function<void, void, bool>
313 {
314 typedef int is_transparent;
315
316 template <typename T1, typename T2>
317 constexpr auto operator()(T1&& lhs, T2&& rhs) const -> decltype(static_cast<T1&&>(lhs) < static_cast<T2&&>(rhs))
318 {
319 return !(static_cast<T1&&>(lhs) == static_cast<T2&&>(rhs));
320 }
321 };
322#endif
323
324 //***************************************************************************
325 template <typename TFunction>
326 class binder1st : public etl::unary_function<typename TFunction::second_argument_type, typename TFunction::result_type>
327 {
328 protected:
329
330 TFunction operation;
331 typename TFunction::first_argument_type value;
332
333 public:
334
335 binder1st(const TFunction& f, const typename TFunction::first_argument_type& v)
336 : operation(f), value(v)
337 {
338 }
339
340 typename TFunction::result_type operator()(typename TFunction::second_argument_type& x) const
341 {
342 return operation(value, x);
343 }
344
345 typename TFunction::result_type operator()(const typename TFunction::second_argument_type& x) const
346 {
347 return operation(value, x);
348 }
349 };
350
351 template <typename F, typename T>
352 binder1st<F> bind1st(const F& f, const T& x)
353 {
354 return binder1st<F>(f, x);
355 }
356
357 //***************************************************************************
358 template <typename TFunction >
359 class binder2nd : public etl::unary_function<typename TFunction::first_argument_type, typename TFunction::result_type>
360 {
361 protected:
362 TFunction operation;
363 typename TFunction::second_argument_type value;
364 public:
365 binder2nd(const TFunction& f, const typename TFunction::second_argument_type& v)
366 : operation(f), value(v)
367 {
368 }
369
370 typename TFunction::result_type operator()(typename TFunction::first_argument_type& x) const
371 {
372 return operation(x, value);
373 }
374
375 typename TFunction::result_type operator()(const typename TFunction::first_argument_type& x) const
376 {
377 return operation(x, value);
378 }
379 };
380
381 template <typename F, typename T>
382 binder2nd<F> bind2nd(const F& f, const T& x)
383 {
384 return binder2nd<F>(f, x);
385 }
386
387 //***************************************************************************
388 template <typename T = void>
389 struct plus
390 {
391 typedef T first_argument_type;
392 typedef T second_argument_type;
393 typedef T result_type;
394
395 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
396 {
397 return lhs + rhs;
398 }
399 };
400
401 //***************************************************************************
402 template <typename T = void>
403 struct minus
404 {
405 typedef T first_argument_type;
406 typedef T second_argument_type;
407 typedef T result_type;
408
409 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
410 {
411 return lhs - rhs;
412 }
413 };
414
415 //***************************************************************************
416 template <typename T = void>
417 struct negate
418 {
419 typedef T argument_type;
420 typedef T result_type;
421
422 ETL_CONSTEXPR T operator()(const T& lhs) const
423 {
424 return -lhs;
425 }
426 };
427
428 //***************************************************************************
429 template <typename T = void>
431 {
432 typedef T first_argument_type;
433 typedef T second_argument_type;
434 typedef T result_type;
435
436 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
437 {
438 return lhs * rhs;
439 }
440 };
441
442 //***************************************************************************
443 template <typename T = void>
444 struct divides
445 {
446 typedef T first_argument_type;
447 typedef T second_argument_type;
448 typedef T result_type;
449
450 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
451 {
452 return lhs / rhs;
453 }
454 };
455
456 //***************************************************************************
457 template <typename T = void>
458 struct modulus
459 {
460 typedef T first_argument_type;
461 typedef T second_argument_type;
462 typedef T result_type;
463
464 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
465 {
466 return lhs % rhs;
467 }
468 };
469
470 //***************************************************************************
471 template <typename T = void>
473 {
474 typedef T first_argument_type;
475 typedef T second_argument_type;
476 typedef T result_type;
477
478 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
479 {
480 return lhs && rhs;
481 }
482 };
483
484 //***************************************************************************
485 template <typename T = void>
487 {
488 typedef T first_argument_type;
489 typedef T second_argument_type;
490 typedef T result_type;
491
492 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
493 {
494 return lhs || rhs;
495 }
496 };
497
498 //***************************************************************************
499 template <typename T = void>
501 {
502 typedef T first_argument_type;
503 typedef T second_argument_type;
504 typedef T result_type;
505
506 ETL_CONSTEXPR T operator()(const T& lhs) const
507 {
508 return !lhs;
509 }
510 };
511
512 //***************************************************************************
513 template <typename T = void>
514 struct bit_and
515 {
516 typedef T first_argument_type;
517 typedef T second_argument_type;
518 typedef T result_type;
519
520 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
521 {
522 return lhs & rhs;
523 }
524 };
525
526 //***************************************************************************
527 template <typename T = void>
528 struct bit_or
529 {
530 typedef T first_argument_type;
531 typedef T second_argument_type;
532 typedef T result_type;
533
534 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
535 {
536 return lhs | rhs;
537 }
538 };
539
540 //***************************************************************************
541 template <typename T = void>
542 struct bit_xor
543 {
544 typedef T first_argument_type;
545 typedef T second_argument_type;
546 typedef T result_type;
547
548 ETL_CONSTEXPR T operator()(const T& lhs, const T& rhs) const
549 {
550 return lhs ^ rhs;
551 }
552 };
553
554 //***************************************************************************
555 template <typename T = void>
556 struct bit_not
557 {
558 typedef T first_argument_type;
559 typedef T second_argument_type;
560 typedef T result_type;
561
562 ETL_CONSTEXPR T operator()(const T& lhs) const
563 {
564 return ~lhs;
565 }
566 };
567
568#if ETL_USING_CPP11
569 namespace private_functional
570 {
571 //***************************************************************************
572 template<typename TReturnType, typename TClassType, typename... TArgs>
573 class mem_fn_impl
574 {
575 public:
576
577 typedef TReturnType(TClassType::* MemberFunctionType)(TArgs...);
578
579 ETL_CONSTEXPR mem_fn_impl(MemberFunctionType member_function_)
580 : member_function(member_function_)
581 {
582 }
583
584 ETL_CONSTEXPR TReturnType operator()(TClassType& instance, TArgs... args) const
585 {
586 return (instance.*member_function)(etl::forward<TArgs>(args)...);
587 }
588
589 private:
590
591 MemberFunctionType member_function;
592 };
593
594 //***************************************************************************
595 template<typename TReturnType, typename TClassType, typename... TArgs>
596 class const_mem_fn_impl
597 {
598 public:
599
600 typedef TReturnType(TClassType::* MemberFunctionType)(TArgs...) const;
601
602 ETL_CONSTEXPR const_mem_fn_impl(MemberFunctionType member_function_)
603 : member_function(member_function_)
604 {
605 }
606
607 ETL_CONSTEXPR TReturnType operator()(const TClassType& instance, TArgs... args) const
608 {
609 return (instance.*member_function)(etl::forward<TArgs>(args)...);
610 }
611
612 private:
613
614 MemberFunctionType member_function;
615 };
616 }
617
618 //***************************************************************************
619 template<typename TReturnType, typename TClassType, typename... TArgs>
620 ETL_CONSTEXPR
621 private_functional::mem_fn_impl<TReturnType, TClassType, TArgs...> mem_fn(TReturnType(TClassType::* member_function)(TArgs...))
622 {
623 return private_functional::mem_fn_impl<TReturnType, TClassType, TArgs...>(member_function);
624 }
625
626 //***************************************************************************
627 template<typename TReturnType, typename TClassType, typename... TArgs>
628 ETL_CONSTEXPR
629 private_functional::const_mem_fn_impl<TReturnType, TClassType, TArgs...> mem_fn(TReturnType(TClassType::* member_function)(TArgs...) const)
630 {
631 return private_functional::const_mem_fn_impl<TReturnType, TClassType, TArgs...>(member_function);
632 }
633#endif
634}
635
636#endif
637
Definition functional.h:327
Definition functional.h:360
Definition functional.h:51
bitset_ext
Definition absolute.h:39
binary_function
Definition functional.h:161
Definition functional.h:515
Definition functional.h:557
Definition functional.h:529
Definition functional.h:543
Definition functional.h:445
Definition functional.h:274
Definition functional.h:248
Definition functional.h:222
Definition functional.h:196
Definition functional.h:170
Definition functional.h:473
Definition functional.h:501
Definition functional.h:487
Definition functional.h:404
Definition functional.h:459
Definition functional.h:431
Definition functional.h:418
Definition functional.h:301
Definition functional.h:390
unary_function
Definition functional.h:151
unwrap_ref_decay.
Definition functional.h:139
unwrap_reference.
Definition functional.h:120