Exemple #1
0
        public static byte[] GetSmallestBytes(this PrimPar type, int value)
        {
            byte[] bytes;

            if (value < 32 && value >= -32)
            {
                bytes    = new byte[1];
                bytes[0] = type.BitwiseOr(PrimPar.Short.BitwiseOr(PrimPar.Value.BitwiseAnd((byte)value)));
            }
            else if (value < sbyte.MaxValue && value > sbyte.MinValue)
            {
                bytes    = new byte[2];
                bytes[0] = type.BitwiseOr(PrimPar.Long).BitwiseOr(PrimPar.OneByte).AsByte();
                bytes[1] = (byte)(value & 0xFF);
            }
            else if (value < short.MaxValue && value > short.MinValue)
            {
                bytes    = new byte[3];
                bytes[0] = type.BitwiseOr(PrimPar.Long).BitwiseOr(PrimPar.TwoBytes).AsByte();
                bytes[1] = (byte)(value & 0xFF);
                bytes[2] = (byte)((value >> 8) & 0xFF);
            }
            else
            {
                bytes    = new byte[5];
                bytes[0] = type.BitwiseOr(PrimPar.Long).BitwiseOr(PrimPar.FourBytes).AsByte();
                bytes[1] = (byte)(value & 0xFF);
                bytes[2] = (byte)((value >> 8) & 0xFF);
                bytes[3] = (byte)((value >> 16) & 0xFF);
                bytes[4] = (byte)((value >> 24) & 0xFF);
            }

            return(bytes);
        }
Exemple #2
0
        internal Variable(PrimPar primParVariableType, int size, ref int offset)
        {
            if (primParVariableType != PrimPar.Local && primParVariableType != PrimPar.Global)
            {
                throw new ArgumentOutOfRangeException("Must be local or global", nameof(primParVariableType));
            }

            if (size < 0)
            {
                throw new ArgumentOutOfRangeException("Cannot be < 0", nameof(size));
            }

            // We can check the size of value types. If T is not a value type, assume it is variable sized.
            if (typeof(T).GetTypeInfo().IsValueType)
            {
                var expectedSize = Marshal.SizeOf <T>();
                if (size != expectedSize)
                {
                    throw new ArgumentOutOfRangeException($"Must be exactly {expectedSize}", nameof(size));
                }
            }

            this.primParVariableType = primParVariableType;
            this.size   = size;
            offset      = AlignIfNeeded(size, offset);
            this.offset = offset;
            bytes       = PrimPar.Variable.BitwiseOr(primParVariableType).GetSmallestBytes(offset);
            offset     += size;
        }
Exemple #3
0
 public static byte AsByte(this PrimPar primPar)
 {
     return((byte)primPar);
 }
Exemple #4
0
 public static PrimPar BitwiseOr(this PrimPar primPar, PrimPar value)
 {
     return((PrimPar)(primPar.AsByte() | (byte)value));
 }
Exemple #5
0
 public static byte BitwiseAnd(this PrimPar primPar, byte value)
 {
     return((byte)(primPar.AsByte() & value));
 }