/// Returns the number of field names of the given type that /// are present in the Message. /// <param name="type"> /// The type of field to count, or B_ANY_TYPE to /// count all field types. /// </param> /// <returns>The number of matching fields, or zero if there are /// no fields of the appropriate type.</returns> /// public int countFields(int type) { if (_fieldTable == null) { return(0); } if (type == B_ANY_TYPE) { return(_fieldTable.Count); } int count = 0; IEnumerator e = _fieldTable.Values.GetEnumerator(); while (e.MoveNext()) { MessageField field = (MessageField)e.Current; if (field.typeCode() == type) { count++; } } return(count); }
/// Sets the given field name to contain the given Flattenable values. /// Any previous field contents are replaced. /// Note that if the objects are Messages, Points, or Rects, /// they will be cloned rather than flattened. /// <param name="name"/> /// <param name="val"> /// Array of Flattenable objects to assign to the field. /// The objects are all flattened and /// the flattened data is put into the Message; /// the objects themselves do not become part of the message. /// </param> public void setFlats(string name, Flattenable [] vals) { int type = vals[0].typeCode(); int len = vals.Length; MessageField field = getCreateOrReplaceField(name, type); // For these types, we have explicit support for holding // the objects in memory, so we'll just clone them switch (type) { case B_MESSAGE_TYPE: { Message [] array = new Message[len]; for (int i = 0; i < len; i++) { array[i] = (Message)vals[i].cloneFlat(); } field.setPayload(array, len); } break; case B_POINT_TYPE: { Point [] array = new Point[len]; for (int i = 0; i < len; i++) { array[i] = (Point)vals[i].cloneFlat(); } field.setPayload(array, len); } break; case B_RECT_TYPE: { Rect [] array = new Rect[len]; for (int i = 0; i < len; i++) { array[i] = (Rect)vals[i].cloneFlat(); } field.setPayload(array, len); } break; default: { byte [][] array = (byte[][])field.getData(); if ((array == null) || (field.size() != len)) { array = new byte[len][]; } for (int i = 0; i < len; i++) { array[i] = flattenToArray(vals[i], array[i]); } field.setPayload(array, len); } break; } }
/// Returns the number of items present in the given field, or /// zero if the field isn't present or is of the wrong type. /// <exception cref="FieldNotFound"/> public int countItemsInField(string name, int type) { MessageField field = getFieldIfExists(name, type); return((field != null) ? field.size() : 0); }
/// Makes an independent clone of this field object /// (a bit expensive) public override Flattenable cloneFlat() { MessageField clone = new MessageField(_type); System.Array newArray; // this will be a copy of our data array switch (_type) { case B_BOOL_TYPE: newArray = new bool[_numItems]; break; case B_INT8_TYPE: newArray = new byte[_numItems]; break; case B_INT16_TYPE: newArray = new short[_numItems]; break; case B_FLOAT_TYPE: newArray = new float[_numItems]; break; case B_INT32_TYPE: newArray = new int[_numItems]; break; case B_INT64_TYPE: newArray = new long[_numItems]; break; case B_DOUBLE_TYPE: newArray = new double[_numItems]; break; case B_STRING_TYPE: newArray = new string[_numItems]; break; case B_POINT_TYPE: newArray = new Point[_numItems]; break; case B_RECT_TYPE: newArray = new Rect[_numItems]; break; case B_MESSAGE_TYPE: newArray = new Message[_numItems]; break; default: newArray = new byte[_numItems][]; break; } newArray = (System.Array)_payload.Clone(); // If the contents of newArray are modifiable, we need to // clone the contents also switch (_type) { case B_POINT_TYPE: { Point[] points = (Point[])newArray; for (int i = 0; i < _numItems; i++) { points[i] = (Point)points[i].cloneFlat(); } } break; case B_RECT_TYPE: { Rect[] rects = (Rect[])newArray; for (int i = 0; i < _numItems; i++) { rects[i] = (Rect)rects[i].cloneFlat(); } } break; case B_MESSAGE_TYPE: { Message[] messages = (Message[])newArray; for (int i = 0; i < _numItems; i++) { messages[i] = (Message)messages[i].cloneFlat(); } } break; default: { if (newArray is byte[][]) { // Clone the byte arrays, since they are modifiable byte[][] array = (byte[][])newArray; for (int i = 0; i < _numItems; i++) { byte[] newBuf = (byte[])array[i].Clone(); array[i] = newBuf; } } } break; } clone.setPayload(newArray, _numItems); return(clone); }