msgcppgenerator.h

Go to the documentation of this file.
00001 //==========================================================================
00002 //  MSGCPPGENERATOR.H - part of
00003 //
00004 //                     OMNeT++/OMNEST
00005 //            Discrete System Simulation in C++
00006 //
00007 //==========================================================================
00008 
00009 /*--------------------------------------------------------------*
00010   Copyright (C) 2002-2008 Andras Varga
00011   Copyright (C) 2006-2008 OpenSim Ltd.
00012 
00013   This file is distributed WITHOUT ANY WARRANTY. See the file
00014   `license' for details on this and other legal matters.
00015 *--------------------------------------------------------------*/
00016 
00017 #ifndef __MSGCPPGENERATOR_H
00018 #define __MSGCPPGENERATOR_H
00019 
00020 #include <iostream>
00021 #include <string>
00022 #include <vector>
00023 #include <map>
00024 #include "nedelements.h"
00025 #include "nederror.h"
00026 
00027 NAMESPACE_BEGIN
00028 
00029 struct MsgCppGeneratorOptions
00030 {
00031     std::string exportdef;
00032     bool generate_classes;
00033     bool generate_descriptors;
00034     bool generate_setters_in_descriptors;
00035 
00036     MsgCppGeneratorOptions() :
00037         generate_classes(true),
00038         generate_descriptors(true),
00039         generate_setters_in_descriptors(true)
00040         {}
00041 };
00042 
00049 class NEDXML_API MsgCppGenerator
00050 {
00051   public:
00052     typedef std::vector<std::string> StringVector;
00053     enum ClassType {UNKNOWN = 0, STRUCT, COBJECT, COWNEDOBJECT, CNAMEDOBJECT, FOREIGN};
00054   protected:
00055     struct TypeDesc
00056     {
00057         const char *fromstring;
00058         const char *tostring;
00059         TypeDesc()
00060             : fromstring(0), tostring(0) {};
00061         TypeDesc(const char *fromstring, const char *tostring)
00062             : fromstring(fromstring), tostring(tostring) {};
00063     };
00064     typedef std::map<std::string,TypeDesc> TypeMap;
00065     static const TypeMap PRIMITIVE_TYPES;
00066 
00067     std::string hFilename;
00068     std::string ccFilename;
00069     std::ostream *hOutp;
00070     std::ostream *ccOutp;
00071     NEDErrorStore *errors;
00072     std::map<std::string,ClassType> classtype;
00073     std::map<std::string,std::string> enumtype;
00074     std::string namespacename;      // as MSG
00075     StringVector namespacenamevector;   // namespacename split by '::'
00076 
00077     // command line options:
00078     MsgCppGeneratorOptions opts;
00079 
00080   protected:
00081     typedef std::map<std::string, std::string> Properties;  //FIXME kell egy reszletesebb modell...
00082 
00083     class ClassInfo {
00084       public:
00085         class FieldInfo {
00086           public:
00087             NEDElement *nedElement;     // pointer to NED element
00088 
00089             std::string fname;      // field name in MSG
00090             std::string ftype;      // field type in MSG
00091             std::string ftypeqname; // fully qualified C++ name of type //TODO should not be needed
00092             std::string fval;       // value (or empty)
00093             bool fisabstract;
00094             bool fispointer;
00095             bool fisarray;
00096             std::string farraysize; // array size in MSG, maybe empty for dynamic arrays
00097             Properties fprops;      // field properties (name, first value of default key)
00098 
00099             // data needed for code generation
00100             std::string fkind;
00101             ClassType classtype;  // cobject/cnamedobject/cownedobject/...
00102             std::string datatype;   // member C++ datatype
00103             std::string argtype;    // setter C++ argument type
00104             std::string rettype;    // getter C++ return type
00105             std::string var;    // name of data member variable
00106             std::string argname;    // setter argument name
00107             std::string varsize;    // data member to store array size
00108             std::string fsizetype;  // type for storing array size
00109             std::string getter;     // getter function name
00110             std::string setter;     // Setter function name
00111             std::string alloc;      // setArraySize() function name
00112             std::string getsize;    // array size getter function name
00113             std::string tostring;   // function to convert datamember to string
00114             std::string fromstring;   // function to convert string to datamember
00115             std::string maybe_c_str;       // uses ".c_str()"
00116             std::string enumname;
00117             std::string enumqname;
00118             bool fnopack;           // @nopack(true)
00119             bool feditable;         // @editable(true)
00120             bool fopaque;         // @opaque(true)        // TODO: @opaque should rather be the attribute of the field's type, not the field itself
00121 
00122           public:
00123             FieldInfo() : nedElement(NULL), fisabstract(false), fispointer(false), fisarray(false), classtype(UNKNOWN), fnopack(false), feditable(false),fopaque(false) {}
00124         };
00125         typedef std::vector<FieldInfo> Fieldlist;
00126 
00127         NEDElement *nedElement;
00128         std::string keyword;        // struct/class/packet from MSG
00129         std::string msgname;        // class name from MSG
00130         std::string msgbase;        // base class name from MSG
00131         Properties props;           // class properties
00132 
00133         bool gap;                   // true if @customize
00134         bool omitgetverb;
00135         ClassType classtype;
00136         std::string msgclass;
00137         std::string realmsgclass;
00138         std::string msgbaseclass;
00139         std::string msgdescclass;
00140         Fieldlist fieldlist;        // list of fields
00141         Fieldlist baseclassFieldlist;   //modified baseclass fields, e.g. baseclass.basefield = value
00142 
00143         bool generate_class;
00144         bool generate_descriptor;
00145         bool generate_setters_in_descriptor;
00146 
00147         //TODO kellenek ezek?
00148         std::string msgqname;
00149         std::string msgbaseqname;
00150 
00151         StringVector implements;    //value vector from @implements
00152 
00153       public:
00154         ClassInfo() : nedElement(NULL), gap(false), omitgetverb(false), classtype(UNKNOWN),
00155               generate_class(true), generate_descriptor(true), generate_setters_in_descriptor(true) {}
00156     };
00157 
00158     class EnumInfo
00159     {
00160       public:
00161         class EnumItem
00162         {
00163           public:
00164             NEDElement *nedElement;
00165             std::string name;
00166             std::string value;
00167             std::string comment;
00168           public:
00169             EnumItem() : nedElement(NULL) {}
00170         };
00171 
00172         EnumElement *nedElement;
00173         std::string enumName;
00174         std::string enumQName;
00175         typedef std::vector<EnumItem> FieldList;
00176         FieldList fieldlist;
00177       public:
00178         EnumInfo() : nedElement(NULL) {}
00179     };
00180   protected:
00181     std::string prefixWithNamespace(const std::string& s);
00182     StringVector lookupExistingClassName(const std::string& s);
00183     StringVector lookupExistingEnumName(const std::string& s);
00184     bool isClassDeclared(const std::string& s) { return classtype.find(s) != classtype.end(); }
00185     ClassType getClassType(const std::string& s);
00186     ClassInfo extractClassInfo(NEDElement *node); // accepts StructElement, ClassElement, MessageElement, PacketElement
00187     void extractClassDecl(NEDElement *node); // accepts StructElementDecl, ClassElementDecl, MessageElementDecl, PacketElementDecl
00188     Properties extractPropertiesOf(NEDElement *node);
00189     void prepareFieldForCodeGeneration(ClassInfo& info, ClassInfo::FieldInfo *it);
00190     void prepareForCodeGeneration(ClassInfo& classInfo);
00191     EnumInfo extractEnumInfo(EnumElement *node); // accepts EnumElement
00192     void generateClass(const ClassInfo& classInfo);
00193     void generateStruct(const ClassInfo& classInfo);
00194     void generateDescriptorClass(const ClassInfo& a);
00195     void generateEnum(const EnumInfo& enumInfo);
00196     void generateNamespaceBegin(NEDElement *element);
00197     void generateNamespaceEnd();
00198     std::string generatePreComment(NEDElement *nedElement);
00199     void generateTemplates();
00200     bool getPropertyAsBool(const Properties& p, const char *name, bool defval);
00201     std::string getProperty(const Properties& p, const char *name, const std::string& defval = std::string());
00202 
00207     void generate(MsgFileElement *fileElement);
00208 
00209   public:
00213     MsgCppGenerator(NEDErrorStore *errors, const MsgCppGeneratorOptions& options);
00214 
00218     ~MsgCppGenerator();
00219 
00223     void generate(MsgFileElement *fileElement, const char *hFile, const char *ccFile);
00224 };
00225 
00226 NAMESPACE_END
00227 
00228 
00229 #endif
00230 
00231 
Generated on Tue Dec 2 11:16:31 2014 for OMNeT++ NEDXML by  doxygen 1.6.3