1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include <iostream>
#include <string>
#include <memory>
#include <sstream>
#include <vector>
#include <list>
#include <cstdlib>
#include <ctime>
template <typename T>
std::string toString (const T& t) {
std::stringstream ss;
ss << t;
return ss.str();
}
class LivingBeing {
public:
class Memento {
private:
std::unique_ptr<LivingBeing> copy;
public:
Memento(): copy (nullptr) {}
Memento (const LivingBeing* being): copy (being->clone()) {} // calls up the move assignment operator
Memento (Memento&& other) noexcept: copy (std::move (other.copy)) {
other.copy.reset();
}
Memento& operator = (Memento&& other) noexcept {
if (this == &other)
return *this;
copy = std::move (other.copy);
other.copy.reset();
return *this;
}
friend class LivingBeing;
};
virtual ~LivingBeing() noexcept = default;
virtual void change() = 0;
virtual void display() const = 0;
virtual Memento createMemento() const {return Memento (this);}
virtual LivingBeing& restoreMemento (const Memento& memento) {return assign (*memento.copy);}
private:
virtual std::unique_ptr<LivingBeing> clone() const = 0;
virtual LivingBeing& assign (const LivingBeing&) = 0 ;
};
template <typename DERIVED>
class LivingBeingCRTP: public LivingBeing {
protected:
virtual std::unique_ptr<LivingBeing> clone() const override {
return std::unique_ptr<LivingBeing>(new DERIVED (static_cast<const DERIVED&>(*this)));
}
virtual LivingBeing& assign (const LivingBeing& being) override { // return LivingBeing& because may throw std::bad_cast
return static_cast<DERIVED&>(*this) = dynamic_cast<const DERIVED&>(being);
}
};
class Fighter: public LivingBeingCRTP<Fighter> {
private:
int i = 0;
double d = 0;
public:
virtual void change() override {i++; d += (float)(std::rand() % 10) / 10;}
virtual void display() const override {std::cout << "Fighter {" + toString(i) + ", " + toString(d) + "}" << std::endl;}
};
class MagicUser: public LivingBeingCRTP<MagicUser> {
private:
std::string name;
public:
virtual void change() override {name = toString (std::rand() % 10) + toString ((float)(std::rand() % 10) / 10);}
virtual void display() const override {std::cout << "MagicUser {" + name + "}" << std::endl;}
};
class Command {
private:
typedef void (LivingBeing::*Action)();
std::vector<LivingBeing*> receivers;
Action action;
static int numReceivers;
static std::vector<std::vector<Command*>> commandList;
static std::vector<std::vector<LivingBeing::Memento>> mementoList;
static std::size_t numCommands;
static std::size_t maxCommands;
public:
Command (const std::vector<LivingBeing*>& newReceivers, Action newAction): receivers (newReceivers), action (newAction) {
numReceivers = receivers.size();
}
virtual void execute() {
if (mementoList.size() < numCommands + 1)
{
mementoList.resize (numCommands + 1);
mementoList[numCommands].resize (numReceivers);
}
for (int i = 0; i < numReceivers; ++i)
mementoList[numCommands][i] = receivers[i]->createMemento();
if (commandList.size() < numCommands + 1)
{
commandList.resize (numCommands + 1);
commandList[numCommands].resize (numReceivers);
}
for (int i = 0; i < numReceivers; ++i)
commandList[numCommands][i] = this;
if (numCommands > maxCommands)
maxCommands = numCommands;
numCommands++;
for (LivingBeing* x: receivers)
(x->*action)();
if (mementoList.size() < numCommands + 1)
{
mementoList.resize (numCommands + 1);
mementoList[numCommands].resize (numReceivers);
}
if (commandList.size() < numCommands + 1)
{
commandList.resize (numCommands + 1);
commandList[numCommands].resize (numReceivers);
}
for (int i = 0; i < numReceivers; ++i)
{
mementoList[numCommands][i] = receivers[i]->createMemento();
commandList[numCommands][i] = this;
}
}
static void undo() {
if (numCommands == 0)
{
std::cout << std::endl << "[There is nothing to undo at this point.]" << std::endl << std::endl;
return;
}
for (int i = 0; i < numReceivers; ++i)
commandList[numCommands - 1][i]->receivers[i]->restoreMemento (mementoList[numCommands - 1][i]);
numCommands--;
}
void static redo() {
if (numCommands > maxCommands)
{
std::cout << std::endl << "[There is nothing to redo at this point.]" << std::endl << std::endl;
return ;
}
for (int i = 0; i < numReceivers; ++i)
commandList[numCommands + 1][i]->receivers[i]->restoreMemento (mementoList[numCommands + 1][i]);
numCommands++;
}
};
int Command::numReceivers;
std::vector<std::vector<Command*>> Command::commandList;
std::vector<std::vector<LivingBeing::Memento>> Command::mementoList;
std::size_t Command::numCommands = 0;
std::size_t Command::maxCommands = 0;
int main() {
srand (std::time (nullptr));
int i;
std::vector<LivingBeing*> players = {new Fighter, new MagicUser, new Fighter, new Fighter, new MagicUser};
Command *commands[2];
commands[1] = new Command (players, &LivingBeing::change); // add whatever other types of commands from LivingBeing functions
for (const LivingBeing* x: players)
x->display();
std::cout << std::endl << "0.Exit, 1.Change, 2.Undo, 3.Redo: ";
std::cin >> i;
while (i != 0)
{
if (i < 0 || i > 3)
{
std::cout << "Enter a proper choice: ";
std::cin >> i;
continue;
}
else if (i == 2)
Command::undo();
else if (i == 3)
Command::redo();
else
commands[i]->execute();
for (const LivingBeing* x: players)
x->display();
std::cout << std::endl << "0.Exit, 1.Change, 2.Undo, 3.Redo: ";
std::cin >> i;
}
}
|