-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundo.h
71 lines (55 loc) · 1.54 KB
/
undo.h
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
#ifndef UNDO_H
#define UNDO_H
/*
* Based on "Inheritance is the Base Class of Evil"
* https://www.youtube.com/watch?v=bIhUE5uUFOA
*/
#include <iostream>
#include <memory>
#include <string>
#include <vector>
template <typename T>
void draw(const T &t, std::ostream &out, size_t position);
class object_t {
public:
template <typename T>
object_t(T x) : self_(std::make_shared<model<T>>(std::move(x))) {}
friend void draw(const object_t &x, std::ostream &out, size_t position) {
x.self_->draw_(out, position);
}
private:
struct concept_t {
virtual ~concept_t() = default;
virtual void draw_(std::ostream &out, size_t position) const = 0;
};
template <typename T>
struct model : concept_t {
model(T x) : data_(std::move(x)) {}
void draw_(std::ostream &out, size_t position) const {
draw(data_, out, position);
}
T data_;
};
std::shared_ptr<const concept_t> self_;
};
using document_t = std::vector<object_t>;
using history_t = std::vector<document_t>;
void commit(history_t &x) {
assert(x.size() && "no history to commit yet");
x.push_back(x.back());
}
void undo(history_t &x) {
assert(x.size() && "no history to undo yet");
x.pop_back();
}
document_t ¤t(history_t &x) {
assert(x.size());
return x.back();
}
template <>
void draw(const document_t &x, std::ostream &out, size_t position) {
out << std::string(position, ' ') << "<document>" << std::endl;
for (const auto &e : x) draw(e, out, position + 2);
out << std::string(position, ' ') << "</document>" << std::endl;
}
#endif