summaryrefslogtreecommitdiffstats
path: root/src/composite-undo-stack-observer.h
blob: daf73b976d02a78b4be7f2081ac050f98edf62b6 (plain)
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 * David Yip <yipdw@rose-hulman.edu>
 *
 * Copyright (c) 2005 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#ifndef SEEN_COMPOSITE_UNDO_COMMIT_OBSERVER_H
#define SEEN_COMPOSITE_UNDO_COMMIT_OBSERVER_H

#include "inkgc/gc-alloc.h"
#include "undo-stack-observer.h"

#include <list>

namespace Inkscape {

struct Event;

/**
 * Aggregates UndoStackObservers for management and triggering in an SPDocument's undo/redo
 * system.
 *
 * Heavily inspired by Inkscape::XML::CompositeNodeObserver.
 */
class CompositeUndoStackObserver : public UndoStackObserver {
public:

	/**
	 * Structure for tracking UndoStackObservers.
	 */
	struct UndoStackObserverRecord {
	public:
		/**
		 * Constructor.
		 *
		 * \param o Reference to the UndoStackObserver that this UndoStackObserverRecord
		 * will track.
		 */
		UndoStackObserverRecord(UndoStackObserver& o) : to_remove(false), _observer(o) { }
		bool to_remove;

		/**
		 * Overloaded equality test operator to facilitate usage of STL find algorithms.
		 */
		bool operator==(UndoStackObserverRecord const& _x) const
		{
			return &(this->_observer) == &(_x._observer);
		}

		/**
		 * Issue a redo event to the UndoStackObserver that is associated with this UndoStackObserverRecord.
		 *
		 * \param log The event log generated by the redo event.
		 */
		void issueRedo(Event* log)
		{
			this->_observer.notifyRedoEvent(log);
		}

		/**
		 * Issue an undo event to the UndoStackObserver that is associated with this
		 * UndoStackObserverRecord.
		 *
		 * \param log The event log generated by the undo event.
		 */
		void issueUndo(Event* log)
		{
			this->_observer.notifyUndoEvent(log);
		}

		/**
		 * Issues a committed event to the UndoStackObserver that is associated with this
		 * UndoStackObserverRecord.
		 *
		 * \param log The event log being committed to the undo stack.
		 */
		void issueUndoCommit(Event* log)
		{
			this->_observer.notifyUndoCommitEvent(log);
		}

		/**
		 * Issue a clear undo event to the UndoStackObserver
		 * that is associated with this
		 * UndoStackObserverRecord.
		 */
		void issueClearUndo()
		{
			this->_observer.notifyClearUndoEvent();
		}
	     
		/**
		 * Issue a clear redo event to the UndoStackObserver
		 * that is associated with this
		 * UndoStackObserverRecord.
		 */
		void issueClearRedo()
		{
			this->_observer.notifyClearRedoEvent();
		}

	private:
		UndoStackObserver& _observer;
	};

	/// A list of UndoStackObserverRecords, used to aggregate multiple UndoStackObservers.
	typedef std::list< UndoStackObserverRecord, GC::Alloc< UndoStackObserverRecord, GC::MANUAL > > UndoObserverRecordList;

	/**
	 * Constructor.
	 */
	CompositeUndoStackObserver();

    ~CompositeUndoStackObserver() override;

	/**
	 * Add an UndoStackObserver.
	 *
	 * \param observer Reference to an UndoStackObserver to add.
	 */
	void add(UndoStackObserver& observer);

	/**
	 * Remove an UndoStackObserver.
	 *
	 * \param observer Reference to an UndoStackObserver to remove.
	 */
	void remove(UndoStackObserver& observer);

	/**
	 * Notify all registered UndoStackObservers of an undo event.
	 *
	 * \param log The event log generated by the undo event.
	 */
	void notifyUndoEvent(Event* log) override;

	/**
	 * Notify all registered UndoStackObservers of a redo event.
	 *
	 * \param log The event log generated by the redo event.
	 */
	void notifyRedoEvent(Event* log) override;

	/**
	 * Notify all registered UndoStackObservers of an event log being committed to the undo stack.
	 *
	 * \param log The event log being committed to the undo stack.
	 */
	void notifyUndoCommitEvent(Event* log) override;

	void notifyClearUndoEvent() override;
	void notifyClearRedoEvent() override;

private:
	// Remove an observer from a given list
	bool _remove_one(UndoObserverRecordList& list, UndoStackObserver& rec);

	// Mark an observer for removal from a given list
	bool _mark_one(UndoObserverRecordList& list, UndoStackObserver& rec);

	// Keep track of whether or not we are notifying observers
	unsigned int _iterating;

	// Observers in the active list
	UndoObserverRecordList _active;

	// Observers to be added
	UndoObserverRecordList _pending;

	// Prevents the observer vector from modifications during 
	// iteration through the vector
	void _lock() { this->_iterating++; }
	void _unlock();
};

}

#endif // SEEN_COMPOSITE_UNDO_COMMIT_OBSERVER_H