From f173041811398817ffce870d30ef3b87d1302736 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sun, 20 Apr 2008 15:47:11 +0000 Subject: - moved kyra functor code to common/func.h - adapted debugger code to use functor code from common/func.h - adapted kyra engine to use functor code from common/func.h svn-id: r31614 --- common/func.h | 74 +++++++++++++++++++++++++++++ engines/kyra/gui.h | 6 +-- engines/kyra/kyra.h | 5 +- engines/kyra/kyra_v2.h | 6 +-- engines/kyra/screen.h | 5 +- engines/kyra/script.h | 10 ++-- engines/kyra/script_tim.h | 5 +- engines/kyra/script_v1.cpp | 2 +- engines/kyra/script_v2.cpp | 4 +- engines/kyra/script_v3.cpp | 2 +- engines/kyra/timer.h | 5 +- engines/kyra/timer_v1.cpp | 2 +- engines/kyra/timer_v2.cpp | 2 +- engines/kyra/timer_v3.cpp | 2 +- engines/kyra/util.h | 114 --------------------------------------------- gui/debugger.cpp | 1 + gui/debugger.h | 26 ++--------- 17 files changed, 111 insertions(+), 160 deletions(-) delete mode 100644 engines/kyra/util.h diff --git a/common/func.h b/common/func.h index cff4e2d304..c2e1d0763e 100644 --- a/common/func.h +++ b/common/func.h @@ -247,6 +247,80 @@ FrontInsertIterator front_inserter(Cont &c) { return FrontInsertIterator(c); } +// functor code + +template +struct Functor0 { + virtual ~Functor0() {} + + virtual bool isValid() const = 0; + virtual Res operator()() const = 0; +}; + +template +class Functor0Mem : public Functor0 { +public: + typedef Res (T::*FuncType)(); + + Functor0Mem(T *t, const FuncType &func) : _t(t), _func(func) {} + + bool isValid() const { return _func != 0; } + Res operator()() const { + return (_t->*_func)(); + } +private: + mutable T *_t; + const FuncType _func; +}; + +template +struct Functor1 : public Common::UnaryFunction { + virtual ~Functor1() {} + + virtual bool isValid() const = 0; + virtual Res operator()(Arg) const = 0; +}; + +template +class Functor1Mem : public Functor1 { +public: + typedef Res (T::*FuncType)(Arg); + + Functor1Mem(T *t, const FuncType &func) : _t(t), _func(func) {} + + bool isValid() const { return _func != 0; } + Res operator()(Arg v1) const { + return (_t->*_func)(v1); + } +private: + mutable T *_t; + const FuncType _func; +}; + +template +struct Functor2 : public Common::BinaryFunction { + virtual ~Functor2() {} + + virtual bool isValid() const = 0; + virtual Res operator()(Arg1, Arg2) const = 0; +}; + +template +class Functor2Mem : public Functor2 { +public: + typedef Res (T::*FuncType)(Arg1, Arg2); + + Functor2Mem(T *t, const FuncType &func) : _t(t), _func(func) {} + + bool isValid() const { return _func != 0; } + Res operator()(Arg1 v1, Arg2 v2) const { + return (_t->*_func)(v1, v2); + } +private: + mutable T *_t; + const FuncType _func; +}; + /** * Base template for hash functor objects, used by HashMap. * This needs to be specialized for every type that you need to hash. diff --git a/engines/kyra/gui.h b/engines/kyra/gui.h index 214f112995..dfbca22441 100644 --- a/engines/kyra/gui.h +++ b/engines/kyra/gui.h @@ -26,18 +26,18 @@ #ifndef KYRA_GUI_H #define KYRA_GUI_H -#include "kyra/util.h" #include "kyra/kyra.h" #include "common/ptr.h" #include "common/array.h" +#include "common/func.h" namespace Kyra { -#define BUTTON_FUNCTOR(type, x, y) Button::Callback(new Functor1Mem(x, y)) +#define BUTTON_FUNCTOR(type, x, y) Button::Callback(new Common::Functor1Mem(x, y)) struct Button { - typedef Functor1 CallbackFunctor; + typedef Common::Functor1 CallbackFunctor; typedef Common::SharedPtr Callback; Button *nextButton; diff --git a/engines/kyra/kyra.h b/engines/kyra/kyra.h index f88b23ea05..0fee9e4e3e 100644 --- a/engines/kyra/kyra.h +++ b/engines/kyra/kyra.h @@ -27,11 +27,13 @@ #define KYRA_KYRA_H #include "engines/engine.h" + #include "common/rect.h" #include "common/array.h" #include "common/events.h" +#include "common/func.h" -#include "kyra/util.h" +#include "kyra/script.h" namespace Common { class InSaveFile; @@ -99,7 +101,6 @@ class Movie; class TextDisplayer; class StaticResource; class TimerManager; -class ScriptHelper; class KyraEngine : public Engine { friend class Debugger; diff --git a/engines/kyra/kyra_v2.h b/engines/kyra/kyra_v2.h index 10953a71a3..d63f8d6e99 100644 --- a/engines/kyra/kyra_v2.h +++ b/engines/kyra/kyra_v2.h @@ -28,12 +28,13 @@ #include "kyra/kyra.h" #include "kyra/script.h" +#include "kyra/script_tim.h" #include "kyra/screen_v2.h" #include "kyra/text_v2.h" #include "kyra/gui_v2.h" -#include "kyra/util.h" #include "common/list.h" +#include "common/func.h" namespace Kyra { @@ -99,7 +100,6 @@ enum kNestedSequencesDemo { class WSAMovieV2; class KyraEngine_v2; class TextDisplayer_v2; -class TIMInterpreter; class Debugger_v2; struct TIM; @@ -329,7 +329,7 @@ protected: void update(); void updateWithText(); - Functor0Mem _updateFunctor; + Common::Functor0Mem _updateFunctor; void updateMouse(); diff --git a/engines/kyra/screen.h b/engines/kyra/screen.h index 82f550f642..4516d4f907 100644 --- a/engines/kyra/screen.h +++ b/engines/kyra/screen.h @@ -27,14 +27,13 @@ #define KYRA_SCREEN_H #include "common/util.h" - -#include "kyra/util.h" +#include "common/func.h" class OSystem; namespace Kyra { -typedef Functor0 UpdateFunctor; +typedef Common::Functor0 UpdateFunctor; class KyraEngine; struct Rect; diff --git a/engines/kyra/script.h b/engines/kyra/script.h index 4ea05ea158..bc92aebc5e 100644 --- a/engines/kyra/script.h +++ b/engines/kyra/script.h @@ -26,13 +26,14 @@ #ifndef KYRA_SCRIPT_H #define KYRA_SCRIPT_H -#include "kyra/kyra.h" -#include "kyra/util.h" - #include "common/stream.h" +#include "common/func.h" namespace Kyra { +struct ScriptState; +typedef Common::Functor1 Opcode; + struct ScriptData { byte *text; uint16 *data; @@ -61,6 +62,9 @@ struct ScriptState { #define ORDR_CHUNK 0x5244524F #define AVTL_CHUNK 0x4C545641 +class Resource; +class KyraEngine; + class ScriptFileParser { public: ScriptFileParser() : _stream(0), _startOffset(0), _endOffset(0) {} diff --git a/engines/kyra/script_tim.h b/engines/kyra/script_tim.h index a76dcccb38..2ffbf89d65 100644 --- a/engines/kyra/script_tim.h +++ b/engines/kyra/script_tim.h @@ -27,12 +27,15 @@ #define KYRA_SCRIPT_TIM_H #include "kyra/kyra.h" -#include "kyra/util.h" #include "common/array.h" +#include "common/func.h" namespace Kyra { +struct TIM; +typedef Common::Functor2 TIMOpcode; + struct TIM { int16 procFunc; uint16 procParam; diff --git a/engines/kyra/script_v1.cpp b/engines/kyra/script_v1.cpp index 226bf140d9..50ad1b8b0c 100644 --- a/engines/kyra/script_v1.cpp +++ b/engines/kyra/script_v1.cpp @@ -1820,7 +1820,7 @@ int KyraEngine_v1::o1_dummy(ScriptState *script) { #pragma mark - -typedef Functor1Mem OpcodeV1; +typedef Common::Functor1Mem OpcodeV1; #define SetOpcodeTable(x) table = &x; #define Opcode(x) table->push_back(new OpcodeV1(this, &KyraEngine_v1::x)) void KyraEngine_v1::setupOpcodeTable() { diff --git a/engines/kyra/script_v2.cpp b/engines/kyra/script_v2.cpp index 0c2f1d2c24..e0b863a60d 100644 --- a/engines/kyra/script_v2.cpp +++ b/engines/kyra/script_v2.cpp @@ -1872,12 +1872,12 @@ int KyraEngine_v2::t2_playSoundEffect(const TIM *tim, const uint16 *param) { #pragma mark - -typedef Functor1Mem OpcodeV2; +typedef Common::Functor1Mem OpcodeV2; #define SetOpcodeTable(x) table = &x; #define Opcode(x) table->push_back(new OpcodeV2(this, &KyraEngine_v2::x)) #define OpcodeUnImpl() table->push_back(new OpcodeV2(this, 0)) -typedef Functor2Mem TIMOpcodeV2; +typedef Common::Functor2Mem TIMOpcodeV2; #define OpcodeTim(x) _timOpcodes.push_back(new TIMOpcodeV2(this, &KyraEngine_v2::x)) #define OpcodeTimUnImpl() _timOpcodes.push_back(TIMOpcodeV2(this, 0)) diff --git a/engines/kyra/script_v3.cpp b/engines/kyra/script_v3.cpp index 58fe9f9087..e309b1ee1c 100644 --- a/engines/kyra/script_v3.cpp +++ b/engines/kyra/script_v3.cpp @@ -580,7 +580,7 @@ int KyraEngine_v3::o3t_getMalcolmShapes(ScriptState *script) { return _malcolmShapes; } -typedef Functor1Mem OpcodeV3; +typedef Common::Functor1Mem OpcodeV3; #define SetOpcodeTable(x) table = &x; #define Opcode(x) table->push_back(new OpcodeV3(this, &KyraEngine_v3::x)) #define OpcodeUnImpl() table->push_back(new OpcodeV3(this, 0)) diff --git a/engines/kyra/timer.h b/engines/kyra/timer.h index 44f2e744c6..0991f0a1b1 100644 --- a/engines/kyra/timer.h +++ b/engines/kyra/timer.h @@ -27,14 +27,14 @@ #define KYRA_TIMER_H #include "kyra/kyra.h" -#include "kyra/util.h" #include "common/list.h" #include "common/stream.h" +#include "common/func.h" namespace Kyra { -typedef Functor1 TimerFunc; +typedef Common::Functor1 TimerFunc; struct TimerEntry { uint8 id; @@ -89,3 +89,4 @@ private: } // end of namespace Kyra #endif + diff --git a/engines/kyra/timer_v1.cpp b/engines/kyra/timer_v1.cpp index c02042f1cc..f2a31da554 100644 --- a/engines/kyra/timer_v1.cpp +++ b/engines/kyra/timer_v1.cpp @@ -33,7 +33,7 @@ namespace Kyra { -#define TimerV1(x) new Functor1Mem(this, &KyraEngine_v1::x) +#define TimerV1(x) new Common::Functor1Mem(this, &KyraEngine_v1::x) void KyraEngine_v1::setupTimers() { debugC(9, kDebugLevelMain | kDebugLevelTimer, "KyraEngine_v1::setupTimers()"); diff --git a/engines/kyra/timer_v2.cpp b/engines/kyra/timer_v2.cpp index 7e21970067..4c97a6dd2d 100644 --- a/engines/kyra/timer_v2.cpp +++ b/engines/kyra/timer_v2.cpp @@ -28,7 +28,7 @@ namespace Kyra { -#define TimerV2(x) new Functor1Mem(this, &KyraEngine_v2::x) +#define TimerV2(x) new Common::Functor1Mem(this, &KyraEngine_v2::x) void KyraEngine_v2::setupTimers() { debugC(9, kDebugLevelMain | kDebugLevelTimer, "KyraEngine_v2::setupTimers()"); diff --git a/engines/kyra/timer_v3.cpp b/engines/kyra/timer_v3.cpp index f9e7c1a571..2211fc6337 100644 --- a/engines/kyra/timer_v3.cpp +++ b/engines/kyra/timer_v3.cpp @@ -28,7 +28,7 @@ namespace Kyra { -#define TimerV3(x) new Functor1Mem(this, &KyraEngine_v3::x) +#define TimerV3(x) new Common::Functor1Mem(this, &KyraEngine_v3::x) void KyraEngine_v3::setupTimers() { debugC(9, kDebugLevelMain | kDebugLevelTimer, "KyraEngine_v3::setupTimers()"); diff --git a/engines/kyra/util.h b/engines/kyra/util.h deleted file mode 100644 index 78925441f3..0000000000 --- a/engines/kyra/util.h +++ /dev/null @@ -1,114 +0,0 @@ -/* ScummVM - Graphic Adventure Engine - * - * ScummVM is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - * - */ - -#ifndef KYRA_UTIL_H -#define KYRA_UTIL_H - -#include "common/func.h" - -namespace Kyra { - -template -struct Functor0 { - virtual ~Functor0() {} - - virtual bool isValid() const = 0; - virtual Res operator()() const = 0; -}; - -template -class Functor0Mem : public Functor0 { -public: - typedef Res (T::*FuncType)(); - - Functor0Mem(T *t, const FuncType &func) : _t(t), _func(func) {} - - bool isValid() const { return _func != 0; } - Res operator()() const { - return (_t->*_func)(); - } -private: - mutable T *_t; - Res (T::*_func)(); -}; - -template -struct Functor1 : public Common::UnaryFunction { - virtual ~Functor1() {} - - virtual bool isValid() const = 0; - virtual Res operator()(Arg) const = 0; -}; - -template -class Functor1Mem : public Functor1 { -public: - typedef Res (T::*FuncType)(Arg); - - Functor1Mem(T *t, const FuncType &func) : _t(t), _func(func) {} - - bool isValid() const { return _func != 0; } - Res operator()(Arg v1) const { - return (_t->*_func)(v1); - } -private: - mutable T *_t; - Res (T::*_func)(Arg); -}; - -template -struct Functor2 : public Common::BinaryFunction { - virtual ~Functor2() {} - - virtual bool isValid() const = 0; - virtual Res operator()(Arg1, Arg2) const = 0; -}; - -template -class Functor2Mem : public Functor2 { -public: - typedef Res (T::*FuncType)(Arg1, Arg2); - - Functor2Mem(T *t, const FuncType &func) : _t(t), _func(func) {} - - bool isValid() const { return _func != 0; } - Res operator()(Arg1 v1, Arg2 v2) const { - return (_t->*_func)(v1, v2); - } -private: - mutable T *_t; - Res (T::*_func)(Arg1, Arg2); -}; - -struct ScriptState; -typedef Functor1 Opcode; - -struct TIM; -typedef Functor2 TIMOpcode; - -} // end of namespace Kyra - -#endif - diff --git a/gui/debugger.cpp b/gui/debugger.cpp index 81a00d717c..99aebe9b62 100644 --- a/gui/debugger.cpp +++ b/gui/debugger.cpp @@ -347,6 +347,7 @@ void Debugger::DVar_Register(const char *varname, void *pointer, int type, int o // Command registration function void Debugger::DCmd_Register(const char *cmdname, Debuglet *debuglet) { + assert(debuglet->isValid()); assert(_dcmd_count < ARRAYSIZE(_dcmds)); strcpy(_dcmds[_dcmd_count].name, cmdname); _dcmds[_dcmd_count].debuglet = debuglet; diff --git a/gui/debugger.h b/gui/debugger.h index ac6ae62e03..0e48f384de 100644 --- a/gui/debugger.h +++ b/gui/debugger.h @@ -25,6 +25,8 @@ #ifndef GUI_DEBUGGER_H #define GUI_DEBUGGER_H +#include "common/func.h" + namespace GUI { // Choose between text console or ScummConsole @@ -47,32 +49,12 @@ public: bool isAttached() const { return _isAttached; } protected: - class Debuglet { - public: - virtual ~Debuglet() {} - virtual bool operator()(int argc, const char **argv) = 0; - }; - - template - class DelegateDebuglet : public Debuglet { - typedef bool (T::*Method)(int argc, const char **argv); - - T *_delegate; - const Method _method; - public: - DelegateDebuglet(T *delegate, Method method) - : _delegate(delegate), _method(method) { - assert(delegate != 0); - } - virtual bool operator()(int argc, const char **argv) { - return (_delegate->*_method)(argc, argv); - }; - }; + typedef Common::Functor2 Debuglet; // Convenicence macro for registering a method of a debugger class // as the current command. #define WRAP_METHOD(cls, method) \ - new DelegateDebuglet(this, &cls::method) + new Common::Functor2Mem(this, &cls::method) enum { DVAR_BYTE, -- cgit v1.2.3