-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathdocument_access.h
More file actions
190 lines (157 loc) · 4.63 KB
/
document_access.h
File metadata and controls
190 lines (157 loc) · 4.63 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
// Aseprite
// Copyright (C) 2001-2015 David Capello
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
#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 {
class LockedDocumentException : public base::Exception {
public:
LockedDocumentException() throw()
: base::Exception("Cannot read or write the active document.\n"
"It is locked by a background task.\n"
"Try again later.") { }
};
// 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 LockedDocumentException();
}
explicit DocumentReader(const DocumentReader& copy, int timeout)
: DocumentAccess(copy)
{
if (m_document && !m_document->lock(Document::ReadLock, timeout))
throw LockedDocumentException();
}
~DocumentReader()
{
// unlock the document
if (m_document)
m_document->unlock();
}
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 LockedDocumentException();
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->lockToWrite(timeout))
throw LockedDocumentException();
m_locked = true;
}
}
~DocumentWriter()
{
unlockWriter();
}
protected:
void unlockWriter()
{
if (m_document && m_locked) {
if (m_from_reader)
m_document->unlockToRead();
else
m_document->unlock();
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();
unlockWriter();
delete m_document;
m_document = NULL;
}
};
} // namespace app
#endif