//========================================================================= /// <summary> /// Write to an array at element idx. /// </summary> /// <param name="initName">Name of the array init.</param> /// <param name="idx">Item in the array.</param> /// <param name="value">The scalar value.</param> //========================================================================= public void writeElement(string initName, int idx, string value) { TInitValue elemValue = null; findElementWrite(initName, idx, TTypedValue.TBaseType.ITYPE_STR, ref elemValue); elemValue.setValue(value); }
//============================================================================ /// <summary> /// Uses the copy constructor to make a clone of a typedvalue's structure. /// It is then added as a member to an array or record. /// this virtual function is expected to be overriden so that new members are /// of the child classes' type. /// </summary> /// <param name="bluePrintValue">Use this TTYpedValue as the blue print.</param> //============================================================================ public override void newMember(TTypedValue bluePrintValue) { TInitValue newElement; newElement = new TInitValue(bluePrintValue); //calls copy constructor addMember(newElement); //add the copy }
//========================================================================= /// <summary> /// Find the element of an array to write to. Resize the array if required. /// </summary> /// <param name="initName">Name of the init.</param> /// <param name="idx">Array item index.</param> /// <param name="baseType">The base type of the item.</param> /// <param name="elemValue">The typed value of the array item.</param> /// <returns>The array item.</returns> //========================================================================= private void findElementWrite(string initName, int idx, TTypedValue.TBaseType baseType, ref TInitValue elemValue) { TInitValue aValue = null; if (idx < 1) { throw (new ArrayIndexException("Attempt to write out of range of array value " + initName)); } findValue(initName, out aValue); // Try to locate the named value if (aValue == null) // First time this array has been { // referred to - create it aValue = new TInitValue(initName, baseType, idx); addValue(aValue); } if (!aValue.isArray()) { throw (new TypeMisMatchException("Attempt to write array to non-array value " + initName)); } if (idx > aValue.count()) { aValue.setElementCount((uint)idx); } elemValue = (TInitValue)aValue.item((uint)idx); }
//============================================================================ /// <summary> /// Adds a scalar to an array or record. /// </summary> /// <param name="sName">Name of the scalar.</param> /// <param name="aType">The basic type for this scalar.</param> /// <returns>A ref to the newly created scalar.</returns> //============================================================================ public override TTypedValue addScalar(String sName, TBaseType aType) { TInitValue newScalar; TTypedValue result = null; if (FIsArray || FIsRecord) { newScalar = new TInitValue(sName, aType); if (isArray()) { // Arrays pass their default value and valid range down to their elements if (newScalar.getDefault() != null) { newScalar.getDefault().copyFrom(FDefault); } if (newScalar.getMin() != null) { newScalar.getMin().copyFrom(FMin); } if (newScalar.getMax() != null) { newScalar.getMax().copyFrom(FMax); } } addMember(newScalar); result = newScalar; } return(result); }
//============================================================================ /// <summary> /// Is current value the default? /// </summary> /// <returns>True if the current value is the default.</returns> //============================================================================ public bool bIsDefault() { Boolean result; if (isScalar()) { result = ((getDefault() != null) && equals(getDefault())); } else if (isArray()) { result = (count() == 0); // zero-length array is default } else { // record result = true; for (uint idx = 1; idx <= count(); ++idx) { TInitValue anItem = new TInitValue(item(idx)); if (anItem != null) { result &= anItem.bIsDefault(); } } } return(result); }
//======================================================================= /// <summary> /// Construct a TCompProperty using a TTypedValue object. It will be /// non readable, non writeable, and not an init property. It's name will /// be "" /// </summary> /// <param name="typedValue">Typed Value to use as the blueprint</param> //======================================================================= public TCompProperty(TTypedValue typedValue) { bRead = false; bWrite = false; bInit = false; InitValue = new TInitValue(typedValue); Name = ""; sDescr = ""; sFullDescr = ""; }
//======================================================================= /// <summary> /// Copy constructor /// </summary> /// <param name="bluePrintValue"></param> //======================================================================= public TCompProperty(TCompProperty bluePrintValue) { bRead = bluePrintValue.bRead; bWrite = bluePrintValue.bWrite; bInit = bluePrintValue.bInit; InitValue = new TInitValue(bluePrintValue.InitValue); sDescr = bluePrintValue.sDescr; sFullDescr = bluePrintValue.sFullDescr; //what about sub field descrs?? }
//========================================================================= /// <summary> /// Find the init property and return the typedvalue. /// </summary> /// <param name="initName">Name of the init property.</param> /// <param name="xValue">The typed value found. null if not found.</param> /// <returns>True if found.</returns> //========================================================================= public bool readValue(string initName, ref TTypedValue xValue) { TInitValue aValue = null; bool result = (findValue(initName, out aValue) >= 0); if (result) { xValue = aValue; } return(result); }
//========================================================================= /// <summary> /// Write the Init value to the property. /// </summary> /// <param name="initName">Name of the destination property.</param> /// <param name="aValue">The Init value to write.</param> //========================================================================= public void writeValue(string initName, ref TInitValue aValue) { TInitValue newValue; findValue(initName, out newValue); if (newValue != null) { newValue.setValue(aValue); } else { aValue.Name = initName; addValue(aValue); } }
//=========================================================================== /// <summary> /// Initialise all the members of the TInitValue. Recurse into fields. /// </summary> /// <param name="item">The TInitValue to initialise</param> //=========================================================================== protected void initialiseMembers(TInitValue item) { if (item.isScalar() && (item.getDefault() != null)) { item.copyFrom(item.getDefault()); } else { for (uint i = 1; i <= item.count(); i++) //for each member of the item { TTypedValue child = item.item(i); initialiseMembers((TInitValue)child); } } }
//========================================================================= /// <summary> /// Write a string to the values list. /// </summary> /// <param name="initName">Name of the init.</param> /// <param name="value">The scalar value.</param> //========================================================================= public void writeValue(string initName, string value) { TInitValue newValue; findValue(initName, out newValue); if (newValue != null) { newValue.setValue(value); } else { newValue = new TInitValue(initName, TTypedValue.TBaseType.ITYPE_STR); newValue.setValue(value); addValue(newValue); } }
//========================================================================= /// <summary> /// Write the typed value to the property. /// </summary> /// <param name="initName">Name of the destination property.</param> /// <param name="xValue">The typed value to write.</param> //========================================================================= public void writeValue(string initName, TTypedValue xValue) { TInitValue newValue; findValue(initName, out newValue); if (newValue != null) { writeData(xValue, newValue); } else { newValue = new TInitValue(xValue); newValue.Name = initName; addValue(newValue); writeData(xValue, newValue); } }
//======================================================================= /// <summary> /// Creates a component property from the XML description of a property. /// </summary> /// <param name="sXML">XML for the property containing the DDML type. e.g. /// <property name="name" access="read" init="F"> /// <type kind="string"><defval></defval> /// </type> /// </property> /// </param> //======================================================================= public TCompProperty(String sXML) { TSDMLParser parser; String access; XmlNode anode; //create a parser. (not very nice creating a second parser but....) parser = new TSDMLParser(sXML); anode = parser.firstElementChild(parser.rootNode(), "type"); InitValue = new TInitValue(parser, anode, ""); bRead = false; bWrite = false; bInit = false; sDescr = ""; sFullDescr = ""; Name = parser.getAttrValue(parser.rootNode(), "name"); InitValue.Name = Name; sDescr = parser.getAttrValue(parser.rootNode(), "descr"); access = parser.getAttrValue(parser.rootNode(), "access"); if (access == "both") { bRead = true; bWrite = true; } if ((access == "read") || (access.Length == 0)) { bRead = true; } if (access == "write") { bWrite = true; } if (parser.getAttrValue(parser.rootNode(), "init") == "T") { bInit = true; initialiseMembers(InitValue); } anode = parser.firstElementChild(parser.rootNode(), "description"); if (anode != null) { sFullDescr = parser.getText(anode); } }
//========================================================================= /// <summary> /// Find the property by name. /// </summary> /// <param name="initName">The name of the init property.</param> /// <param name="aValue">The init value found. Returns null if not found.</param> /// <returns>The index in the internal property values list.</returns> //========================================================================= private int findValue(string initName, out TInitValue aValue) { aValue = null; int result = FValues.Count - 1; while ((result >= 0) && (aValue == null)) { if (FValues[result].Name == initName) { aValue = FValues[result]; } else { result--; } } return(result); }
//============================================================================ /// <summary> /// Copy constructor. This constructor makes a copy of the source's structure. /// For specialised child classes, this constructor should be overriden. /// </summary> /// <param name="typedValue">TTypedValue to use as the source.</param> //============================================================================ public TInitValue(TTypedValue typedValue) : base(typedValue) { //FMax = null; //FMin = null; FDefault = null; FDescr = ""; //required in this derived class initTypeCopy(typedValue); //calls suitable virtual functions TInitValue other = (TInitValue)typedValue; if (other != null) { if (other.FDefault != null) { if (FDefault == null) { FDefault = new TDDMLValue("defval", FBaseType); } FDefault.copyFrom(other.FDefault); } if (other.FMin != null) { if (FMin == null) { FMin = new TDDMLValue("minval", FBaseType); } FMin.copyFrom(other.FMin); } if (other.FMax != null) { if (FMax == null) { FMax = new TDDMLValue("maxval", FBaseType); } FMax.copyFrom(other.FMax); } setDescr(other.getDescr(), 255); } }
//============================================================================ /// <summary> /// Finds children nodes in the XML doc and creates member items of this type. /// </summary> //============================================================================ protected override void getFldElemList() { TInitValue newMember; XmlNode memberNode; string buf; //builds the child list using the parent's parser and just shifts the //parser's topElement domnode respectively. memberNode = parser.firstMember(parser.rootNode()); while (memberNode != null) { newMember = new TInitValue(parser, memberNode, ""); buf = parser.getAttrValue(memberNode, "descr"); newMember.setDescr(buf, 512); FMembers.Add(newMember); //add to the list of children memberNode = parser.nextMember(memberNode); } if (FIsArray && (FBaseType != TTypedValue.TBaseType.ITYPE_DEF)) { makeDefaultAndRange(); } }
//============================================================================ /// <summary> /// Formats the xml text for the 'field' or 'element' description of the typed /// value stored as a TPropertyInfo or TDriverInfo. /// </summary> /// <param name="attrInfo">The TypedValue attribute.</param> /// <param name="indent">Indentation in chars.</param> /// <param name="tab">Number of spaces in each tab</param> /// <returns>XML text for the field</returns> //============================================================================ protected override String writeFieldInfo(TTypedValue attrInfo, int indent, int tab) { uint i; uint iFirst; int oneIndent; int nextIndent; int startIndent; String CR = ""; //determine how much to indent this description oneIndent = 0; startIndent = 0; if (indent > -1) { oneIndent = tab; startIndent = indent; CR = "\r\n"; } nextIndent = indent; String sIndent = new String(' ', startIndent + oneIndent); StringBuilder xml = new StringBuilder(""); xml.Append(" kind=\"" + attrInfo.typeName() + "\""); if (attrInfo.isArray()) { xml.Append(" array=\"T\""); } if ((attrInfo.units().Length > 0) && (attrInfo.units()[0] != '-')) { xml.Append(" unit=\"" + attrInfo.units() + "\""); } xml.Append(">" + CR); //if a scalar or array of scalars then write the defval|minval|maxval elements if (attrInfo.isScalar() || (attrInfo.isArray() && attrInfo.baseType() == TTypedValue.TBaseType.ITYPE_DEF)) { TInitValue initInfo = new TInitValue(attrInfo); if (initInfo != null) { if (initInfo.getDefault() != null) { xml.Append(sIndent + "<defval>" + initInfo.getDefault().asEscapedString() + "</defval>" + CR); } if (initInfo.getMin() != null) { xml.Append(sIndent + "<minval>" + initInfo.getMin().asEscapedString() + "</minval>" + CR); } if (initInfo.getMax() != null) { xml.Append(sIndent + "<maxval>" + initInfo.getMax().asEscapedString() + "</maxval>" + CR); } } } //now nest into the fields/elements sIndent = new String(' ', startIndent); if (!attrInfo.isScalar()) { // Special handling for non-scalar arrays of length 0, to ensure type definition is output iFirst = (uint)((attrInfo.isArray() && attrInfo.count() == 0) ? 0 : 1); for (i = iFirst; i <= attrInfo.count(); i++) { //for each child field if (attrInfo.isArray() && attrInfo.baseType() == TTypedValue.TBaseType.ITYPE_DEF) { xml.Append(sIndent + "<element"); xml.Append(writeFieldInfo(attrInfo.item(i), nextIndent, oneIndent)); xml.Append(sIndent + "</element>" + CR); } else if (attrInfo.isRecord()) { xml.Append(sIndent + "<field name=\"" + attrInfo.item(i).Name + "\""); xml.Append(writeFieldInfo(attrInfo.item(i), nextIndent, oneIndent)); xml.Append(sIndent + "</field>" + CR); } } } return(xml.ToString()); }
//========================================================================= /// <summary> /// Add a component property to the internal list. /// </summary> /// <param name="propInit">Property item.</param> /// <returns>The count of items in the internal list of properties.</returns> //========================================================================= protected int addValue(TInitValue propInit) { FValues.Add(propInit); return(FValues.Count); }
//========================================================================= /// <summary> /// Find the property and return the Init value. /// </summary> /// <param name="initName">Name of the init property.</param> /// <param name="aValue">The Init value found. null if not found.</param> /// <returns>True if found.</returns> //========================================================================= public bool readValue(string initName, ref TInitValue aValue) { return(findValue(initName, out aValue) >= 0); }