function_objects.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Ifdefs
10 *****************************************************************************/
11 
12 #ifndef ECL_UTILITIES_FUNCTION_OBJECTS_HPP_
13 #define ECL_UTILITIES_FUNCTION_OBJECTS_HPP_
14 
15 /*****************************************************************************
16 ** Includes
17 *****************************************************************************/
18 
19 #include <ecl/concepts/nullary_function.hpp>
20 #include "../utilities/references.hpp"
21 
22 /*****************************************************************************
23 ** Namespaces
24 *****************************************************************************/
25 
26 namespace ecl {
27 
28 /*****************************************************************************
29 ** Using
30 *****************************************************************************/
31 
32 /*****************************************************************************
33 ** Interface [NullaryFunction]
34 *****************************************************************************/
45 template <typename R = void>
46 class NullaryFunction {
47 public:
48  typedef R result_type;
49  virtual result_type operator()() = 0;
50  virtual ~NullaryFunction() {};
51 };
52 
53 /*****************************************************************************
54 ** Interface [UnaryFunction]
55 *****************************************************************************/
56 
68 template <typename A, typename R = void>
69 class UnaryFunction {
70 public:
71  typedef R result_type;
72  typedef A argument_type;
77  virtual result_type operator()(argument_type arg) = 0;
78  virtual ~UnaryFunction() {};
79 };
80 
81 /*****************************************************************************
82 ** Interface [BinaryFunction]
83 *****************************************************************************/
96 template <typename A1, typename A2, typename R = void>
97 class BinaryFunction {
98 public:
99  typedef R result_type;
100  typedef A1 first_argument_type;
101  typedef A2 second_argument_type;
109  virtual result_type operator()(first_argument_type arg1, second_argument_type arg2) = 0;
110  virtual ~BinaryFunction() {};
111 };
112 
113 /*****************************************************************************
114 ** Interface [FreeFunctions]
115 *****************************************************************************/
125 template <typename R = void>
126 class NullaryFreeFunction : public NullaryFunction<R>
127 {
128 public:
136  NullaryFreeFunction( R (*function)() ) : free_function(function) {}
137  virtual ~NullaryFreeFunction() {};
146  R operator()() { return free_function(); }
147 
148 private:
149  R (*free_function)();
150 };
151 
159 template <>
160 class NullaryFreeFunction<void> : public NullaryFunction<void>
161 {
162 public:
170  NullaryFreeFunction( void (*function)() ) : free_function(function) {}
171 
172  virtual ~NullaryFreeFunction() {};
181  void operator()() { free_function(); }
182 
183 private:
184  void (*free_function)();
185 };
186 
199 template <typename A, typename R = void>
200 class UnaryFreeFunction : public UnaryFunction<A,R>
201 {
202 public:
211  UnaryFreeFunction( R (*function)(A) ) : free_function(function) {}
212 
213  virtual ~UnaryFreeFunction() {};
222  R operator()(A a) { return free_function(a); }
223 
224 private:
225  R (*free_function)(A);
226 };
227 
237 template <typename A>
238 class UnaryFreeFunction<A,void> : public UnaryFunction<A,void>
239 {
240 public:
249  UnaryFreeFunction( void (*function)(A) ) : free_function(function) {}
250 
251  virtual ~UnaryFreeFunction() {};
259  void operator()(A a) { free_function(a); }
260 
261 private:
262  void (*free_function)(A);
263 };
264 
265 /*****************************************************************************
266 ** Interface [BoundFreeFunctions]
267 *****************************************************************************/
291 template <typename A, typename R = void>
292 class BoundUnaryFreeFunction : public NullaryFunction<R>
293 {
294 public:
303  BoundUnaryFreeFunction( R (*function)(A), A a ) : free_function(function), argument(a) {}
304  virtual ~BoundUnaryFreeFunction() {};
312  R operator()() { return free_function(argument); }
313 
314 private:
315  R (*free_function)(A);
316  A argument;
317 };
318 
328 template <typename A>
329 class BoundUnaryFreeFunction<A,void> : public NullaryFunction<void>
330 {
331 public:
340  BoundUnaryFreeFunction( void (*function)(A), A a ) : free_function(function), argument(a) {}
341  virtual ~BoundUnaryFreeFunction() {};
349  void operator()() { free_function(argument); }
350 
351 private:
352  void (*free_function)(A);
353  A argument;
354 };
355 
356 /*****************************************************************************
357  * Interface [MemberFunctions]
358  *****************************************************************************
359  * Could make copies+refs here, just like mem_fun and mem_fun_ref but I can't
360  * see the need for copy style setups.
361  ****************************************************************************/
362 
390 template <typename C, typename R = void>
391 class NullaryMemberFunction : public UnaryFunction<C&,R> {
392 public:
401  NullaryMemberFunction( R (C::*function)()) : member_function(function) {}
402  virtual ~NullaryMemberFunction() {};
414  R operator()(C &class_object) {
415  return (class_object.*member_function)();
416  }
417 private:
418  R (C::*member_function)();
419 };
420 
431 template <typename C>
432 class NullaryMemberFunction<C,void> : public UnaryFunction<C&,void> {
433 public:
442  NullaryMemberFunction( void (C::*function)()) : member_function(function) {}
443  virtual ~NullaryMemberFunction() {};
453  void operator()(C &class_object) {
454  (class_object.*member_function)();
455  }
456 private:
457  void (C::*member_function)();
458 };
459 
486 template <typename C, typename A, typename R = void>
487 class UnaryMemberFunction : public BinaryFunction<C&,A,R> {
488 public:
497  UnaryMemberFunction( R (C::*function)(A)) : member_function(function) {}
498  virtual ~UnaryMemberFunction() {};
509  R operator()(C &class_object, A a) {
510  return (class_object.*member_function)(a);
511  }
512 private:
513  R (C::*member_function)(A);
514 };
515 
526 template <typename C, typename A>
527 class UnaryMemberFunction<C,A,void> : public BinaryFunction<C&,A,void> {
528 public:
537  UnaryMemberFunction( void (C::*function)(A)) : member_function(function) {}
538  virtual ~UnaryMemberFunction() {};
549  void operator()(C &class_object, A a) {
550  (class_object.*member_function)(a);
551  }
552 private:
553  void (C::*member_function)(A);
554 };
555 
556 /*****************************************************************************
557 ** Interface [BoundMemberFunctions]
558 *****************************************************************************/
559 
585 template <typename C, typename R = void>
586 class BoundNullaryMemberFunction : public NullaryFunction<R> {
587 public:
596  BoundNullaryMemberFunction( R (C::*function)(), C &class_object) :
597  member_class(class_object),
598  member_function(function)
599  {}
600  virtual ~BoundNullaryMemberFunction() {};
609  R operator()() { return (member_class.*member_function)(); }
610 
611 private:
612  C &member_class;
613  R (C::*member_function)();
614 };
615 
627 template <typename C>
628 class BoundNullaryMemberFunction<C,void> : public NullaryFunction<void> {
629 public:
638  BoundNullaryMemberFunction( void (C::*function)(), C &class_object) :
639  member_class(class_object),
640  member_function(function)
641  {}
642 
643  virtual ~BoundNullaryMemberFunction() {};
649  void operator()() { (member_class.*member_function)(); }
650 
651 private:
652  C &member_class;
653  void (C::*member_function)();
654 };
655 
684 template <typename C, typename A, typename R = void>
685 class PartiallyBoundUnaryMemberFunction : public UnaryFunction<A,R> {
686 public:
695  PartiallyBoundUnaryMemberFunction( R (C::*function)(A), C &class_object) :
696  member_class(class_object),
697  member_function(function)
698  {}
699  virtual ~PartiallyBoundUnaryMemberFunction() {};
708  R operator()(A a) {
709  return (member_class.*member_function)(a);
710  }
711 private:
712  C &member_class;
713  void (C::*member_function)(A);
714 };
743 template <typename C, typename A, typename R = void>
744 class BoundUnaryMemberFunction : public NullaryFunction<R> {
745 public:
755  BoundUnaryMemberFunction( R (C::*function)(A), C &class_object, A a) :
756  member_class(class_object),
757  member_function(function),
758  argument(a)
759  {}
760  virtual ~BoundUnaryMemberFunction() {};
768  R operator()() {
769  return (member_class.*member_function)(argument);
770  }
771 private:
772  C &member_class;
773  void (C::*member_function)(A);
774  A argument;
775 };
776 
806 template <typename C, typename A, typename B, typename R = void>
807 class PartiallyBoundBinaryMemberFunction : public BinaryFunction<A,B,R> {
808 public:
817  PartiallyBoundBinaryMemberFunction( R (C::*function)(A, B), C &class_object) :
818  member_class(class_object),
819  member_function(function)
820  {}
821  virtual ~PartiallyBoundBinaryMemberFunction() {};
831  R operator()(A a, B b) {
832  return (member_class.*member_function)(a, b);
833  }
834 private:
835  C &member_class;
836  void (C::*member_function)(A, B);
837 };
838 /*****************************************************************************
839 ** Function Object Wrappers
840 *****************************************************************************/
854 template <typename FunctionObject, typename Result = void>
855 class NullaryFunctionCopy : public NullaryFunction<Result>
856 {
857 public:
866  NullaryFunctionCopy(const FunctionObject &f_o ) : function_object(f_o) {
868  }
869  virtual ~NullaryFunctionCopy() {};
878  Result operator()() { return function_object(); }
879 
880 private:
881  FunctionObject function_object;
882 };
883 
893 template <typename FunctionObject>
894 class NullaryFunctionCopy<FunctionObject,void> : public NullaryFunction<void>
895 {
896 public:
905  explicit NullaryFunctionCopy( const FunctionObject &f_o ) : function_object(f_o) {
907  }
908  virtual ~NullaryFunctionCopy() {};
915  void operator()() { function_object(); }
916 
917 private:
918  FunctionObject function_object;
919 };
920 
934 template <typename FunctionObject, typename Result = void>
935 class NullaryFunctionReference : public NullaryFunction<Result>
936 {
937 public:
945  explicit NullaryFunctionReference( const ReferenceWrapper<FunctionObject> &wrapper ) : function_object(wrapper.reference()) {
947  }
948  virtual ~NullaryFunctionReference() {};
957  Result operator()() { return function_object(); }
958 
959 private:
960  FunctionObject &function_object;
961 };
962 
974 template <typename FunctionObject>
975 class NullaryFunctionReference< FunctionObject, void > : public NullaryFunction<void>
976 {
977 public:
985  explicit NullaryFunctionReference( const ReferenceWrapper<FunctionObject> &wrapper ) : function_object(wrapper.reference()) {
987  }
988  virtual ~NullaryFunctionReference() {};
995  void operator()() { function_object(); }
996 
997 private:
998  FunctionObject &function_object;
999 };
1000 
1015 template <typename FunctionObject, typename T, typename Result = void>
1016 class UnaryFunctionCopy : public UnaryFunction<T, Result>
1017 {
1018 public:
1027  UnaryFunctionCopy(const FunctionObject &f_o ) : function_object(f_o) {
1028 // ecl_compile_time_concept_check(UnaryFunction<FunctionObject>);
1029  }
1030  virtual ~UnaryFunctionCopy() {};
1039  Result operator()(T t) { return function_object(t); }
1040 
1041 private:
1042  FunctionObject function_object;
1043 };
1044 
1054 template <typename FunctionObject, typename T>
1055 class UnaryFunctionCopy<FunctionObject,T,void> : public UnaryFunction<T, void>
1056 {
1057 public:
1066  explicit UnaryFunctionCopy( const FunctionObject &f_o ) : function_object(f_o) {
1067 // ecl_compile_time_concept_check(UnaryFunction<FunctionObject>);
1068  }
1069  virtual ~UnaryFunctionCopy() {};
1076  void operator()(T t) { function_object(t); }
1077 
1078 private:
1079  FunctionObject function_object;
1080 };
1081 
1095 template <typename FunctionObject, typename T, typename Result = void>
1096 class UnaryFunctionReference : public UnaryFunction<T, Result>
1097 {
1098 public:
1106  explicit UnaryFunctionReference( const ReferenceWrapper<FunctionObject> &wrapper ) : function_object(wrapper.reference()) {
1107 // ecl_compile_time_concept_check(UnaryFunction<FunctionObject>);
1108  }
1109 
1110  virtual ~UnaryFunctionReference() {};
1118  Result operator()(T t) { return function_object(t); }
1119 
1120 private:
1121  FunctionObject &function_object;
1122 };
1123 
1135 template <typename ReferenceWrapper, typename T>
1136 class UnaryFunctionReference< ReferenceWrapper, T, void > : public UnaryFunction<T,void>
1137 {
1138 public:
1139  typedef typename ReferenceWrapper::type FunctionObject;
1147  explicit UnaryFunctionReference( const ReferenceWrapper &wrapper ) : function_object(wrapper.reference()) {
1148 // ecl_compile_time_concept_check(UnaryFunction<FunctionObject>);
1149  }
1150  virtual ~UnaryFunctionReference() {};
1157  void operator()(T t) { function_object(t); }
1158 
1159 private:
1160  FunctionObject &function_object;
1161 };
1162 
1163 /*****************************************************************************
1164 ** Nullary Function Generators
1165 *****************************************************************************/
1166 
1179 template <typename R>
1181  return NullaryFreeFunction<R>( function );
1182 }
1183 
1197 template <typename A, typename R>
1199  return UnaryFreeFunction<A,R>( function );
1200 }
1201 
1217 template <typename A, typename R, typename I>
1219  return BoundUnaryFreeFunction<A,R>( function, a );
1220 }
1221 
1238 template <typename A, typename R, typename I>
1239 BoundUnaryFreeFunction<A,R> generateFunctionObject( R (*function)(A), const I& a ) {
1240  return BoundUnaryFreeFunction<A,R>( function, a );
1241 }
1242 
1256 template <typename C, typename R>
1258  return NullaryMemberFunction<C,R>( function );
1259 }
1260 
1275 template <typename C, typename R>
1277  return BoundNullaryMemberFunction<C,R>( function, c );
1278 }
1279 
1294 template <typename C, typename A, typename R>
1296  return UnaryMemberFunction<C,A,R>( function );
1297 }
1298 
1316 template <typename C, typename A, typename R>
1318  return PartiallyBoundUnaryMemberFunction<C,A,R>( function, c);
1319 }
1320 
1338 template <typename C, typename A, typename R, typename I>
1339 BoundUnaryMemberFunction<C,A,R> generateFunctionObject( R (C::*function)(A), C& c, I& a ) {
1340  // The I here is a bit of a trick...if you use A in the constructor above instead of I
1341  // then it gets confused trying to deduce A as often the function arg and the input value
1342  // are different types (e.g. const int& as opposed to int). So fix A in the template
1343  // direction first, then pass it a type I which can be converted on the fly where needed.
1344  return BoundUnaryMemberFunction<C,A,R>( function, c, a );
1345 }
1346 
1365 template <typename C, typename A, typename R, typename I>
1366 BoundUnaryMemberFunction<C,A,R> generateFunctionObject( R (C::*function)(A), C& c, const I& a ) {
1367  // This one differs from the previous due to the const reference - this allows things like
1368  // temporaries to pass through unscathed to a function with an argument like const int&.
1369  return BoundUnaryMemberFunction<C,A,R>( function, c, a );
1370 }
1371 
1372 }; // namespace ecl
1373 
1374 #endif /* ECL_UTILITIES_FUNCTION_OBJECTS_HPP_ */
Nullary function object for bound unary global/static functions.
Binary function object for unary member functions.
virtual result_type operator()()=0
Unary function object for partially bound unary member functions.
Unary function object for member functions without arguments.
Nullary function object for bound nullary member functions.
BoundUnaryFreeFunction< A, R > generateFunctionObject(R(*function)(A), I &a)
Generate a nullary function object from a bound unary global/static function.
virtual ~NullaryFunction()
Nullary function object for bound unary member functions.
Unary function object for global/static functions.
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
Nullary function object for void global/static functions.


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:37