ScummVM API documentation
func.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_FUNC_H
23 #define COMMON_FUNC_H
24 
25 #include "common/scummsys.h"
26 
27 namespace Common {
28 
42 template<class Arg, class Result>
43 struct UnaryFunction {
44  typedef Arg ArgumenType;
45  typedef Result ResultType;
46 };
47 
51 template<class Arg1, class Arg2, class Result>
53  typedef Arg1 FirstArgumentType;
54  typedef Arg2 SecondArgumentType;
55  typedef Result ResultType;
56 };
57 
61 template<class T>
62 struct EqualTo : public BinaryFunction<T, T, bool> {
63  bool operator()(const T &x, const T &y) const { return x == y; }
64 };
65 
69 template<class T>
70 struct Less : public BinaryFunction<T, T, bool> {
71  bool operator()(const T &x, const T &y) const { return x < y; }
72 };
73 
77 template<class T>
78 struct Greater : public BinaryFunction<T, T, bool> {
79  bool operator()(const T &x, const T &y) const { return x > y; }
80 };
81 
82 template<class Op>
83 class Binder1st : public UnaryFunction<typename Op::SecondArgumentType, typename Op::ResultType> {
84 private:
85  Op _op;
86  typename Op::FirstArgumentType _arg1;
87 public:
88  Binder1st(const Op &op, typename Op::FirstArgumentType arg1) : _op(op), _arg1(arg1) {}
89 
90  typename Op::ResultType operator()(typename Op::SecondArgumentType v) const {
91  return _op(_arg1, v);
92  }
93 };
94 
99 template<class Op>
100 inline Binder1st<Op> bind1st(const Op &op, typename Op::FirstArgumentType t) {
101  return Binder1st<Op>(op, t);
102 }
103 
104 template<class Op>
105 class Binder2nd : public UnaryFunction<typename Op::FirstArgumentType, typename Op::ResultType> {
106 private:
107  Op _op;
108  typename Op::SecondArgumentType _arg2;
109 public:
110  Binder2nd(const Op &op, typename Op::SecondArgumentType arg2) : _op(op), _arg2(arg2) {}
111 
112  typename Op::ResultType operator()(typename Op::FirstArgumentType v) const {
113  return _op(v, _arg2);
114  }
115 };
116 
121 template<class Op>
122 inline Binder2nd<Op> bind2nd(const Op &op, typename Op::SecondArgumentType t) {
123  return Binder2nd<Op>(op, t);
124 }
125 
126 template<class Arg, class Result>
127 class PointerToUnaryFunc : public UnaryFunction<Arg, Result> {
128 private:
129  Result (*_func)(Arg);
130 public:
131  typedef Result (*FuncType)(Arg);
132 
133  PointerToUnaryFunc(const FuncType &func) : _func(func) {}
134  Result operator()(Arg v) const {
135  return _func(v);
136  }
137 };
138 
139 template<class Arg1, class Arg2, class Result>
140 class PointerToBinaryFunc : public BinaryFunction<Arg1, Arg2, Result> {
141 private:
142  Result (*_func)(Arg1, Arg2);
143 public:
144  typedef Result (*FuncType)(Arg1, Arg2);
145 
146  PointerToBinaryFunc(const FuncType &func) : _func(func) {}
147  Result operator()(Arg1 v1, Arg2 v2) const {
148  return _func(v1, v2);
149  }
150 };
151 
155 template<class Arg, class Result>
156 inline PointerToUnaryFunc<Arg, Result> ptr_fun(Result (*func)(Arg)) {
157  return PointerToUnaryFunc<Arg, Result>(func);
158 }
159 
163 template<class Arg1, class Arg2, class Result>
164 inline PointerToBinaryFunc<Arg1, Arg2, Result> ptr_fun(Result (*func)(Arg1, Arg2)) {
166 }
167 
168 template<class Result, class T>
169 class MemFunc0 : public UnaryFunction<T *, Result> {
170 private:
171  Result (T::*_func)();
172 public:
173  typedef Result (T::*FuncType)();
174 
175  MemFunc0(const FuncType &func) : _func(func) {}
176  Result operator()(T *v) const {
177  return (v->*_func)();
178  }
179 };
180 
181 template<class Result, class T>
182 class ConstMemFunc0 : public UnaryFunction<T *, Result> {
183 private:
184  Result (T::*_func)() const;
185 public:
186  typedef Result (T::*FuncType)() const;
187 
188  ConstMemFunc0(const FuncType &func) : _func(func) {}
189  Result operator()(const T *v) const {
190  return (v->*_func)();
191  }
192 };
193 
194 template<class Result, class Arg, class T>
195 class MemFunc1 : public BinaryFunction<T *, Arg, Result> {
196 private:
197  Result (T::*_func)(Arg);
198 public:
199  typedef Result (T::*FuncType)(Arg);
200 
201  MemFunc1(const FuncType &func) : _func(func) {}
202  Result operator()(T *v1, Arg v2) const {
203  return (v1->*_func)(v2);
204  }
205 };
206 
207 template<class Result, class Arg, class T>
208 class ConstMemFunc1 : public BinaryFunction<T *, Arg, Result> {
209 private:
210  Result (T::*_func)(Arg) const;
211 public:
212  typedef Result (T::*FuncType)(Arg) const;
213 
214  ConstMemFunc1(const FuncType &func) : _func(func) {}
215  Result operator()(const T *v1, Arg v2) const {
216  return (v1->*_func)(v2);
217  }
218 };
219 
225 template<class Result, class T>
226 inline MemFunc0<Result, T> mem_fun(Result (T::*f)()) {
227  return MemFunc0<Result, T>(f);
228 }
229 
235 template<class Result, class T>
236 inline ConstMemFunc0<Result, T> mem_fun(Result (T::*f)() const) {
237  return ConstMemFunc0<Result, T>(f);
238 }
239 
246 template<class Result, class Arg, class T>
247 inline MemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg)) {
248  return MemFunc1<Result, Arg, T>(f);
249 }
250 
257 template<class Result, class Arg, class T>
258 inline ConstMemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg) const) {
260 }
261 
262 template<class Result, class T>
263 class MemFuncRef0 : public UnaryFunction<T &, Result> {
264 private:
265  Result (T::*_func)();
266 public:
267  typedef Result (T::*FuncType)();
268 
269  MemFuncRef0(const FuncType &func) : _func(func) {}
270  Result operator()(T &v) const {
271  return (v.*_func)();
272  }
273 };
274 
275 template<class Result, class T>
276 class ConstMemFuncRef0 : public UnaryFunction<T &, Result> {
277 private:
278  Result (T::*_func)() const;
279 public:
280  typedef Result (T::*FuncType)() const;
281 
282  ConstMemFuncRef0(const FuncType &func) : _func(func) {}
283  Result operator()(const T &v) const {
284  return (v.*_func)();
285  }
286 };
287 
288 template<class Result, class Arg, class T>
289 class MemFuncRef1 : public BinaryFunction<T &, Arg, Result> {
290 private:
291  Result (T::*_func)(Arg);
292 public:
293  typedef Result (T::*FuncType)(Arg);
294 
295  MemFuncRef1(const FuncType &func) : _func(func) {}
296  Result operator()(T &v1, Arg v2) const {
297  return (v1.*_func)(v2);
298  }
299 };
300 
301 template<class Result, class Arg, class T>
302 class ConstMemFuncRef1 : public BinaryFunction<T &, Arg, Result> {
303 private:
304  Result (T::*_func)(Arg) const;
305 public:
306  typedef Result (T::*FuncType)(Arg) const;
307 
308  ConstMemFuncRef1(const FuncType &func) : _func(func) {}
309  Result operator()(const T &v1, Arg v2) const {
310  return (v1.*_func)(v2);
311  }
312 };
313 
321 template<class Result, class T>
322 inline MemFuncRef0<Result, T> mem_fun_ref(Result (T::*f)()) {
323  return MemFuncRef0<Result, T>(f);
324 }
325 
332 template<class Result, class T>
333 inline ConstMemFuncRef0<Result, T> mem_fun_Ref(Result (T::*f)() const) {
334  return ConstMemFuncRef0<Result, T>(f);
335 }
336 
344 template<class Result, class Arg, class T>
345 inline MemFuncRef1<Result, Arg, T> mem_fun_ref(Result (T::*f)(Arg)) {
346  return MemFuncRef1<Result, Arg, T>(f);
347 }
348 
356 template<class Result, class Arg, class T>
357 inline ConstMemFuncRef1<Result, Arg, T> mem_fun_ref(Result (T::*f)(Arg) const) {
359 }
360 
361 // functor code
362 
368 template<class Res>
369 struct Functor0 {
370  virtual ~Functor0() {}
371 
372  virtual bool isValid() const = 0;
373  virtual Res operator()() const = 0;
374 };
375 
388 template<class Res, class T>
389 class Functor0Mem : public Functor0<Res> {
390 public:
391  typedef Res (T::*FuncType)();
392 
393  Functor0Mem(T *t, const FuncType &func) : _t(t), _func(func) {}
394 
395  bool isValid() const { return _func != 0 && _t != 0; }
396  Res operator()() const {
397  return (_t->*_func)();
398  }
399 private:
400  mutable T *_t;
401  const FuncType _func;
402 };
403 
436 template<class Arg, class Res>
437 struct Functor1 : public UnaryFunction<Arg, Res> {
438  virtual ~Functor1() {}
439 
440  virtual bool isValid() const = 0;
441  virtual Res operator()(Arg) const = 0;
442 };
443 
451 template<class Arg, class Res, class T>
452 class Functor1Mem : public Functor1<Arg, Res> {
453 public:
454  typedef Res (T::*FuncType)(Arg);
455 
456  Functor1Mem(T *t, const FuncType &func) : _t(t), _func(func) {}
457 
458  bool isValid() const { return _func != 0 && _t != 0; }
459  Res operator()(Arg v1) const {
460  return (_t->*_func)(v1);
461  }
462 private:
463  mutable T *_t;
464  const FuncType _func;
465 };
466 
472 template<class Arg1, class Arg2, class Res>
473 struct Functor2 : public BinaryFunction<Arg1, Arg2, Res> {
474  virtual ~Functor2() {}
475 
476  virtual bool isValid() const = 0;
477  virtual Res operator()(Arg1, Arg2) const = 0;
478 };
479 
485 template<class Arg1, class Arg2, class Res>
486 class Functor2Fun : public Functor2<Arg1, Arg2, Res> {
487 public:
488  typedef Res (*FuncType)(Arg1, Arg2);
489 
490  Functor2Fun(const FuncType func) : _func(func) {}
491 
492  bool isValid() const { return _func != 0; }
493  Res operator()(Arg1 v1, Arg2 v2) const {
494  return (*_func)(v1, v2);
495  }
496 private:
497  const FuncType _func;
498 };
499 
507 template<class Arg1, class Arg2, class Res, class T>
508 class Functor2Mem : public Functor2<Arg1, Arg2, Res> {
509 public:
510  typedef Res (T::*FuncType)(Arg1, Arg2);
511 
512  Functor2Mem(T *t, const FuncType &func) : _t(t), _func(func) {}
513 
514  bool isValid() const { return _func != 0 && _t != 0; }
515  Res operator()(Arg1 v1, Arg2 v2) const {
516  return (_t->*_func)(v1, v2);
517  }
518 private:
519  mutable T *_t;
520  const FuncType _func;
521 };
522 
527 template<typename T> struct Hash;
528 
529 
530 #define GENERATE_TRIVIAL_HASH_FUNCTOR(T) \
531  template<> struct Hash<T> : public UnaryFunction<T, uint> { \
532  uint operator()(T val) const { return (uint)val; } \
533  }
534 
535 GENERATE_TRIVIAL_HASH_FUNCTOR(bool);
536 GENERATE_TRIVIAL_HASH_FUNCTOR(char);
537 GENERATE_TRIVIAL_HASH_FUNCTOR(signed char);
538 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned char);
539 GENERATE_TRIVIAL_HASH_FUNCTOR(short);
540 GENERATE_TRIVIAL_HASH_FUNCTOR(int);
541 GENERATE_TRIVIAL_HASH_FUNCTOR(long);
542 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned short);
543 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned int);
544 GENERATE_TRIVIAL_HASH_FUNCTOR(unsigned long);
545 
546 #undef GENERATE_TRIVIAL_HASH_FUNCTOR
547 
550 } // End of namespace Common
551 
552 #endif
MemFuncRef0< Result, T > mem_fun_ref(Result(T::*f)())
Definition: func.h:322
Definition: func.h:78
Definition: func.h:127
Definition: func.h:508
Definition: func.h:140
Definition: func.h:208
Definition: func.h:527
Definition: func.h:52
Definition: func.h:389
ConstMemFuncRef0< Result, T > mem_fun_Ref(Result(T::*f)() const)
Definition: func.h:333
PointerToUnaryFunc< Arg, Result > ptr_fun(Result(*func)(Arg))
Definition: func.h:156
Definition: func.h:369
MemFunc0< Result, T > mem_fun(Result(T::*f)())
Definition: func.h:226
Definition: func.h:289
Definition: func.h:105
Definition: func.h:195
Definition: func.h:276
Definition: algorithm.h:29
Definition: func.h:452
Binder2nd< Op > bind2nd(const Op &op, typename Op::SecondArgumentType t)
Definition: func.h:122
Binder1st< Op > bind1st(const Op &op, typename Op::FirstArgumentType t)
Definition: func.h:100
Definition: func.h:437
Definition: func.h:62
Definition: func.h:302
Definition: func.h:83
Definition: func.h:263
Definition: func.h:169
Definition: func.h:182
Definition: func.h:473
Definition: func.h:43
Definition: func.h:486
Definition: func.h:70