Exemplo n.º 1
0
        private void EnsureValueFitsType(int value, PlyType type)
        {
            int max = int.MaxValue;

            if (type == PlyType.Char)
            {
                max = char.MaxValue;
            }
            else if (type == PlyType.Uchar)
            {
                max = byte.MaxValue;
            }
            else if (type == PlyType.Short)
            {
                max = short.MaxValue;
            }
            else if (type == PlyType.Ushort)
            {
                max = ushort.MaxValue;
            }

            if (value > max)
            {
                throw new ValueDoesNotFitTypeException(
                          "The array element count is too big for the header's predefined array size type.", value, type);
            }
        }
Exemplo n.º 2
0
        public static int GetTypeSize(PlyType type)
        {
            int s;

            switch (type)
            {
            case PlyType.Char:
            case PlyType.Uchar:
                s = 1;
                break;

            case PlyType.Short:
            case PlyType.Ushort:
                s = 2;
                break;

            case PlyType.Int:
            case PlyType.Uint:
            case PlyType.Float:
                s = 4;
                break;

            case PlyType.Double:
                s = 8;
                break;

            default:
                throw new ArgumentException($"Cannot get size of the {nameof(PlyType)}. Given type is not known.", nameof(type));
            }
            return(s);
        }
Exemplo n.º 3
0
        private byte[] ReadBytesFor(PlyType dataType)
        {
            var typeSize  = PlyTypeConverter.GetTypeSize(dataType);
            var readBytes = _binaryReader.ReadBytes(typeSize);

            return(readBytes.FlipIfTrue(_reverseByteOrder));
        }
Exemplo n.º 4
0
        public static string ToStringRepresentation(PlyType dataType)
        {
            if (!Enum.IsDefined(typeof(PlyType), dataType))
            {
                throw new ArgumentException($"Cannot convert {nameof(PlyType)}. Given type is invalid.", nameof(dataType));
            }

            return(dataType.ToString().ToLowerInvariant());
        }
Exemplo n.º 5
0
        public static Type ToNative(PlyType dataType)
        {
            Type t;

            switch (dataType)
            {
            case PlyType.Char:
                t = typeof(sbyte);
                break;

            case PlyType.Uchar:
                t = typeof(byte);
                break;

            case PlyType.Short:
                t = typeof(short);
                break;

            case PlyType.Ushort:
                t = typeof(ushort);
                break;

            case PlyType.Int:
                t = typeof(int);
                break;

            case PlyType.Uint:
                t = typeof(uint);
                break;

            case PlyType.Float:
                t = typeof(float);
                break;

            case PlyType.Double:
                t = typeof(double);
                break;

            default:
                throw new ArgumentException($"Cannot convert {nameof(PlyType)}. Given type is invalid.", nameof(dataType));
            }
            return(t);
        }
Exemplo n.º 6
0
        public static T ParseStringToNativeType <T>(string value, PlyType type)
        {
            object parsed;

            switch (type)
            {
            case PlyType.Float:
                parsed = float.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Int:
                parsed = int.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Char:
                parsed = sbyte.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Uchar:
                parsed = byte.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Short:
                parsed = short.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Ushort:
                parsed = ushort.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Uint:
                parsed = uint.Parse(value, CultureInfo.InvariantCulture);
                break;

            case PlyType.Double:
                parsed = double.Parse(value, CultureInfo.InvariantCulture);
                break;

            default:
                throw new ArgumentException();
            }
            return((T)parsed);
        }
Exemplo n.º 7
0
        public static object GetPropertyValues(string token, PlyType type, out int intValue, out uint uintValue, out double doubleValue)
        {
            switch (type.PlyPropertyType)
            {
            case PlyPropertyType.Char:
            case PlyPropertyType.UChar:
            case PlyPropertyType.Short:
            case PlyPropertyType.UShort:
            case PlyPropertyType.Int:
                intValue    = Convert.ToInt32(token);
                uintValue   = (uint)intValue;
                doubleValue = intValue;
                break;

            case PlyPropertyType.UInt:
                uintValue   = Convert.ToUInt32(token);
                intValue    = (int)uintValue;
                doubleValue = uintValue;
                break;

            case PlyPropertyType.Float:
            case PlyPropertyType.Double:
                doubleValue = Convert.ToDouble(token);
                intValue    = (int)doubleValue;
                uintValue   = (uint)doubleValue;
                break;

            default:
                throw new ArgumentException("Unknown type: " + type);
            }

            return(type.PlyPropertyType switch
            {
                PlyPropertyType.Char => Convert.ToByte(token),
                PlyPropertyType.Double => Convert.ToDouble(token),
                PlyPropertyType.Float => Convert.ToSingle(token),
                PlyPropertyType.Int => Convert.ToInt32(token),
                PlyPropertyType.Short => Convert.ToInt16(token),
                PlyPropertyType.UChar => Convert.ToByte(token),
                PlyPropertyType.UInt => Convert.ToUInt32(token),
                PlyPropertyType.UShort => Convert.ToUInt16(token),
                _ => throw new ArgumentException("Unknown type: " + type),
            });
Exemplo n.º 8
0
        public static byte[] ToBytes <T>(T val, PlyType dataType) where T : IConvertible
        {
            byte[] bytes;
            switch (dataType)
            {
            case PlyType.Char:
                bytes = BitConverter.GetBytes(Convert.ToSByte(val));
                break;

            case PlyType.Uchar:
                bytes = BitConverter.GetBytes(Convert.ToByte(val));
                break;

            case PlyType.Short:
                bytes = BitConverter.GetBytes(Convert.ToInt16(val));
                break;

            case PlyType.Ushort:
                bytes = BitConverter.GetBytes(Convert.ToUInt16(val));
                break;

            case PlyType.Int:
                bytes = BitConverter.GetBytes(Convert.ToInt32(val));
                break;

            case PlyType.Uint:
                bytes = BitConverter.GetBytes(Convert.ToUInt32(val));
                break;

            case PlyType.Float:
                bytes = BitConverter.GetBytes(Convert.ToSingle(val));
                break;

            case PlyType.Double:
                bytes = BitConverter.GetBytes(Convert.ToDouble(val));
                break;

            default:
                throw new ArgumentException($"Cannot convert {nameof(PlyType)}. Given type is invalid.", nameof(dataType));
            }
            return(bytes);
        }
Exemplo n.º 9
0
        public static T ParseBytesToNative <T>(byte[] value, PlyType dataType)
        {
            object parsed = null;

            switch (dataType)
            {
            case PlyType.Char:
                parsed = BitConverter.ToChar(value, 0);
                break;

            case PlyType.Uchar:
                parsed = value[0];
                break;

            case PlyType.Short:
                parsed = BitConverter.ToInt16(value, 0);
                break;

            case PlyType.Ushort:
                parsed = BitConverter.ToUInt16(value, 0);
                break;

            case PlyType.Int:
                parsed = BitConverter.ToInt32(value, 0);
                break;

            case PlyType.Uint:
                parsed = BitConverter.ToUInt32(value, 0);
                break;

            case PlyType.Float:
                parsed = BitConverter.ToSingle(value, 0);
                break;

            case PlyType.Double:
                parsed = BitConverter.ToDouble(value, 0);
                break;
            }
            return((T)parsed);
        }
Exemplo n.º 10
0
 public ValueDoesNotFitTypeException(string reason, object value, PlyType dataType)
     : base(reason)
 {
     DataType = dataType;
     Value    = value;
 }