-
Notifications
You must be signed in to change notification settings - Fork 185
/
gsiClassBase.h
580 lines (504 loc) · 14.6 KB
/
gsiClassBase.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
KLayout Layout Viewer
Copyright (C) 2006-2017 Matthias Koefferlein
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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _HDR_gsiClassBase
#define _HDR_gsiClassBase
#include "tlObject.h"
#include "tlObjectCollection.h"
#include "tlException.h"
#include "tlExpression.h"
#include "tlAssert.h"
#include "tlHeap.h"
#include "gsiExpression.h"
#include "gsiObject.h"
#include "gsiSerialisation.h"
#include "gsiMethods.h"
#include <list>
#include <map>
#include <vector>
#include <typeinfo>
#include <memory>
namespace gsi
{
/**
* @brief A base class for client-specific data per class
*
* Objects of this type are used inside the ClassBase object to store
* information specific for certain clients.
*/
class PerClassClientSpecificData
{
public:
PerClassClientSpecificData ()
{
// .. nothing yet ..
}
virtual ~PerClassClientSpecificData()
{
// .. nothing yet ..
}
};
/**
* @brief The basic object to declare a class
*
* This object represents a GSI class. It provides the methods to translate the typeless object
* pointer to a real C++ object and to perform some basic operations on the object.
* It also provides metainformation such as method declarations and similar.
*/
class GSI_PUBLIC ClassBase
: public tl::Object
{
public:
typedef tl::weak_collection<ClassBase> class_collection;
typedef class_collection::const_iterator class_iterator;
typedef Methods::iterator method_iterator;
/**
* @brief Constructor
*
* The constructor supplies information about the connector class (can be 0), a documentation string and the method declarations.
*/
ClassBase (const std::string &doc, const Methods &mm, bool do_register = true);
/**
* @brief Destructor
*/
virtual ~ClassBase ();
/**
* @brief Gets the pointer to the base class declaration object (can be 0)
*/
const ClassBase *base () const
{
return mp_base;
}
/**
* @brief Returns a pointer to the type_info object if the class is an adaptor
*
* If the class adapts another type (specifically enums), this method will
* return a pointer to the adapted type's type_info.
* In other cases, the return value is 0.
*/
virtual const std::type_info *adapted_type_info () const
{
return 0;
}
/**
* @brief Returns the "real" (consolidated) declaration object
*
* The actual declaration object may be different from this declaration because of the class extension
* mechanism. Using that mechanism, class declarations can extend over multiple ClassBase objects. The
* individual declarations are merged and a consolidated class declaration object is provided.
*/
virtual const ClassBase *declaration () const
{
return 0;
}
/**
* @brief Consolidates the declaration
*
* Before all classes are finalized and the script interpreters start to instantiate them, all
* class declarations are consolidated. If the consolidate method returns true, the class declaration
* is kept. Otherwise it is discarded.
*/
virtual bool consolidate () const
{
return false;
}
/**
* @brief Gets the class name
*/
const std::string &name () const
{
return m_name;
}
/**
* @brief Gets the method declaration collection
*/
Methods &methods ()
{
return m_methods;
}
/**
* @brief Gets the documentation string
*/
const std::string &doc () const
{
return m_doc;
}
/**
* @brief Merge base and extension declarations
*
* This method must be called at least once!
* It produces the consolidates class declaration object.
*/
static void merge_declarations ();
/**
* @brief Gets the parent declaration object
* This returns the parent class if this class is a child class.
*/
const ClassBase *parent () const
{
return mp_parent;
}
/**
* @brief Gets the fully qualified name (A::B::..)
*/
std::string qname () const;
/**
* @brief Adds a child class
*/
void add_child_class (const ClassBase *cls);
/**
* @brief Iterates all child classes (begin)
*/
tl::weak_collection<ClassBase>::const_iterator begin_child_classes () const
{
return m_child_classes.begin ();
}
/**
* @brief Iterates all subclasses (end)
*/
tl::weak_collection<ClassBase>::const_iterator end_child_classes () const
{
return m_child_classes.end ();
}
/**
* @brief Iterates all classes present (begin)
*/
static class_iterator begin_classes ()
{
return collection ().begin ();
}
/**
* @brief Iterates all classes present (end)
*/
static class_iterator end_classes ()
{
return collection ().end ();
}
/**
* @brief Iterates the methods (begin)
*/
method_iterator begin_methods () const
{
return m_methods.begin ();
}
/**
* @brief Iterates the methods (end)
*/
method_iterator end_methods () const
{
return m_methods.end ();
}
/**
* @brief Returns true, if this class is derived from the given base class
*/
bool is_derived_from (const ClassBase *base) const;
/**
* @brief Returns true, if this class can be used to initialize an object with type "target"
*/
bool can_convert_to (const ClassBase *target) const;
/**
* @brief Creates a new object initialized from "obj" of type "target"
*/
void *create_obj_from (const ClassBase *target, void *obj) const;
/**
* @brief Class implementation: destroys the given object
*
* The implementation of this method is supposed to delete the object and
* free any resources associated with that object. After that operation,
* the object pointer is invalid.
*
* This method may not be available if "can_destroy" is false.
*/
virtual void destroy (void * /*obj*/) const
{
tl_assert (false);
}
/**
* @brief Class implementation: creates an object (default constructor)
*
* Creates and default-initializes the given object. This method may not be
* available if there is no default constructor (can_default_create is false).
* In that case, some static factory method must be provided.
*/
virtual void *create () const
{
tl_assert (false);
return 0;
}
/**
* @brief Creates a class representing an adapted object
*
* This method will create a new object representing the adapted object x.
*/
virtual void *create_from_adapted (const void * /*x*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Creates a class representing an adapted object
*
* This method will create a new object representing the adapted object x.
* It will consume the object given by x.
*/
virtual void *create_from_adapted_consume (void * /*x*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Returns a pointer to the adapted object from an adaptor class
*/
virtual const void *adapted_from_obj (const void * /*obj*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Returns a new pointer to the adapted object from an adaptor class
*/
virtual void *create_adapted_from_obj (const void * /*obj*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Class implementation: clones the given object
*
* The implementation is supposed to create a deep copy of the source object.
* This method may not be available if can_copy is false.
*/
virtual void *clone (const void * /*src*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Class implementation: assigns the source to the target object
*
* The implementation is supposed to overwrite the target with the contents
* of the source. This is a deep copy.
* This method may not be available if can_copy is false.
*/
virtual void assign (void * /*target*/, const void * /*src*/) const
{
tl_assert (false);
}
/**
* @brief Class implementation: destruction supported predicate
*
* This flag is true, if the class supports destruction of objects of this kind.
*/
virtual bool can_destroy () const
{
tl_assert (false);
return false;
}
/**
* @brief Class implementation: copy supported predicate
*
* This flag is true, if the class supports copying of objects of this kind.
*/
virtual bool can_copy () const
{
tl_assert (false);
return false;
}
/**
* @brief Class implementation: default construction supported predicate
*
* This flag is true, if the class supports default construction of objects of this kind.
*/
virtual bool can_default_create () const
{
tl_assert (false);
return false;
}
/**
* @brief Class implementation: check C++ type of object
*
* This method returns true, if objects of this class are compatible with
* the given C++ type.
*/
virtual bool is_of_type (const std::type_info & /*ti*/) const
{
tl_assert (false);
return false;
}
/**
* @brief Returns true, if the given object can be cast to this class
*
* When this method is called, it is guaranteed that the object is at least of
* the base class type. This implies that there is a base class when this method
* is called.
*/
virtual bool can_upcast (const void * /*p*/) const
{
tl_assert (false);
return false;
}
/**
* @brief Class implementation: gets C++ type of object
*
* This method delivers the C++ type_info object of this class.
*/
virtual const std::type_info &type () const
{
tl_assert (false);
return typeid (void);
}
/**
* @brief Class implementation: gets C++ type of object
*
* This method delivers the class declaration for an subclassed object p. p must be of a
* subclass (derived class) of this class. This method will the declaration object
* of object p.
*/
virtual const ClassBase *subclass_decl (const void * /*p*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Class Implementation: Returns true, if the object is managed
* "managed" objects are those which are derived from gsi::ObjectBase.
*/
virtual bool is_managed () const
{
tl_assert (false);
return false;
}
/**
* @brief Gets the basic gsi::ObjectBase object from a generic pointer
* This method will return 0, if the object is not managed.
* If required is false, a return value of 0 is permitted, indicating that
* no dynamic allocation of a gsi::ObjectBase has happened yet. This is useful
* in case of the Qt-GSI bridge which means a special Qt object needs to be
* created in order to provide the gsi::ObjectBase interface. If required is
* false, this initialization does not need to happen.
*/
virtual gsi::ObjectBase *gsi_object (void * /*p*/, bool /*required*/ = true) const
{
tl_assert (false);
return 0;
}
/**
* @brief Class Implementation: provide variant binding information (constness)
*/
virtual const tl::VariantUserClassBase *var_cls (bool /*is_const*/) const
{
tl_assert (false);
return 0;
}
/**
* @brief Class Implementation: provide variant binding information (meta/class object)
*/
virtual const tl::VariantUserClassBase *var_cls_cls () const
{
tl_assert (false);
return 0;
}
/**
* @brief Post-construction initialization
*
* This method will be called by the GSI system to provide initialization after
* the static initialization. Some schemes cannot be implementation statically, plus
* the initialization order is indetermined for static initialization.
* In that case, this initialization step is useful. It will call the initialize
* method on all method declarations.
*/
virtual void initialize ()
{
m_methods.initialize ();
}
/**
* @brief Adds a method to the class
* The class becomes owner of the method object.
* This method is public to allow dynamic extension of the documentation
* through scripts.
* Don't use it for other purposes.
*/
void add_method (MethodBase *method, bool base_class = false);
/**
* @brief Sets the per-client data for the given client index
* This method is const to preserve the general const semantics of the class
* while allowing clients to register information.
*/
void set_data (int ch, PerClassClientSpecificData *data) const
{
mp_data[ch].reset (data);
}
/**
* @brief Gets the per-client data for the given client index
*/
PerClassClientSpecificData *data (int ch) const
{
return mp_data[ch].get ();
}
/**
* @brief Sets the per-client data for gsi::Expressions
* This is a special slot since no client index is available for gsi::Expressions.
*/
void set_gsi_data (PerClassClientSpecificData *data) const
{
set_data (ClientIndex::Basic, data);
}
/**
* @brief Gets the per-client data for gsi::Expressions
*/
PerClassClientSpecificData *gsi_data () const
{
return data (ClientIndex::Basic);
}
protected:
static const class_collection &collection ();
const tl::weak_collection<ClassBase> &subclasses () const
{
return m_subclasses;
}
void set_name (const std::string &n)
{
m_name = n;
}
void set_parent (const ClassBase *parent);
void set_base (const ClassBase *base);
private:
const ClassBase *mp_base, *mp_parent;
std::string m_doc;
Methods m_methods;
std::string m_name;
tl::weak_collection<ClassBase> m_child_classes, m_subclasses;
mutable std::auto_ptr<PerClassClientSpecificData> mp_data[ClientIndex::MaxClientIndex];
static class_collection *mp_class_collection;
static unsigned int m_class_count;
};
/**
* @brief Accessor to a declaration through name
*/
GSI_PUBLIC const ClassBase *class_by_name (const std::string &name);
/**
* @brief Returns true if there is a class with the given name
*/
GSI_PUBLIC bool has_class (const std::string &name);
/**
* @brief Find a class declaration through the type info
*/
GSI_PUBLIC const ClassBase *class_by_typeinfo (const std::type_info &ti);
/**
* @brief Returns true if there is a class with the given type info
*/
GSI_PUBLIC bool has_class (const std::type_info &ti);
}
#endif