[Scummvm-cvs-logs] SF.net SVN: scummvm:[33425] scummvm/trunk/common/func.h

lordhoto at users.sourceforge.net lordhoto at users.sourceforge.net
Tue Jul 29 22:09:31 CEST 2008


Revision: 33425
          http://scummvm.svn.sourceforge.net/scummvm/?rev=33425&view=rev
Author:   lordhoto
Date:     2008-07-29 20:09:30 +0000 (Tue, 29 Jul 2008)

Log Message:
-----------
Documentation for func.h.

Modified Paths:
--------------
    scummvm/trunk/common/func.h

Modified: scummvm/trunk/common/func.h
===================================================================
--- scummvm/trunk/common/func.h	2008-07-29 19:45:55 UTC (rev 33424)
+++ scummvm/trunk/common/func.h	2008-07-29 20:09:30 UTC (rev 33425)
@@ -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 @@
 	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 @@
 	}
 };
 
+/**
+ * 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 @@
 	}
 };
 
+/**
+ * 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 @@
 	}
 };
 
+/**
+ * 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 @@
 	}
 };
 
+/**
+ * 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 @@
 
 // functor code
 
+/**
+ * Generic functor object for function objects without parameters.
+ *
+ * @see Functor1
+ */
 template<class Res>
 struct Functor0 {
 	virtual ~Functor0() {}
@@ -211,6 +267,18 @@
 	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 @@
 	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 @@
 	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 @@
 	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 @@
 	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:


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list