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 
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>
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>
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>
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>
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>
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);
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);
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>
587 public:
596  BoundNullaryMemberFunction( R (C::*function)(), C &class_object) :
597  member_class(class_object),
598  member_function(function)
599  {}
609  R operator()() { return (member_class.*member_function)(); }
610 
611 private:
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 
649  void operator()() { (member_class.*member_function)(); }
650 
651 private:
653  void (C::*member_function)();
654 };
655 
684 template <typename C, typename A, typename R = void>
686 public:
695  PartiallyBoundUnaryMemberFunction( R (C::*function)(A), C &class_object) :
696  member_class(class_object),
697  member_function(function)
698  {}
708  R operator()(A a) {
709  return (member_class.*member_function)(a);
710  }
711 private:
713  void (C::*member_function)(A);
714 };
743 template <typename C, typename A, typename R = void>
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  {}
769  return (member_class.*member_function)(argument);
770  }
771 private:
773  void (C::*member_function)(A);
775 };
776 
806 template <typename C, typename A, typename B, typename R = void>
808 public:
817  PartiallyBoundBinaryMemberFunction( R (C::*function)(A, B), C &class_object) :
818  member_class(class_object),
819  member_function(function)
820  {}
831  R operator()(A a, B b) {
832  return (member_class.*member_function)(a, b);
833  }
834 private:
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>
936 {
937 public:
945  explicit NullaryFunctionReference( const ReferenceWrapper<FunctionObject> &wrapper ) : function_object(wrapper.reference()) {
947  }
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  }
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 
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:
1147  explicit UnaryFunctionReference( const ReferenceWrapper &wrapper ) : function_object(wrapper.reference()) {
1148 // ecl_compile_time_concept_check(UnaryFunction<FunctionObject>);
1149  }
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_ */
R result_type
The result type.
void operator()(C &class_object)
This ensures any children objects are deleted correctly.
Embedded control libraries.
void operator()()
This ensures any children objects are deleted correctly.
UnaryFunctionCopy(const FunctionObject &f_o)
UnaryFunction child constructor for unary function objects.
Result operator()(T t)
This ensures any children objects are deleted correctly.
R operator()(A a)
This ensures any children objects are deleted correctly.
PartiallyBoundUnaryMemberFunction(R(C::*function)(A), C &class_object)
Binds a unary member function and creates a nullary function object.
A2 second_argument_type
The second argument type.
BoundNullaryMemberFunction(R(C::*function)(), C &class_object)
Binds a unary member function and creates a nullary function object.
void operator()(T t)
This ensures any children objects are deleted correctly.
Nullary function object for bound unary global/static functions.
UnaryFunctionCopy(const FunctionObject &f_o)
UnaryFunction child constructor for unary function objects.
Create a NullaryFunction object composited from an existing function object.
Binary function object for unary member functions.
R operator()()
This ensures any children objects are deleted correctly.
R operator()(A a)
This ensures any children objects are deleted correctly.
UnaryFreeFunction(void(*function)(A))
Unary function object constructor for global/static unary functions with no return type...
NullaryMemberFunction(void(C::*function)())
Unary function object constructor for void member functions.
BoundUnaryFreeFunction(R(*function)(A), A a)
Binds a unary function and creates a nullary function object.
void operator()()
This ensures any children objects are deleted correctly.
virtual result_type operator()()=0
Virtual function call required by nullary function objects.
Provides a wrapper which allows the original object to be passed like a reference.
Definition: references.hpp:85
PartiallyBoundBinaryMemberFunction(R(C::*function)(A, B), C &class_object)
Binds a binary member function and creates a binary function object.
BoundNullaryMemberFunction(void(C::*function)(), C &class_object)
Binds a unary member function and creates a nullary function object.
Unary function object for partially bound unary member functions.
Create a UnaryFunction object composited from an existing function object.
void operator()(A a)
This ensures any children objects are deleted correctly.
Creates a unary function from a reference wrapper.
R operator()(C &class_object)
This ensures any children objects are deleted correctly.
Unary function object for member functions without arguments.
R operator()()
This ensures any children objects are deleted correctly.
UnaryFunctionReference(const ReferenceWrapper< FunctionObject > &wrapper)
Creates a UnaryFunction descendant from a reference wrapper.
Virtual interface definition for binary function objects.
R result_type
The result type.
R operator()(C &class_object, A a)
This ensures any children objects are deleted correctly.
Creates a nullary function from a reference wrapper.
Nullary function object for bound nullary member functions.
A argument_type
The first argument type.
UnaryFreeFunction(R(*function)(A))
Unary function object constructor for global/static functions.
NullaryFunctionReference(const ReferenceWrapper< FunctionObject > &wrapper)
Creates a NullaryFunction descendant from a reference wrapper.
Result operator()()
This ensures any children objects are deleted correctly.
void operator()()
This ensures any children objects are deleted correctly.
NullaryMemberFunction(R(C::*function)())
Unary function object constructor for member functions without arguments.
Binary function object for partially bound binary member functions.
NullaryFunctionCopy(const FunctionObject &f_o)
NullaryFunction child constructor for nullary function objects.
void operator()()
This ensures any children objects are deleted correctly.
Nullary function object for bound unary member functions.
void operator()()
This ensures any children objects are deleted correctly.
UnaryMemberFunction(R(C::*function)(A))
Binary function object constructor for unary member functions.
UnaryFunctionReference(const ReferenceWrapper &wrapper)
Creates a UnaryFunction descendant from a reference wrapper.
Unary function object for global/static functions.
Result operator()(T t)
This ensures any children objects are deleted correctly.
#define ecl_compile_time_concept_check(Model)
R result_type
The result type.
Nullary function object for void global/static functions.
UnaryMemberFunction(void(C::*function)(A))
Binary function object constructor for unary member functions.
BoundUnaryFreeFunction(void(*function)(A), A a)
Binds a unary function and creates a nullary function object.
A1 first_argument_type
The first argument type.
NullaryFreeFunction(void(*function)())
Nullary function object constructor for void global/static functions with no args.
BoundUnaryMemberFunction(R(C::*function)(A), C &class_object, A a)
Binds a unary member function and creates a nullary function object.
NullaryFreeFunction(R(*function)())
Nullary function object constructor for global/static functions with no args.
R operator()(A a, B b)
This ensures any children objects are deleted correctly.
NullaryFunctionReference(const ReferenceWrapper< FunctionObject > &wrapper)
Creates a NullaryFunction descendant from a reference wrapper.
Virtual interface definition for unary function objects.
R operator()()
This ensures any children objects are deleted correctly.
NullaryFunctionCopy(const FunctionObject &f_o)
NullaryFunction child constructor for nullary function objects.
void operator()(C &class_object, A a)
This ensures any children objects are deleted correctly.
Virtual interface definition for nullary function objects.
Result operator()()
This ensures any children objects are deleted correctly.
NullaryFreeFunction< R > generateFunctionObject(R(*function)())
Generate a nullary function object from a void global/static function.
void operator()(T t)
This ensures any children objects are deleted correctly.
R operator()()
This ensures any children objects are deleted correctly.


ecl_utilities
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:28