ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

func.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifndef COMMON_FUNC_H
00024 #define COMMON_FUNC_H
00025 
00026 #include "common/scummsys.h"
00027 
00028 namespace Common {
00029 
00033 template<class Arg, class Result>
00034 struct UnaryFunction {
00035     typedef Arg ArgumenType;
00036     typedef Result ResultType;
00037 };
00038 
00042 template<class Arg1, class Arg2, class Result>
00043 struct BinaryFunction {
00044     typedef Arg1 FirstArgumentType;
00045     typedef Arg2 SecondArgumentType;
00046     typedef Result ResultType;
00047 };
00048 
00052 template<class T>
00053 struct EqualTo : public BinaryFunction<T, T, bool> {
00054     bool operator()(const T &x, const T &y) const { return x == y; }
00055 };
00056 
00060 template<class T>
00061 struct Less : public BinaryFunction<T, T, bool> {
00062     bool operator()(const T &x, const T &y) const { return x < y; }
00063 };
00064 
00068 template<class T>
00069 struct Greater : public BinaryFunction<T, T, bool> {
00070     bool operator()(const T &x, const T &y) const { return x > y; }
00071 };
00072 
00073 template<class Op>
00074 class Binder1st : public UnaryFunction<typename Op::SecondArgumentType, typename Op::ResultType> {
00075 private:
00076     Op _op;
00077     typename Op::FirstArgumentType _arg1;
00078 public:
00079     Binder1st(const Op &op, typename Op::FirstArgumentType arg1) : _op(op), _arg1(arg1) {}
00080 
00081     typename Op::ResultType operator()(typename Op::SecondArgumentType v) const {
00082         return _op(_arg1, v);
00083     }
00084 };
00085 
00090 template<class Op>
00091 inline Binder1st<Op> bind1st(const Op &op, typename Op::FirstArgumentType t) {
00092     return Binder1st<Op>(op, t);
00093 }
00094 
00095 template<class Op>
00096 class Binder2nd : public UnaryFunction<typename Op::FirstArgumentType, typename Op::ResultType> {
00097 private:
00098     Op _op;
00099     typename Op::SecondArgumentType _arg2;
00100 public:
00101     Binder2nd(const Op &op, typename Op::SecondArgumentType arg2) : _op(op), _arg2(arg2) {}
00102 
00103     typename Op::ResultType operator()(typename Op::FirstArgumentType v) const {
00104         return _op(v, _arg2);
00105     }
00106 };
00107 
00112 template<class Op>
00113 inline Binder2nd<Op> bind2nd(const Op &op, typename Op::SecondArgumentType t) {
00114     return Binder2nd<Op>(op, t);
00115 }
00116 
00117 template<class Arg, class Result>
00118 class PointerToUnaryFunc : public UnaryFunction<Arg, Result> {
00119 private:
00120     Result (*_func)(Arg);
00121 public:
00122     typedef Result (*FuncType)(Arg);
00123 
00124     PointerToUnaryFunc(const FuncType &func) : _func(func) {}
00125     Result operator()(Arg v) const {
00126         return _func(v);
00127     }
00128 };
00129 
00130 template<class Arg1, class Arg2, class Result>
00131 class PointerToBinaryFunc : public BinaryFunction<Arg1, Arg2, Result> {
00132 private:
00133     Result (*_func)(Arg1, Arg2);
00134 public:
00135     typedef Result (*FuncType)(Arg1, Arg2);
00136 
00137     PointerToBinaryFunc(const FuncType &func) : _func(func) {}
00138     Result operator()(Arg1 v1, Arg2 v2) const {
00139         return _func(v1, v2);
00140     }
00141 };
00142 
00146 template<class Arg, class Result>
00147 inline PointerToUnaryFunc<Arg, Result> ptr_fun(Result (*func)(Arg)) {
00148     return PointerToUnaryFunc<Arg, Result>(func);
00149 }
00150 
00154 template<class Arg1, class Arg2, class Result>
00155 inline PointerToBinaryFunc<Arg1, Arg2, Result> ptr_fun(Result (*func)(Arg1, Arg2)) {
00156     return PointerToBinaryFunc<Arg1, Arg2, Result>(func);
00157 }
00158 
00159 template<class Result, class T>
00160 class MemFunc0 : public UnaryFunction<T *, Result> {
00161 private:
00162     Result (T::*_func)();
00163 public:
00164     typedef Result (T::*FuncType)();
00165 
00166     MemFunc0(const FuncType &func) : _func(func) {}
00167     Result operator()(T *v) const {
00168         return (v->*_func)();
00169     }
00170 };
00171 
00172 template<class Result, class T>
00173 class ConstMemFunc0 : public UnaryFunction<T *, Result> {
00174 private:
00175     Result (T::*_func)() const;
00176 public:
00177     typedef Result (T::*FuncType)() const;
00178 
00179     ConstMemFunc0(const FuncType &func) : _func(func) {}
00180     Result operator()(const T *v) const {
00181         return (v->*_func)();
00182     }
00183 };
00184 
00185 template<class Result, class Arg, class T>
00186 class MemFunc1 : public BinaryFunction<T *, Arg, Result> {
00187 private:
00188     Result (T::*_func)(Arg);
00189 public:
00190     typedef Result (T::*FuncType)(Arg);
00191 
00192     MemFunc1(const FuncType &func) : _func(func) {}
00193     Result operator()(T *v1, Arg v2) const {
00194         return (v1->*_func)(v2);
00195     }
00196 };
00197 
00198 template<class Result, class Arg, class T>
00199 class ConstMemFunc1 : public BinaryFunction<T *, Arg, Result> {
00200 private:
00201     Result (T::*_func)(Arg) const;
00202 public:
00203     typedef Result (T::*FuncType)(Arg) const;
00204 
00205     ConstMemFunc1(const FuncType &func) : _func(func) {}
00206     Result operator()(const T *v1, Arg v2) const {
00207         return (v1->*_func)(v2);
00208     }
00209 };
00210 
00216 template<class Result, class T>
00217 inline MemFunc0<Result, T> mem_fun(Result (T::*f)()) {
00218     return MemFunc0<Result, T>(f);
00219 }
00220 
00226 template<class Result, class T>
00227 inline ConstMemFunc0<Result, T> mem_fun(Result (T::*f)() const) {
00228     return ConstMemFunc0<Result, T>(f);
00229 }
00230 
00237 template<class Result, class Arg, class T>
00238 inline MemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg)) {
00239     return MemFunc1<Result, Arg, T>(f);
00240 }
00241 
00248 template<class Result, class Arg, class T>
00249 inline ConstMemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg) const) {
00250     return ConstMemFunc1<Result, Arg, T>(f);
00251 }
00252 
00253 template<class Result, class T>
00254 class MemFuncRef0 : public UnaryFunction<T &, Result> {
00255 private:
00256     Result (T::*_func)();
00257 public:
00258     typedef Result (T::*FuncType)();
00259 
00260     MemFuncRef0(const FuncType &func) : _func(func) {}
00261     Result operator()(T &v) const {
00262         return (v.*_func)();
00263     }
00264 };
00265 
00266 template<class Result, class T>
00267 class ConstMemFuncRef0 : public UnaryFunction<T &, Result> {
00268 private:
00269     Result (T::*_func)() const;
00270 public:
00271     typedef Result (T::*FuncType)() const;
00272 
00273     ConstMemFuncRef0(const FuncType &func) : _func(func) {}
00274     Result operator()(const T &v) const {
00275         return (v.*_func)();
00276     }
00277 };
00278 
00279 template<class Result, class Arg, class T>
00280 class MemFuncRef1 : public BinaryFunction<T &, Arg, Result> {
00281 private:
00282     Result (T::*_func)(Arg);
00283 public:
00284     typedef Result (T::*FuncType)(Arg);
00285 
00286     MemFuncRef1(const FuncType &func) : _func(func) {}
00287     Result operator()(T &v1, Arg v2) const {
00288         return (v1.*_func)(v2);
00289     }
00290 };
00291 
00292 template<class Result, class Arg, class T>
00293 class ConstMemFuncRef1 : public BinaryFunction<T &, Arg, Result> {
00294 private:
00295     Result (T::*_func)(Arg) const;
00296 public:
00297     typedef Result (T::*FuncType)(Arg) const;
00298 
00299     ConstMemFuncRef1(const FuncType &func) : _func(func) {}
00300     Result operator()(const T &v1, Arg v2) const {
00301         return (v1.*_func)(v2);
00302     }
00303 };
00304 
00312 template<class Result, class T>
00313 inline MemFuncRef0<Result, T> mem_fun_ref(Result (T::*f)()) {
00314     return MemFuncRef0<Result, T>(f);
00315 }
00316 
00323 template<class Result, class T>
00324 inline ConstMemFuncRef0<Result, T> mem_fun_Ref(Result (T::*f)() const) {
00325     return ConstMemFuncRef0<Result, T>(f);
00326 }
00327 
00335 template<class Result, class Arg, class T>
00336 inline MemFuncRef1<Result, Arg, T> mem_fun_ref(Result (T::*f)(Arg)) {
00337     return MemFuncRef1<Result, Arg, T>(f);
00338 }
00339 
00347 template<class Result, class Arg, class T>
00348 inline ConstMemFuncRef1<Result, Arg, T> mem_fun_ref(Result (T::*f)(Arg) const) {
00349     return ConstMemFuncRef1<Result, Arg, T>(f);
00350 }
00351 
00352 // functor code
00353 
00359 template<class Res>
00360 struct Functor0 {
00361     virtual ~Functor0() {}
00362 
00363     virtual bool isValid() const = 0;
00364     virtual Res operator()() const = 0;
00365 };
00366 
00379 template<class Res, class T>
00380 class Functor0Mem : public Functor0<Res> {
00381 public:
00382     typedef Res (T::*FuncType)();
00383 
00384     Functor0Mem(T *t, const FuncType &func) : _t(t), _func(func) {}
00385 
00386     bool isValid() const { return _func != 0 && _t != 0; }
00387     Res operator()() const {
00388         return (_t->*_func)();
00389     }
00390 private:
00391     mutable T *_t;
00392     const FuncType _func;
00393 };
00394 
00427 template<class Arg, class Res>
00428 struct Functor1 : public UnaryFunction<Arg, Res> {
00429     virtual ~Functor1() {}
00430 
00431     virtual bool isValid() const = 0;
00432     virtual Res operator()(Arg) const = 0;
00433 };
00434 
00442 template<class Arg, class Res, class T>
00443 class Functor1Mem : public Functor1<Arg, Res> {
00444 public:
00445     typedef Res (T::*FuncType)(Arg);
00446 
00447     Functor1Mem(T *t, const FuncType &func) : _t(t), _func(func) {}
00448 
00449     bool isValid() const { return _func != 0 && _t != 0; }
00450     Res operator()(Arg v1) const {
00451         return (_t->*_func)(v1);
00452     }
00453 private:
00454     mutable T *_t;
00455     const FuncType _func;
00456 };
00457 
00463 template<class Arg1, class Arg2, class Res>
00464 struct Functor2 : public BinaryFunction<Arg1, Arg2, Res> {
00465     virtual ~Functor2() {}
00466 
00467     virtual bool isValid() const = 0;
00468     virtual Res operator()(Arg1, Arg2) const = 0;
00469 };
00470 
00476 template<class Arg1, class Arg2, class Res>
00477 class Functor2Fun : public Functor2<Arg1, Arg2, Res> {
00478 public:
00479     typedef Res (*FuncType)(Arg1, Arg2);
00480 
00481     Functor2Fun(const FuncType func) : _func(func) {}
00482 
00483     bool isValid() const { return _func != 0; }
00484     Res operator()(Arg1 v1, Arg2 v2) const {
00485         return (*_func)(v1, v2);
00486     }
00487 private:
00488     const FuncType _func;
00489 };
00490 
00498 template<class Arg1, class Arg2, class Res, class T>
00499 class Functor2Mem : public Functor2<Arg1, Arg2, Res> {
00500 public:
00501     typedef Res (T::*FuncType)(Arg1, Arg2);
00502 
00503     Functor2Mem(T *t, const FuncType &func) : _t(t), _func(func) {}
00504 
00505     bool isValid() const { return _func != 0 && _t != 0; }
00506     Res operator()(Arg1 v1, Arg2 v2) const {
00507         return (_t->*_func)(v1, v2);
00508     }
00509 private:
00510     mutable T *_t;
00511     const FuncType _func;
00512 };
00513 
00518 template<typename T> struct Hash;
00519 
00520 
00521 #define GENERATE_TRIVIAL_HASH_FUNCTOR(T) \
00522     template<> struct Hash<T> : public UnaryFunction<T, uint> { \
00523         uint operator()(T val) const { return (uint)val; } \
00524     }
00525 
00526 GENERATE_TRIVIAL_HASH_FUNCTOR(bool);
00527 GENERATE_TRIVIAL_HASH_FUNCTOR(char);
00528 GENERATE_TRIVIAL_HASH_FUNCTOR(signed char);
00529 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned char);
00530 GENERATE_TRIVIAL_HASH_FUNCTOR(short);
00531 GENERATE_TRIVIAL_HASH_FUNCTOR(int);
00532 GENERATE_TRIVIAL_HASH_FUNCTOR(long);
00533 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned short);
00534 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned int);
00535 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned long);
00536 
00537 #undef GENERATE_TRIVIAL_HASH_FUNCTOR
00538 
00539 } // End of namespace Common
00540 
00541 #endif


Generated on Sat Jul 20 2019 05:00:47 for ResidualVM by doxygen 1.7.1
curved edge   curved edge