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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/*
Ousía
Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file Property.hpp
*
* Contains classes for describing properties, which allow to generically access
* object members via Getter and Setter functions. This functionality is needed
* for building scripting language interfaces.
*
* @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de)
*/
#ifndef _OUSIA_PROPERTY_HPP_
#define _OUSIA_PROPERTY_HPP_
#include <memory>
#include "Exceptions.hpp"
#include "Function.hpp"
#include "Variant.hpp"
namespace ousia {
/**
* Exception type used for signaling exceptions in the context of Properties,
* such as calls to not set Getters or Setters.
*/
class PropertyException : public LoggableException {
public:
using LoggableException::LoggableException;
};
// Forward declaration
class RttiType;
namespace RttiTypes {
extern const RttiType None;
}
/**
* Structure describing the type of a property -- which consists of a "outer"
* type (which may either be a primitive variant type such as e.g.
* RttiTypes::Int or any other RttiType instance) and an inner type, which
* describes the type contained within a container type such as RttiTypes::Array
* or RttiTypes::Map.
*/
struct PropertyType {
// Variable declaring the empty property type.
static const PropertyType None;
/**
* Outer type, may be any RttiType instance. If set to RttiType::None, any
* outer type is acceptable.
*/
const RttiType &type;
/**
* Describes the inner type of the property used when the outer type is a
* container type such as RttiType::Array or RttiType::Map. If set to
* RttiType::None any inner type is acceptable.
*/
const RttiType &innerType;
/**
* Creates a new instance of the PropertyType class with both inner and
* outer type being set to RttiTypes::None and thus allowing any type to be
* represented by this property.
*/
PropertyType() : type(RttiTypes::None), innerType(RttiTypes::None){};
/**
* Creates a new instance of the PropertyType class with the given outer
* type.
*
* @param type is the "outer" type of the PropertyType instance which may
* be any RttiType instances or RttiType::None, in which case all types are
* allowed.
*/
PropertyType(const RttiType &type)
: type(type), innerType(RttiTypes::None){};
/**
* Creates a new instance of the PropertyType class with the given outer and
* inner type.
*
* @param type is the "outer" type of the PropertyType instance which may
* be any RttiType instances or RttiType::None, in which case all types are
* allowed.
* @param innerType is the inner type of the PropertyType instance, which is
* relevant if the outer type is set to a basic container type, namely
* RttiTypes::Array or RttiTypes::Map.
*/
PropertyType(const RttiType &type, const RttiType &innerType)
: type(type), innerType(innerType){};
};
/**
* Represents an abstract Function with a property type attached to it. This is
* used as base class for Getter and Setter classes.
*/
class PropertyFunction : public Function {
private:
/**
* Boolean field indicating whether the function is valid or not (a callback
* function was provided or not
*/
bool valid;
protected:
/**
* Constructor of the PropertyFunction class with a preset propertyType
* reference.
*
* @param valid specifies whether a callback function was given or not.
*/
PropertyFunction(bool valid)
: valid(valid), propertyType(&PropertyType::None){};
public:
/**
* Returns the type associated with the property function.
*/
PropertyType const *propertyType;
/**
* Returns true if a callback function was given, false otherwise.
*/
bool isValid() { return valid; }
};
/**
* Abstract function type representing a Getter function. Provides validation
* functions, yet does not have the ability to actually call a Getter. This
* functionality is provided by the Getter template class, which provides an
* easy to use callback mechanism.
*/
class GetterFunction : public PropertyFunction {
protected:
/**
* Makes sure no arguments are given, throws an exception if any arguments
* are provided.
*
* @param args is an array containing the arguments.
*/
void validateArguments(Variant::arrayType &args) const;
/**
* Makes sure the result adhers to the specified property type. Throws an
* exception if this is not the case.
*
* @param res is the result that should be validated.
*/
void validateResult(Variant &res) const;
using PropertyFunction::PropertyFunction;
public:
/**
* Returns the value of the property for the given object.
*
* @param obj is the instance for which the value of the property should be
* returned.
* @return the value retrieved from the object pointed at by obj.
*/
Variant get(void *obj);
};
/**
* Class representing the getter function of a property.
*
* @tparam T is the type of the object on which the getter should be executed.
*/
template <class T>
class Getter : public GetterFunction {
public:
/**
* Callback function type used to access the getter.
*
* @param thisRef is a reference to the object from which the value should
* be retrieved.
* @return the retrieved value. The result is checked to be of the specified
* type of the property.
*/
using Callback = Variant (*)(const T *thisRef);
private:
/**
* Callback function pointer used to access the getter.
*/
Callback callback;
protected:
/**
* Calls the callback function and validates the given arguments and the
* callback result.
*
* @param args is a list of arguments passed to the getter. Should be empty.
* @param thisRef is a reference to the object from which the value should
* be retrieved.
* @return the retrieved value. The result is checked to be of the specified
* type of the property.
*/
Variant doCall(Variant::arrayType &args, void *thisRef) const override
{
if (!callback) {
throw PropertyException("Property is writeonly.");
}
// Make sure the input arguments are valid
validateArguments(args);
// Call the actual callback function and make sure the output
// arguments
// are valid
Variant res = callback(static_cast<T *>(thisRef));
validateResult(res);
// Return the validated result
return res;
}
public:
/**
* Creates an invalid getter instance with no callback function.
*/
Getter() : GetterFunction(false), callback(nullptr) {}
/**
* Create a getter with the given callback function.
*
* @param callback is the underlying callback function to be used to
* get the value from an instance of type T.
*/
Getter(Callback callback)
: GetterFunction(callback != nullptr), callback(callback)
{
}
};
/**
* Abstract function type representing a Setter function. Provides validation
* functions, yet does not have the ability to actually call a Setter. This
* functionality is provided by the Setter template class, which provides an
* easy to use callback mechanism.
*/
class SetterFunction : public PropertyFunction {
protected:
/**
* Makes sure exactly one argument with the specified type is given.
*
* @param args is an array containing the arguments.
*/
void validateArguments(Variant::arrayType &args) const;
using PropertyFunction::PropertyFunction;
public:
/**
* Sets the value of the property for the given object.
*
* @param value is the new property value that should be set.
* @param obj is the instance for which the value of the property should be
* returned.
*/
void set(const Variant &value, void *obj);
};
/**
* Class representing the Setter function of a property.
*
* @tparam T is the type of the object on which the setter should be executed.
*/
template <class T>
class Setter : public SetterFunction {
public:
/**
* Callback function type used to access the setter.
*
* @param value is the value that should be set. The value has been
* validated for compliance with the specified property type.
* @param thisRef is a reference to the object from which the value should
* be retrieved.
*/
using Callback = void (*)(const Variant &value, T *thisRef);
private:
/**
* Callback function pointer used to access the setter.
*/
Callback callback;
protected:
/**
* Calls the callback function and validates the given arguments and the
* callback result.
*
* @param args is a list of arguments passed to the setter. Should contain
* exactly one argument.
* @param thisRef is a reference to the object in which the value should
* be set.
* @return a variant containing nullptr.
*/
Variant doCall(Variant::arrayType &args, void *thisRef) const override
{
if (!callback) {
throw PropertyException("Property is readonly.");
}
// Make sure the input argument is valid and call the callback function
validateArguments(args);
callback(args[0], static_cast<T *>(thisRef));
return nullptr;
}
public:
/**
* Creates an invalid setter instance with no callback function.
*/
Setter() : SetterFunction(false), callback(nullptr) {}
/**
* Create a setter with the given callback function.
*
* @param callback is the underlying callback function to be used to
* set the value of an instance of type T.
*/
Setter(Callback callback)
: SetterFunction(callback != nullptr), callback(callback)
{
}
};
/**
* Class describing a generic Property of an object of a not-yet specified type.
*/
class PropertyDescriptor {
private:
/**
* Description of the type of the property, consisting of an inner and an
* outer type.
*/
const PropertyType type;
/**
* Object used to read the value of the property.
*/
std::unique_ptr<GetterFunction> getter;
/**
* Object used to write values of the property. The setter may be invalid
* in which case the property is read only.
*/
std::unique_ptr<SetterFunction> setter;
protected:
/**
* Base constructor of the PropertyDescriptor class, called by all other
* constructors.
*
* @param type describes the type of the PropertyDescriptor.
* @param getter is the getter function used for reading the property. The
* getter function must be valid, writeonly properties are not supported.
* @param setter is the setter function used for writing the property. The
* setter function may be invalid, in which case the property is readonly.
*/
PropertyDescriptor(const PropertyType &type,
std::unique_ptr<GetterFunction> getter,
std::unique_ptr<SetterFunction> setter);
public:
/**
* Returns true if this is a read only property.
*
* @return true if no (valid) setter was given in the constructor, false
* otherwise.
*/
bool isReadonly() const { return !(setter->isValid()); }
/**
* Returns the type described by the property.
*
* @return the PropertyType instance describing the type of this property.
*/
const PropertyType &getType() const { return type; }
/**
* Returns the value of the property for the given object.
*
* @param obj is the instance for which the value of the property should be
* returned.
* @return the value retrieved from the object pointed at by obj.
*/
Variant get(void *obj) const { return getter->get(obj); }
/**
* Sets the value of the property for the given object.
*
* @param value is the new property value that should be set.
* @param obj is the object for which the property should be updated.
*/
void set(const Variant &value, void *obj) const { setter->set(value, obj); }
};
/**
* Class representing a Property of an object of type T. Provides convenient
* constructors for the construction of the underlying PropertyDescriptor.
*
* @tparam T is the type with that field that should be accessed through this
* property.
*/
template <class T>
class Property : public PropertyDescriptor {
public:
/**
* Constructor of the Property class, creates a property with no type
* restrictions.
*
* @param getter is a Getter for accessing the described property for
* objects of type T.
* @param setter is a Setter for writing the described property for objects
* of type T.
*/
Property(const Getter<T> &getter, const Setter<T> &setter = Setter<T>{})
: PropertyDescriptor(
PropertyType{},
std::unique_ptr<GetterFunction>{new Getter<T>{getter}},
std::unique_ptr<SetterFunction>{new Setter<T>{setter}})
{
}
/**
* Constructor of the Property class.
*
* @param type is the type of the field that can be accessed by the
* property. This may either be a primitive variant type such as e.g.
* RttiTypes::Int or any other RttiType instance
* @param getter is a Getter for accessing the described property for
* objects of type T.
* @param setter is a Setter for writing the described property for objects
* of type T.
*/
Property(const RttiType &type, const Getter<T> &getter,
const Setter<T> &setter = Setter<T>{})
: PropertyDescriptor(
PropertyType{type},
std::unique_ptr<GetterFunction>{new Getter<T>{getter}},
std::unique_ptr<SetterFunction>{new Setter<T>{setter}})
{
}
/**
* Constructor of the Property class.
*
* @param type is the type of the field that can be accessed by the
* property. This may either be a primitive variant type such as e.g.
* RttiTypes::Int or any other RttiType instance.
* @param innerType is only relevant if type is set to either
* RttiTypes::Array or RttiTypes::Map. In this case the innerType describes
* the type of the elements stored inside these containers.
* @param getter is a Getter for accessing the described property for
* objects of type T.
* @param setter is a Setter for writing the described property for objects
* of type T.
*/
Property(const RttiType &type, const RttiType &innerType,
const Getter<T> &getter, const Setter<T> &setter = Setter<T>{})
: PropertyDescriptor(
PropertyType{type, innerType},
std::unique_ptr<GetterFunction>{new Getter<T>{getter}},
std::unique_ptr<SetterFunction>{new Setter<T>{setter}})
{
}
/**
* Returns the value of the property for the given object.
*
* @param obj is the instance for which the value of the property should be
* returned.
* @return the value retrieved from the object pointed at by obj.
*/
Variant get(T *obj) { return PropertyDescriptor::get(obj); }
/**
* Sets the value of the property for the given object.
*
* @param value is the new property value that should be set.
* @param obj is the object for which the property should be updated.
*/
void set(const Variant &value, T *obj)
{
PropertyDescriptor::set(value, obj);
}
};
}
#endif /* _OUSIA_PROPERTY_HPP_ */
|