mirror of
https://github.com/libretro/scummvm.git
synced 2025-01-23 11:04:44 +00:00
Documentation for func.h.
svn-id: r33425
This commit is contained in:
parent
aaa4d104f7
commit
68f41290a3
119
common/func.h
119
common/func.h
@ -29,12 +29,18 @@
|
||||
|
||||
namespace Common {
|
||||
|
||||
/**
|
||||
* Generic unary function.
|
||||
*/
|
||||
template<class Arg, class Result>
|
||||
struct UnaryFunction {
|
||||
typedef Arg ArgumenType;
|
||||
typedef Result ResultType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic binary function.
|
||||
*/
|
||||
template<class Arg1, class Arg2, class Result>
|
||||
struct BinaryFunction {
|
||||
typedef Arg1 FirstArgumentType;
|
||||
@ -42,16 +48,25 @@ struct BinaryFunction {
|
||||
typedef Result ResultType;
|
||||
};
|
||||
|
||||
/**
|
||||
* Predicate to check for equallity of two data elements.
|
||||
*/
|
||||
template<class T>
|
||||
struct EqualTo : public BinaryFunction<T, T, bool> {
|
||||
bool operator()(const T &x, const T &y) const { return x == y; }
|
||||
};
|
||||
|
||||
/**
|
||||
* Predicate to check for x being less than y.
|
||||
*/
|
||||
template<class T>
|
||||
struct Less : public BinaryFunction<T, T, bool> {
|
||||
bool operator()(const T &x, const T &y) const { return x < y; }
|
||||
};
|
||||
|
||||
/**
|
||||
* Predicate to check for x being greater than y.
|
||||
*/
|
||||
template<class T>
|
||||
struct Greater : public BinaryFunction<T, T, bool> {
|
||||
bool operator()(const T &x, const T &y) const { return x > y; }
|
||||
@ -70,6 +85,10 @@ 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) {
|
||||
return Binder1st<Op>(op, t);
|
||||
@ -88,6 +107,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.
|
||||
*/
|
||||
template<class Op, class T>
|
||||
inline Binder2nd<Op> bind2nd(const Op &op, const T &t) {
|
||||
return Binder2nd<Op>(op, t);
|
||||
@ -119,11 +142,17 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates an unary function object from a function pointer.
|
||||
*/
|
||||
template<class Arg, class Result>
|
||||
inline PointerToUnaryFunc<Arg, Result> ptr_fun(Result (*func)(Arg)) {
|
||||
return PointerToUnaryFunc<Arg, Result>(func);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an binary function object from a function pointer.
|
||||
*/
|
||||
template<class Arg1, class Arg2, class Result>
|
||||
inline PointerToBinaryFunc<Arg1, Arg2, Result> ptr_fun(Result (*func)(Arg1, Arg2)) {
|
||||
return PointerToBinaryFunc<Arg1, Arg2, Result>(func);
|
||||
@ -181,21 +210,43 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a unary function object from a class member function pointer.
|
||||
* The parameter passed to the function object is the 'this' pointer to
|
||||
* be used for the function call.
|
||||
*/
|
||||
template<class Result, class T>
|
||||
inline MemFunc0<Result, T> mem_fun(Result (T::*f)()) {
|
||||
return MemFunc0<Result, T>(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a unary function object from a class member function pointer.
|
||||
* The parameter passed to the function object is the 'this' pointer to
|
||||
* be used for the function call.
|
||||
*/
|
||||
template<class Result, class T>
|
||||
inline ConstMemFunc0<Result, T> mem_fun(Result (T::*f)() const) {
|
||||
return ConstMemFunc0<Result, T>(f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a binary function object from a class member function pointer.
|
||||
* The first parameter passed to the function object is the 'this' pointer to
|
||||
* be used for the function call.
|
||||
* The second one is the parameter passed to the member function.
|
||||
*/
|
||||
template<class Result, class Arg, class T>
|
||||
inline MemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg)) {
|
||||
return MemFunc1<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 'this' pointer to
|
||||
* be used for the function call.
|
||||
* The second one is the parameter passed to the member function.
|
||||
*/
|
||||
template<class Result, class Arg, class T>
|
||||
inline ConstMemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg) const) {
|
||||
return ConstMemFunc1<Result, Arg, T>(f);
|
||||
@ -203,6 +254,11 @@ inline ConstMemFunc1<Result, Arg, T> mem_fun(Result (T::*f)(Arg) const) {
|
||||
|
||||
// functor code
|
||||
|
||||
/**
|
||||
* Generic functor object for function objects without parameters.
|
||||
*
|
||||
* @see Functor1
|
||||
*/
|
||||
template<class Res>
|
||||
struct Functor0 {
|
||||
virtual ~Functor0() {}
|
||||
@ -211,6 +267,18 @@ struct Functor0 {
|
||||
virtual Res operator()() const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Functor object for a class member function without parameter.
|
||||
*
|
||||
* Example creation:
|
||||
*
|
||||
* Foo bar;
|
||||
* Functor0Men<void, Foo> myFunctor(&bar, &Foo::myFunc);
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* myFunctor();
|
||||
*/
|
||||
template<class Res, class T>
|
||||
class Functor0Mem : public Functor0<Res> {
|
||||
public:
|
||||
@ -227,6 +295,38 @@ private:
|
||||
const FuncType _func;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic functor object for unary function objects.
|
||||
*
|
||||
* A typical usage for an unary function object is for executing opcodes
|
||||
* in a script interpreter. To achieve that one can create an Common::Array
|
||||
* object with 'Functor1<Arg, Res> *' as type. Now after the right engine version
|
||||
* has been determined and the opcode table to use is found one could easily
|
||||
* add the opcode implementations like this:
|
||||
*
|
||||
* Common::Array<Functor1<ScriptState, void> *> opcodeTable;
|
||||
* opcodeTable[0] = new Functor1Mem<ScriptState, void, MyEngine>(&myEngine, &MyEngine_v1::o1_foo);
|
||||
* opcodeTable[1] = new Functor1Mem<ScriptState, void, MyEngine>(&myEngine, &MyEngine_v2::o2_foo);
|
||||
* // unimplemented/unused opcode
|
||||
* opcodeTable[2] = 0;
|
||||
* etc.
|
||||
*
|
||||
* This makes it easy to add member functions of different classes as
|
||||
* opcode functions to the function table. Since with the generic
|
||||
* Functor1<ScriptState, void> object the only requirement for an
|
||||
* function to be used is 'ScriptState' as argument and 'void' as return
|
||||
* value.
|
||||
*
|
||||
* Now for calling the opcodes one has simple to do:
|
||||
* if (opcodeTable[opcodeNum] && opcodeTable[opcodeNum]->isValid())
|
||||
* (*opcodeTable[opcodeNum])(scriptState);
|
||||
* else
|
||||
* warning("Unimplemented opcode %d", opcodeNum);
|
||||
*
|
||||
* If you want to see an real world example check the kyra engine.
|
||||
* Files: engines/kyra/script.cpp and .h and engine/kyra/script_*.cpp
|
||||
* are interesting for that matter.
|
||||
*/
|
||||
template<class Arg, class Res>
|
||||
struct Functor1 : public Common::UnaryFunction<Arg, Res> {
|
||||
virtual ~Functor1() {}
|
||||
@ -235,6 +335,13 @@ struct Functor1 : public Common::UnaryFunction<Arg, Res> {
|
||||
virtual Res operator()(Arg) const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Functor object for an unary class member function.
|
||||
* Usage is like with Functor0Mem. The resulting functor object
|
||||
* will take one parameter though.
|
||||
*
|
||||
* @see Functor0Men
|
||||
*/
|
||||
template<class Arg, class Res, class T>
|
||||
class Functor1Mem : public Functor1<Arg, Res> {
|
||||
public:
|
||||
@ -251,6 +358,11 @@ private:
|
||||
const FuncType _func;
|
||||
};
|
||||
|
||||
/**
|
||||
* Generic functor object for binary function objects.
|
||||
*
|
||||
* @see Functor1
|
||||
*/
|
||||
template<class Arg1, class Arg2, class Res>
|
||||
struct Functor2 : public Common::BinaryFunction<Arg1, Arg2, Res> {
|
||||
virtual ~Functor2() {}
|
||||
@ -259,6 +371,13 @@ struct Functor2 : public Common::BinaryFunction<Arg1, Arg2, Res> {
|
||||
virtual Res operator()(Arg1, Arg2) const = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Functor object for a binary class member function.
|
||||
* Usage is like with Functor0Mem. The resulting functor object
|
||||
* will take two parameter though.
|
||||
*
|
||||
* @see Functor0Men
|
||||
*/
|
||||
template<class Arg1, class Arg2, class Res, class T>
|
||||
class Functor2Mem : public Functor2<Arg1, Arg2, Res> {
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user