/
record.h
435 lines (375 loc) · 13.6 KB
/
record.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright © 2009-2013 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>
*/
#ifndef LIBDENG2_RECORD_H
#define LIBDENG2_RECORD_H
#include "../ISerializable"
#include "../String"
#include "../Variable"
#include "../Value"
#include "../Audience"
#include "../Log"
#include <QMap>
#include <QList>
namespace de {
class ArrayValue;
class Function;
class NativeFunctionSpec;
/**
* A set of variables. A record may have any number of subrecords. Note
* that the members of a record do not have an order.
*
* A @em subrecord is a record that is owned by one of the members of the
* main record. The ownership chain is as follows: Record -> Variable ->
* RecordValue -> Record.
*
* @see http://en.wikipedia.org/wiki/Record_(computer_science)
*
* @ingroup data
*/
class DENG2_PUBLIC Record : public ISerializable, public LogEntry::Arg::Base,
DENG2_OBSERVES(Variable, Deletion)
{
public:
/// Unknown variable name was given. @ingroup errors
DENG2_ERROR(NotFoundError);
/// All variables and subrecords in the record must have a name. @ingroup errors
DENG2_ERROR(UnnamedError);
/// Attempted to get the value of a variable while expecting the wrong type. @ingroup errors
DENG2_ERROR(ValueTypeError);
typedef QMap<String, Variable *> Members;
typedef QMap<String, Record *> Subrecords;
typedef std::pair<String, String> KeyValue;
typedef QList<KeyValue> List;
DENG2_DEFINE_AUDIENCE2(Deletion, void recordBeingDeleted(Record &record))
public:
Record();
/**
* Constructs a copy of another record.
*
* @param other Record to copy.
*/
Record(Record const &other);
virtual ~Record();
/**
* Deletes all the variables in the record.
*/
void clear();
enum CopyBehavior {
AllMembers,
IgnoreDoubleUnderscoreMembers
};
/**
* Adds a copy of each member of another record into this record. The
* previous contents of this record are untouched as long as they have no
* members with the same names as in @a other.
*
* @param other Record whose members are to be copied.
* @param behavior Copy behavior.
*/
void copyMembersFrom(Record const &other, CopyBehavior behavior = AllMembers);
/**
* Assignment operator.
*/
Record &operator = (Record const &other);
/**
* Determines if the record contains a variable or a subrecord named @a variableName.
*/
bool has(String const &name) const;
/**
* Determines if the record contains a variable named @a variableName.
*/
bool hasMember(String const &variableName) const;
/**
* Determines if the record contains a subrecord named @a subrecordName.
*/
bool hasSubrecord(String const &subrecordName) const;
/**
* Adds a new variable to the record.
*
* @param variable Variable to add. Record gets ownership.
* The variable must have a name.
*
* @return @a variable, for convenience.
*/
Variable &add(Variable *variable);
/**
* Removes a variable from the record.
*
* @param variable Variable owned by the record.
*
* @return Caller gets ownership of the removed variable.
*/
Variable *remove(Variable &variable);
/**
* Adds a new variable to the record with a NoneValue. If there is an existing
* variable with the given name, the old variable is deleted first.
*
* @param variableName Name of the variable.
*
* @return The new variable.
*/
Variable &add(String const &variableName);
/**
* Adds a number variable to the record. The variable is set up to only accept
* number values. An existing variable with the same name is deleted first.
*
* @param variableName Name of the variable.
* @param number Value of the variable.
*
* @return The number variable.
*/
Variable &addNumber(String const &variableName, Value::Number const &number);
/**
* Adds a number variable to the record with a Boolean semantic hint. The variable is
* set up to only accept number values. An existing variable with the same name is
* deleted first.
*
* @param variableName Name of the variable.
* @param booleanValue Value of the variable (@c true or @c false).
*
* @return The number variable.
*/
Variable &addBoolean(String const &variableName, bool booleanValue);
/**
* Adds a text variable to the record. The variable is set up to only accept
* text values. An existing variable with the same name is deleted first.
*
* @param variableName Name of the variable.
* @param text Value of the variable.
*
* @return The text variable.
*/
Variable &addText(String const &variableName, Value::Text const &text);
Variable &addTime(String const &variableName, Time const &time);
/**
* Adds an array variable to the record. The variable is set up to only accept
* array values. An existing variable with the same name is deleted first.
*
* @param variableName Name of the variable.
* @param array Value for the new variable (ownership taken). If not
* provided, an empty array will be created for the variable.
*
* @return The array variable.
*/
Variable &addArray(String const &variableName, ArrayValue *array = 0);
/**
* Adds a dictionary variable to the record. The variable is set up to only accept
* dictionary values. An existing variable with the same name is deleted first.
*
* @param variableName Name of the variable.
*
* @return The dictionary variable.
*/
Variable &addDictionary(String const &variableName);
/**
* Adds a block variable to the record. The variable is set up to only accept
* block values. An existing variable with the same name is deleted first.
*
* @param variableName Name of the variable.
*
* @return The block variable.
*/
Variable &addBlock(String const &variableName);
/**
* Adds a function variable to the record. The variable is set up to only
* accept function values. An existing variable with the same name is deleted first.
*
* @param variableName Name of the variable.
* @param func Function. The variable's value will hold a
* reference to the Function; the caller may release
* its reference afterwards.
*
* @return The function variable.
*/
Variable &addFunction(String const &variableName, Function *func);
/**
* Adds a new subrecord to the record. Adds a variable named @a name and gives
* ownership of @a subrecord to it. An existing subrecord with the same name is
* deleted first.
*
* @param name Name to use for the subrecord. This must be a valid variable name.
* @param subrecord Record to add. This record gets ownership
* of the subrecord.
*
* @return @a subrecord, for convenience.
*/
Record &add(String const &name, Record *subrecord);
/**
* Adds a new empty subrecord to the record. Adds a variable named @a name and
* creates a new record owned by it. An existing subrecord with the same name is
* deleted first.
*
* @param name Name to use for the subrecord. This must be a valid variable name.
*
* @return The new subrecord.
*/
Record &addRecord(String const &name);
/**
* Removes a subrecord from the record.
*
* @param name Name of subrecord owned by this record.
*
* @return Caller gets ownership of the removed record.
*/
Record *remove(String const &name);
// Convenient value getters:
Value const &get(String const &name) const;
dint geti(String const &name) const;
dint geti(String const &name, dint defaultValue) const;
bool getb(String const &name) const;
bool getb(String const &name, bool defaultValue) const;
duint getui(String const &name) const;
duint getui(String const &name, duint defaultValue) const;
ddouble getd(String const &name) const;
ddouble getd(String const &name, ddouble defaultValue) const;
String gets(String const &name) const;
String gets(String const &name, String const &defaultValue) const;
ArrayValue const &geta(String const &name) const;
template <typename ValueType>
ValueType const &getAs(String const &name) const {
ValueType const *v = get(name).maybeAs<ValueType>();
if(!v)
{
throw ValueTypeError("Record::getAs", String("Cannot cast to expected type (") +
DENG2_TYPE_NAME(ValueType) + ")");
}
return *v;
}
/**
* Sets the value of a variable, creating the variable if needed.
*
* @param name Name of the variable. May contain subrecords using the dot notation.
* @param value Value for the variable.
*
* @return Variable whose value was set.
*/
Variable &set(String const &name, bool value);
/// @copydoc set()
Variable &set(String const &name, char const *value);
/// @copydoc set()
Variable &set(String const &name, Value::Text const &value);
/// @copydoc set()
Variable &set(String const &name, Value::Number const &value);
/// @copydoc set()
Variable &set(String const &name, dint32 value);
/// @copydoc set()
Variable &set(String const &name, duint32 value);
/**
* Sets the value of a variable, creating the variable if it doesn't exist.
*
* @param name Name of the variable. May contain subrecords using the dot notation.
* @param value Array to use as the value of the variable. Ownership taken.
*/
Variable &set(String const &name, ArrayValue *value);
/**
* Looks up a variable in the record. Variables in subrecords can be accessed
* using the member notation: <code>subrecord-name.variable-name</code>
*
* @param name Variable name.
*
* @return Variable.
*/
Variable &operator [] (String const &name);
/**
* Looks up a variable in the record. Variables in subrecords can be accessed
* using the member notation: <code>subrecord-name.variable-name</code>
*
* @param name Variable name.
*
* @return Variable (non-modifiable).
*/
Variable const &operator [] (String const &name) const;
/**
* Looks up a subrecord in the record.
*
* @param name Name of the subrecord.
*
* @return Subrecord.
*/
Record &subrecord(String const &name);
/**
* Looks up a subrecord in the record.
*
* @param name Name of the subrecord.
*
* @return Subrecord (non-modifiable).
*/
Record const &subrecord(String const &name) const;
/**
* Returns a non-modifiable map of the members.
*/
Members const &members() const;
/**
* Collects a map of all the subrecords present in the record.
*/
Subrecords subrecords() const;
/**
* Creates a text representation of the record. Each variable name is
* prefixed with @a prefix.
*
* @param prefix Prefix for each variable name.
* @param lines NULL (used for recursion into subrecords).
*
* @return Text representation.
*/
String asText(String const &prefix, List *lines) const;
/**
* Convenience template for getting the value of a variable in a
* specific type.
*
* @param name Name of variable.
*
* @return Value cast to a specific value type.
*/
template <typename ValueType>
ValueType const &value(String const &name) const {
return (*this)[name].value<ValueType>();
}
/**
* Convenience method for finding the Function referenced by a member.
*
* @param name Name of member.
*
* @return The function, or @c NULL if the member does not exist or
* has some other value than FunctionValue.
*/
Function const *function(String const &name) const;
/**
* Adds a new native function to the record according to the specification.
*
* @param spec Native function specification.
*
* @return Reference to this record.
*/
Record &operator << (NativeFunctionSpec const &spec);
// Implements ISerializable.
void operator >> (Writer &to) const;
void operator << (Reader &from);
// Implements LogEntry::Arg::Base.
LogEntry::Arg::Type logEntryArgType() const { return LogEntry::Arg::StringArgument; }
String asText() const { return asText("", 0); }
// Observes Variable deletion.
void variableBeingDeleted(Variable &variable);
private:
DENG2_PRIVATE(d)
};
/// Converts the record into a human-readable text representation.
DENG2_PUBLIC QTextStream &operator << (QTextStream &os, Record const &record);
} // namespace de
#endif /* LIBDENG2_RECORD_H */