msgtypetable.h Source File

NEDXML API: msgtypetable.h Source File
NEDXML API
msgtypetable.h
1//==========================================================================
2// MSGTYPETABLE.H - part of
3//
4// OMNeT++/OMNEST
5// Discrete System Simulation in C++
6//
7//==========================================================================
8
9/*--------------------------------------------------------------*
10 Copyright (C) 2002-2017 Andras Varga
11 Copyright (C) 2006-2017 OpenSim Ltd.
12
13 This file is distributed WITHOUT ANY WARRANTY. See the file
14 `license' for details on this and other legal matters.
15*--------------------------------------------------------------*/
16
17#ifndef __OMNETPP_NEDXML_MSGTYPETABLE_H
18#define __OMNETPP_NEDXML_MSGTYPETABLE_H
19
20#include <iostream>
21#include <sstream>
22#include <string>
23#include <vector>
24#include <map>
25#include <set>
26
27#include "nedxmldefs.h"
28
29namespace omnetpp {
30namespace nedxml {
31
32class CplusplusElement;
33
39class NEDXML_API MsgTypeTable
40{
41 public:
42 typedef std::vector<std::string> StringVector;
43
44 class Property
45 {
46 protected:
47 ASTNode *astNode = nullptr; // pointer to property element in AST
48 std::string name;
49 std::string index;
50 std::map<std::string, StringVector> propertyValue;
51 public:
52 Property() {}
53 Property(const std::string& name, const std::string& index, ASTNode *astNode) : astNode(astNode), name(name), index(index) {}
54 Property(const Property& other) = default;
55 const std::string& getName() const {return name;}
56 const std::string& getIndex() const {return index;}
57 std::string getIndexedName() const;
58 ASTNode *getASTNode() const {return astNode;}
59 StringVector getValue(const std::string& key="") const;
60 std::string getValueAsString() const;
61 void addValue(const std::string& key, const std::string& value);
62 };
63
64 class Properties
65 {
66 protected:
67 std::vector<Property> properties;
68 public:
69 void add(const Property& p) {properties.push_back(p);}
70 bool empty() const {return properties.empty();}
71 const std::vector<Property>& getAll() const {return properties;}
72 const Property *get(const std::string& name, const std::string& index="") const;
73 bool contains(const std::string& name, const std::string& index="") const {return get(name, index) != nullptr;}
74 };
75
76 class FieldInfo {
77 public:
78 ASTNode *astNode; // pointer to field element in AST
79
80 std::string name; // field name in MSG
81 std::string type; // field type in MSG, without 'const' and '*' modifiers
82 std::string typeQName; // fully qualified C++ name of type
83 std::string value; // value (or empty). for arrays, this is the value for one array element (we have no syntax for initializing with a list of values)
84 std::string symbolicConstant; // symbolic name for local field index in the generated class descriptor
85 bool isAbstract; // "abstract" keyword specified for field
86 bool isConst; // "const" keyword specified for field
87 bool byValue; // @byValue(true); whether value should be passed by value (instead of by reference) in setters/getters
88 bool isPointer; // field is a pointer or pointer array ("*" syntax)
89 bool isOwnedPointer; // from @owned; if true, field is a pointer or pointer array, and allocated memory is owned by the object (needs to be duplicated in dup(), and deleted in destructor).
90 // if field type is also cOwnedObject, take()/drop() calls should be generated
91 bool isArray; // field is an array ("[]" or "[size]" syntax)
92 bool isDynamicArray; // if field is a dynamic array
93 bool isFixedArray; // if field is a fixed-size array
94 std::string arraySize; // if field is an array: array size (string inside the square brackets)
95 Properties props; // field properties (name, first value of default key)
96
97 // data needed for code generation
98 bool isClass; // field type is a class (true) or struct (false)
99 bool iscObject; // field type is derived from cObject
100 bool iscNamedObject; // field type is derived from cNamedObject
101 bool iscOwnedObject; // field type is derived from cOwnedObject
102 std::string baseDataType;// dataType without modifiers like '*' or 'const'
103 std::string dataType; // member C++ datatype
104 std::string argType; // setter C++ argument type
105 std::string returnType; // getter C++ return type
106 std::string mutableReturnType; // mutableGetter C++ return type
107 std::string var; // name of data member variable
108 std::string argName; // setter argument name
109 std::string sizeVar; // data member to store size of dynamic array
110 std::string sizeType; // type of array sizes and array indices
111 std::string getter; // getter function name: "T getter() const;" "const T& getter() const" default value is getFoo
112 std::string getterForUpdate; // mutable getter function name: "T& getterForUpdate();" default value is getFooForUpdate
113 bool hasGetterForUpdate; // whether a getterForUpdate method needs to be generated
114 bool allowReplace; // @allowReplace; whether setter of an owned pointer field is allowed to delete an already-set object
115 std::string remover; // remover function name (for pointers: drop object if owned, set pointer to nullptr, return object)
116 std::string dropper; // deprecated alias for remover
117 std::string clone; // @clone; code to duplicate (one array element of) the field (for owned pointers)
118 std::string setter; // @setter; setter function name
119 std::string inserter; // @inserter; inserter function name (insert element to dynamic array)
120 std::string appender; // @appender; appender function name (append element to dynamic array)
121 std::string eraser; // @eraser; eraser function name (erase element from dynamic array)
122 std::string sizeSetter; // setArraySize() function name
123 std::string sizeGetter; // array size getter function name
124 bool hasStrMethod; // the field's type has an str() method
125 std::string toString; // function to convert data to string, defined in property @toString
126 // if tostring begins with a dot, then it is taken as member function call, parentheses needed in property; otherwise toString is understood as name of a normal function, do not use parentheses
127 // std::string <function>(<datatype>); // @toString(function)
128 // std::string <function>(const <datatype>&); // @toString(function)
129 // const char * <function>(<datatype>); // @toString(function)
130 // const char * <function>(const <datatype>&); // @toString(function)
131 // std::string <datatype>::<function>(...); // @toString(.function(...))
132 // const char * <datatype>::<function>(...); // @toString(.function(...))
133 std::string fromString; // function to convert string to data member, defined in property @fromString
134 // <datatype> <function>(const char *); // @fromString(function)
135 std::string toValue; // function to convert data to cValue, defined in property @toValue; syntax is similar to toString
136 std::string fromValue; // function to convert data from cValue, defined in property @fromValue; syntax is similar to fromString
137 std::string getterConversion; // currently only with strings: ".c_str()"
138 std::string enumName; // from @enum
139 std::string enumQName; // fully qualified type name of enum
140 bool nopack; // @nopack(true)
141 bool isOpaque; // @opaque(true), means that field type is treated as atomic (has no fields), i.e. has no descriptor
142 bool overrideGetter; // @overrideGetter|@override, used when field getter function overrides a function in base class
143 bool overrideSetter; // @overrideSetter|@override, used when field setter function overrides a function in base class
144 bool isCustom; // @custom; if true, do not generate any data member or code for the field.
145 bool isCustomImpl; // @customImpl: if true: do not generate implementation for the field's accessor methods
146
147 // The following members only affect the generated class descriptor, not the class itself
148 bool isEditable; // @editable(true): field value is editable via the descriptor's setFieldValueFromString() method
149 bool isReplaceable; // @replaceable(true): field value is a pointer which can be set via the descriptor's setFieldStructValuePointer() method
150 bool isResizable; // @resizable(true): field is an array whose size can be set via the descriptor's setFieldArraySize() method
151 };
152
153 class EnumItem
154 {
155 public:
156 ASTNode *astNode;
157 std::string name;
158 std::string value;
159 std::string comment;
160 };
161
162 class ClassInfo {
163 public:
164 typedef std::vector<FieldInfo> Fieldlist;
165
166 ASTNode *astNode = nullptr;
167 std::string keyword; // struct/class/packet from MSG
168 std::string name; // class name from MSG
169 std::string qname; // qualified name from MSG (namespace :: name)
170 Properties props; // class properties
171
172 bool classInfoComplete = false; // whether following fields are filled in
173 bool classBeingAnalyzed = false;
174 bool fieldsComplete = false; // whether fieldList and baseclassFieldlist are filled in
175 bool fieldsBeingAnalyzed = false;
176
177 bool isEnumClass = false; // if enum: whether this is to be defined as "enum class" in C++
178 std::string enumBaseType; // if enum: underlying data type, e.g. "uint8_t"
179 bool isDeclaration = false; // if enum: i.e. not a definition
180 std::vector<EnumItem> enumItems; // if enum: list of enum fields
181
182 std::string extendsQName; // fully qualified name of base type
183 std::string extendsName; // base type's name from MSG
184 bool customize; // from @customize
185 bool omitGetVerb; // from @omitGetVerb
186 bool isEnum = false; // whether this is an enum
187 bool isClass; // if isEnum==false: whether this is a class or a struct
188 bool isPolymorphic; // whether the type is polymorphic (has virtual member functions)
189 bool iscObject; // whether type is subclassed from cObject
190 bool iscNamedObject; // whether type is subclassed from cNamedObject
191 bool iscOwnedObject; // whether type is subclassed from cOwnedObject
192 bool isAbstract; // whether class can be instantiated (base class not abstract, and has no abstract fields)
193 bool isImported; // whether this type imported from an another msg file
194 bool subclassable; // whether this type can be subclasses (e.g. "int" or final classes cannot)
195 bool supportsPtr; // whether type supports creating a pointer (or pointer array) from it
196 std::string namespaceName;
197 std::string className; // name of actual C++ class or struct generated (<name>_Base when @customize is present)
198 std::string classQName; // className, but fully qualified
199 std::string realClass; // name of intended C++ class, usually the same as <name>
200 std::string baseClass; // C++ base class
201 std::string descriptorClass; // name of C++ descriptor class to be generated
202 std::string fieldNameSuffix; // member variable suffix, e.g. "_var" or "_m"
203
204 Fieldlist fieldList; // list of fields
205 Fieldlist baseclassFieldlist; // assignments to fields declared in super classes
206
207 bool generateTypeDefinition = true; // whether the code for this class/struct/enum should be generated
208 bool generateDescriptor = true;
209 bool generateSettersInDescriptor = true;
210 bool generateCastFunction = false;
211
212 std::vector<std::string> rootClasses; // root(s) of its C++ class hierarchy
213 StringVector implementsQNames; // qnames of additional base classes, from @implements property
214 std::string beforeChange; // @beforeChange; method to be called before mutator methods
215 std::string str; // @str; expression to be returned from str() method
216
217 std::string classExtraCode; // code to be inserted into the class declaration
218 std::map<std::string, CplusplusElement*> methodCplusplusBlocks; // keyed by method name
219 mutable std::set<std::string> usedMethodCplusplusBlocks; // collects method names; used during generation
220
221 // The following members describe how the class should behave when instantiated as field
222 std::string defaultValue; // default value (or empty)
223 bool isOpaque; // from @opaque
224 bool byValue; // from @byValue, default value is false
225 bool isEditable; // from @editable
226 std::string dataTypeBase; // member C++ datatype
227 std::string argTypeBase; // setter C++ argument type
228 std::string returnTypeBase; // getter C++ return type
229 std::string toString; // function to convert data to string, defined in property @toString
230 std::string fromString; // @fromString; function to convert string to data member, defined in property @fromString
231 std::string toValue; // function to convert data to cValue, defined in property @toValue
232 std::string fromValue; // @fromString; function to convert cValue to data member, defined in property @fromValue
233 std::string clone; // @clone; code to clone a dynamically allocated object of this type (for owned pointer fields)
234 std::string getterConversion; // @getterConversion; conversion from storage type to return type in getters
235 };
236
237 private:
238 Properties globalProperties;
239 std::map<std::string,ClassInfo> definedClasses;
240 std::vector<ASTNode*> importedMsgFiles;
241
242 protected:
243 void initDescriptors();
244 std::string prefixWithNamespace(const std::string& namespaceName, const std::string& name) {
245 return !namespaceName.empty() ? namespaceName + "::" + name : name;
246 }
247
248 public:
249 MsgTypeTable() {initDescriptors();}
250 ~MsgTypeTable();
251 StringVector lookupExistingEnumName(const std::string& name, const std::string& contextNamespace);
252 bool isClassDefined(const std::string& classqname) { return definedClasses.find(classqname) != definedClasses.end(); }
253 ClassInfo *findClassInfo(const std::string& classqname);
254 ClassInfo& getClassInfo(const std::string& classqname);
255 const Properties& getGlobalProperties() const {return globalProperties;}
256 void storeMsgFile(ASTNode *tree) {importedMsgFiles.push_back(tree);}
257 void addGlobalProperty(const Property& p) {globalProperties.add(p);}
258 void addClass(const ClassInfo& classInfo) {definedClasses[classInfo.qname] = classInfo;} // TODO assert not already there
259};
260
261} // namespace nedxml
262} // namespace omnetpp
263
264
265#endif
266
267
Definition astnode.h:88