OMNeT++ NEDXML  5.4.1
msgcompilerold.h
Go to the documentation of this file.
1 //==========================================================================
2 // MSGCOMPILEROLD.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_MSGCOMPILEROLD_H
18 #define __OMNETPP_NEDXML_MSGCOMPILEROLD_H
19 
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 #include <map>
25 #include <set>
26 
27 #include "errorstore.h"
28 #include "msgelements.h"
29 
30 namespace omnetpp {
31 namespace nedxml {
32 
39 {
40  std::string exportDef;
44 
46  generateClasses(true),
47  generateDescriptors(true),
48  generateSettersInDescriptors(true)
49  {}
50 };
51 
61 {
62  public:
63  typedef std::vector<std::string> StringVector;
64 
65  enum ClassType {
66  UNKNOWN = 0,
67  STRUCT, // struct
68  NONCOBJECT, // class not derived from cObject
69  COBJECT, // class derived from cObject
70  COWNEDOBJECT, // class derived from cOwnedObject
71  CNAMEDOBJECT // class derived from cNamedObject
72  };
73 
74  protected:
75  struct TypeDesc
76  {
77  const char *msgTypeName;
78  const char *cppTypeName;
79  const char *fromstring;
80  const char *tostring;
81  const char *emptyValue;
82  };
83 
84  static TypeDesc _PRIMITIVE_TYPES[];
85  typedef std::map<std::string,TypeDesc> TypeDescMap;
86  TypeDescMap PRIMITIVE_TYPES;
87 
88  static const char *_RESERVED_WORDS[];
89  typedef std::set<std::string> WordSet;
90  WordSet RESERVED_WORDS;
91 
92  std::string hFilename;
93  std::string ccFilename;
94  std::ostream *hOutp;
95  std::ostream *ccOutp;
97  std::map<std::string,ClassType> classType;
98  std::map<std::string,std::string> enumType;
99  std::string namespaceName; // as MSG
100  StringVector namespaceNameVector; // namespacename split by '::'
101 
102  // command line options:
104 
105  protected:
106  typedef std::map<std::string, std::string> Properties;
107 
108  class ClassInfo {
109  public:
110  class FieldInfo {
111  public:
113 
114  std::string fname; // field name in MSG
115  std::string ftype; // field type in MSG
116  std::string ftypeqname; // fully qualified C++ name of type
117  std::string fval; // value (or empty)
120  bool fisarray;
121  std::string farraysize; // array size in MSG, maybe empty for dynamic arrays
122  Properties fprops; // field properties (name, first value of default key)
123 
124  // data needed for code generation
125  bool fisprimitivetype; // whether primitive or compound type
126  ClassType classtype; // cobject/cnamedobject/cownedobject/...
127  std::string datatype; // member C++ datatype
128  std::string argtype; // setter C++ argument type
129  std::string rettype; // getter C++ return type
130  std::string var; // name of data member variable
131  std::string argname; // setter argument name
132  std::string varsize; // data member to store array size
133  std::string fsizetype; // type for storing array size
134  std::string getter; // getter function name
135  std::string setter; // Setter function name
136  std::string alloc; // setArraySize() function name
137  std::string getsize; // array size getter function name
138  std::string tostring; // function to convert data to string, defined in property @tostring, default values for primitive types found in MsgCppGenerator::TypeMap MsgCppGenerator::PRIMITIVE_TYPES
139  // if tostring begins a dot, then uses as member function, parentheses needed in property; otherwise tostring implemented as name of a normal function, do not use parentheses
140  // std::string <function>(<datatype>); // @tostring(function)
141  // std::string <function>(const <datatype>&); // @tostring(function)
142  // const char * <function>(<datatype>); // @tostring(function)
143  // const char * <function>(const <datatype>&); // @tostring(function)
144  // std::string <datatype>::<function>(...); // @tostring(.function(...))
145  // const char * <datatype>::<function>(...); // @tostring(.function(...))
146  std::string fromstring; // function to convert string to data member, defined in property @fromstring, default values for primitive types found in MsgCppGenerator::TypeMap MsgCppGenerator::PRIMITIVE_TYPES
147  // <datatype> <function>(const char *); // @fromstring(function)
148  std::string maybe_c_str; // uses ".c_str()"
149  std::string enumname;
150  std::string enumqname;
151  bool fnopack; // @nopack(true)
152  bool feditable; // @editable(true)
153  bool editNotDisabled; // true when field doesn't have property "@editable(false)"
154  bool fopaque; // @opaque(true)
155 
156  public:
157  FieldInfo() : astNode(nullptr), fisabstract(false), fispointer(false), fisarray(false), classtype(UNKNOWN), fnopack(false), feditable(false),fopaque(false) {}
158  };
159  typedef std::vector<FieldInfo> Fieldlist;
160 
162  std::string keyword; // struct/class/packet from MSG
163  std::string msgname; // class name from MSG
164  std::string msgbase; // base class name from MSG
165  Properties props; // class properties
166 
167  bool gap; // true if @customize
170  std::string msgclass;
171  std::string realmsgclass;
172  std::string msgbaseclass;
173  std::string msgdescclass;
174  std::string fieldnamesuffix; // e.g. "_var"
175  Fieldlist fieldlist; // list of fields
176  Fieldlist baseclassFieldlist; //modified baseclass fields, e.g. baseclass.basefield = value
177 
181 
182  std::string msgqname;
183  std::string msgbaseqname;
184 
185  StringVector implements; //value vector from @implements
186 
187  public:
188  ClassInfo() : nedElement(nullptr), gap(false), omitgetverb(false), classtype(UNKNOWN),
189  generate_class(true), generate_descriptor(true), generate_setters_in_descriptor(true) {}
190  };
191 
192  class EnumInfo
193  {
194  public:
195  class EnumItem
196  {
197  public:
199  std::string name;
200  std::string value;
201  std::string comment;
202  public:
203  EnumItem() : nedElement(nullptr) {}
204  };
205 
206  EnumElement *nedElement;
207  std::string enumName;
208  std::string enumQName;
209  typedef std::vector<EnumItem> FieldList;
210  FieldList fieldlist;
211  public:
212  EnumInfo() : nedElement(nullptr) {}
213  };
214 
215  protected:
216  void initDescriptors();
217  std::string prefixWithNamespace(const std::string& s);
218  StringVector lookupExistingClassName(const std::string& s);
219  StringVector lookupExistingEnumName(const std::string& s);
220  bool isClassDeclared(const std::string& classqname) { return classType.find(classqname) != classType.end(); }
221  void addClassType(const std::string& classqname, ClassType type, ASTNode *context);
222  ClassType getClassType(const std::string& classqname);
223  ClassInfo extractClassInfo(ASTNode *node); // accepts StructElement, ClassElement, MessageElement, PacketElement
224  void extractClassDecl(ASTNode *node); // accepts StructElementDecl, ClassElementDecl, MessageElementDecl, PacketElementDecl
225  Properties extractPropertiesOf(ASTNode *node);
226  void prepareFieldForCodeGeneration(ClassInfo& info, ClassInfo::FieldInfo *it);
227  void prepareForCodeGeneration(ClassInfo& classInfo);
228  EnumInfo extractEnumInfo(EnumElement *node); // accepts EnumElement
229  void generateClass(const ClassInfo& classInfo);
230  void generateStruct(const ClassInfo& classInfo);
231  void generateDescriptorClass(const ClassInfo& a);
232  void generateEnum(const EnumInfo& enumInfo);
233  void generateNamespaceBegin(ASTNode *element);
234  void generateNamespaceEnd();
235  std::string generatePreComment(ASTNode *nedElement);
236  void generateTemplates();
237  bool getPropertyAsBool(const Properties& p, const char *name, bool defval);
238  std::string getProperty(const Properties& p, const char *name, const std::string& defval = std::string());
239  void generate(MsgFileElement *fileElement);
240  std::string makeFuncall(const std::string& var, const std::string& funcTemplate, bool withIndex=false, const std::string& value="");
241 
242  public:
246  MsgCompilerOld(ErrorStore *errors, const MsgCompilerOptionsOld& options);
247 
251  ~MsgCompilerOld();
252 
257  void generate(MsgFileElement *fileElement, const char *hFile, const char *ccFile);
258 };
259 
260 } // namespace nedxml
261 } // namespace omnetpp
262 
263 
264 #endif
265 
266 
std::string value
Definition: msgcompilerold.h:200
std::set< std::string > WordSet
Definition: msgcompilerold.h:89
const char * cppTypeName
Definition: msgcompilerold.h:78
std::string ccFilename
Definition: msgcompilerold.h:93
std::string maybe_c_str
Definition: msgcompilerold.h:148
#define NEDXML_API
Definition: nedxmldefs.h:31
bool generateSettersInDescriptors
Definition: msgcompilerold.h:43
bool generateClasses
Definition: msgcompilerold.h:41
std::string msgdescclass
Definition: msgcompilerold.h:173
std::map< std::string, ClassType > classType
Definition: msgcompilerold.h:97
std::string fieldnamesuffix
Definition: msgcompilerold.h:174
Fieldlist baseclassFieldlist
Definition: msgcompilerold.h:176
std::string fname
Definition: msgcompilerold.h:114
FieldList fieldlist
Definition: msgcompilerold.h:210
std::string tostring
Definition: msgcompilerold.h:138
Definition: msgcompilerold.h:69
bool generate_class
Definition: msgcompilerold.h:178
std::string exportDef
Definition: msgcompilerold.h:40
std::map< std::string, TypeDesc > TypeDescMap
Definition: msgcompilerold.h:85
std::string setter
Definition: msgcompilerold.h:135
FieldInfo()
Definition: msgcompilerold.h:157
const char * fromstring
Definition: msgcompilerold.h:79
std::string name
Definition: msgcompilerold.h:199
const char * msgTypeName
Definition: msgcompilerold.h:77
const char * emptyValue
Definition: msgcompilerold.h:81
StringVector namespaceNameVector
Definition: msgcompilerold.h:100
bool fisabstract
Definition: msgcompilerold.h:118
std::string datatype
Definition: msgcompilerold.h:127
Definition: msgcompilerold.h:75
std::string varsize
Definition: msgcompilerold.h:132
std::string var
Definition: msgcompilerold.h:130
bool omitgetverb
Definition: msgcompilerold.h:168
ASTNode * nedElement
Definition: msgcompilerold.h:161
std::string alloc
Definition: msgcompilerold.h:136
std::string argname
Definition: msgcompilerold.h:131
Definition: msgcompilerold.h:192
std::string fval
Definition: msgcompilerold.h:117
std::string msgbaseclass
Definition: msgcompilerold.h:172
bool fopaque
Definition: msgcompilerold.h:154
ASTNode * astNode
Definition: msgcompilerold.h:112
ASTNode * nedElement
Definition: msgcompilerold.h:198
Properties props
Definition: msgcompilerold.h:165
const char * tostring
Definition: msgcompilerold.h:80
EnumInfo()
Definition: msgcompilerold.h:212
EnumItem()
Definition: msgcompilerold.h:203
std::string fsizetype
Definition: msgcompilerold.h:133
std::string farraysize
Definition: msgcompilerold.h:121
bool feditable
Definition: msgcompilerold.h:152
std::string msgbase
Definition: msgcompilerold.h:164
Definition: msgcompilerold.h:68
ClassType classtype
Definition: msgcompilerold.h:126
ClassInfo()
Definition: msgcompilerold.h:188
bool generate_setters_in_descriptor
Definition: msgcompilerold.h:180
Definition: msgcompilerold.h:67
StringVector implements
Definition: msgcompilerold.h:185
std::string comment
Definition: msgcompilerold.h:201
std::vector< FieldInfo > Fieldlist
Definition: msgcompilerold.h:159
std::map< std::string, std::string > enumType
Definition: msgcompilerold.h:98
std::string msgclass
Definition: msgcompilerold.h:170
std::string enumqname
Definition: msgcompilerold.h:150
std::string getsize
Definition: msgcompilerold.h:137
bool fnopack
Definition: msgcompilerold.h:151
std::string getter
Definition: msgcompilerold.h:134
std::string msgbaseqname
Definition: msgcompilerold.h:183
Definition: msgcompilerold.h:70
EnumElement * nedElement
Definition: msgcompilerold.h:206
std::vector< EnumItem > FieldList
Definition: msgcompilerold.h:209
Generates C++ code from a MSG file. Covers the MSG language of OMNeT++ 4.x, and 5.x up to 5.2.
Definition: msgcompilerold.h:60
std::vector< std::string > StringVector
Definition: msgcompilerold.h:63
bool gap
Definition: msgcompilerold.h:167
Definition: msgcompilerold.h:108
std::string ftype
Definition: msgcompilerold.h:115
std::string rettype
Definition: msgcompilerold.h:129
Options for MsgCompilerOld.
Definition: msgcompilerold.h:38
std::string argtype
Definition: msgcompilerold.h:128
ClassType classtype
Definition: msgcompilerold.h:169
bool fisarray
Definition: msgcompilerold.h:120
bool fisprimitivetype
Definition: msgcompilerold.h:125
std::string namespaceName
Definition: msgcompilerold.h:99
Properties fprops
Definition: msgcompilerold.h:122
Definition: astbuilder.h:25
std::string fromstring
Definition: msgcompilerold.h:146
std::string enumQName
Definition: msgcompilerold.h:208
bool fispointer
Definition: msgcompilerold.h:119
bool generate_descriptor
Definition: msgcompilerold.h:179
std::string msgqname
Definition: msgcompilerold.h:182
std::string msgname
Definition: msgcompilerold.h:163
std::string enumName
Definition: msgcompilerold.h:207
bool isClassDeclared(const std::string &classqname)
Definition: msgcompilerold.h:220
std::map< std::string, std::string > Properties
Definition: msgcompilerold.h:106
std::string realmsgclass
Definition: msgcompilerold.h:171
bool generateDescriptors
Definition: msgcompilerold.h:42
std::string enumname
Definition: msgcompilerold.h:149
Definition: astnode.h:75
ErrorStore * errors
Definition: msgcompilerold.h:96
Definition: errorstore.h:37
std::ostream * ccOutp
Definition: msgcompilerold.h:95
Fieldlist fieldlist
Definition: msgcompilerold.h:175
std::string keyword
Definition: msgcompilerold.h:162
bool editNotDisabled
Definition: msgcompilerold.h:153
ClassType
Definition: msgcompilerold.h:65
MsgCompilerOptionsOld opts
Definition: msgcompilerold.h:103
std::string hFilename
Definition: msgcompilerold.h:92
WordSet RESERVED_WORDS
Definition: msgcompilerold.h:90
std::ostream * hOutp
Definition: msgcompilerold.h:94
std::string ftypeqname
Definition: msgcompilerold.h:116
TypeDescMap PRIMITIVE_TYPES
Definition: msgcompilerold.h:86
MsgCompilerOptionsOld()
Definition: msgcompilerold.h:45