diff options
author | Johannes Schickel | 2008-07-31 13:36:13 +0000 |
---|---|---|
committer | Johannes Schickel | 2008-07-31 13:36:13 +0000 |
commit | 342d0bd2876aff9f39ea249b10d61d2a92202791 (patch) | |
tree | 7295d4f9456bc4c6b0c0f1c3a52058639a20697a | |
parent | 6ed4beb1bfc1ffe7a2ca9f99dbd63eaf9c245bea (diff) | |
download | scummvm-rg350-342d0bd2876aff9f39ea249b10d61d2a92202791.tar.gz scummvm-rg350-342d0bd2876aff9f39ea249b10d61d2a92202791.tar.bz2 scummvm-rg350-342d0bd2876aff9f39ea249b10d61d2a92202791.zip |
- 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
-rw-r--r-- | common/func.h | 119 | ||||
-rw-r--r-- | test/common/func.h | 60 |
2 files changed, 169 insertions, 10 deletions
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<class Op, class T> -inline Binder1st<Op> bind1st(const Op &op, const T &t) { +template<class Op> +inline Binder1st<Op> bind1st(const Op &op, typename Op::FirstArgumentType t) { return Binder1st<Op>(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<class Op, class T> -inline Binder2nd<Op> bind2nd(const Op &op, const T &t) { +template<class Op> +inline Binder2nd<Op> bind2nd(const Op &op, typename Op::SecondArgumentType t) { return Binder2nd<Op>(op, t); } @@ -159,7 +159,7 @@ inline PointerToBinaryFunc<Arg1, Arg2, Result> ptr_fun(Result (*func)(Arg1, Arg2 } template<class Result, class T> -class MemFunc0 : public UnaryFunction<T*, Result> { +class MemFunc0 : public UnaryFunction<T *, Result> { 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<Result, Arg, T> mem_fun(Result (T::*f)(Arg) const) { return ConstMemFunc1<Result, Arg, T>(f); } +template<class Result, class T> +class MemFuncRef0 : public UnaryFunction<T &, Result> { +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 Result, class T> +class ConstMemFuncRef0 : public UnaryFunction<T &, Result> { +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 Result, class Arg, class T> +class MemFuncRef1 : public BinaryFunction<T &, Arg, Result> { +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 Result, class Arg, class T> +class ConstMemFuncRef1 : public BinaryFunction<T &, Arg, Result> { +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<class Result, class T> +inline MemFuncRef0<Result, T> mem_fun_ref(Result (T::*f)()) { + return MemFuncRef0<Result, T>(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<class Result, class T> +inline ConstMemFuncRef0<Result, T> mem_fun_Ref(Result (T::*f)() const) { + return ConstMemFuncRef0<Result, T>(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<class Result, class Arg, class T> +inline MemFuncRef1<Result, Arg, T> mem_fun_ref(Result (T::*f)(Arg)) { + return MemFuncRef1<Result, Arg, T>(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<class Result, class Arg, class T> +inline ConstMemFuncRef1<Result, Arg, T> mem_fun_ref(Result (T::*f)(Arg) const) { + return ConstMemFuncRef1<Result, Arg, T>(f); +} + // functor code /** diff --git a/test/common/func.h b/test/common/func.h new file mode 100644 index 0000000000..bf9b2ddff9 --- /dev/null +++ b/test/common/func.h @@ -0,0 +1,60 @@ +#include <cxxtest/TestSuite.h> + +#include "common/func.h" + +void myFunction1(int &dst, const int src) { dst = src; } +void myFunction2(const int src, int &dst) { dst = src; } + +class FuncTestSuite : public CxxTest::TestSuite +{ + public: + void test_bind1st() { + int dst = 0; + Common::bind1st(Common::ptr_fun(myFunction1), dst)(1); + TS_ASSERT_EQUALS(dst, 1); + } + + void test_bind2nd() { + int dst = 0; + Common::bind2nd(Common::ptr_fun(myFunction2), dst)(1); + TS_ASSERT_EQUALS(dst, 1); + } + + struct Foo { + void fooAdd(int &foo) { + ++foo; + } + + void fooSub(int &foo) const { + --foo; + } + }; + + void test_mem_fun_ref() { + Foo myFoos[4]; + int counter = 0; + + Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun_ref(&Foo::fooAdd), counter)); + TS_ASSERT_EQUALS(counter, 4); + + Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun_ref(&Foo::fooSub), counter)); + TS_ASSERT_EQUALS(counter, 0); + } + + void test_mem_fun() { + Foo *myFoos[4]; + for (int i = 0; i < 4; ++i) + myFoos[i] = new Foo; + + int counter = 0; + + Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun(&Foo::fooAdd), counter)); + TS_ASSERT_EQUALS(counter, 4); + + Common::for_each(myFoos, myFoos+4, Common::bind2nd(Common::mem_fun(&Foo::fooSub), counter)); + TS_ASSERT_EQUALS(counter, 0); + + for (int i = 0; i < 4; ++i) + delete myFoos[i]; + } +}; |