Ejemplo n.º 1
0
        public UInt32EnumFormatter()
        {
            Func <UInt32, UInt32> identity = Identity;

            this.serializeCast   = (T x) => { return(CastTo <UInt32> .From <T>(x)); };
            this.deserializeCast = (UInt32 x) => { return(CastTo <T> .From <UInt32>(x)); };
        }
Ejemplo n.º 2
0
        public Int64EnumFormatter()
        {
            Func <Int64, Int64> identity = Identity;

            this.serializeCast   = (T x) => { return(CastTo <Int64> .From <T>(x)); };
            this.deserializeCast = (Int64 x) => { return(CastTo <T> .From <Int64>(x)); };
        }
Ejemplo n.º 3
0
        public ByteEnumFormatter()
        {
            Func <Byte, Byte> identity = Identity;

            this.serializeCast   = (T x) => { return(CastTo <Byte> .From <T>(x)); };
            this.deserializeCast = (Byte x) => { return(CastTo <T> .From <Byte>(x)); };
        }
Ejemplo n.º 4
0
        public NullableUInt64EnumFormatter()
        {
            Func <UInt64, UInt64> identity = Identity;

            this.serializeCast   = (T x) => { return(CastTo <UInt64> .From <T>(x)); };
            this.deserializeCast = (UInt64 x) => { return(CastTo <T> .From <UInt64>(x)); };
        }
Ejemplo n.º 5
0
        public NullableInt16EnumFormatter()
        {
            Func <Int16, Int16> identity = Identity;

            this.serializeCast   = (T x) => { return(CastTo <Int16> .From <T>(x)); };
            this.deserializeCast = (Int16 x) => { return(CastTo <T> .From <Int16>(x)); };
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns all of the set flags
        /// </summary>
        public IEnumerator <FlagType> GetEnumerator()
        {
            if (ActiveFlags <= 0)
            {
                yield break;
            }

            int FlagNum = 0;

            for (int i = 0; i < Size; i++)
            {
                uint mask = unchecked ((uint)-1);
                for (int j = 0; j < CHUNK_SIZE; j++)
                {
                    mask <<= 1;
                    if ((Get_Chunk_Data(i) & mask) != 0)
                    {
                        yield return(CastTo <FlagType> .From(FlagNum));
                    }
                    FlagNum++;
                }
            }

            yield break;
        }
Ejemplo n.º 7
0
        public NullableSByteEnumFormatter()
        {
            Func <SByte, SByte> identity = Identity;

            this.serializeCast   = (T x) => { return(CastTo <SByte> .From <T>(x)); };
            this.deserializeCast = (SByte x) => { return(CastTo <T> .From <SByte>(x)); };
        }
        private T ApplyConversion()
        {
            T value;

            if (this.converter is null)
            {
                if (typeof(T) == typeof(string) && this.option.OptionType == CommandOptionType.SingleValue)
                {
                    value = CastTo <T> .From(this.option.Value());
                }
                else if (typeof(T).IsAssignableFrom(typeof(List <T>)) && this.option.OptionType == CommandOptionType.MultipleValue)
                {
                    value = CastTo <T> .From(this.option.Values);
                }
                else if (typeof(T).IsAssignableFrom(typeof(bool)) && this.option.OptionType == CommandOptionType.NoValue)
                {
                    value = CastTo <T> .From(this.option.HasValue());
                }
                else
                {
                    throw new FormatException($"You cannot bind directly from the option {this.option.LongName} with type {this.option.OptionType} option to a property of type {typeof(T)}, without providing a suitable converter.");
                }
            }
            else
            {
                if (this.converter is Func <string, T> stringConverter && this.option.OptionType == CommandOptionType.SingleValue)
                {
                    value = stringConverter(this.option.Value());
                }
Ejemplo n.º 9
0
        public void TestCastReferenceTypes()
        {
            var from = new Tuple <ulong>(0x0123456789abcdef);
            var to   = new Tuple <uint, uint>(0x89abcdef, 0x01234567);

            Assert.Throws <TypeInitializationException>(() => CastTo <Tuple <uint, uint> > .From(@from));
        }
        public static T Evaluate <T>(this BindingExpression bindingExpression)
        {
            bindingExpression.ThrowIfNull(nameof(bindingExpression));

            object?resolvedSource = bindingExpression.ResolvedSource;

            if (resolvedSource is null)
            {
                throw new ArgumentException(
                          "Failed to evalute binding expression. Source is not resolved.",
                          nameof(bindingExpression)
                          );
            }

            Type resolvedType = resolvedSource.GetType();

            PropertyInfo?prop = resolvedType.GetProperty(
                bindingExpression.ResolvedSourcePropertyName
                );

            if (prop is null)
            {
                throw new ArgumentException(
                          "Failed to evalute binding expression.", nameof(bindingExpression)
                          );
            }

            return(CastTo <T> .From(prop.GetValue(bindingExpression.ResolvedSource)));
        }
Ejemplo n.º 11
0
 private CastDefinition(TIn inputValue, CastTo <TIn, TOut> castMethod, int id)
 {
     From       = typeof(TIn);
     To         = typeof(TOut);
     Id         = id;
     InputValue = inputValue;
     Cast       = castMethod;
 }
Ejemplo n.º 12
0
 object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     if (value is TSource v)
     {
         return(Convert(v));
     }
     return(Convert(CastTo <TSource> .From(value)));
 }
Ejemplo n.º 13
0
        public void TestCastValueTypes()
        {
            int updateOrdinal = (int)AddUpdateRemove.Update;
            int removeOrdinal = (int)AddUpdateRemove.Remove;

            Assert.AreEqual(AddUpdateRemove.Update, CastTo <AddUpdateRemove> .From(updateOrdinal));
            Assert.AreEqual(AddUpdateRemove.Remove, CastTo <AddUpdateRemove> .From(removeOrdinal));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Retreives this value as the requested type if possible
        /// </summary>
        public T AsEnum <T>()
        {
            if (Type != EAttributeType.Enumerated)
            {
                throw new Exception(ExceptionMessages.ERROR_Attribute_Type_Mismatch);
            }

            return(CastTo <T> .From(Value));
        }
Ejemplo n.º 15
0
    public void Allocate <TEnum>(TEnum T)
    {
        _data = new TValue[CastTo <int> .From(T)];

        for (int i = 0; i < _data.Length; ++i)
        {
            _data[i] = default(TValue);
        }
    }
Ejemplo n.º 16
0
        public TEnum[] ReadUInt8Array <TEnum>(int length)
            where TEnum : Enum
        {
            TEnum[] data = new TEnum[length];
            for (int i = 0; i < length; i++)
            {
                data[i] = CastTo <TEnum> .From(this.ReadUInt8());
            }

            return(data);
        }
Ejemplo n.º 17
0
 public TValue Get <TEnum>(TEnum T)
 {
     try
     {
         return(_data[CastTo <int> .From(T)]);
     }
     catch (Exception ex)
     {
         ex.ToString();
         return(default(TValue));
     }
 }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            object o = (int)1234;
            var    v = CastTo <byte> .From(o);

            //int r = o as int;

            Expression <Func <object, int> > e = T => (int)T;

            ParameterExpression p = Expression.Parameter(typeof(object), "source");
            var u = Expression.Unbox(p, typeof(int));
            var c = Expression.ConvertChecked(u, typeof(int));
            var l = Expression.Lambda <Func <object, int> >(c, p).Compile();
            //int v = l(o);
            //Expression.Unbox(p,
        }
Ejemplo n.º 19
0
 public TEnum ReadUInt16 <TEnum>() => CastTo <TEnum> .From(this.ReadUInt16());
Ejemplo n.º 20
0
 public TEnum ReadInt16 <TEnum>()
     where TEnum : Enum => CastTo <TEnum> .From(this.ReadInt16());
Ejemplo n.º 21
0
 public void Set <TEnum>(TEnum Tenum, TValue Tvalue)
 {
     _data[CastTo <int> .From(Tenum)] = Tvalue;
 }
Ejemplo n.º 22
0
 internal override bool Equals(TEnum lhs, TEnum rhs)
 {
     return(CastTo <long> .From <TEnum>(lhs) == CastTo <long> .From <TEnum>(rhs));
 }
Ejemplo n.º 23
0
        internal override TEnum Deserialize(ISettingSerializer serializer)
        {
            long persistedValue = serializer.Deserialize(this, CastTo <long> .From <TEnum>(DefaultValue));

            return(CastTo <TEnum> .From <long>(persistedValue));
        }
Ejemplo n.º 24
0
 public CastDefinition(TIn inputValue, CastTo <TIn, TOut> castMethod, Type expectedException, [CallerLineNumber] int id = 0) : this(inputValue, castMethod, id)
 {
     ExpectedException = expectedException;
     IsValid           = false;
 }
Ejemplo n.º 25
0
 public CastDefinition(TIn inputValue, CastTo <TIn, TOut> castMethod, TOut expectedResult, [CallerLineNumber] int id = 0) : this(inputValue, castMethod, id)
 {
     ExpectedResult = expectedResult;
     IsValid        = true;
 }
Ejemplo n.º 26
0
 public TEnum ReadInt16 <TEnum>()
     where TEnum : Enum
 {
     return(CastTo <TEnum> .From(this.ReadInt16()));
 }
Ejemplo n.º 27
0
 public TEnum ReadUInt16 <TEnum>()
 {
     return(CastTo <TEnum> .From(this.ReadUInt16()));
 }
Ejemplo n.º 28
0
 public static EnumType FromInt(int t)
 {
     return(CastTo <EnumType> .From(t));
 }
Ejemplo n.º 29
0
 public static int ToInt(EnumType t)
 {
     return(CastTo <int> .From(t));
 }
Ejemplo n.º 30
0
            internal NumericRangeTermsEnum(NumericRangeQuery <T> outerInstance, TermsEnum tenum)
                : base(tenum)
            {
                this.outerInstance = outerInstance;
                switch (this.outerInstance.dataType)
                {
                case NumericType.INT64:
                case NumericType.DOUBLE:
                {
                    // lower
                    long minBound;
                    if (this.outerInstance.dataType == NumericType.INT64)
                    {
                        minBound = (!this.outerInstance.min.HasValue) ? long.MinValue : CastTo <long> .From(this.outerInstance.min.Value);
                    }
                    else
                    {
                        if (Debugging.AssertsEnabled)
                        {
                            Debugging.Assert(this.outerInstance.dataType == NumericType.DOUBLE);
                        }
                        minBound = (!this.outerInstance.min.HasValue) ? INT64_NEGATIVE_INFINITY
                                    : NumericUtils.DoubleToSortableInt64(CastTo <double> .From(this.outerInstance.min.Value));
                    }
                    if (!this.outerInstance.minInclusive && this.outerInstance.min != null)
                    {
                        if (minBound == long.MaxValue)
                        {
                            break;
                        }
                        minBound++;
                    }

                    // upper
                    long maxBound;
                    if (this.outerInstance.dataType == NumericType.INT64)
                    {
                        maxBound = (!this.outerInstance.max.HasValue) ? long.MaxValue : CastTo <long> .From(this.outerInstance.max.Value);
                    }
                    else
                    {
                        if (Debugging.AssertsEnabled)
                        {
                            Debugging.Assert(this.outerInstance.dataType == NumericType.DOUBLE);
                        }
                        maxBound = (!this.outerInstance.max.HasValue) ? INT64_POSITIVE_INFINITY
                                    : NumericUtils.DoubleToSortableInt64(CastTo <double> .From(this.outerInstance.max.Value));
                    }
                    if (!this.outerInstance.maxInclusive && this.outerInstance.max != null)
                    {
                        if (maxBound == long.MinValue)
                        {
                            break;
                        }
                        maxBound--;
                    }

                    NumericUtils.SplitInt64Range(new Int64RangeBuilderAnonymousClass(this), this.outerInstance.precisionStep, minBound, maxBound);
                    break;
                }

                case NumericType.INT32:
                case NumericType.SINGLE:
                {
                    // lower
                    int minBound;
                    if (this.outerInstance.dataType == NumericType.INT32)
                    {
                        minBound = (!this.outerInstance.min.HasValue) ? int.MinValue : CastTo <int> .From(this.outerInstance.min.Value);
                    }
                    else
                    {
                        if (Debugging.AssertsEnabled)
                        {
                            Debugging.Assert(this.outerInstance.dataType == NumericType.SINGLE);
                        }
                        minBound = (!this.outerInstance.min.HasValue) ? INT32_NEGATIVE_INFINITY
                                    : NumericUtils.SingleToSortableInt32(CastTo <float> .From(this.outerInstance.min.Value));
                    }
                    if (!this.outerInstance.minInclusive && this.outerInstance.min != null)
                    {
                        if (minBound == int.MaxValue)
                        {
                            break;
                        }
                        minBound++;
                    }

                    // upper
                    int maxBound;
                    if (this.outerInstance.dataType == NumericType.INT32)
                    {
                        maxBound = (!this.outerInstance.max.HasValue) ? int.MaxValue : CastTo <int> .From(this.outerInstance.max.Value);
                    }
                    else
                    {
                        if (Debugging.AssertsEnabled)
                        {
                            Debugging.Assert(this.outerInstance.dataType == NumericType.SINGLE);
                        }
                        maxBound = (!this.outerInstance.max.HasValue) ? INT32_POSITIVE_INFINITY
                                    : NumericUtils.SingleToSortableInt32(CastTo <float> .From(this.outerInstance.max.Value));
                    }
                    if (!this.outerInstance.maxInclusive && this.outerInstance.max != null)
                    {
                        if (maxBound == int.MinValue)
                        {
                            break;
                        }
                        maxBound--;
                    }

                    NumericUtils.SplitInt32Range(new Int32RangeBuilderAnonymousClass(this), this.outerInstance.precisionStep, minBound, maxBound);
                    break;
                }

                default:
                    // should never happen
                    throw new ArgumentException("Invalid NumericType");
                }

                termComp = Comparer;
            }