Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cclassdescriptor.h @ 2f5cc443

History | View | Annotate | Download (9.91 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  CCLASSDESCRIPTOR.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cClassDescriptor  : metainfo about structs and classes
9
//
10
//==========================================================================
11
12
/*--------------------------------------------------------------*
13
  Copyright (C) 1992-2008 Andras Varga
14
  Copyright (C) 2006-2008 OpenSim Ltd.
15

16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
18
*--------------------------------------------------------------*/
19
20
#ifndef __CCLASSDESCRIPTOR_H
21
#define __CCLASSDESCRIPTOR_H
22
23
#include <string>
24
#include "cownedobject.h"
25
#include "simtime.h"
26
27
NAMESPACE_BEGIN
28
29
30
/**
31
 * Abstract base class for structure description classes, used mainly
32
 * with message subclassing.
33
 *
34
 * Subclasses of cClassDescriptor encapsulate the kind of reflection
35
 * information (in the Java sense) which is needed by Tkenv to display
36
 * fields in a message, struct or object created with the .msg syntax.
37
 * The cClassDescriptor subclass is generated along with the message class,
38
 * (struct, object, etc.).
39
 *
40
 * When Tkenv encounters a message object, it creates an appropriate
41
 * cClassDescriptor object and uses that to find out what fields the
42
 * message object has, what are their values etc. The message object
43
 * is said to be the `client object' of the cClassDescriptor object.
44
 *
45
 * @ingroup Internals
46
 */
47
class SIM_API cClassDescriptor : public cNoncopyableOwnedObject
48
{
49
  public:
50
    /// Field types.
51
    enum {
52
        FD_ISARRAY = 0x01,    ///< field is an array: int a[]; int a[10];
53
        FD_ISCOMPOUND = 0x02, ///< basic type (T) is struct or class: T a; T *a; T a[10]; T *a[]
54
        FD_ISPOINTER = 0x04,  ///< field is pointer or pointer array: T *a; T *a[]; T *a[10];
55
        FD_ISCOBJECT = 0x08,  ///< if ISCOMPOUND: basic type (T) subclasses from cObject
56
        FD_ISCOWNEDOBJECT = 0x10, ///< if ISCOMPOUND: basic type (T) subclasses from cOwnedObject
57
        FD_ISEDITABLE = 0x20, ///< whether field supports setFieldAsString()
58
        FD_NONE = 0x0
59
    };
60
61
  private:
62
    std::string baseclassname;
63
    cClassDescriptor *baseclassdesc;
64
    int inheritancechainlength;
65
    int extendscobject;
66
67
  protected:
68
    // utility functions for converting from/to strings
69
    static std::string long2string(long l);
70
    static long string2long(const char *s);
71
    static std::string ulong2string(unsigned long l);
72
    static unsigned long string2ulong(const char *s);
73
    static std::string int642string(int64 l);
74
    static int64 string2int64(const char *s);
75
    static std::string uint642string(uint64 l);
76
    static uint64 string2uint64(const char *s);
77
    static std::string bool2string(bool b);
78
    static bool string2bool(const char *s);
79
    static std::string double2string(double d);
80
    static std::string double2string(SimTime t) {return t.str();}
81
    static double string2double(const char *s);
82
    static std::string enum2string(long e, const char *enumname);
83
    static long string2enum(const char *s, const char *enumname);
84
    static std::string oppstring2string(const char *s) {return s?s:"";}
85
    static std::string oppstring2string(const opp_string& s) {return s.c_str();}
86
    static std::string oppstring2string(const std::string& s)  {return s;}
87
    static void string2oppstring(const char *s, opp_string& str) {str = s?s:"";}
88
    static void string2oppstring(const char *s, std::string& str) {str = s?s:"";}
89
90
  public:
91
    /** @name Constructors, destructor, assignment. */
92
    //@{
93
94
    /**
95
     * Constructor.
96
     */
97
    cClassDescriptor(const char *classname, const char *_baseclassname=NULL);
98
99
    /**
100
     * Destructor.
101
     */
102
    virtual ~cClassDescriptor();
103
    //@}
104
105
    /** @name Getting descriptor for an object or a struct. */
106
    //@{
107
108
    /**
109
     * Returns the descriptor object for the given class. The returned
110
     * descriptor object is a singleton, and must not be deleted.
111
     */
112
    static cClassDescriptor *getDescriptorFor(const char *classname);
113
114
    /**
115
     * Returns the descriptor object for the given object. This can return
116
     * descriptor for a base class, if there is no exact match.
117
     * The returned descriptor object is a singleton, and must not be deleted.
118
     */
119
    static cClassDescriptor *getDescriptorFor(cObject *object);
120
    //@}
121
122
    /** @name Querying and setting fields of the client object. */
123
    //@{
124
125
    /**
126
     * Returns true if this descriptor supports the given object's class.
127
     * If obj can be cast (dynamic_cast) to the class the descriptor supports,
128
     * the method should return true.
129
     */
130
    virtual bool doesSupport(cObject *obj) const {return false;}
131
132
    /**
133
     * Returns the descriptor for the base class, if available.
134
     */
135
    virtual cClassDescriptor *getBaseClassDescriptor() const;
136
137
    /**
138
     * Returns true if cObject's class descriptor is present on the inheritance chain.
139
     */
140
    bool extendsCObject() const;
141
142
    /**
143
     * Returns the number of base classes up to the root -- as far as
144
     * it is reflected in the descriptors.
145
     */
146
    int getInheritanceChainLength() const;
147
148
    /**
149
     * Returns the value of the given property of the descriptor as a single string.
150
     * Returns NULL if there is no such property. For structured property values
151
     * (with multiple keys and/or list(s) inside), the value is returned as a
152
     * single unparsed string.
153
     */
154
    virtual const char *getProperty(const char *propertyname) const = 0;
155
156
    /**
157
     * Must be redefined in subclasses to return the number of fields
158
     * in the client object.
159
     */
160
    virtual int getFieldCount(void *object) const = 0;
161
162
    /**
163
     * Must be redefined in subclasses to return the name of a field
164
     * in the client object.
165
     * The argument must be in the 0..getFieldCount()-1 range, inclusive.
166
     */
167
    virtual const char *getFieldName(void *object, int field) const = 0;
168
169
    /**
170
     * Returns the index of the field with the given name, or -1 if not found.
171
     * cClassDescriptor provides an default implementation, but it is
172
     * recommended to replace it in subclasses with a more efficient version.
173
     */
174
    virtual int findField(void *object, const char *fieldName) const;
175
176
    /**
177
     * Must be redefined in subclasses to return the type flags of a field
178
     * in the client object. Flags is a binary OR of the following:
179
     * FD_ISARRAY, FD_ISCOMPOUND, FD_ISPOINTER, FD_ISCOWNEDOBJECT, FD_ISCOBJECT.
180
     * The argument must be in the 0..getFieldCount()-1 range, inclusive.
181
     */
182
    virtual unsigned int getFieldTypeFlags(void *object, int field) const = 0;
183
184
    /** @name Utility functions based on getFieldTypeFlags() */
185
    //@{
186
    bool getFieldIsArray(void *object, int field) const {return getFieldTypeFlags(object, field) & FD_ISARRAY;}
187
    bool getFieldIsCompound(void *object, int field) const {return getFieldTypeFlags(object, field) & FD_ISCOMPOUND;}
188
    bool getFieldIsPointer(void *object, int field) const {return getFieldTypeFlags(object, field) & FD_ISPOINTER;}
189
    bool getFieldIsCObject(void *object, int field) const {return getFieldTypeFlags(object, field) & (FD_ISCOBJECT|FD_ISCOWNEDOBJECT);}
190
    bool getFieldIsCOwnedObject(void *object, int field) const {return getFieldTypeFlags(object, field) & FD_ISCOWNEDOBJECT;}
191
    bool getFieldIsEditable(void *object, int field) const {return getFieldTypeFlags(object, field) & FD_ISEDITABLE;}
192
    //@}
193
194
    /**
195
     * Returns the name of the class on which the given field was declared.
196
     */
197
    virtual const char *getFieldDeclaredOn(void *object, int field) const;
198
199
    /**
200
     * Must be redefined in subclasses to return the type of a field
201
     * in the client object as a string.
202
     * The argument must be in the 0..getFieldCount()-1 range, inclusive.
203
     */
204
    virtual const char *getFieldTypeString(void *object, int field) const = 0;
205
206
    /**
207
     * Returns the value of the given property of the field as a single string.
208
     * Returns NULL if there is no such property. For structured property values
209
     * (with multiple keys and/or list(s) inside), the value is returned as a
210
     * single unparsed string.
211
     */
212
    virtual const char *getFieldProperty(void *object, int field, const char *propertyname) const = 0;
213
214
    /**
215
     * Must be redefined in subclasses to return the array size of a field
216
     * in the client object. If the field is not an array, it should return 0.
217
     */
218
    virtual int getArraySize(void *object, int field) const = 0;
219
220
    /**
221
     * Must be redefined in subclasses to return the value of the given field
222
     * in the client object as a string. For compound fields, the message
223
     * compiler generates code which calls operator<<.
224
     */
225
    virtual std::string getFieldAsString(void *object, int field, int i) const = 0;
226
227
    /**
228
     * DEPRECATED: Use the getFieldAsString(void*, int, int) instead.
229
     */
230
    _OPPDEPRECATED virtual bool getFieldAsString(void *object, int field, int i, char *buf, int bufsize) const;
231
232
    /**
233
     * Must be redefined in subclasses to set the value of a field
234
     * in the client object from the given value string.
235
     * Returns true if successful, and false if an error occurred or the
236
     * field does not support setting.
237
     */
238
    virtual bool setFieldAsString(void *object, int field, int i, const char *value) const = 0;
239
240
    /**
241
     * Must be redefined in subclasses to return the type name of a compound field
242
     * in the client object. If it is a pointer, the "*" is removed. The return value
243
     * may be used then as classname to create a descriptor object for this compound field.
244
     */
245
    virtual const char *getFieldStructName(void *object, int field) const = 0;
246
247
    /**
248
     * Must be redefined in subclasses to return the pointer of a compound field
249
     * in the client object.
250
     */
251
    virtual void *getFieldStructPointer(void *object, int field, int i) const = 0;
252
    //@}
253
};
254
255
NAMESPACE_END
256
257
258
#endif
259