private static void PreprocessConsts(ConstsDef def) { if (def.Type == "string") { foreach (var constant in def.Constants) { constant.Value = "\"" + constant.Value + "\""; } } }
public override void FormatConsts(ConstsDef def) { if (nativeTypes.ContainsKey(def.Type)) { def.NativeType = nativeTypes[def.Type]; } else { return; } PreprocessConsts(def); FormatComment(0, def.Comment); Indent(0); Out.WriteLine("public static class {0}", def.Name); Indent(0); Out.WriteLine("{"); foreach (var constant in def.Constants) { FormatComment(1, constant.Comment); Indent(1); Out.Write("public const {0} {1}", def.NativeType, constant.Name); if (!String.IsNullOrEmpty(constant.Value)) { Out.Write(" = {0}", constant.Value); } Out.Write(';'); Out.WriteLine(); } Out.WriteLine(); Indent(1); Out.WriteLine("private static ConstsInfo<{0}> info;", def.NativeType); Out.WriteLine(); Indent(1); Out.WriteLine("static {0}()", def.Name); Indent(1); Out.WriteLine("{"); Indent(2); Out.WriteLine("info = new ConstsInfo<{0}>();", def.NativeType); foreach (var constant in def.Constants) { Indent(2); Out.WriteLine("info.Add(\"{0}\", {1});", constant.Name, constant.Value); } Indent(1); Out.WriteLine("}"); Out.WriteLine(); Indent(1); Out.WriteLine("public static bool ContainsName(string name)", def.NativeType); Indent(1); Out.WriteLine("{"); Indent(2); Out.WriteLine("return info.ContainsName(name);"); Indent(1); Out.WriteLine("}"); Out.WriteLine(); Indent(1); Out.WriteLine("public static bool ContainsValue({0} value)", def.NativeType); Indent(1); Out.WriteLine("{"); Indent(2); Out.WriteLine("return info.ContainsValue(value);"); Indent(1); Out.WriteLine("}"); Out.WriteLine(); Indent(1); Out.WriteLine("public static string GetName({0} value)", def.NativeType); Indent(1); Out.WriteLine("{"); Indent(2); Out.WriteLine("return info.GetName(value);"); Indent(1); Out.WriteLine("}"); Out.WriteLine(); Indent(1); Out.WriteLine("public static {0} Parse(string name)", def.NativeType); Indent(1); Out.WriteLine("{"); Indent(2); Out.WriteLine("return info.Parse(name);"); Indent(1); Out.WriteLine("}"); Out.WriteLine(); Indent(1); Out.WriteLine("public static bool TryParse(string name, out {0} result)", def.NativeType); Indent(1); Out.WriteLine("{"); Indent(2); Out.WriteLine("return info.TryParse(name, out result);"); Indent(1); Out.WriteLine("}"); Indent(0); Out.WriteLine("}"); }
private bool ParseConsts(Document doc, XmlElement elem, string comment) { var name = elem.GetAttribute("name"); var type = elem.GetAttribute("type"); if (String.IsNullOrEmpty(name)) { return false; } if (String.IsNullOrEmpty(type)) { type = "int32"; // default type } var def = new ConstsDef(); def.Name = name; def.Type = type; def.Comment = comment; string subComment = null; var node = elem.FirstChild; for ( ; node != null; node = node.NextSibling) { if (node.NodeType != XmlNodeType.Element) { if (node.NodeType == XmlNodeType.Comment) { subComment = node.Value.Trim(); } else { subComment = null; } continue; } var child = (XmlElement)node; if (child.IsEmpty) { continue; } switch (child.Name) { case "const": if (ParseConstant(def, child, subComment) == false) { return false; } break; default: break; } subComment = null; } doc.Definitions.Add(def); return true; }
public abstract void FormatConsts(ConstsDef def);
private bool ParseConstant(ConstsDef def, XmlElement elem, string comment) { var name = elem.GetAttribute("name"); if (String.IsNullOrEmpty(name)) { return false; } var element = new ConstsDef.Constant(); element.Name = name; element.Value = elem.InnerText.Trim(); element.Comment = comment; def.Constants.Add(element); return true; }