//attributes have to be simple types, either enum, string, int, double, bool private static void AddXMLAttributte(FileLevelInfo info, XmlAttributeAttribute xa, ClassSnippet snip, FieldInfo finfo) { string fieldName = xa.AttributeName; string dtype = SetupSimpleType(info, snip, xa.DataType, fieldName, finfo.FieldType.Name); snip.fromXml += "\t"+fieldName+" = FromString_" + dtype +"(pEm, pEm->Attribute(\"" + fieldName + "\"));\n"; snip.toXml += "\tpEm->SetAttribute(\"" + fieldName + "\", ToString_" + dtype + "(" + fieldName + "));\n"; snip.declarations += "\t" + dtype + " " + fieldName + ";\n"; }
private static void AddSimpleXMLElement(FileLevelInfo info, XmlElementAttribute xea, ClassSnippet snip, FieldInfo finfo) { string fieldName = xea.ElementName; string dtype = SetupSimpleType(info, snip, xea.DataType, fieldName, finfo.FieldType.Name); string nodeGetter = "pNode->FirstChildElement(\"" + fieldName + "\")"; snip.fromXml += "\t" + fieldName + " = FromString_" + dtype + "(pNode, GetValue(\"" + fieldName + "\", pNode->FirstChildElement(\"" + fieldName + "\"), pNode));\n"; snip.toXml += "\tTiXmlElement* n" + fieldName + " = new TiXmlElement(\"" + fieldName+ "\");\n"; snip.toXml += "\tTiXmlText* nt" + fieldName + " = new TiXmlText(ToString_" + dtype + "(" + fieldName + "));\n"; snip.toXml += "\tn" + fieldName + "->LinkEndChild(nt"+fieldName+");\n"; snip.toXml += "\tpEm->LinkEndChild(n" + fieldName + ");\n"; snip.declarations += "\t" + dtype + " " + fieldName + ";\n"; }
private static string SetupSimpleType(FileLevelInfo info, ClassSnippet snip, string dtype, string fieldName, string fieldType) { if (dtype == null || dtype.Length == 0) { if (fieldType != null && info.enumNames.ContainsKey(fieldType)) { fieldName = fieldType; } if (info.enumNames.ContainsKey(fieldName)) { dtype = (string)info.enumNames[fieldName]; snip.dependencies.Add(fieldName); return(dtype); } else { throw new Exception("Bad simple type: " + fieldName + " : " + dtype); } } if (dtype == "integer") { dtype = "int"; } if (dtype == "boolean") { dtype = "bool"; } if (!info.knownSimpleTypes.ContainsKey(dtype)) { if (!info.unKnownSimpleTypes.ContainsKey(dtype)) { info.unKnownSimpleTypes.Add(dtype, dtype); } dtype = "string"; } return(dtype); }
//attributes have to be simple types, either enum, string, int, double, bool private static void AddXMLAttributte(FileLevelInfo info, XmlAttributeAttribute xa, ClassSnippet snip, FieldInfo finfo) { string fieldName = xa.AttributeName; string dtype = SetupSimpleType(info, snip, xa.DataType, fieldName, finfo.FieldType.Name); snip.fromXml += "\t" + fieldName + " = FromString_" + dtype + "(pEm, pEm->Attribute(\"" + fieldName + "\"));\n"; snip.toXml += "\tpEm->SetAttribute(\"" + fieldName + "\", ToString_" + dtype + "(" + fieldName + "));\n"; snip.declarations += "\t" + dtype + " " + fieldName + ";\n"; }
private static void AddSimpleXMLElement(FileLevelInfo info, XmlElementAttribute xea, ClassSnippet snip, FieldInfo finfo) { string fieldName = xea.ElementName; string dtype = SetupSimpleType(info, snip, xea.DataType, fieldName, finfo.FieldType.Name); string nodeGetter = "pNode->FirstChildElement(\"" + fieldName + "\")"; snip.fromXml += "\t" + fieldName + " = FromString_" + dtype + "(pNode, GetValue(\"" + fieldName + "\", pNode->FirstChildElement(\"" + fieldName + "\"), pNode));\n"; snip.toXml += "\tTiXmlElement* n" + fieldName + " = new TiXmlElement(\"" + fieldName + "\");\n"; snip.toXml += "\tTiXmlText* nt" + fieldName + " = new TiXmlText(ToString_" + dtype + "(" + fieldName + "));\n"; snip.toXml += "\tn" + fieldName + "->LinkEndChild(nt" + fieldName + ");\n"; snip.toXml += "\tpEm->LinkEndChild(n" + fieldName + ");\n"; snip.declarations += "\t" + dtype + " " + fieldName + ";\n"; }
//when we add an element it either needs to be private static void AddXMLElement(FileLevelInfo info, XmlElementAttribute xea, ClassSnippet snip, FieldInfo finfo) { if (xea.Type == null) { AddSimpleXMLElement(info, xea, snip, finfo); return; } string typeName = xea.Type.ToString(); string[] ss = typeName.Split(new char[] { '.' }); typeName = ss[ss.Length - 1]; string typesuffix = "_t"; string fromNamespace = getCppNamespace(xea.Namespace); string fullName; string swigFullName; string ename = xea.ElementName; if (xea.Type.Namespace == "System") { if (!info.knownSimpleTypes.ContainsKey(typeName.ToLower())) { throw new Exception("Unknown system array type : " + typeName); } fromNamespace = info.nSpace; typesuffix = ""; typeName = (string)info.knownSimpleTypes[typeName.ToLower()]; fullName = typeName; swigFullName = info.nSpace + "::" + typeName; if (!info.collections.ContainsKey(typeName)) { info.collections.Add(typeName, typeName); } } else if (fromNamespace == info.nSpace) { if (typeName != snip.type.Name) { //don't make a class dependent on itself. snip.dependencies.Add(typeName); } fullName = typeName; swigFullName = info.nSpace + "::" + typeName; } else { fullName = fromNamespace + "::" + typeName; swigFullName = fullName; if (!info.includes.ContainsKey(fromNamespace)) { info.includes.Add(fromNamespace, fullName); } } bool isCollection = false; if (info.collections.ContainsKey(typeName)) { snip.declarations += "#ifdef SWIG\n%immutable " + ename + "Vector;\n#endif\n"; snip.classDeclaration = "#ifdef SWIG\n}\n%template(" + ename + "_c) std::vector<" + swigFullName + typesuffix + "*>;\nnamespace " + info.nSpace + "{\n#endif\n" + snip.classDeclaration; snip.declarations += "\tprivate: collectedType < " + fullName + typesuffix + " > " + ename + ";\n"; snip.declarations += "\tpublic: vector < " + fullName + typesuffix + "* >& " + ename + "Vector;\n"; if (snip.constructor == null) { snip.constructor = snip.className + "::" + snip.className + "():\n\t\t"; } else { snip.constructor += ",\n\t\t"; } snip.constructor += ename + "(\"" + typeName + "\"), " + ename + "Vector(" + ename + ".collection)"; if (snip.needsChildren != null) { snip.needsChildren += " && "; } snip.needsChildren += ename + ".size() == 0"; isCollection = true; } else { snip.declarations += "#ifdef SWIG\n%immutable " + ename + ";\n#endif\n"; snip.declarations += "\t" + fullName + typesuffix + " " + ename + ";\n"; } snip.toXml += "\t" + ename + ".toXml(pEm, true, aIgnoreValid);\n"; string nodeName = "pNode->FirstChildElement(\"" + ename + "\")"; if (ename == snip.type.Name || isCollection) { nodeName = "pNode"; } snip.fromXml += "\t" + ename + ".fromXml(" + nodeName + ");\n"; }
private static void BuildClassSnippets(FileLevelInfo info, ClassSnippet snip) { Type t = snip.type; FieldInfo[] fi = t.GetFields(); snip.className = t.Name + "_t"; snip.classDeclaration = "class " + snip.className + " : "; //set the base class string baseClass = null; if (t.BaseType != typeof(System.Object)) { string nspace = getCppNamespace(t.BaseType.Namespace); if (t.BaseType.Namespace != t.Namespace) { if (!info.includes.ContainsKey(nspace)) { info.includes.Add(nspace, t.BaseType.FullName); } } else { snip.dependencies.Add(t.BaseType.Name); } baseClass = nspace + "::" + t.BaseType.Name + "_t"; snip.classDeclaration += "public " + baseClass; } else { snip.classDeclaration += "public IXMLDataBound "; } snip.classDeclaration += "{\npublic:\n\tvoid toXml(TiXmlNode* pParent, bool aCreateNode, bool aIgnoreValid);\n\tvoid fromXml(TiXmlNode* pNode);\n"; snip.constructor = null; snip.declarations = ""; snip.fromXml = "void " + snip.className + " :: fromXml(TiXmlNode* pNode){\n"; snip.toXml = ""; snip.needsChildren = null; snip.fromXml += "\tif(pNode == NULL)return;\n"; snip.fromXml += "\tXML_CHECK(\"" + t.Name + "\",pNode->Type() == TiXmlNode::ELEMENT);\n"; snip.fromXml += "\tTiXmlElement* pEm = pNode->ToElement();\n"; snip.fromXml += "\tXML_CHECK(\"" + t.Name + "\",pEm != 0);\n"; if (baseClass != null) { snip.fromXml += "\tthis->" + baseClass + "::fromXml(pNode);\n"; } foreach (FieldInfo f in fi) { if (f.DeclaringType != t) { //Console.WriteLine("Skipped " + f.Name + " in " + t.ToString() + " since its allready defined in: " + f.DeclaringType.ToString()); continue; } object[] atts = f.GetCustomAttributes(true); foreach (Attribute a in atts) { if (a.GetType() == typeof(XmlAttributeAttribute)) { AddXMLAttributte(info, (XmlAttributeAttribute)a, snip, f); } if (a.GetType() == typeof(XmlElementAttribute)) { AddXMLElement(info, (XmlElementAttribute)a, snip, f); } } } if (snip.constructor == null) { snip.constructor = ""; } else { //when we have had to construct snip.constructor += "{};\n"; snip.classDeclaration += "\t" + snip.className + "();\n\n"; } snip.fromXml += "\tvalid=true;\n};\n"; string tempToXml = snip.toXml; snip.toXml = "void " + snip.className + " :: toXml(TiXmlNode* pParent, bool aCreateNode, bool aIgnoreValid){\n"; if (snip.needsChildren != null) { snip.toXml += "\tif(" + snip.needsChildren + ")return;\n"; } snip.toXml += "\tif(!aIgnoreValid && !valid) return;\n"; snip.toXml += "\tTiXmlElement * pEm;\n\tif(aCreateNode){\n\t\tpEm = new TiXmlElement(\"" + t.Name + "\");\n"; snip.toXml += "\t\tpParent->LinkEndChild(pEm);\n\t}else{\n\t\tpEm = pParent->ToElement();\n\t}\n"; if (baseClass != null) { snip.toXml += "\tthis->" + baseClass + "::toXml(pEm, false, aIgnoreValid);\n"; } snip.toXml += tempToXml; snip.toXml += "};\n"; snip.declaration = snip.classDeclaration + snip.declarations + "};"; snip.definition = snip.constructor + snip.fromXml + snip.toXml; }
//when we add an element it either needs to be private static void AddXMLElement(FileLevelInfo info, XmlElementAttribute xea, ClassSnippet snip, FieldInfo finfo) { if (xea.Type == null) { AddSimpleXMLElement(info, xea, snip, finfo); return; } string typeName = xea.Type.ToString(); string[] ss = typeName.Split(new char[] { '.' }); typeName = ss[ss.Length - 1]; string typesuffix = "_t"; string fromNamespace = getCppNamespace(xea.Namespace); string fullName; string swigFullName; string ename = xea.ElementName; if (xea.Type.Namespace == "System") { if (!info.knownSimpleTypes.ContainsKey(typeName.ToLower())) { throw new Exception("Unknown system array type : " + typeName); } fromNamespace = info.nSpace; typesuffix = ""; typeName = (string)info.knownSimpleTypes[typeName.ToLower()]; fullName = typeName; swigFullName = info.nSpace + "::" + typeName; if (!info.collections.ContainsKey(typeName)) info.collections.Add(typeName, typeName); } else if (fromNamespace == info.nSpace) { if (typeName != snip.type.Name) { //don't make a class dependent on itself. snip.dependencies.Add(typeName); } fullName = typeName; swigFullName = info.nSpace +"::"+typeName; } else { fullName = fromNamespace + "::" + typeName; swigFullName = fullName; if (!info.includes.ContainsKey(fromNamespace)) info.includes.Add(fromNamespace, fullName); } bool isCollection = false; if (info.collections.ContainsKey(typeName)) { snip.declarations += "#ifdef SWIG\n%immutable " + ename + "Vector;\n#endif\n"; snip.classDeclaration = "#ifdef SWIG\n}\n%template(" + ename + "_c) std::vector<" + swigFullName + typesuffix + "*>;\nnamespace " + info.nSpace + "{\n#endif\n" + snip.classDeclaration; snip.declarations += "\tprivate: collectedType < " + fullName + typesuffix+" > " + ename + ";\n"; snip.declarations += "\tpublic: vector < " + fullName + typesuffix+"* >& "+ename+"Vector;\n"; if (snip.constructor == null) { snip.constructor = snip.className + "::" + snip.className + "():\n\t\t"; } else { snip.constructor += ",\n\t\t"; } snip.constructor += ename + "(\"" + typeName + "\"), "+ename+"Vector(" + ename + ".collection)"; if (snip.needsChildren != null) snip.needsChildren += " && "; snip.needsChildren += ename + ".size() == 0"; isCollection = true; } else { snip.declarations += "#ifdef SWIG\n%immutable " + ename + ";\n#endif\n"; snip.declarations += "\t" + fullName + typesuffix +" " + ename + ";\n"; } snip.toXml += "\t" + ename + ".toXml(pEm, true, aIgnoreValid);\n"; string nodeName = "pNode->FirstChildElement(\"" + ename + "\")"; if (ename == snip.type.Name || isCollection) nodeName = "pNode"; snip.fromXml += "\t" + ename + ".fromXml(" + nodeName + ");\n"; }
private static string SetupSimpleType(FileLevelInfo info, ClassSnippet snip, string dtype, string fieldName, string fieldType) { if (dtype == null || dtype.Length == 0) { if (fieldType != null && info.enumNames.ContainsKey(fieldType)) { fieldName = fieldType; } if (info.enumNames.ContainsKey(fieldName)) { dtype = (string)info.enumNames[fieldName]; snip.dependencies.Add(fieldName); return dtype; } else { throw new Exception("Bad simple type: " + fieldName + " : " + dtype); } } if (dtype == "integer") dtype = "int"; if (dtype == "boolean") dtype = "bool"; if (!info.knownSimpleTypes.ContainsKey(dtype)) { if(!info.unKnownSimpleTypes.ContainsKey(dtype)) info.unKnownSimpleTypes.Add(dtype,dtype); dtype = "string"; } return dtype; }
private static void BuildClassSnippets(FileLevelInfo info, ClassSnippet snip) { Type t = snip.type; FieldInfo[] fi = t.GetFields(); snip.className = t.Name + "_t"; snip.classDeclaration = "class " + snip.className + " : "; //set the base class string baseClass = null; if (t.BaseType != typeof(System.Object)) { string nspace = getCppNamespace(t.BaseType.Namespace); if (t.BaseType.Namespace != t.Namespace) { if (!info.includes.ContainsKey(nspace)) info.includes.Add(nspace, t.BaseType.FullName); } else snip.dependencies.Add(t.BaseType.Name); baseClass = nspace + "::" + t.BaseType.Name + "_t"; snip.classDeclaration += "public " + baseClass; } else { snip.classDeclaration += "public IXMLDataBound "; } snip.classDeclaration += "{\npublic:\n\tvoid toXml(TiXmlNode* pParent, bool aCreateNode, bool aIgnoreValid);\n\tvoid fromXml(TiXmlNode* pNode);\n"; snip.constructor = null; snip.declarations = ""; snip.fromXml = "void " + snip.className + " :: fromXml(TiXmlNode* pNode){\n"; snip.toXml = ""; snip.needsChildren = null; snip.fromXml += "\tif(pNode == NULL)return;\n"; snip.fromXml += "\tXML_CHECK(\""+t.Name+"\",pNode->Type() == TiXmlNode::ELEMENT);\n"; snip.fromXml += "\tTiXmlElement* pEm = pNode->ToElement();\n"; snip.fromXml += "\tXML_CHECK(\"" + t.Name + "\",pEm != 0);\n"; if (baseClass != null) { snip.fromXml += "\tthis->" + baseClass + "::fromXml(pNode);\n"; } foreach (FieldInfo f in fi) { if (f.DeclaringType != t) { //Console.WriteLine("Skipped " + f.Name + " in " + t.ToString() + " since its allready defined in: " + f.DeclaringType.ToString()); continue; } object[] atts = f.GetCustomAttributes(true); foreach (Attribute a in atts) { if (a.GetType() == typeof(XmlAttributeAttribute)) { AddXMLAttributte(info, (XmlAttributeAttribute)a, snip, f); } if (a.GetType() == typeof(XmlElementAttribute)) { AddXMLElement(info, (XmlElementAttribute)a, snip, f); } } } if (snip.constructor == null) snip.constructor = ""; else { //when we have had to construct snip.constructor += "{};\n"; snip.classDeclaration += "\t" + snip.className + "();\n\n"; } snip.fromXml += "\tvalid=true;\n};\n"; string tempToXml = snip.toXml; snip.toXml = "void " + snip.className + " :: toXml(TiXmlNode* pParent, bool aCreateNode, bool aIgnoreValid){\n"; if (snip.needsChildren != null) { snip.toXml += "\tif(" + snip.needsChildren + ")return;\n"; } snip.toXml += "\tif(!aIgnoreValid && !valid) return;\n"; snip.toXml += "\tTiXmlElement * pEm;\n\tif(aCreateNode){\n\t\tpEm = new TiXmlElement(\"" + t.Name + "\");\n"; snip.toXml += "\t\tpParent->LinkEndChild(pEm);\n\t}else{\n\t\tpEm = pParent->ToElement();\n\t}\n"; if (baseClass != null) { snip.toXml += "\tthis->" + baseClass + "::toXml(pEm, false, aIgnoreValid);\n"; } snip.toXml += tempToXml; snip.toXml += "};\n"; snip.declaration = snip.classDeclaration + snip.declarations + "};"; snip.definition = snip.constructor + snip.fromXml + snip.toXml; }