The TurnCmd class does not inherit from Command anymore

This commit is contained in:
Olivier Teulière 2012-02-19 16:51:25 +01:00
parent 59fac223f4
commit de2d2777e2
5 changed files with 19 additions and 16 deletions

View file

@ -22,6 +22,7 @@
#include "navigation.h" #include "navigation.h"
#include "turn_cmd.h" #include "turn_cmd.h"
#include "command.h"
#include "game_exception.h" #include "game_exception.h"
#include "debug.h" #include "debug.h"
#include "encoding.h" #include "encoding.h"
@ -38,7 +39,7 @@ Navigation::Navigation()
Navigation::~Navigation() Navigation::~Navigation()
{ {
BOOST_FOREACH(Command *c, m_turnCommands) BOOST_FOREACH(TurnCmd *c, m_turnCommands)
{ {
delete c; delete c;
} }
@ -180,7 +181,7 @@ void Navigation::clearFuture()
} }
const vector<TurnCmd *> & Navigation::getCommands() const const vector<TurnCmd *> & Navigation::getTurns() const
{ {
return m_turnCommands; return m_turnCommands;
} }
@ -191,7 +192,7 @@ void Navigation::print() const
LOG_DEBUG("=== Commands history ==="); LOG_DEBUG("=== Commands history ===");
LOG_DEBUG("Current position right after turn " << m_currTurn - 1); LOG_DEBUG("Current position right after turn " << m_currTurn - 1);
int index = 0; int index = 0;
BOOST_FOREACH(const Command *c, m_turnCommands) BOOST_FOREACH(const TurnCmd *c, m_turnCommands)
{ {
LOG_DEBUG(index << " " << lfw(c->toString())); LOG_DEBUG(index << " " << lfw(c->toString()));
++index; ++index;

View file

@ -55,7 +55,7 @@ class Navigation
*/ */
void clearFuture(); void clearFuture();
const vector<TurnCmd *> & getCommands() const; const vector<TurnCmd *> & getTurns() const;
/** /**
* Print the contents of the commands history, to ease debugging * Print the contents of the commands history, to ease debugging

View file

@ -22,6 +22,7 @@
#include <sstream> #include <sstream>
#include "turn_cmd.h" #include "turn_cmd.h"
#include "command.h"
#include "player.h" #include "player.h"
@ -30,8 +31,6 @@ INIT_LOGGER(game, TurnCmd);
TurnCmd::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) 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 // Undo commands in the reverse order of execution
vector<Command*>::reverse_iterator it; vector<Command*>::reverse_iterator it;

View file

@ -23,17 +23,21 @@
#include <vector> #include <vector>
#include "command.h"
#include "logging.h" #include "logging.h"
using namespace std; using namespace std;
class Command;
/** /**
* This class implements both the Command and Composite design patterns. * This class encapsulates commands, and almost behaves as one itself.
* It encapsulates commands, while still behaving like one. * 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(); DEFINE_LOGGER();
@ -55,9 +59,8 @@ class TurnCmd: public Command
virtual wstring toString() const; virtual wstring toString() const;
protected: void execute();
virtual void doExecute(); void undo();
virtual void doUndo();
private: private:
vector<Command *> m_commands; vector<Command *> m_commands;

View file

@ -156,7 +156,7 @@ void XmlWriter::write(const Game &iGame, const string &iFileName)
#if 0 #if 0
iGame.getNavigation().print(); iGame.getNavigation().print();
#endif #endif
const vector<TurnCmd *> &turnCmdVect = iGame.getNavigation().getCommands(); const vector<TurnCmd *> &turnCmdVect = iGame.getNavigation().getTurns();
BOOST_FOREACH(const TurnCmd *turn, turnCmdVect) BOOST_FOREACH(const TurnCmd *turn, turnCmdVect)
{ {
if (turn->getCommands().empty() && turn == turnCmdVect.back()) if (turn->getCommands().empty() && turn == turnCmdVect.back())