-
Notifications
You must be signed in to change notification settings - Fork 7
/
functional.h
1267 lines (1044 loc) · 37.5 KB
/
functional.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_FUNCTIONAL_H
#define EASTL_FUNCTIONAL_H
#include <eastl/EABase/eabase.h>
#include <eastl/internal/config.h>
#include <eastl/internal/move_help.h>
#include <eastl/type_traits.h>
#include <eastl/internal/functional_base.h>
#include <eastl/internal/mem_fn.h>
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
///////////////////////////////////////////////////////////////////////
// Primary C++ functions
///////////////////////////////////////////////////////////////////////
template <typename T = void>
struct plus
{
EA_CPP14_CONSTEXPR T operator()(const T& a, const T& b) const
{ return a + b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/plus_void
template <>
struct plus<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) + eastl::forward<B>(b))
{ return eastl::forward<A>(a) + eastl::forward<B>(b); }
};
template <typename T = void>
struct minus
{
EA_CPP14_CONSTEXPR T operator()(const T& a, const T& b) const
{ return a - b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/minus_void
template <>
struct minus<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) - eastl::forward<B>(b))
{ return eastl::forward<A>(a) - eastl::forward<B>(b); }
};
template <typename T = void>
struct multiplies
{
EA_CPP14_CONSTEXPR T operator()(const T& a, const T& b) const
{ return a * b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/multiplies_void
template <>
struct multiplies<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) * eastl::forward<B>(b))
{ return eastl::forward<A>(a) * eastl::forward<B>(b); }
};
template <typename T = void>
struct divides
{
EA_CPP14_CONSTEXPR T operator()(const T& a, const T& b) const
{ return a / b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/divides_void
template <>
struct divides<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) / eastl::forward<B>(b))
{ return eastl::forward<A>(a) / eastl::forward<B>(b); }
};
template <typename T = void>
struct modulus
{
EA_CPP14_CONSTEXPR T operator()(const T& a, const T& b) const
{ return a % b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/modulus_void
template <>
struct modulus<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) % eastl::forward<B>(b))
{ return eastl::forward<A>(a) % eastl::forward<B>(b); }
};
template <typename T = void>
struct negate
{
EA_CPP14_CONSTEXPR T operator()(const T& a) const
{ return -a; }
};
// http://en.cppreference.com/w/cpp/utility/functional/negate_void
template <>
struct negate<void>
{
typedef int is_transparent;
template<typename T>
EA_CPP14_CONSTEXPR auto operator()(T&& t) const
-> decltype(-eastl::forward<T>(t))
{ return -eastl::forward<T>(t); }
};
template <typename T = void>
struct equal_to
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a == b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/equal_to_void
template <>
struct equal_to<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) == eastl::forward<B>(b))
{ return eastl::forward<A>(a) == eastl::forward<B>(b); }
};
template <typename T, typename Compare>
bool validate_equal_to(const T& a, const T& b, Compare compare)
{
return compare(a, b) == compare(b, a);
}
template <typename T = void>
struct not_equal_to
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a != b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/not_equal_to_void
template <>
struct not_equal_to<void>
{
typedef int is_transparent;
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) != eastl::forward<B>(b))
{ return eastl::forward<A>(a) != eastl::forward<B>(b); }
};
template <typename T, typename Compare>
bool validate_not_equal_to(const T& a, const T& b, Compare compare)
{
return compare(a, b) == compare(b, a); // We want the not equal comparison results to be equal.
}
/// str_equal_to
///
/// Compares two 0-terminated string types.
/// The T types are expected to be iterators or act like iterators.
/// The expected behavior of str_less is the same as (strcmp(p1, p2) == 0).
///
/// Example usage:
/// hashSet<const char*, hash<const char*>, str_equal_to<const char*> > stringHashSet;
///
/// Note:
/// You couldn't use str_equal_to like this:
/// bool result = equal("hi", "hi" + 2, "ho", str_equal_to<const char*>());
/// This is because equal tests an array of something, with each element by
/// the comparison function. But str_equal_to tests an array of something itself.
///
/// To consider: Update this code to use existing word-based comparison optimizations,
/// such as that used in the EAStdC Strcmp function.
///
template <typename T>
struct str_equal_to
{
EA_CPP14_CONSTEXPR bool operator()(T a, T b) const
{
while(*a && (*a == *b))
{
++a;
++b;
}
return (*a == *b);
}
};
template <typename T = void>
struct greater
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a > b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/greater_void
template <>
struct greater<void>
{
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) > eastl::forward<B>(b))
{ return eastl::forward<A>(a) > eastl::forward<B>(b); }
};
template <typename T, typename Compare>
bool validate_greater(const T& a, const T& b, Compare compare)
{
return !compare(a, b) || !compare(b, a); // If (a > b), then !(b > a)
}
template <typename T, typename Compare>
bool validate_less(const T& a, const T& b, Compare compare)
{
return !compare(a, b) || !compare(b, a); // If (a < b), then !(b < a)
}
/// str_less
///
/// Compares two 0-terminated string types.
/// The T types are expected to be iterators or act like iterators,
/// and that includes being a pointer to a C character array.
/// The expected behavior of str_less is the same as (strcmp(p1, p2) < 0).
/// This function is not Unicode-correct and it's not guaranteed to work
/// with all Unicode strings.
///
/// Example usage:
/// set<const char*, str_less<const char*> > stringSet;
///
/// To consider: Update this code to use existing word-based comparison optimizations,
/// such as that used in the EAStdC Strcmp function.
///
template <typename T>
struct str_less
{
bool operator()(T a, T b) const
{
while(static_cast<typename make_unsigned<typename remove_pointer<T>::type>::type>(*a) ==
static_cast<typename make_unsigned<typename remove_pointer<T>::type>::type>(*b))
{
if(*a == 0)
return (*b != 0);
++a;
++b;
}
char aValue = static_cast<typename remove_pointer<T>::type>(*a);
char bValue = static_cast<typename remove_pointer<T>::type>(*b);
typename make_unsigned<char>::type aValueU = static_cast<typename make_unsigned<char>::type>(aValue);
typename make_unsigned<char>::type bValueU = static_cast<typename make_unsigned<char>::type>(bValue);
return aValueU < bValueU;
//return (static_cast<typename make_unsigned<typename remove_pointer<T>::type>::type>(*a) <
// static_cast<typename make_unsigned<typename remove_pointer<T>::type>::type>(*b));
}
};
template <typename T = void>
struct greater_equal
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a >= b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/greater_equal_void
template <>
struct greater_equal<void>
{
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) >= eastl::forward<B>(b))
{ return eastl::forward<A>(a) >= eastl::forward<B>(b); }
};
template <typename T, typename Compare>
bool validate_greater_equal(const T& a, const T& b, Compare compare)
{
return !compare(a, b) || !compare(b, a); // If (a >= b), then !(b >= a)
}
template <typename T = void>
struct less_equal
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a <= b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/less_equal_void
template <>
struct less_equal<void>
{
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) <= eastl::forward<B>(b))
{ return eastl::forward<A>(a) <= eastl::forward<B>(b); }
};
template <typename T, typename Compare>
bool validate_less_equal(const T& a, const T& b, Compare compare)
{
return !compare(a, b) || !compare(b, a); // If (a <= b), then !(b <= a)
}
template <typename T = void>
struct logical_and
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a && b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/logical_and_void
template <>
struct logical_and<void>
{
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) && eastl::forward<B>(b))
{ return eastl::forward<A>(a) && eastl::forward<B>(b); }
};
template <typename T = void>
struct logical_or
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const T& b) const
{ return a || b; }
};
// http://en.cppreference.com/w/cpp/utility/functional/logical_or_void
template <>
struct logical_or<void>
{
template<typename A, typename B>
EA_CPP14_CONSTEXPR auto operator()(A&& a, B&& b) const
-> decltype(eastl::forward<A>(a) || eastl::forward<B>(b))
{ return eastl::forward<A>(a) || eastl::forward<B>(b); }
};
template <typename T = void>
struct logical_not
{
EA_CPP14_CONSTEXPR bool operator()(const T& a) const
{ return !a; }
};
// http://en.cppreference.com/w/cpp/utility/functional/logical_not_void
template <>
struct logical_not<void>
{
template<typename T>
EA_CPP14_CONSTEXPR auto operator()(T&& t) const
-> decltype(!eastl::forward<T>(t))
{ return !eastl::forward<T>(t); }
};
///////////////////////////////////////////////////////////////////////
// Dual type functions
///////////////////////////////////////////////////////////////////////
// deprecated. consider using the specialization equal_to<> instead.
template <typename T, typename U>
struct EASTL_REMOVE_AT_2024_APRIL equal_to_2
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const U& b) const
{ return a == b; }
template <typename T_ = T, typename U_ = U, typename = eastl::enable_if_t<!eastl::is_same_v<eastl::remove_const_t<T_>, eastl::remove_const_t<U_>>>>
EA_CPP14_CONSTEXPR bool operator()(const U& b, const T& a) const
{ return b == a; }
};
template <typename T, typename U>
struct EASTL_REMOVE_AT_2024_APRIL not_equal_to_2
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const U& b) const
{ return a != b; }
template <typename T_ = T, typename U_ = U, typename = eastl::enable_if_t<!eastl::is_same_v<eastl::remove_const_t<T_>, eastl::remove_const_t<U_>>>>
EA_CPP14_CONSTEXPR bool operator()(const U& b, const T& a) const
{ return b != a; }
};
template <typename T, typename U>
struct EASTL_REMOVE_AT_2024_APRIL less_2
{
EA_CPP14_CONSTEXPR bool operator()(const T& a, const U& b) const
{ return a < b; }
template <typename T_ = T, typename U_ = U, typename = eastl::enable_if_t<!eastl::is_same_v<eastl::remove_const_t<T_>, eastl::remove_const_t<U_>>>>
EA_CPP14_CONSTEXPR bool operator()(const U& b, const T& a) const
{ return b < a; }
};
EASTL_INTERNAL_DISABLE_DEPRECATED() // '*': was declared deprecated
/// unary_negate
///
template <typename Predicate>
class EASTL_REMOVE_AT_2024_APRIL unary_negate
{
protected:
Predicate mPredicate;
public:
explicit unary_negate(const Predicate& a)
: mPredicate(a) {}
EA_CPP14_CONSTEXPR bool operator()(const typename Predicate::argument_type& a) const
{ return !mPredicate(a); }
};
template <typename Predicate>
EASTL_REMOVE_AT_2024_APRIL inline EA_CPP14_CONSTEXPR unary_negate<Predicate> not1(const Predicate& predicate)
{ return unary_negate<Predicate>(predicate); }
/// binary_negate
///
template <typename Predicate>
class EASTL_REMOVE_AT_2024_APRIL binary_negate
{
protected:
Predicate mPredicate;
public:
explicit binary_negate(const Predicate& a)
: mPredicate(a) { }
EA_CPP14_CONSTEXPR bool operator()(const typename Predicate::first_argument_type& a, const typename Predicate::second_argument_type& b) const
{ return !mPredicate(a, b); }
};
template <typename Predicate>
EASTL_REMOVE_AT_2024_APRIL inline EA_CPP14_CONSTEXPR binary_negate<Predicate> not2(const Predicate& predicate)
{ return binary_negate<Predicate>(predicate); }
/// unary_compose
///
template<typename Operation1, typename Operation2>
struct EASTL_REMOVE_AT_2024_APRIL unary_compose
{
protected:
Operation1 op1;
Operation2 op2;
public:
unary_compose(const Operation1& x, const Operation2& y)
: op1(x), op2(y) {}
typename Operation1::result_type operator()(const typename Operation2::argument_type& x) const
{ return op1(op2(x)); }
typename Operation1::result_type operator()(typename Operation2::argument_type& x) const
{ return op1(op2(x)); }
};
template<typename Operation1,typename Operation2>
EASTL_REMOVE_AT_2024_APRIL inline unary_compose<Operation1,Operation2>
compose1(const Operation1& op1, const Operation2& op2)
{
return unary_compose<Operation1, Operation2>(op1,op2);
}
/// binary_compose
///
template <class Operation1, class Operation2, class Operation3>
class EASTL_REMOVE_AT_2024_APRIL binary_compose
{
protected:
Operation1 op1;
Operation2 op2;
Operation3 op3;
public:
// Support binary functors too.
typedef typename Operation2::argument_type first_argument_type;
typedef typename Operation3::argument_type second_argument_type;
binary_compose(const Operation1& x, const Operation2& y, const Operation3& z)
: op1(x), op2(y), op3(z) { }
typename Operation1::result_type operator()(const typename Operation2::argument_type& x) const
{ return op1(op2(x),op3(x)); }
typename Operation1::result_type operator()(typename Operation2::argument_type& x) const
{ return op1(op2(x),op3(x)); }
typename Operation1::result_type operator()(const typename Operation2::argument_type& x,const typename Operation3::argument_type& y) const
{ return op1(op2(x),op3(y)); }
typename Operation1::result_type operator()(typename Operation2::argument_type& x, typename Operation3::argument_type& y) const
{ return op1(op2(x),op3(y)); }
};
template <class Operation1, class Operation2, class Operation3>
EASTL_REMOVE_AT_2024_APRIL inline binary_compose<Operation1, Operation2, Operation3>
compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3)
{
return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
}
///////////////////////////////////////////////////////////////////////
// pointer_to_unary_function
///////////////////////////////////////////////////////////////////////
/// pointer_to_unary_function
///
/// This is an adapter template which converts a pointer to a standalone
/// function to a function object. This allows standalone functions to
/// work in many cases where the system requires a function object.
///
/// Example usage:
/// ptrdiff_t Rand(ptrdiff_t n) { return rand() % n; } // Note: The C rand function is poor and slow.
/// pointer_to_unary_function<ptrdiff_t, ptrdiff_t> randInstance(Rand);
/// random_shuffle(pArrayBegin, pArrayEnd, randInstance);
///
template <typename Arg, typename Result>
class EASTL_REMOVE_AT_2024_APRIL pointer_to_unary_function
: public unary_function<Arg, Result>
{
protected:
Result (*mpFunction)(Arg);
public:
pointer_to_unary_function()
{ }
explicit pointer_to_unary_function(Result (*pFunction)(Arg))
: mpFunction(pFunction) { }
Result operator()(Arg x) const
{ return mpFunction(x); }
};
/// ptr_fun
///
/// This ptr_fun is simply shorthand for usage of pointer_to_unary_function.
///
/// Example usage (actually, you don't need to use ptr_fun here, but it works anyway):
/// int factorial(int x) { return (x > 1) ? (x * factorial(x - 1)) : x; }
/// transform(pIntArrayBegin, pIntArrayEnd, pIntArrayBegin, ptr_fun(factorial));
///
template <typename Arg, typename Result>
EASTL_REMOVE_AT_2024_APRIL inline pointer_to_unary_function<Arg, Result>
ptr_fun(Result (*pFunction)(Arg))
{ return pointer_to_unary_function<Arg, Result>(pFunction); }
///////////////////////////////////////////////////////////////////////
// pointer_to_binary_function
///////////////////////////////////////////////////////////////////////
/// pointer_to_binary_function
///
/// This is an adapter template which converts a pointer to a standalone
/// function to a function object. This allows standalone functions to
/// work in many cases where the system requires a function object.
///
template <typename Arg1, typename Arg2, typename Result>
class EASTL_REMOVE_AT_2024_APRIL pointer_to_binary_function
: public binary_function<Arg1, Arg2, Result>
{
protected:
Result (*mpFunction)(Arg1, Arg2);
public:
pointer_to_binary_function()
{ }
explicit pointer_to_binary_function(Result (*pFunction)(Arg1, Arg2))
: mpFunction(pFunction) {}
Result operator()(Arg1 x, Arg2 y) const
{ return mpFunction(x, y); }
};
/// This ptr_fun is simply shorthand for usage of pointer_to_binary_function.
///
/// Example usage (actually, you don't need to use ptr_fun here, but it works anyway):
/// int multiply(int x, int y) { return x * y; }
/// transform(pIntArray1Begin, pIntArray1End, pIntArray2Begin, pIntArray1Begin, ptr_fun(multiply));
///
template <typename Arg1, typename Arg2, typename Result>
EASTL_REMOVE_AT_2024_APRIL inline pointer_to_binary_function<Arg1, Arg2, Result>
ptr_fun(Result (*pFunction)(Arg1, Arg2))
{ return pointer_to_binary_function<Arg1, Arg2, Result>(pFunction); }
///////////////////////////////////////////////////////////////////////
// mem_fun
// mem_fun1
//
// Note that mem_fun calls member functions via *pointers* to classes
// and not instances of classes. mem_fun_ref is for calling functions
// via instances of classes or references to classes.
//
// NOTE:
// mem_fun was deprecated in C++11 and removed in C++17, in favor
// of the more general mem_fn and bind.
//
///////////////////////////////////////////////////////////////////////
/// mem_fun_t
///
/// Member function with no arguments.
///
template <typename Result, typename T>
class EASTL_REMOVE_AT_2024_APRIL mem_fun_t
: public unary_function<T*, Result>
{
public:
typedef Result (T::*MemberFunction)();
inline explicit mem_fun_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(T* pT) const
{
return (pT->*mpMemberFunction)();
}
protected:
MemberFunction mpMemberFunction;
};
/// mem_fun1_t
///
/// Member function with one argument.
///
template <typename Result, typename T, typename Argument>
class EASTL_REMOVE_AT_2024_APRIL mem_fun1_t
: public binary_function<T*, Argument, Result>
{
public:
typedef Result (T::*MemberFunction)(Argument);
inline explicit mem_fun1_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(T* pT, Argument arg) const
{
return (pT->*mpMemberFunction)(arg);
}
protected:
MemberFunction mpMemberFunction;
};
/// const_mem_fun_t
///
/// Const member function with no arguments.
/// Note that we inherit from unary_function<const T*, Result>
/// instead of what the C++ standard specifies: unary_function<T*, Result>.
/// The C++ standard is in error and this has been recognized by the defect group.
///
template <typename Result, typename T>
class EASTL_REMOVE_AT_2024_APRIL const_mem_fun_t
: public unary_function<const T*, Result>
{
public:
typedef Result (T::*MemberFunction)() const;
inline explicit const_mem_fun_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(const T* pT) const
{
return (pT->*mpMemberFunction)();
}
protected:
MemberFunction mpMemberFunction;
};
/// const_mem_fun1_t
///
/// Const member function with one argument.
/// Note that we inherit from unary_function<const T*, Result>
/// instead of what the C++ standard specifies: unary_function<T*, Result>.
/// The C++ standard is in error and this has been recognized by the defect group.
///
template <typename Result, typename T, typename Argument>
class EASTL_REMOVE_AT_2024_APRIL const_mem_fun1_t
: public binary_function<const T*, Argument, Result>
{
public:
typedef Result (T::*MemberFunction)(Argument) const;
inline explicit const_mem_fun1_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(const T* pT, Argument arg) const
{
return (pT->*mpMemberFunction)(arg);
}
protected:
MemberFunction mpMemberFunction;
};
/// mem_fun
///
/// This is the high level interface to the mem_fun_t family.
///
/// Example usage:
/// struct TestClass { void print() { puts("hello"); } }
/// TestClass* pTestClassArray[3] = { ... };
/// forEach(pTestClassArray, pTestClassArray + 3, &TestClass::print);
///
/// Note: using conventional inlining here to avoid issues on GCC/Linux
///
template <typename Result, typename T>
EASTL_REMOVE_AT_2024_APRIL inline mem_fun_t<Result, T>
mem_fun(Result (T::*MemberFunction)())
{
return eastl::mem_fun_t<Result, T>(MemberFunction);
}
template <typename Result, typename T, typename Argument>
EASTL_REMOVE_AT_2024_APRIL inline mem_fun1_t<Result, T, Argument>
mem_fun(Result (T::*MemberFunction)(Argument))
{
return eastl::mem_fun1_t<Result, T, Argument>(MemberFunction);
}
template <typename Result, typename T>
EASTL_REMOVE_AT_2024_APRIL inline const_mem_fun_t<Result, T>
mem_fun(Result (T::*MemberFunction)() const)
{
return eastl::const_mem_fun_t<Result, T>(MemberFunction);
}
template <typename Result, typename T, typename Argument>
EASTL_REMOVE_AT_2024_APRIL inline const_mem_fun1_t<Result, T, Argument>
mem_fun(Result (T::*MemberFunction)(Argument) const)
{
return eastl::const_mem_fun1_t<Result, T, Argument>(MemberFunction);
}
///////////////////////////////////////////////////////////////////////
// mem_fun_ref
// mem_fun1_ref
//
///////////////////////////////////////////////////////////////////////
/// mem_fun_ref_t
///
template <typename Result, typename T>
class EASTL_REMOVE_AT_2024_APRIL mem_fun_ref_t
: public unary_function<T, Result>
{
public:
typedef Result (T::*MemberFunction)();
inline explicit mem_fun_ref_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(T& t) const
{
return (t.*mpMemberFunction)();
}
protected:
MemberFunction mpMemberFunction;
};
/// mem_fun1_ref_t
///
template <typename Result, typename T, typename Argument>
class EASTL_REMOVE_AT_2024_APRIL mem_fun1_ref_t
: public binary_function<T, Argument, Result>
{
public:
typedef Result (T::*MemberFunction)(Argument);
inline explicit mem_fun1_ref_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(T& t, Argument arg) const
{
return (t.*mpMemberFunction)(arg);
}
protected:
MemberFunction mpMemberFunction;
};
/// const_mem_fun_ref_t
///
template <typename Result, typename T>
class EASTL_REMOVE_AT_2024_APRIL const_mem_fun_ref_t
: public unary_function<T, Result>
{
public:
typedef Result (T::*MemberFunction)() const;
inline explicit const_mem_fun_ref_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(const T& t) const
{
return (t.*mpMemberFunction)();
}
protected:
MemberFunction mpMemberFunction;
};
/// const_mem_fun1_ref_t
///
template <typename Result, typename T, typename Argument>
class EASTL_REMOVE_AT_2024_APRIL const_mem_fun1_ref_t
: public binary_function<T, Argument, Result>
{
public:
typedef Result (T::*MemberFunction)(Argument) const;
inline explicit const_mem_fun1_ref_t(MemberFunction pMemberFunction)
: mpMemberFunction(pMemberFunction)
{
// Empty
}
inline Result operator()(const T& t, Argument arg) const
{
return (t.*mpMemberFunction)(arg);
}
protected:
MemberFunction mpMemberFunction;
};
/// mem_fun_ref
/// Example usage:
/// struct TestClass { void print() { puts("hello"); } }
/// TestClass testClassArray[3];
/// forEach(testClassArray, testClassArray + 3, &TestClass::print);
///
/// Note: using conventional inlining here to avoid issues on GCC/Linux
///
template <typename Result, typename T>
EASTL_REMOVE_AT_2024_APRIL inline mem_fun_ref_t<Result, T>
mem_fun_ref(Result (T::*MemberFunction)())
{
return eastl::mem_fun_ref_t<Result, T>(MemberFunction);
}
template <typename Result, typename T, typename Argument>
EASTL_REMOVE_AT_2024_APRIL inline mem_fun1_ref_t<Result, T, Argument>
mem_fun_ref(Result (T::*MemberFunction)(Argument))
{
return eastl::mem_fun1_ref_t<Result, T, Argument>(MemberFunction);
}
template <typename Result, typename T>
EASTL_REMOVE_AT_2024_APRIL inline const_mem_fun_ref_t<Result, T>
mem_fun_ref(Result (T::*MemberFunction)() const)
{
return eastl::const_mem_fun_ref_t<Result, T>(MemberFunction);
}
template <typename Result, typename T, typename Argument>
EASTL_REMOVE_AT_2024_APRIL inline const_mem_fun1_ref_t<Result, T, Argument>
mem_fun_ref(Result (T::*MemberFunction)(Argument) const)
{
return eastl::const_mem_fun1_ref_t<Result, T, Argument>(MemberFunction);
}
EASTL_INTERNAL_RESTORE_DEPRECATED()
// not_fn_ret
// not_fn_ret is a implementation specified return type of eastl::not_fn.
// The type name is not specified but it does have mandated functions that conforming implementations must support.
//
// http://en.cppreference.com/w/cpp/utility/functional/not_fn
//
template <typename F>
struct not_fn_ret
{
explicit not_fn_ret(F&& f) : mDecayF(eastl::forward<F>(f)) {}
not_fn_ret(not_fn_ret&& f) = default;
not_fn_ret(const not_fn_ret& f) = default;
// overloads for lvalues
template <class... Args>
auto operator()(Args&&... args) &
-> decltype(!eastl::declval<eastl::invoke_result_t<eastl::decay_t<F>&, Args...>>())
{ return !eastl::invoke(mDecayF, eastl::forward<Args>(args)...); }
template <class... Args>
auto operator()(Args&&... args) const &
-> decltype(!eastl::declval<eastl::invoke_result_t<eastl::decay_t<F> const&, Args...>>())
{ return !eastl::invoke(mDecayF, eastl::forward<Args>(args)...); }
// overloads for rvalues
template <class... Args>
auto operator()(Args&&... args) &&
-> decltype(!eastl::declval<eastl::invoke_result_t<eastl::decay_t<F>, Args...>>())
{ return !eastl::invoke(eastl::move(mDecayF), eastl::forward<Args>(args)...); }
template <class... Args>
auto operator()(Args&&... args) const &&
-> decltype(!eastl::declval<eastl::invoke_result_t<eastl::decay_t<F> const, Args...>>())
{ return !eastl::invoke(eastl::move(mDecayF), eastl::forward<Args>(args)...); }
eastl::decay_t<F> mDecayF;
};
/// not_fn
///
/// Creates an implementation specified functor that returns the complement of the callable object it was passed.
/// not_fn is intended to replace the C++03-era negators eastl::not1 and eastl::not2.
///
/// http://en.cppreference.com/w/cpp/utility/functional/not_fn
///
/// Example usage:
///
/// auto nf = eastl::not_fn([]{ return false; });
/// assert(nf()); // return true
///
template <class F>
inline not_fn_ret<F> not_fn(F&& f)