/// 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; } }
/// Restores our state from the given stream. /// Assumes that our _type is already set correctly. /// <exception cref="IOException"/> /// public override void unflatten(BinaryReader reader, int numBytes) { // For fixed-size types, calculating the number of items // to read is easy... int flattenedCount = flattenedItemSize(); if (flattenedCount > 0) _numItems = numBytes / flattenedCount; switch(_type) { case B_BOOL_TYPE: { bool [] array = new bool[_numItems]; for (int i=0; i<_numItems; i++) array[i] = (reader.ReadByte() > 0) ? true : false; _payload = array; } break; case B_INT8_TYPE: { byte [] array = reader.ReadBytes(_numItems); _payload = array; } break; case B_INT16_TYPE: { short [] array = new short[_numItems]; for (int i=0; i<_numItems; i++) array[i] = reader.ReadInt16(); _payload = array; } break; case B_FLOAT_TYPE: { float [] array = new float[_numItems]; for (int i=0; i<_numItems; i++) array[i] = reader.ReadSingle(); _payload = array; } break; case B_INT32_TYPE: { int [] array = new int[_numItems]; for (int i=0; i<_numItems; i++) array[i] = reader.ReadInt32(); _payload = array; } break; case B_INT64_TYPE: { long [] array = new long[_numItems]; for (int i=0; i<_numItems; i++) array[i] = reader.ReadInt64(); _payload = array; } break; case B_DOUBLE_TYPE: { double [] array = new double[_numItems]; for (int i=0; i<_numItems; i++) array[i] = reader.ReadDouble(); _payload = array; } break; case B_POINT_TYPE: { Point [] array = new Point[_numItems]; for (int i=0; i<_numItems; i++) { Point p = array[i] = new Point(); p.unflatten(reader, p.flattenedSize()); } _payload = array; } break; case B_RECT_TYPE: { Rect [] array = new Rect[_numItems]; for (int i=0; i<_numItems; i++) { Rect r = array[i] = new Rect(); r.unflatten(reader, r.flattenedSize()); } _payload = array; } break; case B_MESSAGE_TYPE: { ArrayList temp = new ArrayList(); while(numBytes > 0) { Message subMessage = new Message(); int subMessageSize = reader.ReadInt32(); subMessage.unflatten(reader, subMessageSize); temp.Add(subMessage); numBytes -= (subMessageSize + 4); // 4 for the size int } _numItems = temp.Count; Message [] array = new Message[_numItems]; for (int j=0; j<_numItems; j++) array[j] = (Message) temp[j]; _payload = array; } break; case B_STRING_TYPE: { Decoder d = Encoding.UTF8.GetDecoder(); _numItems = reader.ReadInt32(); string [] array = new string[_numItems]; byte [] byteArray = null; char [] charArray = null; for (int i=0; i<_numItems; i++) { int nextStringLen = reader.ReadInt32(); byteArray = reader.ReadBytes(nextStringLen); int charsRequired = d.GetCharCount(byteArray, 0, nextStringLen); if (charArray == null || charArray.Length < charsRequired) charArray = new char[charsRequired]; int charsDecoded = d.GetChars(byteArray, 0, byteArray.Length, charArray, 0); array[i] = new string(charArray, 0, charsDecoded - 1); } _payload = array; } break; default: { _numItems = reader.ReadInt32(); byte [][] array = new byte[_numItems][]; for (int i=0; i<_numItems; i++) { int length = reader.ReadInt32(); array[i] = new byte[length]; array[i] = reader.ReadBytes(length); } _payload = array; } break; } }
public Rect getRect(string name, Rect def) { try { return getRect(name); } catch (MessageException) { return def; } }
public void setRects(string name, Rect [] vals) { setObjects(name, B_RECT_TYPE, vals, vals.Length); }
/// 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; }
public void setRect(string name, Rect val) { MessageField field = getCreateOrReplaceField(name, B_RECT_TYPE); Rect [] array = (Rect[]) field.getData(); if ((array == null)||(field.size() != 1)) array = new Rect[1]; array[0] = val; field.setPayload(array, 1); }
public bool contains(Rect p) { return ((contains(p.leftTop()))&&(contains(p.rightTop()))&& (contains(p.leftBottom()))&&(contains(p.rightBottom()))); }
public Rect(Rect rhs) { setEqualTo(rhs); }
/// <summary> /// Returns a rectangle whose area is a superset of the union of /// this rectangle's and (r)'s /// </summary> public Rect unify(Rect r) { Rect ret = new Rect(this); if (r.left < ret.left) ret.left = r.left; if (r.right > ret.right) ret.right = r.right; if (r.top < ret.top) ret.top = r.top; if (r.bottom > ret.bottom) ret.bottom = r.bottom; return ret; }
public bool intersects(Rect r) { return (r.intersect(this).isValid()); }