Beispiel #1
0
 /// Unflattens the given array into the given object.
 /// <exception cref="UnflattenFormatException"/>
 ///
 private void unflattenFromArray(Flattenable flat, byte [] buf)
 {
     using (MemoryStream memStream = new MemoryStream(buf)) {
         BinaryReader reader = new BinaryReader(memStream);
         flat.unflatten(reader, buf.Length);
     }
 }
Beispiel #2
0
        /// Retrieves the first Flattenable value in the given field.
        /// <param name="name"/>
        /// <param name="returnObject">
        /// A Flattenable object that, on success, will be set to reflect
        /// the value held in this field. This object will not be referenced
        /// by this Message.
        /// </param>
        public void getFlat(string name, Flattenable returnObject)
        {
            MessageField field = getField(name);

            if (returnObject.allowsTypeCode(field.typeCode()))
            {
                object o = field.getData();
                if (o is byte[][])
                {
                    unflattenFromArray(returnObject, ((byte[][])o)[0]);
                }
                else if (o is Message[])
                {
                    returnObject.setEqualTo(((Message[])o)[0]);
                }
                else if (o is Point[])
                {
                    returnObject.setEqualTo(((Point[])o)[0]);
                }
                else if (o is Rect[])
                {
                    returnObject.setEqualTo(((Rect[])o)[0]);
                }
                else
                {
                    throw new FieldTypeMismatchException(name + " isn't a flattened-data field");
                }
            }
            else
            {
                throw new FieldTypeMismatchException("Passed-in object doesn't like typeCode " + whatString(field.typeCode()));
            }
        }
Beispiel #3
0
        /// Sets the given field name to contain the flattened bytes of
        /// the single given Flattenable object. Any previous field contents
        /// are replaced.  The type code of the field is determined by
        /// calling val.typeCode().
        /// (val) will be flattened and the resulting bytes kept.
        /// (val) does not become part of the Message object.
        /// <param name="name"/>
        /// <param name="val">
        /// the object whose bytes are to be flattened out and put into
        /// this field.</param>

        public void setFlat(string name, Flattenable val)
        {
            int          type  = val.typeCode();
            MessageField field = getCreateOrReplaceField(name, type);

            object payload = field.getData();

            switch (type)
            {
            // For these types, we have explicit support for holding
            // the objects in memory, so we'll just clone them
            case B_MESSAGE_TYPE:
            {
                Message [] array =
                    ((payload != null) && (((Message[])payload).Length == 1)) ? ((Message[])payload) : new Message[1];

                array[1] = (Message)val.cloneFlat();
                field.setPayload(array, 1);
            }
            break;

            case B_POINT_TYPE:
            {
                Point [] array =
                    ((payload != null) && (((Point[])payload).Length == 1)) ?
                    ((Point[])payload) : new Point[1];
                array[1] = (Point)val.cloneFlat();
                field.setPayload(array, 1);
            }
            break;

            case B_RECT_TYPE:
            {
                Rect [] array =
                    ((payload != null) && (((Rect[])payload).Length == 1)) ?
                    ((Rect[])payload) : new Rect[1];
                array[1] = (Rect)val.cloneFlat();
                field.setPayload(array, 1);
            }
            break;

            // For everything else, we have to store the objects as byte buffers
            default:
            {
                byte [][] array =
                    ((payload != null) && (((byte[][])payload).Length == 1)) ?
                    ((byte[][])payload) : new byte[1][];
                array[0] = flattenToArray(val, array[0]);
                field.setPayload(array, 1);
            }
            break;
            }
        }
Beispiel #4
0
        /// Sets this Message equal to (c)
        /// <param name="c">What to clone.</param>
        /// <exception cref="System.InvalidCastException"/>
        public override void setEqualTo(Flattenable c)
        {
            clear();
            Message copyMe = (Message)c;

            what = copyMe.what;
            IEnumerator fields = copyMe.fieldNames();

            while (fields.MoveNext())
            {
                copyMe.copyField((string)fields.Current, this);
            }
        }
Beispiel #5
0
        /// Flattens the given flattenable object into an array and
        /// returns it.  Pass in an old array to use if you like;
        /// it may be reused, or not. */
        private byte[] flattenToArray(Flattenable flat, byte[] optOldBuf)
        {
            int fs = flat.flattenedSize();

            if ((optOldBuf == null) || (optOldBuf.Length < fs))
            {
                optOldBuf = new byte[fs];
            }

            using (MemoryStream memStream = new MemoryStream(optOldBuf)) {
                BinaryWriter writer = new BinaryWriter(memStream);
                flat.flatten(writer);
            }

            return(optOldBuf);
        }
Beispiel #6
0
        /// 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;

            }
        }
Beispiel #7
0
        /// Sets the given field name to contain the flattened bytes of 
        /// the single given Flattenable object. Any previous field contents
        /// are replaced.  The type code of the field is determined by 
        /// calling val.typeCode().
        /// (val) will be flattened and the resulting bytes kept.
        /// (val) does not become part of the Message object.
        /// <param name="name"/>
        /// <param name="val">
        /// the object whose bytes are to be flattened out and put into 
        /// this field.</param>
        public void setFlat(string name, Flattenable val)
        {
            int type = val.typeCode();
            MessageField field = getCreateOrReplaceField(name, type);

            object payload = field.getData();

            switch(type) {
              // For these types, we have explicit support for holding
              // the objects in memory, so we'll just clone them
            case B_MESSAGE_TYPE:
              {
            Message [] array =
              ((payload != null)&&(((Message[])payload).Length == 1)) ? ((Message[])payload) : new Message[1];

            array[1] = (Message) val.cloneFlat();
            field.setPayload(array, 1);
              }
              break;

            case B_POINT_TYPE:
              {
            Point [] array =
              ((payload != null)&&(((Point[])payload).Length == 1)) ?
              ((Point[])payload) : new Point[1];
            array[1] = (Point) val.cloneFlat();
            field.setPayload(array, 1);
              }
              break;

            case B_RECT_TYPE:
              {
            Rect [] array =
              ((payload != null)&&(((Rect[])payload).Length == 1)) ?
              ((Rect[])payload) : new Rect[1];
            array[1] = (Rect) val.cloneFlat();
            field.setPayload(array, 1);
              }
              break;

              // For everything else, we have to store the objects as byte buffers
            default:
              {
            byte [][] array =
              ((payload != null)&&(((byte[][])payload).Length == 1)) ?
              ((byte[][])payload) : new byte[1][];
            array[0] = flattenToArray(val, array[0]);
            field.setPayload(array, 1);
              }
              break;
            }
        }
Beispiel #8
0
 /// Sets this Message equal to (c)
 /// <param name="c">What to clone.</param>
 /// <exception cref="System.InvalidCastException"/>
 public override void setEqualTo(Flattenable c)
 {
     clear();
     Message copyMe = (Message)c;
     what = copyMe.what;
     IEnumerator fields = copyMe.fieldNames();
     while(fields.MoveNext())  {
       copyMe.copyField((string)fields.Current, this);
     }
 }
Beispiel #9
0
        /// Retrieves the contents of the given field as an array of 
        /// Flattenable values. 
        /// <param name="name">Name of the field to look for 
        /// </param> Flattenable values in.
        /// <param name="returnObjects">Should be an array of pre-allocated 
        /// Flattenable objects of the correct type.  On success, this 
        /// array's objects will be set to the proper states as determined by 
        /// the held data in this Message.  All the objects should be of the 
        /// same type.  This method will unflatten as many objects as exist or 
        /// can fit in the array.  These objects will not be referenced by 
        /// this Message.</param>
        /// <returns>The number of objects in (returnObjects) that were 
        /// actually unflattened.  May be less than (returnObjects.length).
        /// </returns>
        /// <exception cref="FieldNotFoundException"/>
        /// <exception cref="FieldTypeMismatchException"/>
        /// <exception cref="UnflattenFormatException"/>
        /// <exception cref="InvalidCastException"/>
        ///
        public int getFlats(string name, Flattenable [] returnObjects)
        {
            MessageField field = getField(name);
            if (returnObjects[0].allowsTypeCode(field.typeCode()))
              {
            object objs = field.getData();
            int num;
            if (objs is byte[][])
              {
            byte [][] bufs = (byte[][]) objs;
            num = (bufs.Length < returnObjects.Length) ? bufs.Length : returnObjects.Length;
            for (int i=0; i<num; i++)
              unflattenFromArray(returnObjects[i], bufs[i]);
              }
            else if (objs is Message[])
              {
            Message [] messages = (Message[]) objs;
            num = (messages.Length < returnObjects.Length) ? messages.Length : returnObjects.Length;
            for (int i=0; i<num; i++)
              returnObjects[i].setEqualTo(messages[i]);
              }
            else if (objs is Point[])
              {
            Point [] points = (Point[]) objs;
            num = (points.Length < returnObjects.Length) ? points.Length : returnObjects.Length;
            for (int i=0; i<num; i++)
              returnObjects[i].setEqualTo(points[i]);
              }
            else if (objs is Rect[])
              {
            Rect [] rects = (Rect[]) objs;
            num = (rects.Length < returnObjects.Length) ? rects.Length : returnObjects.Length;
            for (int i=0; i<num; i++)
              returnObjects[i].setEqualTo(rects[i]);
              }
            else throw new FieldTypeMismatchException(name + " wasn't an unflattenable data field");

            return num;
              }
            else throw new FieldTypeMismatchException("Passed-in objects doen't like typeCode " + whatString(field.typeCode()));
        }
Beispiel #10
0
 /// Retrieves the first Flattenable value in the given field. 
 /// <param name="name"/>
 /// <param name="returnObject"> 
 /// A Flattenable object that, on success, will be set to reflect 
 /// the value held in this field. This object will not be referenced 
 /// by this Message.
 /// </param>
 public void getFlat(string name, Flattenable returnObject)
 {
     MessageField field = getField(name);
     if (returnObject.allowsTypeCode(field.typeCode()))
       {
     object o = field.getData();
     if (o is byte[][])
       unflattenFromArray(returnObject, ((byte[][])o)[0]);
     else if (o is Message[])
       returnObject.setEqualTo(((Message[])o)[0]);
     else if (o is Point[])
       returnObject.setEqualTo(((Point[])o)[0]);
     else if (o is Rect[])
       returnObject.setEqualTo(((Rect[])o)[0]);
     else
       throw new FieldTypeMismatchException(name + " isn't a flattened-data field");
       }
     else
       throw new FieldTypeMismatchException("Passed-in object doesn't like typeCode " + whatString(field.typeCode()));
 }
Beispiel #11
0
 /// Unimplemented, throws a ClassCastException exception
 public override void setEqualTo(Flattenable setFromMe)
 {
     throw new System.InvalidCastException("MessageField.setEqualTo() not supported");
 }
Beispiel #12
0
 /// Unflattens the given array into the given object.  
 /// <exception cref="UnflattenFormatException"/>
 ///
 private void unflattenFromArray(Flattenable flat, byte [] buf)
 {
     using (MemoryStream memStream = new MemoryStream(buf)) {
       BinaryReader reader = new BinaryReader(memStream);
       flat.unflatten(reader, buf.Length);
     }
 }
Beispiel #13
0
        /// Flattens the given flattenable object into an array and 
        /// returns it.  Pass in an old array to use if you like; 
        /// it may be reused, or not. */
        private byte[] flattenToArray(Flattenable flat, byte[] optOldBuf)
        {
            int fs = flat.flattenedSize();
            if ((optOldBuf == null)||(optOldBuf.Length < fs))
              optOldBuf = new byte[fs];

            using (MemoryStream memStream = new MemoryStream(optOldBuf)) {
              BinaryWriter writer = new BinaryWriter(memStream);
              flat.flatten(writer);
            }

            return optOldBuf;
        }
Beispiel #14
0
 public override void setEqualTo(Flattenable setFromMe)
 {
     Point p = (Point) setFromMe;
       set(p.x, p.y);
 }
Beispiel #15
0
 public override void setEqualTo(Flattenable rhs)
 {
     Rect copyMe = (Rect) rhs;
       set(copyMe.left, copyMe.top, copyMe.right, copyMe.bottom);
 }
Beispiel #16
0
 /// Unimplemented, throws a ClassCastException exception
 public override void setEqualTo(Flattenable setFromMe)
 {
     throw new System.InvalidCastException("MessageField.setEqualTo() not supported");
 }