/
variable.cpp
322 lines (274 loc) · 7.27 KB
/
variable.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2009-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Variable"
#include "de/Value"
#include "de/NoneValue"
#include "de/NumberValue"
#include "de/TextValue"
#include "de/ArrayValue"
#include "de/DictionaryValue"
#include "de/BlockValue"
#include "de/TimeValue"
#include "de/RecordValue"
#include "de/Reader"
#include "de/Writer"
#include "de/Log"
namespace de {
DE_PIMPL_NOREF(Variable)
{
String name;
/// Value of the variable.
Value *value;
/// Mode flags.
Flags flags;
Impl() : value(nullptr) {}
Impl(Impl const &other)
: de::IPrivate()
, name (other.name)
, value(other.value->duplicate())
, flags(other.flags)
{}
~Impl()
{
delete value;
}
DE_PIMPL_AUDIENCE(Deletion)
DE_PIMPL_AUDIENCE(Change)
DE_PIMPL_AUDIENCE(ChangeFrom)
};
DE_AUDIENCE_METHOD(Variable, Deletion)
DE_AUDIENCE_METHOD(Variable, Change)
DE_AUDIENCE_METHOD(Variable, ChangeFrom)
Variable::Variable(const String &name, Value *initial, Flags const &m)
: d(new Impl)
{
d->name = name;
d->flags = m;
std::unique_ptr<Value> v(initial);
if (!initial)
{
v.reset(new NoneValue);
}
verifyName(d->name);
if (initial)
{
verifyValid(*v);
}
d->value = v.release();
}
Variable::Variable(Variable const &other)
: ISerializable()
, d(new Impl(*other.d))
{}
Variable::~Variable()
{
DE_FOR_AUDIENCE2(Deletion, i) i->variableBeingDeleted(*this);
}
String const &Variable::name() const
{
return d->name;
}
Variable &Variable::operator = (Value *v)
{
set(v);
return *this;
}
Variable &Variable::operator = (String const &textValue)
{
set(new TextValue(textValue));
return *this;
}
Variable &Variable::set(Value *v)
{
DE_ASSERT(v);
std::unique_ptr<Value> val(v);
// If the value would change, must check if this is allowed.
verifyWritable(*v);
verifyValid(*v);
std::unique_ptr<Value> oldValue(d->value); // old value deleted afterwards
d->value = val.release();
d->flags |= ValueHasChanged;
// We'll only determine if actual change occurred if someone is interested.
if (!audienceForChange().isEmpty() || !audienceForChangeFrom().isEmpty())
{
bool notify;
try
{
// Did it actually change? Let's compare...
notify = !oldValue || oldValue->compare(*v);
}
catch (Error const &)
{
// Perhaps the values weren't comparable?
notify = true;
}
if (notify)
{
DE_FOR_AUDIENCE2(Change, i) i->variableValueChanged(*this, *d->value);
DE_FOR_AUDIENCE2(ChangeFrom, i) i->variableValueChangedFrom(*this, *oldValue, *d->value);
}
}
return *this;
}
Variable &Variable::set(Value const &v)
{
set(v.duplicate());
return *this;
}
Value const &Variable::value() const
{
DE_ASSERT(d->value);
return *d->value;
}
Value &Variable::value()
{
DE_ASSERT(d->value);
return *d->value;
}
Value *Variable::valuePtr()
{
return d->value;
}
Value const *Variable::valuePtr() const
{
return d->value;
}
bool Variable::operator==(const String &text) const
{
return d->value && d->value->compare(TextValue(text)) == 0;
}
Record &Variable::valueAsRecord()
{
return value<RecordValue>().dereference();
}
Record const &Variable::valueAsRecord() const
{
return value<RecordValue>().dereference();
}
ArrayValue const &Variable::array() const
{
return value<ArrayValue>();
}
ArrayValue &Variable::array()
{
return value<ArrayValue>();
}
Variable::operator Record & ()
{
return valueAsRecord();
}
Variable::operator Record const & () const
{
return valueAsRecord();
}
Variable::operator String () const
{
return value().asText();
}
Variable::operator ddouble () const
{
return value().asNumber();
}
Flags Variable::flags() const
{
return d->flags;
}
void Variable::setFlags(Flags const &flags, FlagOpArg operation)
{
applyFlagOperation(d->flags, flags, operation);
}
Variable &Variable::setReadOnly()
{
d->flags |= ReadOnly;
return *this;
}
bool Variable::isValid(Value const &v) const
{
/// @todo Make sure this actually works and add func, record, ref.
if ((!d->flags.testFlag(AllowNone) && dynamic_cast<NoneValue const *>(&v) ) ||
(!d->flags.testFlag(AllowNumber) && dynamic_cast<NumberValue const *>(&v) ) ||
(!d->flags.testFlag(AllowText) && dynamic_cast<TextValue const *>(&v) ) ||
(!d->flags.testFlag(AllowArray) && dynamic_cast<ArrayValue const *>(&v) ) ||
(!d->flags.testFlag(AllowDictionary) && dynamic_cast<DictionaryValue const *>(&v)) ||
(!d->flags.testFlag(AllowBlock) && dynamic_cast<BlockValue const *>(&v) ) ||
(!d->flags.testFlag(AllowTime) && dynamic_cast<TimeValue const *>(&v) ) )
{
return false;
}
// It's ok.
return true;
}
void Variable::verifyValid(Value const &v) const
{
if (!isValid(v))
{
/// @throw InvalidError Value @a v is not allowed by the variable.
throw InvalidError("Variable::verifyValid",
"Value type is not allowed by the variable '" + d->name + "'");
}
}
void Variable::verifyWritable(Value const &attemptedNewValue)
{
if (d->flags & ReadOnly)
{
Value const ¤tValue = *d->value;
if (d->value && typeid(currentValue) == typeid(attemptedNewValue) &&
!d->value->compare(attemptedNewValue))
{
// This is ok: the value doesn't change.
return;
}
/// @throw ReadOnlyError The variable is in read-only mode.
throw ReadOnlyError("Variable::verifyWritable",
"Variable '" + d->name + "' is in read-only mode");
}
}
void Variable::verifyName(String const &s)
{
if (s.indexOf('.'))
{
/// @throw NameError The name cannot contain periods '.'.
throw NameError("Variable::verifyName", "Name contains '.': " + s);
}
}
void Variable::operator >> (Writer &to) const
{
if (!(d->flags & NoSerialize))
{
to << d->name << duint32(d->flags) << *d->value;
}
}
void Variable::operator << (Reader &from)
{
duint32 modeFlags = 0;
from >> d->name >> modeFlags;
d->flags = Flags(modeFlags);
delete d->value;
try
{
d->value = Value::constructFrom(from);
}
catch (Error const &)
{
// Always need to have a value.
d->value = new NoneValue();
throw;
}
}
} // namespace de