private static void BuildEnumSnippets(FileLevelInfo info, EnumSnippet snip) { string[] s = Enum.GetNames(snip.type); string enumName = (string)info.enumNames[snip.type.Name]; string enumPrefix = ""; bool needsUnderScore = true; bool needsPrefix = false; for (int i = 0; i < s.Length; i++) { string n = s[i]; if (info.enumValues.ContainsKey(n)) { needsPrefix = true; } else { info.enumValues.Add(n, snip.type); } if (n.StartsWith("_")) { int result; if (Int32.TryParse(n.Substring(1), out result)) { needsPrefix = true; } s[i] = n.Substring(1); } } if (needsPrefix) { enumPrefix = snip.type.Name.ToUpper(); if (needsUnderScore) { enumPrefix += "_"; } } string enumDef = ""; string enumDeclarataion = "enum " + enumName + " {\n"; foreach (string n in s) { enumDeclarataion += "\t" + enumPrefix + n + ",\n"; } enumDeclarataion += "};\n"; string definition = enumName + " FromString_" + enumName + "(TiXmlNode* apParent, const char* aValue)"; enumDeclarataion += "\t" + definition + ";\n"; enumDef += definition + " {\n"; foreach (string n in s) { enumDef += "\tif(aValue != NULL && strcmp(aValue,\"" + n + "\") == 0) return " + enumPrefix + n + ";\n"; } enumDef += "\tstd::ostringstream oss;\n\toss << \"String \" << (aValue==NULL?\"null\":aValue) << \" isnt valid for " + enumName; enumDef += " at \" << apParent->Row() << \":\" << apParent->Column();"; enumDef += "\n\tif(IXMLDataBound::msExceptOnFailure){throw apl::Exception(LOCATION, oss.str());}"; enumDef += "\n\telse{std::cout << oss.str() << std::endl; return " + enumPrefix + s[0] + ";}\n"; enumDef += "};\n"; definition = "string ToString_" + enumName + "(" + enumName + " aValue)"; enumDeclarataion += "\t" + definition + ";\n"; enumDef += definition + " {\n"; enumDef += "\tswitch(aValue){\n"; foreach (string n in s) { enumDef += "\tcase " + enumPrefix + n + ": return \"" + n + "\";\n"; } enumDef += "\t}\n\tstd::ostringstream oss;\n\toss << \"Bad Enum value \" << aValue << \" for type " + enumName + "\";"; enumDef += "\n\tthrow apl::Exception(LOCATION, oss.str());\n"; enumDef += "};\n"; snip.declaration = enumDeclarataion; snip.definition = enumDef; }
private static void BuildEnumSnippets(FileLevelInfo info, EnumSnippet snip) { string[] s = Enum.GetNames(snip.type); string enumName = (string)info.enumNames[snip.type.Name]; string enumPrefix = ""; bool needsUnderScore = true; bool needsPrefix = false; for(int i=0; i <s.Length; i++) { string n = s[i]; if (info.enumValues.ContainsKey(n)) { needsPrefix = true; } else { info.enumValues.Add(n, snip.type); } if (n.StartsWith("_")) { int result; if (Int32.TryParse(n.Substring(1), out result)) { needsPrefix = true; } s[i] = n.Substring(1); } } if (needsPrefix) { enumPrefix = snip.type.Name.ToUpper(); if (needsUnderScore) enumPrefix += "_"; } string enumDef = ""; string enumDeclarataion = "enum " + enumName + " {\n"; foreach (string n in s) enumDeclarataion += "\t" + enumPrefix + n + ",\n"; enumDeclarataion += "};\n"; string definition = enumName + " FromString_" + enumName + "(TiXmlNode* apParent, const char* aValue)"; enumDeclarataion += "\t" + definition + ";\n"; enumDef += definition + " {\n"; foreach (string n in s) enumDef += "\tif(aValue != NULL && strcmp(aValue,\"" + n + "\") == 0) return " + enumPrefix + n + ";\n"; enumDef += "\tstd::ostringstream oss;\n\toss << \"String \" << (aValue==NULL?\"null\":aValue) << \" isnt valid for " + enumName; enumDef += " at \" << apParent->Row() << \":\" << apParent->Column();"; enumDef += "\n\tif(IXMLDataBound::msExceptOnFailure){throw apl::Exception(LOCATION, oss.str());}"; enumDef += "\n\telse{std::cout << oss.str() << std::endl; return "+enumPrefix + s[0]+";}\n"; enumDef += "};\n"; definition = "string ToString_" + enumName + "(" + enumName + " aValue)"; enumDeclarataion += "\t" + definition + ";\n"; enumDef += definition + " {\n"; enumDef += "\tswitch(aValue){\n"; foreach (string n in s) enumDef += "\tcase " + enumPrefix + n + ": return \"" + n + "\";\n"; enumDef += "\t}\n\tstd::ostringstream oss;\n\toss << \"Bad Enum value \" << aValue << \" for type "+enumName+"\";"; enumDef += "\n\tthrow apl::Exception(LOCATION, oss.str());\n"; enumDef += "};\n"; snip.declaration = enumDeclarataion; snip.definition = enumDef; }