From de2d2777e22e273f855d036b5638940ef5cfdfdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Sun, 19 Feb 2012 16:51:25 +0100 Subject: [PATCH] The TurnCmd class does not inherit from Command anymore --- game/navigation.cpp | 7 ++++--- game/navigation.h | 2 +- game/turn_cmd.cpp | 7 +++---- game/turn_cmd.h | 17 ++++++++++------- game/xml_writer.cpp | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) diff --git a/game/navigation.cpp b/game/navigation.cpp index 654e355..2b7d75d 100644 --- a/game/navigation.cpp +++ b/game/navigation.cpp @@ -22,6 +22,7 @@ #include "navigation.h" #include "turn_cmd.h" +#include "command.h" #include "game_exception.h" #include "debug.h" #include "encoding.h" @@ -38,7 +39,7 @@ Navigation::Navigation() Navigation::~Navigation() { - BOOST_FOREACH(Command *c, m_turnCommands) + BOOST_FOREACH(TurnCmd *c, m_turnCommands) { delete c; } @@ -180,7 +181,7 @@ void Navigation::clearFuture() } -const vector & Navigation::getCommands() const +const vector & Navigation::getTurns() const { return m_turnCommands; } @@ -191,7 +192,7 @@ void Navigation::print() const LOG_DEBUG("=== Commands history ==="); LOG_DEBUG("Current position right after turn " << m_currTurn - 1); int index = 0; - BOOST_FOREACH(const Command *c, m_turnCommands) + BOOST_FOREACH(const TurnCmd *c, m_turnCommands) { LOG_DEBUG(index << " " << lfw(c->toString())); ++index; diff --git a/game/navigation.h b/game/navigation.h index ae026bf..a7fe22d 100644 --- a/game/navigation.h +++ b/game/navigation.h @@ -55,7 +55,7 @@ class Navigation */ void clearFuture(); - const vector & getCommands() const; + const vector & getTurns() const; /** * Print the contents of the commands history, to ease debugging diff --git a/game/turn_cmd.cpp b/game/turn_cmd.cpp index a4c936b..8b399c5 100644 --- a/game/turn_cmd.cpp +++ b/game/turn_cmd.cpp @@ -22,6 +22,7 @@ #include #include "turn_cmd.h" +#include "command.h" #include "player.h" @@ -30,8 +31,6 @@ INIT_LOGGER(game, TurnCmd); TurnCmd::TurnCmd() { - // Fake execution - execute(); } @@ -51,7 +50,7 @@ void TurnCmd::addAndExecute(Command *iCmd) } -void TurnCmd::doExecute() +void TurnCmd::execute() { BOOST_FOREACH(Command *cmd, m_commands) { @@ -61,7 +60,7 @@ void TurnCmd::doExecute() } -void TurnCmd::doUndo() +void TurnCmd::undo() { // Undo commands in the reverse order of execution vector::reverse_iterator it; diff --git a/game/turn_cmd.h b/game/turn_cmd.h index 417ddff..ae79a5f 100644 --- a/game/turn_cmd.h +++ b/game/turn_cmd.h @@ -23,17 +23,21 @@ #include -#include "command.h" #include "logging.h" using namespace std; +class Command; + /** - * This class implements both the Command and Composite design patterns. - * It encapsulates commands, while still behaving like one. + * This class encapsulates commands, and almost behaves as one itself. + * The main difference with a normal command (and thus with the composite pattern) + * is that a TurnCmd provides partial execution: some of the commands it contains + * (the first ones) can be executed, whereas others not. + * The ones which can be executed are the ones flagged "auto-executable". */ -class TurnCmd: public Command +class TurnCmd { DEFINE_LOGGER(); @@ -55,9 +59,8 @@ class TurnCmd: public Command virtual wstring toString() const; - protected: - virtual void doExecute(); - virtual void doUndo(); + void execute(); + void undo(); private: vector m_commands; diff --git a/game/xml_writer.cpp b/game/xml_writer.cpp index 5768097..68b7858 100644 --- a/game/xml_writer.cpp +++ b/game/xml_writer.cpp @@ -156,7 +156,7 @@ void XmlWriter::write(const Game &iGame, const string &iFileName) #if 0 iGame.getNavigation().print(); #endif - const vector &turnCmdVect = iGame.getNavigation().getCommands(); + const vector &turnCmdVect = iGame.getNavigation().getTurns(); BOOST_FOREACH(const TurnCmd *turn, turnCmdVect) { if (turn->getCommands().empty() && turn == turnCmdVect.back())