Exemplo n.º 1
0
        public static RRBaseArray ArrayToRRBaseArray(Array i, RRBaseArray o)
        {
            if (i == null)
            {
                return(null);
            }
            DataTypes type = DataTypeUtil.TypeIDFromString(RobotRaconteurNode.GetTypeString(i.GetType().GetElementType()));

            if (i.Length != o.size())
            {
                throw new Exception("Length mismatch");
            }
            if (o.GetTypeID() != type)
            {
                throw new Exception("Type mismatch");
            }

            /*GCHandle h = GCHandle.Alloc(i, GCHandleType.Pinned);
             * rr_memcpy.memcpy(o.void_ptr(), h.AddrOfPinnedObject(), i.Length * o.ElementSize());
             * h.Free();*/

            switch (type)
            {
            case DataTypes.double_t: {
                var a = (double[])i;
                MessageElementDataUtil.DoublesToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.single_t: {
                var a = (float[])i;
                MessageElementDataUtil.FloatsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.int8_t: {
                var a = (sbyte[])i;
                MessageElementDataUtil.BytesToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.uint8_t: {
                var a = (byte[])i;
                MessageElementDataUtil.BytesToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.int16_t: {
                var a = (short[])i;
                MessageElementDataUtil.ShortsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.uint16_t: {
                var a = (ushort[])i;
                MessageElementDataUtil.ShortsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.int32_t: {
                var a = (int[])i;
                MessageElementDataUtil.IntsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.uint32_t: {
                var a = (uint[])i;
                MessageElementDataUtil.IntsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.int64_t: {
                var a = (long[])i;
                MessageElementDataUtil.LongsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.uint64_t: {
                var a = (ulong[])i;
                MessageElementDataUtil.LongsToRRBaseArray(a, a.Length, o);
                return(o);
            }

            case DataTypes.cdouble_t: {
                var a = (CDouble[])i;
                var b = new double[a.Length * 2];
                for (int j = 0; j < a.Length; j++)
                {
                    b[j * 2]     = a[j].Real;
                    b[j * 2 + 1] = a[j].Imag;
                }
                MessageElementDataUtil.DoublesToComplexRRBaseArray(b, b.Length, o);
                return(o);
            }

            case DataTypes.csingle_t: {
                var a = (CSingle[])i;
                var b = new float[a.Length * 2];
                for (int j = 0; j < a.Length; j++)
                {
                    b[j * 2]     = a[j].Real;
                    b[j * 2 + 1] = a[j].Imag;
                }
                MessageElementDataUtil.FloatsToComplexRRBaseArray(b, b.Length, o);
                return(o);
            }

            case DataTypes.bool_t: {
                var a = (bool[])i;
                var b = new byte[a.Length];
                for (int j = 0; j < a.Length; j++)
                {
                    b[j] = a[j] ? (byte)1 : (byte)0;
                }
                MessageElementDataUtil.BytesToBoolRRBaseArray(b, b.Length, o);
                return(o);
            }

            default:
                throw new DataTypeException("Invalid RRBaseArray type");
            }

            return(o);
        }