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

#include <Command.h>

Inheritance diagram for CompositeCommand:

List of all members.

Public Member Functions

void addCommand (ICommand *command)
virtual void execute ()
virtual void executeCumulative (ICommand *command)
virtual bool isCumulative (ICommand *command)
virtual void undo ()
virtual ~CompositeCommand ()

Protected Attributes

std::list< ICommand * > _commands

Detailed Description

This class implement ICommand on a list of child commands When executing this command, all children are executed in the order they were added When undoing this command, all children are undone in the order they were added

Definition at line 58 of file Command.h.


Constructor & Destructor Documentation

CompositeCommand::~CompositeCommand ( ) [virtual]

Destructor

Definition at line 9 of file Command.cpp.

References _commands, and FOREACH.

{
    //we must delete all child commands when this is being deleted
    FOREACH(iter, std::list<ICommand*>, _commands)
        delete *iter;
}

Member Function Documentation

void CompositeCommand::addCommand ( ICommand command)

Adds a command to the collection

Definition at line 19 of file Command.cpp.

References _commands.

{
    _commands.push_back(command);
}
void CompositeCommand::execute ( ) [virtual]

Executes all children of this command NOTE: this always returns true

Implements ICommand.

Definition at line 78 of file Command.cpp.

References _commands, and FOREACH.

{
    FOREACH(iter, std::list<ICommand*>, _commands)
        (*iter)->execute();
}
void CompositeCommand::executeCumulative ( ICommand command) [virtual]

Applies cumulative changes in this object to the given object

Reimplemented from ICommand.

Definition at line 58 of file Command.cpp.

References _commands.

{
    CompositeCommand* cmd = dynamic_cast<CompositeCommand*>(command);

    //go through all the commands between each composite
    std::list<ICommand*>::const_iterator myIter = _commands.begin();
    std::list<ICommand*>::const_iterator theirIter = cmd->_commands.begin();
    while(myIter != _commands.end())
    {
        //apply from this object to the given objects
        (*myIter)->executeCumulative(*theirIter);
        ++myIter;
        ++theirIter;
    }
}
bool CompositeCommand::isCumulative ( ICommand command) [virtual]

Checks if all commands in this object are cumulative

Reimplemented from ICommand.

Definition at line 27 of file Command.cpp.

References _commands.

{
    //the command must be a composite
    CompositeCommand* cmd = dynamic_cast<CompositeCommand*>(command);
    if(cmd == NULL)
        return false;

    //it must contain the same number of commands
    if(cmd->_commands.size() != _commands.size())
        return false;

    //each command must be equivalent to its matching command
    std::list<ICommand*>::const_iterator myIter = _commands.begin();
    std::list<ICommand*>::const_iterator theirIter = cmd->_commands.begin();
    while(myIter != _commands.end())
    {
        //if they do not match, reject
        if(!((*myIter)->isCumulative(*theirIter)))
            return false;
        //otherwise move on
        ++myIter;
        ++theirIter;
    }

    //if all of the above is met, they must be cumulative
    return true;
}
void CompositeCommand::undo ( ) [virtual]

Undoes all children of this command

Implements ICommand.

Definition at line 87 of file Command.cpp.

References _commands, and FOREACH.

{
    FOREACH(iter, std::list<ICommand*>, _commands)
        (*iter)->undo();
}

Member Data Documentation

std::list<ICommand*> CompositeCommand::_commands [protected]

The list of child commands

Definition at line 64 of file Command.h.

Referenced by addCommand(), execute(), executeCumulative(), isCumulative(), undo(), and ~CompositeCommand().


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