ScummVM API documentation
Common::Functor1< Arg, Res > Struct Template Referenceabstract

#include <func.h>

Inheritance diagram for Common::Functor1< Arg, Res >:
Common::UnaryFunction< Arg, Res > Common::Functor1Mem< Arg, Res, T >

Public Member Functions

virtual bool isValid () const =0
 
virtual Res operator() (Arg) const =0
 

Additional Inherited Members

- Public Types inherited from Common::UnaryFunction< Arg, Res >
typedef Arg ArgumenType
 
typedef Res ResultType
 

Detailed Description

template<class Arg, class Res>
struct Common::Functor1< Arg, Res >

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_v1>(&myEngine, &MyEngine_v1::o1_foo); opcodeTable[1] = new Functor1Mem<ScriptState, void, MyEngine_v2>(&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 a real-world example, check the kyra engine. Files: engines/kyra/script.cpp and .h and engines/kyra/script_*.cpp are interesting for that matter.


The documentation for this struct was generated from the following file: