msgcppgenerator.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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;
00075 StringVector namespacenamevector;
00076
00077
00078 MsgCppGeneratorOptions opts;
00079
00080 protected:
00081 typedef std::map<std::string, std::string> Properties;
00082
00083 class ClassInfo {
00084 public:
00085 class FieldInfo {
00086 public:
00087 NEDElement *nedElement;
00088
00089 std::string fname;
00090 std::string ftype;
00091 std::string ftypeqname;
00092 std::string fval;
00093 bool fisabstract;
00094 bool fispointer;
00095 bool fisarray;
00096 std::string farraysize;
00097 Properties fprops;
00098
00099
00100 std::string fkind;
00101 ClassType classtype;
00102 std::string datatype;
00103 std::string argtype;
00104 std::string rettype;
00105 std::string var;
00106 std::string argname;
00107 std::string varsize;
00108 std::string fsizetype;
00109 std::string getter;
00110 std::string setter;
00111 std::string alloc;
00112 std::string getsize;
00113 std::string tostring;
00114 std::string fromstring;
00115 std::string maybe_c_str;
00116 std::string enumname;
00117 std::string enumqname;
00118 bool fnopack;
00119 bool feditable;
00120 bool fopaque;
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;
00129 std::string msgname;
00130 std::string msgbase;
00131 Properties props;
00132
00133 bool gap;
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;
00141 Fieldlist baseclassFieldlist;
00142
00143 bool generate_class;
00144 bool generate_descriptor;
00145 bool generate_setters_in_descriptor;
00146
00147
00148 std::string msgqname;
00149 std::string msgbaseqname;
00150
00151 StringVector 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);
00187 void extractClassDecl(NEDElement *node);
00188 Properties extractPropertiesOf(NEDElement *node);
00189 void prepareFieldForCodeGeneration(ClassInfo& info, ClassInfo::FieldInfo *it);
00190 void prepareForCodeGeneration(ClassInfo& classInfo);
00191 EnumInfo extractEnumInfo(EnumElement *node);
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