forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_access.h
More file actions
237 lines (194 loc) · 5.95 KB
/
document_access.h
File metadata and controls
237 lines (194 loc) · 5.95 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Aseprite
// Copyright (C) 2001-2016 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_DOCUMENT_ACCESS_H_INCLUDED
#define APP_DOCUMENT_ACCESS_H_INCLUDED
#pragma once
#include "base/exception.h"
#include "app/context.h"
#include "app/document.h"
#include <exception>
namespace app {
// TODO remove exceptions and use "DocumentAccess::operator bool()"
class LockedDocumentException : public base::Exception {
public:
LockedDocumentException(const char* msg) throw()
: base::Exception(msg) { }
};
class CannotReadDocumentException : public LockedDocumentException {
public:
CannotReadDocumentException() throw()
: LockedDocumentException("Cannot read the sprite.\n"
"It is being modified by another command.\n"
"Try again.") { }
};
class CannotWriteDocumentException : public LockedDocumentException {
public:
CannotWriteDocumentException() throw()
: LockedDocumentException("Cannot modify the sprite.\n"
"It is being used by another command.\n"
"Try again.") { }
};
// This class acts like a wrapper for the given document. It's
// specialized by DocumentReader/Writer to handle document read/write
// locks.
class DocumentAccess {
public:
DocumentAccess() : m_document(NULL) { }
DocumentAccess(const DocumentAccess& copy) : m_document(copy.m_document) { }
explicit DocumentAccess(Document* document) : m_document(document) { }
~DocumentAccess() { }
DocumentAccess& operator=(const DocumentAccess& copy)
{
m_document = copy.m_document;
return *this;
}
operator Document* () { return m_document; }
operator const Document* () const { return m_document; }
Document* operator->()
{
ASSERT(m_document != NULL);
return m_document;
}
const Document* operator->() const
{
ASSERT(m_document != NULL);
return m_document;
}
protected:
Document* m_document;
};
// Class to view the document's state. Its constructor request a
// reader-lock of the document, or throws an exception in case that
// the lock cannot be obtained.
class DocumentReader : public DocumentAccess {
public:
DocumentReader() {
}
explicit DocumentReader(Document* document, int timeout)
: DocumentAccess(document) {
if (m_document && !m_document->lock(Document::ReadLock, timeout))
throw CannotReadDocumentException();
}
explicit DocumentReader(const DocumentReader& copy, int timeout)
: DocumentAccess(copy) {
if (m_document && !m_document->lock(Document::ReadLock, timeout))
throw CannotReadDocumentException();
}
~DocumentReader() {
unlock();
}
protected:
void unlock() {
if (m_document) {
m_document->unlock();
m_document = nullptr;
}
}
private:
// Disable operator=
DocumentReader& operator=(const DocumentReader&);
};
// Class to modify the document's state. Its constructor request a
// writer-lock of the document, or throws an exception in case that
// the lock cannot be obtained. Also, it contains a special
// constructor that receives a DocumentReader, to elevate the
// reader-lock to writer-lock.
class DocumentWriter : public DocumentAccess {
public:
DocumentWriter()
: m_from_reader(false)
, m_locked(false) {
}
explicit DocumentWriter(Document* document, int timeout)
: DocumentAccess(document)
, m_from_reader(false)
, m_locked(false) {
if (m_document) {
if (!m_document->lock(Document::WriteLock, timeout))
throw CannotWriteDocumentException();
m_locked = true;
}
}
// Constructor that can be used to elevate the given reader-lock to
// writer permission.
explicit DocumentWriter(const DocumentReader& document, int timeout)
: DocumentAccess(document)
, m_from_reader(true)
, m_locked(false) {
if (m_document) {
if (!m_document->upgradeToWrite(timeout))
throw CannotWriteDocumentException();
m_locked = true;
}
}
~DocumentWriter() {
unlock();
}
protected:
void unlock() {
if (m_document && m_locked) {
if (m_from_reader)
m_document->downgradeToRead();
else
m_document->unlock();
m_document = nullptr;
m_locked = false;
}
}
private:
bool m_from_reader;
bool m_locked;
// Non-copyable
DocumentWriter(const DocumentWriter&);
DocumentWriter& operator=(const DocumentWriter&);
DocumentWriter& operator=(const DocumentReader&);
};
// Used to destroy the active document in the context.
class DocumentDestroyer : public DocumentWriter {
public:
explicit DocumentDestroyer(Context* context, Document* document, int timeout)
: DocumentWriter(document, timeout) {
}
void destroyDocument() {
ASSERT(m_document != NULL);
m_document->close();
Document* doc = m_document;
unlock();
delete doc;
m_document = nullptr;
}
};
class WeakDocumentReader : public DocumentAccess {
public:
WeakDocumentReader() {
}
explicit WeakDocumentReader(Document* doc)
: DocumentAccess(doc)
, m_weak_lock(RWLock::WeakUnlocked) {
if (m_document)
m_document->weakLock(&m_weak_lock);
}
~WeakDocumentReader() {
weakUnlock();
}
bool isLocked() const {
return (m_weak_lock == RWLock::WeakLocked);
}
protected:
void weakUnlock() {
if (m_document && m_weak_lock != RWLock::WeakUnlocked) {
m_document->weakUnlock();
m_document = nullptr;
}
}
private:
// Disable operator=
WeakDocumentReader(const WeakDocumentReader&);
WeakDocumentReader& operator=(const WeakDocumentReader&);
RWLock::WeakLock m_weak_lock;
};
} // namespace app
#endif