/// <summary> /// Copies 'F', but changes the function OutputName (m_outputName). /// /// This constructor is used mostly to change the name of a function when /// the output language does not support overloading. /// </summary> /// <param name="F">fgs to be copied.</param> /// <param name="newName">New name for the copy of 'F'.</param> public fgs(fgs F, string newName) { m_name = F.m_name; m_outputName = newName; m_returnTypeName = F.m_returnTypeName; m_argumentTypeNames = F.m_argumentTypeNames; m_argumentVariableNames = F.m_argumentVariableNames; m_argumentPtr = F.m_argumentPtr; m_argumentArr = F.m_argumentArr; m_floatNames = F.m_floatNames; m_metricName = F.m_metricName; m_comment = F.m_comment; m_options = F.m_options; m_hashCode = ComputeHashCode(); }
/// <summary> /// Return true when this equals the value of 'obj'. /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(System.Object obj) { // If parameter is null return false. if (obj == null) { return(false); } // If parameter cannot be cast to fgs return false. fgs B = obj as fgs; if (B == null) { return(false); } else { return(CompareTo(obj) == 0); } }
/// <summary> /// IComparable.CompareTo implementation. /// /// This function is not used to find function by FindFunctionEx() /// because only part of the FGS needs to be compared for that. /// /// m_outputName is not compared. /// </summary> /// <param name="obj">The object to which 'this' is compared</param> public int CompareTo(object obj) { if (obj is fgs) { fgs B = (fgs)obj; int C; if ((C = m_name.CompareTo(B.m_name)) != 0) { return(C); } // if ((C = m_outputName.CompareTo(B.m_outputName)) != 0) return C; if ((C = CompareArrays(m_argumentTypeNames, B.m_argumentTypeNames)) != 0) { return(C); } if ((C = m_returnTypeName.CompareTo(B.m_returnTypeName)) != 0) { return(C); } if ((C = m_metricName.CompareTo(B.m_metricName)) != 0) { return(C); } // do not use compare the comment! if ((C = CompareArrays(m_floatNames, B.m_floatNames)) != 0) { return(C); } if ((C = CompareArrays(m_argumentVariableNames, B.m_argumentVariableNames)) != 0) { return(C); } if ((C = CompareBoolArrays(m_argumentPtr, B.m_argumentPtr)) != 0) { return(C); } if ((C = CompareBoolArrays(m_argumentArr, B.m_argumentArr)) != 0) { return(C); } if (m_options.Count < B.m_options.Count) { return(-1); } else if (m_options.Count > B.m_options.Count) { return(1); } else { // convert options to arrays, then compare: string[] OA = OptionsToStringArray(); string[] OB = B.OptionsToStringArray(); if ((C = CompareArrays(OA, OB)) != 0) { return(C); } } return(0); // this equals 'B' } throw new ArgumentException("object is not a G25.fgs"); }
public static void ParseFunction(Specification S, XmlElement E) { // storage for all info: String functionName = null; String outputFunctionName = null; const int MAX_NB_ARGS = 100; String returnTypeName = ""; String[] argumentTypeNames = new String[MAX_NB_ARGS]; String[] argumentVariableName = new String[MAX_NB_ARGS]; List<String> floatNames = new List<string>(); String metricName = "default"; String comment = ""; int nbArgs = 0; Dictionary<String, String> options = new Dictionary<string, string>(); { // handle attributes XmlAttributeCollection A = E.Attributes; // handle all attributes for (int i = 0; i < A.Count; i++) { // functionName if (A[i].Name == XML_NAME) functionName = A[i].Value; // functionName else if (A[i].Name == XML_OUTPUT_NAME) outputFunctionName = A[i].Value; // metricName else if (A[i].Name == XML_METRIC) metricName = A[i].Value.ToLower(); // comment else if (A[i].Name == XML_COMMENT) comment = A[i].Value; // floatType else if (A[i].Name == XML_FLOAT_TYPE) floatNames.Add(A[i].Value); // return type else if (A[i].Name == XML_RETURN_TYPE) { returnTypeName = A[i].Value; if (!S.IsTypeName(returnTypeName)) { if (returnTypeName.ToLower() == XML_SCALAR) // "scalar" is also allowed as returntype { returnTypeName = XML_SCALAR; } else throw new G25.UserException("Error parsing function '" + functionName + "': '" + returnTypeName + "' is not a type (inside element '" + XML_FUNCTION + "')."); } } // argNameX else if (A[i].Name.StartsWith(XML_ARGNAME)) { int argNameIdx = 0; try { argNameIdx = System.Int32.Parse(A[i].Name.Substring(XML_ARGNAME.Length)) - 1; } catch (System.Exception) { throw new G25.UserException("Error parsing function '" + functionName + "': invalid attribute '" + A[i].Name + "' in element '" + XML_FUNCTION + "'."); } if ((argNameIdx >= argumentVariableName.Length) || (argNameIdx < 0)) throw new G25.UserException("Error parsing function '" + functionName + "': invalid attribute index '" + A[i].Name + "' in element '" + XML_FUNCTION + "'."); argumentVariableName[argNameIdx] = A[i].Value; } // argX else if (A[i].Name.StartsWith(XML_ARG)) { int argIdx = 0; try { argIdx = System.Int32.Parse(A[i].Name.Substring(XML_ARG.Length)) - 1; } catch (System.Exception) { throw new G25.UserException("Error parsing function '" + functionName + "': invalid attribute '" + A[i].Name + "' in element '" + XML_FUNCTION + "'."); } if ((argIdx >= argumentTypeNames.Length) || (argIdx < 0)) throw new G25.UserException("Error parsing function '" + functionName + "': invalid attribute index '" + A[i].Name + "' in element '" + XML_FUNCTION + "'."); string typeName = A[i].Value; if (!S.IsTypeName(typeName)) { // it may be a constant, like 'e1', try adding a "_t" if (S.IsTypeName(typeName + Specification.CONSTANT_TYPE_SUFFIX)) { typeName = typeName + Specification.CONSTANT_TYPE_SUFFIX; } else throw new G25.UserException("Error parsing function '" + functionName + "': '" + typeName + "' is not a type (inside element '" + XML_FUNCTION + "')"); } argumentTypeNames[argIdx] = typeName; if (argIdx >= nbArgs) nbArgs = argIdx + 1; } else if (A[i].Name.StartsWith(XML_OPTION)) { String optionName = A[i].Name.Substring(XML_OPTION.Length).ToLower(); if (optionName.Length > 0) { String optionValue = A[i].Value; options[optionName] = optionValue; } } } // check if function name was specified: if (functionName == null) throw new G25.UserException("Missing attribute '" + XML_NAME + "' in element '" + XML_FUNCTION + "'"); // if output function name is missing, just use the regular function name if (outputFunctionName == null) outputFunctionName = functionName; // if no float type are specified, copy all from specification if (floatNames.Count == 0) { foreach (FloatType FT in S.m_floatTypes) floatNames.Add(FT.type); } // resize arguments arrays: Array.Resize(ref argumentTypeNames, nbArgs); Array.Resize(ref argumentVariableName, nbArgs); // check for nulls in argument arrays for (int i = 0; i < argumentTypeNames.Length; i++) { if (argumentTypeNames[i] == null) throw new G25.UserException("XML parsing error in function '" + functionName + "': Missing attribute '" + XML_ARG + (1 + i).ToString() + "' in element '" + XML_FUNCTION + "'"); if (argumentVariableName[i] == null) argumentVariableName[i] = fgs.DefaultArgumentName(i); } } fgs F = new fgs(functionName, outputFunctionName, returnTypeName, argumentTypeNames, argumentVariableName, floatNames.ToArray(), metricName, comment, options); S.m_functions.Add(F); }