Elevate Engine 1
Loading...
Searching...
No Matches
Command.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <stack>
5#include <memory>
6#include <utility>
7
8namespace Elevate
9{
10 class Command
11 {
12 public:
13 virtual ~Command() = default;
14 virtual void Execute() = 0;
15 virtual void Undo() = 0;
16 virtual bool IsUndoable() const { return true; }
17 };
18
20 class MacroCommand : public Command
21 {
22 public:
23 MacroCommand(std::vector<std::unique_ptr<Command>>&& commands) : m_commands(std::move(commands)) { }
24
25 inline virtual void Execute() override {
26 for (auto it = m_commands.begin(); it != m_commands.end(); ++it) {
27 (*it)->Execute();
28 }
29 }
30 inline virtual void Undo() override {
31 for (auto it = m_commands.rbegin(); it != m_commands.rend(); ++it) {
32 (*it)->Undo();
33 }
34 }
35 private:
36 std::vector<std::unique_ptr<Command>> m_commands;
37 };
38
40 {
41 public:
42 inline void PushCommand(std::unique_ptr<Command> command)
43 {
44 m_executeStack.push(std::move(command));
45 }
46 protected:
47 void ExecuteStack();
48 void Execute(std::unique_ptr<Command> command);
49
50 void Undo();
51 void Redo();
52 private:
53 std::stack<std::unique_ptr<Command>> m_executeStack;
54 std::stack<std::unique_ptr<Command>> m_undoStack;
55 std::stack<std::unique_ptr<Command>> m_redoStack;
56 };
57}
void PushCommand(std::unique_ptr< Command > command)
Definition Command.h:42
void Execute(std::unique_ptr< Command > command)
Definition Command.cpp:16
virtual ~Command()=default
virtual void Execute()=0
virtual void Undo()=0
virtual bool IsUndoable() const
Definition Command.h:16
Class to combine multiples commands to execute then or undo them in batches.
Definition Command.h:21
virtual void Undo() override
Definition Command.h:30
virtual void Execute() override
Definition Command.h:25
MacroCommand(std::vector< std::unique_ptr< Command > > &&commands)
Definition Command.h:23