Shadowrun: Awakened 29 September 2011 - Build 871
Public Member Functions | Protected Member Functions | Protected Attributes
CommandInvoker Class Reference

#include <Command.h>

List of all members.

Public Member Functions

 CommandInvoker ()
template<class T >
TgetInvoker ()
virtual ICommandgetLastCommand ()
template<class T >
TgetLastCommand ()
__inline bool getRedoEnabled () const
__inline bool getUndoEnabled () const
bool isRedoAvailable ()
bool isUndoAvailable ()
virtual void redo ()
void setRedoEnabled (bool val)
void setUndoEnabled (bool val)
virtual void submitCommand (ICommand *command)
virtual void undo ()
virtual ~CommandInvoker ()

Protected Member Functions

void addCommandToUndo (ICommand *command)
void clearRedo ()
void clearUndo ()

Protected Attributes

std::list< ICommand * > _redoCommands
bool _redoEnabled
std::list< ICommand * > _undoCommands
bool _undoEnabled

Detailed Description

This class implements part of a command pattern http://en.wikipedia.org/wiki/Command_pattern Defines a class that invokes commands and tracks them for undo/redo

Definition at line 82 of file Command.h.


Constructor & Destructor Documentation

CommandInvoker::CommandInvoker ( )

Constructor

Definition at line 137 of file Command.cpp.

                              :
    _undoEnabled(true),
    _redoEnabled(true)
{
}
CommandInvoker::~CommandInvoker ( ) [virtual]

Destructor

Definition at line 146 of file Command.cpp.

References clearRedo(), and clearUndo().

{
    clearUndo();
    clearRedo();
}

Member Function Documentation

void CommandInvoker::addCommandToUndo ( ICommand command) [protected]

Adds a command to the undo list This also maintains the undo list's maximum size

Definition at line 121 of file Command.cpp.

References _undoCommands, _undoEnabled, and MAX_UNDO_COUNT.

Referenced by redo(), and submitCommand().

{
    //if undo is not enabled, then we cannot add this
    if(!_undoEnabled)
        return;

    //add command to list
    _undoCommands.push_back(command);
    //if list is too long now, trim it down
    if(_undoCommands.size() > MAX_UNDO_COUNT)
        _undoCommands.pop_front();
}
void CommandInvoker::clearRedo ( ) [protected]

Clears the list of commands able to be redone This frees their associated memory as well

Definition at line 109 of file Command.cpp.

References _redoCommands, and FOREACH.

Referenced by setRedoEnabled(), submitCommand(), and ~CommandInvoker().

{
    FOREACH(iter, std::list<ICommand*>, _redoCommands)
        delete *iter;
    
    _redoCommands.clear();
}
void CommandInvoker::clearUndo ( ) [protected]

Clears the list of commands able to be undone This frees their associated memory as well

Definition at line 97 of file Command.cpp.

References _undoCommands, and FOREACH.

Referenced by setUndoEnabled(), and ~CommandInvoker().

{
    FOREACH(iter, std::list<ICommand*>, _undoCommands)
        delete *iter;
    
    _undoCommands.clear();
}
template<class T >
T* CommandInvoker::getInvoker ( ) [inline]

Gets this this command invoker as the given class This is a convenience function for commands designed

Definition at line 128 of file Command.h.

References T.

    {
        return dynamic_cast<T*>(this);
    }
template<class T >
ICommand * CommandInvoker::getLastCommand ( ) [inline]

Gets the last command submitted to this invoker This returns it dymanically case into the template type

Gets the last command executed by this invoker

Definition at line 112 of file Command.h.

References T.

Referenced by submitCommand(), and undo().

    {
        ICommand* command = getLastCommand();
        return dynamic_cast<T*>(command);
    }
virtual ICommand* CommandInvoker::getLastCommand ( ) [virtual]

Gets the last command submitted to this invoker

__inline bool CommandInvoker::getRedoEnabled ( ) const [inline]

Definition at line 165 of file Command.h.

__inline bool CommandInvoker::getUndoEnabled ( ) const [inline]

Definition at line 161 of file Command.h.

bool CommandInvoker::isRedoAvailable ( )

Returns true if the redo function can do any work

Checks if it is possible to perform a redo

Definition at line 241 of file Command.cpp.

References _redoCommands.

{
    return _redoCommands.size() != 0;
}
bool CommandInvoker::isUndoAvailable ( )

Returns true if the undo function can do any work

Checks if it is possible to perform an undo

Definition at line 233 of file Command.cpp.

References _undoCommands.

{
    return _undoCommands.size() != 0;
}
void CommandInvoker::redo ( ) [virtual]

Gets the last command that was undone and calls execute on it

Executes the last command that was undone

Definition at line 213 of file Command.cpp.

References _redoCommands, _redoEnabled, addCommandToUndo(), and ICommand::execute().

{
    //ensure there is a command to redo
    if(_redoCommands.size() == 0 || !_redoEnabled)
        return;

    // Get the last command from the list
    ICommand* lastCommandUndone = _redoCommands.back();
    _redoCommands.pop_back();

    //execute it 
    lastCommandUndone->execute();

    //then put it back on the undo list
    addCommandToUndo(lastCommandUndone);
}
void CommandInvoker::setRedoEnabled ( bool  enabled)

Sets redo as enabled/disabled when redo is disable, the redo list is emptied

Definition at line 261 of file Command.cpp.

References _redoEnabled, and clearRedo().

{
    _redoEnabled = enabled;
    if(!_redoEnabled)
        clearRedo();
}
void CommandInvoker::setUndoEnabled ( bool  enabled)

Sets undo as enabled/disabled when undo is disable, the undo list is emptied

Definition at line 250 of file Command.cpp.

References _undoEnabled, and clearUndo().

{
    _undoEnabled = enabled;
    if(!_undoEnabled)
        clearUndo();
}
void CommandInvoker::submitCommand ( ICommand command) [virtual]

Submits a command to be executed by the invoker If undo is enabled, then this command is saved

Submits a command to this invoker from the outside This also clears the list of commands able to be redone NOTE: This invoker assumes memory management of the submitted command

Definition at line 168 of file Command.cpp.

References addCommandToUndo(), clearRedo(), ICommand::execute(), ICommand::executeCumulative(), getLastCommand(), and ICommand::isCumulative().

{
    //check if the command is cumulative
    if(command->isCumulative(getLastCommand()))
    {
        //if command is cumulative, then execute as cumulative
        //then delete because necessary state is now in last command
        command->executeCumulative(getLastCommand());
        delete command;
    }
    else
    {
        //if command is non-cumulative, then execute and add to undo
        command->execute();
        addCommandToUndo(command);      
    }

    //submitting a new command always invalidates redo
    clearRedo();
}
void CommandInvoker::undo ( ) [virtual]

Gets the last command submitted and calls undo on it

Undoes the last command

Definition at line 192 of file Command.cpp.

References _redoCommands, _redoEnabled, _undoCommands, _undoEnabled, getLastCommand(), and ICommand::undo().

{
    //ensure there is a command to undo
    if(_undoCommands.size() == 0 || !_undoEnabled)
        return;

    //get the last command
    ICommand* lastCommand = getLastCommand();
    _undoCommands.pop_back();

    //undo the command
    lastCommand->undo();

    //save to redo list
    if(_redoEnabled)
        _redoCommands.push_back(lastCommand);
}

Member Data Documentation

std::list<ICommand*> CommandInvoker::_redoCommands [protected]

History of previously undone commands

Definition at line 88 of file Command.h.

Referenced by clearRedo(), isRedoAvailable(), redo(), and undo().

Definition at line 96 of file Command.h.

Referenced by redo(), setRedoEnabled(), and undo().

std::list<ICommand*> CommandInvoker::_undoCommands [protected]

History of submitted commands

Definition at line 93 of file Command.h.

Referenced by addCommandToUndo(), clearUndo(), isUndoAvailable(), and undo().

Definition at line 95 of file Command.h.

Referenced by addCommandToUndo(), setUndoEnabled(), and undo().


The documentation for this class was generated from the following files:

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net