Skip to content

Instantly share code, notes, and snippets.

@grodtron
Created November 14, 2012 00:20

Revisions

  1. Gordon Bailey (iMac-OSX) revised this gist Nov 14, 2012. 2 changed files with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    *.o
    *.swp
    *.swo
    *.zip
    test
    Binary file removed command_pattern.zip
    Binary file not shown.
  2. Gordon Bailey (iMac-OSX) revised this gist Nov 14, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion deleteme.txt
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    s
  3. Gordon Bailey (iMac-OSX) revised this gist Nov 14, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions deleteme.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    s
  4. grodtron created this gist Nov 14, 2012.
    1 change: 1 addition & 0 deletions deleteme.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    s
  5. Gordon Bailey (iMac-OSX) revised this gist Nov 14, 2012. 11 changed files with 264 additions and 2 deletions.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    *.o
    *.swp
    *.swo
    test
    11 changes: 11 additions & 0 deletions Command.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #ifndef COMMAND_H_
    #define COMMAND_H_

    class Command {
    public:
    virtual void doit() = 0;
    virtual void undo() = 0;
    virtual ~Command(){}
    };

    #endif
    44 changes: 44 additions & 0 deletions CommandStack.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    /*
    * CommandStack.cpp
    *
    * Created on: 05.11.2012
    * Author: dsinnig
    */

    #include "CommandStack.h"
    #include <iostream>


    CommandStack::CommandStack (int size) : size(size), currentNumberOfCommands(0) {
    commands = new Command*[size];
    }

    CommandStack::~CommandStack() {
    clear(); // delete all of the commands
    delete [] commands; // delete the array itself
    }

    Command* CommandStack::pop() {
    return (currentNumberOfCommands > 0) ? commands[--currentNumberOfCommands] : NULL;
    }

    void CommandStack::push(Command* aCommand) {
    if (currentNumberOfCommands < size)
    commands[currentNumberOfCommands++] = aCommand;
    }

    bool CommandStack::empty() {
    return currentNumberOfCommands == 0;
    }

    // Clear the entire stack, deleting all the commands that it held
    void CommandStack::clear(){
    // Loop over all of the commands in the stack
    while(currentNumberOfCommands > 0){
    --currentNumberOfCommands;
    // delete the current command, and set the pointer
    // to NULL (because it's a good habit to do so)
    delete commands[currentNumberOfCommands];
    commands[currentNumberOfCommands] = NULL;
    }
    }
    33 changes: 33 additions & 0 deletions CommandStack.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    /*
    * CommandStack.h
    *
    * Created on: 05.11.2012
    * Author: dsinnig
    */

    #ifndef COMMANDSTACK_H_
    #define COMMANDSTACK_H_

    #include "Command.h"

    class CommandStack {
    public:

    CommandStack (int size);
    virtual ~CommandStack();

    Command* pop();

    void push(Command* aCommand);

    bool empty();

    void clear();

    private:
    int size;
    int currentNumberOfCommands;
    Command** commands;
    };

    #endif /* COMMANDSTACK_H_ */
    29 changes: 29 additions & 0 deletions Commands.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #include "Commands.h"

    AddCommand::AddCommand(int * target, int x)
    : target(target), x(x)
    {

    }

    void AddCommand::doit(){
    *target += x;
    }

    void AddCommand::undo(){
    *target -= x;
    }

    MultCommand::MultCommand(int * target, int x)
    : target(target), x(x)
    {

    }

    void MultCommand::doit(){
    *target *= x;
    }

    void MultCommand::undo(){
    *target /= x;
    }
    29 changes: 29 additions & 0 deletions Commands.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #ifndef COMMANDS_H_
    #define COMMANDS_H_

    #include "Command.h"

    class AddCommand : public Command {
    private:
    int * target;
    int x;

    public:
    AddCommand(int * target, int x);
    void doit();
    void undo();
    };

    class MultCommand : public Command {
    private:
    int * target;
    int x;

    public:
    MultCommand(int * target, int x);
    void doit();
    void undo();
    };


    #endif
    30 changes: 30 additions & 0 deletions Invoker.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #include "Invoker.h"

    Invoker::Invoker()
    : undoStack(16),
    redoStack(16)
    {

    }

    void Invoker::execute(Command * command){
    command->doit();
    undoStack.push(command);
    redoStack.clear();
    }

    void Invoker::undo(){
    if(!undoStack.empty()){
    Command * command = undoStack.pop();
    command->undo();
    redoStack.push(command);
    }
    }

    void Invoker::redo(){
    if(!redoStack.empty()){
    Command * command = redoStack.pop();
    command->doit();
    undoStack.push(command);
    }
    }
    22 changes: 22 additions & 0 deletions Invoker.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #ifndef INVOKER_H_
    #define INVOKER_H_

    #include "CommandStack.h"
    #include "Command.h"

    class Invoker {

    private:
    CommandStack undoStack;
    CommandStack redoStack;

    public:
    Invoker();

    void execute(Command *);
    void undo();
    void redo();

    };

    #endif
    4 changes: 2 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,10 @@
    all: test

    %.o: %.cpp
    g++ -c $< -o $@
    g++ -ggdb -c $< -o $@

    test: $(shell ls -1 *.cpp | sed 's/\.cpp/\.o/')
    g++ $^ -o $@
    g++ -ggdb $^ -o $@

    clean:
    rm -f *.o
    Binary file added command_pattern.zip
    Binary file not shown.
    63 changes: 63 additions & 0 deletions main.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    #include "Commands.h"
    #include "Invoker.h"

    // This function will ask the user for input in a loop
    // and execute commands, or redo/undo them based on that
    // input
    void commandLoop(){

    // The invoker of the commands
    Invoker invoker;

    // the receiver of the commands
    int receiver;

    // initialize the receiver
    cout << "Set initial value to: ";
    cin >> receiver;

    while(1){

    char in;
    int val;

    cout << "Enter command [ '+' (add) | '*' (multiply) | 'u' (undo) | 'r' (redo) ]: ";
    cin >> in;

    switch (in){
    case '+':
    cout << "Enter value: ";
    cin >> val;
    invoker.execute(new AddCommand(&receiver, val));
    break;
    case '*':
    cout << "Enter value: ";
    cin >> val;
    invoker.execute(new MultCommand(&receiver, val));
    break;
    case 'u':
    invoker.undo();
    break;
    case 'r':
    invoker.redo();
    break;
    default:
    // end the loop
    return;
    }

    cout << "== Value is now: " << receiver << endl;
    }
    }

    int main(){

    commandLoop();

    return 0;
    }
  6. Gordon Bailey (iMac-OSX) created this gist Nov 7, 2012.
    3 changes: 3 additions & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    *.o
    *.swp
    *.swo
    13 changes: 13 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    .PHONY: clean all

    all: test

    %.o: %.cpp
    g++ -c $< -o $@

    test: $(shell ls -1 *.cpp | sed 's/\.cpp/\.o/')
    g++ $^ -o $@

    clean:
    rm -f *.o
    rm -f test