From 342d0bd2876aff9f39ea249b10d61d2a92202791 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Thu, 31 Jul 2008 13:36:13 +0000 Subject: - Added Common::mem_fun_ref for object references instead of pointers. - Added simple tests for a little bit functionallity from common/func.h svn-id: r33470 --- common/func.h | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 109 insertions(+), 10 deletions(-) (limited to 'common') diff --git a/common/func.h b/common/func.h index 1c045b9e5d..6aa5b76ed4 100644 --- a/common/func.h +++ b/common/func.h @@ -78,7 +78,7 @@ private: Op _op; typename Op::FirstArgumentType _arg1; public: - Binder1st(const Op &op, const typename Op::FirstArgumentType &arg1) : _op(op), _arg1(arg1) {} + Binder1st(const Op &op, typename Op::FirstArgumentType arg1) : _op(op), _arg1(arg1) {} typename Op::ResultType operator()(typename Op::SecondArgumentType v) const { return _op(_arg1, v); @@ -89,8 +89,8 @@ public: * Transforms a binary function object into an unary function object. * To achieve that the first parameter is bound to the passed value t. */ -template -inline Binder1st bind1st(const Op &op, const T &t) { +template +inline Binder1st bind1st(const Op &op, typename Op::FirstArgumentType t) { return Binder1st(op, t); } @@ -100,7 +100,7 @@ private: Op _op; typename Op::SecondArgumentType _arg2; public: - Binder2nd(const Op &op, const typename Op::SecondArgumentType &arg2) : _op(op), _arg2(arg2) {} + Binder2nd(const Op &op, typename Op::SecondArgumentType arg2) : _op(op), _arg2(arg2) {} typename Op::ResultType operator()(typename Op::FirstArgumentType v) const { return _op(v, _arg2); @@ -109,10 +109,10 @@ public: /** * Transforms a binary function object into an unary function object. - * To achieve that the second parameter is bound to the passed value t. + * To achieve that the first parameter is bound to the passed value t. */ -template -inline Binder2nd bind2nd(const Op &op, const T &t) { +template +inline Binder2nd bind2nd(const Op &op, typename Op::SecondArgumentType t) { return Binder2nd(op, t); } @@ -159,7 +159,7 @@ inline PointerToBinaryFunc ptr_fun(Result (*func)(Arg1, Arg2 } template -class MemFunc0 : public UnaryFunction { +class MemFunc0 : public UnaryFunction { private: Result (T::*_func)(); public: @@ -179,7 +179,7 @@ public: typedef Result (T::*FuncType)() const; ConstMemFunc0(const FuncType &func) : _func(func) {} - Result operator()(T *v) const { + Result operator()(const T *v) const { return (v->*_func)(); } }; @@ -205,7 +205,7 @@ public: typedef Result (T::*FuncType)(Arg) const; ConstMemFunc1(const FuncType &func) : _func(func) {} - Result operator()(T *v1, Arg v2) const { + Result operator()(const T *v1, Arg v2) const { return (v1->*_func)(v2); } }; @@ -252,6 +252,105 @@ inline ConstMemFunc1 mem_fun(Result (T::*f)(Arg) const) { return ConstMemFunc1(f); } +template +class MemFuncRef0 : public UnaryFunction { +private: + Result (T::*_func)(); +public: + typedef Result (T::*FuncType)(); + + MemFuncRef0(const FuncType &func) : _func(func) {} + Result operator()(T &v) const { + return (v.*_func)(); + } +}; + +template +class ConstMemFuncRef0 : public UnaryFunction { +private: + Result (T::*_func)() const; +public: + typedef Result (T::*FuncType)() const; + + ConstMemFuncRef0(const FuncType &func) : _func(func) {} + Result operator()(const T &v) const { + return (v.*_func)(); + } +}; + +template +class MemFuncRef1 : public BinaryFunction { +private: + Result (T::*_func)(Arg); +public: + typedef Result (T::*FuncType)(Arg); + + MemFuncRef1(const FuncType &func) : _func(func) {} + Result operator()(T &v1, Arg v2) const { + return (v1.*_func)(v2); + } +}; + +template +class ConstMemFuncRef1 : public BinaryFunction { +private: + Result (T::*_func)(Arg) const; +public: + typedef Result (T::*FuncType)(Arg) const; + + ConstMemFuncRef1(const FuncType &func) : _func(func) {} + Result operator()(const T &v1, Arg v2) const { + return (v1.*_func)(v2); + } +}; + +/** + * Creates a unary function object from a class member function pointer. + * The parameter passed to the function object is the object instance to + * be used for the function call. Note unlike mem_fun, it takes a reference + * as parameter. Note unlike mem_fun, it takes a reference + * as parameter. + */ +template +inline MemFuncRef0 mem_fun_ref(Result (T::*f)()) { + return MemFuncRef0(f); +} + +/** + * Creates a unary function object from a class member function pointer. + * The parameter passed to the function object is the object instance to + * be used for the function call. Note unlike mem_fun, it takes a reference + * as parameter. + */ +template +inline ConstMemFuncRef0 mem_fun_Ref(Result (T::*f)() const) { + return ConstMemFuncRef0(f); +} + +/** + * Creates a binary function object from a class member function pointer. + * The first parameter passed to the function object is the object instance to + * be used for the function call. Note unlike mem_fun, it takes a reference + * as parameter. + * The second one is the parameter passed to the member function. + */ +template +inline MemFuncRef1 mem_fun_ref(Result (T::*f)(Arg)) { + return MemFuncRef1(f); +} + +/** + * Creates a binary function object from a class member function pointer. + * The first parameter passed to the function object is the object instance to + * be used for the function call. Note unlike mem_fun, it takes a reference + * as parameter. + * The second one is the parameter passed to the member function. + */ +template +inline ConstMemFuncRef1 mem_fun_ref(Result (T::*f)(Arg) const) { + return ConstMemFuncRef1(f); +} + // functor code /** -- cgit v1.2.3