/
iundo.h
129 lines (110 loc) · 3.43 KB
/
iundo.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
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
#pragma once
/// \file
/// \brief The undo-system interface. Uses the 'memento' pattern.
#include "imodule.h"
#include <cstddef>
#include <memory>
#include <sigc++/signal.h>
class IMapFileChangeTracker;
/**
* greebo: An UndoMemento has to be allocated on the heap
* and contains all the information that is needed to describe
* the status of an Undoable.
*/
class IUndoMemento
{
public:
virtual ~IUndoMemento() {}
};
typedef std::shared_ptr<IUndoMemento> IUndoMementoPtr;
/* greebo: This is the abstract base class for an Undoable object.
* Derive from this class if your instance/object should be Undoable.
*
* The exportState method has to allocate and return a new UndoMemento
* with all the necessary data to restore the current state.
*
* The importState() method should re-import the values saved in the
* UndoMemento
*/
class IUndoable
{
public:
virtual ~IUndoable() {}
virtual IUndoMementoPtr exportState() const = 0;
virtual void importState(const IUndoMementoPtr& state) = 0;
};
/**
* Undoables request their associated StateSaver to save their current state.
* The state saver might call the Undoable's exportState() method or not,
* depending on whether the Undoable has already been saved during
* the current operation's lifetime.
* To acquire an UndoStateSaver, use UndoSystem::getStateSaver().
*/
class IUndoStateSaver
{
public:
virtual ~IUndoStateSaver() {}
virtual void save(IUndoable& undoable) = 0;
};
const std::string MODULE_UNDOSYSTEM("UndoSystem");
class UndoSystem :
public RegisterableModule
{
public:
// Undoable objects need to call this to get hold of a StateSaver instance
// which will take care of exporting and saving the state. The passed map file change
// tracker will be notified when the state is saved.
virtual IUndoStateSaver* getStateSaver(IUndoable& undoable, IMapFileChangeTracker& tracker) = 0;
virtual void releaseStateSaver(IUndoable& undoable) = 0;
virtual std::size_t size() const = 0;
virtual void start() = 0;
virtual void finish(const std::string& command) = 0;
virtual void undo() = 0;
virtual void redo() = 0;
virtual void clear() = 0;
// Emitted after an undo operation is fully completed, allows objects to refresh their state
virtual sigc::signal<void>& signal_postUndo() = 0;
// Emitted after a redo operation is fully completed, allows objects to refresh their state
virtual sigc::signal<void>& signal_postRedo() = 0;
// greebo: This finishes the current operation and removes
// it immediately from the stack, therefore it never existed.
virtual void cancel() = 0;
/**
* Observer implementation which gets notified
* on undo/redo perations.
*/
class Tracker
{
public:
virtual ~Tracker() {}
virtual void clear() = 0;
virtual void begin() = 0;
virtual void undo() = 0;
virtual void redo() = 0;
};
virtual void attachTracker(Tracker& tracker) = 0;
virtual void detachTracker(Tracker& tracker) = 0;
};
// The accessor function
inline UndoSystem& GlobalUndoSystem() {
// Cache the reference locally
static UndoSystem& _undoSystem(
*std::static_pointer_cast<UndoSystem>(
module::GlobalModuleRegistry().getModule(MODULE_UNDOSYSTEM)
)
);
return _undoSystem;
}
class UndoableCommand
{
const std::string _command;
public:
UndoableCommand(const std::string& command) :
_command(command)
{
GlobalUndoSystem().start();
}
~UndoableCommand() {
GlobalUndoSystem().finish(_command);
}
};