Exemplo n.º 1
0
        private static void Main(string[] args)
        {
            IHasValue[] stuffWithValue = new IHasValue[10];
            IHasValue   previous       = null;

            stuffWithValue[0] = new Building("Small hut", 1000f, 25f);
            stuffWithValue[1] = new MilitaryUnit(3, 50, 27);
            stuffWithValue[2] = new SettlerUnit(2, 10);
            stuffWithValue[3] = new Building("Cave", 5, 37.5f);
            stuffWithValue[4] = new MilitaryUnit(10, 10, 5);
            stuffWithValue[5] = new Building("Skyscrapper", 15005f, 2700.2f);
            stuffWithValue[6] = new Building("Stadium", 15005f, 668.7f);
            stuffWithValue[7] = new SettlerUnit(2, 20);
            stuffWithValue[8] = new SettlerUnit(2, 20);
            stuffWithValue[9] = new MilitaryUnit(120, 120, 25);

            foreach (IHasValue ihv in stuffWithValue)
            {
                Console.WriteLine("{0}, {1} to previous",
                                  ihv.GetType().Name,
                                  ihv.Equals(previous) ? "equal" : "NOT equal");

                previous = ihv;
            }
        }
Exemplo n.º 2
0
        private static void StoreSingleValue(
            CommandlineArgument opt,
            IHasValue input,
            ParserOptions options,
            List <string> uncollectedArgs,
            Dictionary <string, object> store,
            HashSet <string> errored
            )
        {
            var prop = opt.Key;

            uncollectedArgs.AddRange(input.AllValues.Except(
                                         new[]
            {
                input.SingleValue
            })
                                     );
            if (store.ContainsKey(prop))
            {
                if (!errored.Contains(opt.Key))
                {
                    errored.Add(opt.Key);
                    options.ReportMultipleValuesForSingleValueArgument($"--{opt.LongName}");
                }
            }

            store[prop] = input.SingleValue;
        }
Exemplo n.º 3
0
        /// <inheritdoc />
        protected override IHasValue Calculate(IHasValue firstArg, IHasValue secondArg)
        {
            decimal firstValue  = firstArg.GetValue();
            decimal secondValue = secondArg.GetValue();

            return(_numberFactory.CreateNumber(firstValue + secondValue));
        }
Exemplo n.º 4
0
        private static void Main(string[] args)
        {
            IHasValue[] stuffWithValue = new IHasValue[] {
                new Building("Small hut", 1000f, 25f),
                new MilitaryUnit(3, 50, 27),
                new SettlerUnit(2, 10),
                new Building("Cave", 5, 37.5f),
                new MilitaryUnit(10, 10, 5),
                new Building("Skyscrapper", 15005f, 2700.2f),
                new Building("Stadium", 15005f, 668.7f),
                new SettlerUnit(2, 20),
                new SettlerUnit(2, 20),
                new MilitaryUnit(120, 120, 25)
            };

            // How many buildings
            Console.WriteLine("No. buildings     : {0}",
                              HowManyOfType <Building>(stuffWithValue));
            Console.WriteLine("No. units (any)   : {0}",
                              HowManyOfType <Unit>(stuffWithValue));
            Console.WriteLine("No. settler units : {0}",
                              HowManyOfType <SettlerUnit>(stuffWithValue));
            Console.WriteLine("No. military units: {0}",
                              HowManyOfType <MilitaryUnit>(stuffWithValue));
        }
Exemplo n.º 5
0
        protected override void ParseRight(Token tk, int opPrecedence, IHasValue Acc, ref IHasValue valueLeft)
        {
            var tt = tk.Type;

            switch (tt)
            {
            case TokenType.Custom:
                switch (tk.CustomType)
                {
                case MathToken.Transpose:
                    NextToken();
                    if (EmitDelegateExpr(ref valueLeft, new Func <Matrix, Matrix>(a => a.Transpose()), "'"))
                    {
                        return;
                    }
                    break;

                default:
                    base.ParseRight(tk, opPrecedence, Acc, ref valueLeft);
                    break;
                }
                break;

            default:
                base.ParseRight(tk, opPrecedence, Acc, ref valueLeft);
                break;
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Extension method adds a null check to setting a value on any IHasValue&gt;T&lt;
 /// So you can safely call myHasValue.Set(value), even if myHasValue is null.
 /// </summary>
 public static void Set <T>(this IHasValue <T> target, T value)
 {
     if (target == null)
     {
         return;
     }
     target.value = value;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Extension method adds a null check to setting a value on any IHasValue&gt;T&lt;
 /// So you can safely call myHasValue.Set(value), even if myHasValue is null.
 /// </summary>
 public static T Get <T>(this IHasValue <T> target, T dftVal = default(T))
 {
     if (target == null)
     {
         return(dftVal);
     }
     return(target.value);
 }
Exemplo n.º 8
0
 public static double?GetDebitValueOrNull(this IHasValue hasValue)
 {
     if (hasValue.Value <= 0)
     {
         return(hasValue.GetDebitValue());
     }
     return(null);
 }
Exemplo n.º 9
0
 public bool Equals(IHasValue other)
 {
     if (other == null)
     {
         return(false);
     }
     return(Value == other.Value);
 }
Exemplo n.º 10
0
        public static void Example()
        {
            var myStruct = new MyStruct();
            myStruct.Value = 10;
            IHasValue hasValue = myStruct;
            hasValue.Value = 20;

            Console.WriteLine("myStruct.Value = {0}", myStruct.Value);
        }
Exemplo n.º 11
0
        public void As_Invalid()
        {
            string        expected = Guid.NewGuid().ToString();
            BespokeString bespoke  = new BespokeString(expected);

            Assert.IsNotInstanceOfType(bespoke, typeof(IHasValue));
            IHasValue duck = DuckType.As <IHasValue>(bespoke);

            Assert.IsNull(duck);
        }
Exemplo n.º 12
0
        internal Cell GetCell(int x, int y)
        {
            string           cellName = Cell.GetCellName(x, y);
            IHasValue <Cell> var      = GetVariable <Cell>(cellName);

            if (var == null)
            {
                return(null);
            }
            return(var.Value);
        }
Exemplo n.º 13
0
 public DelegateExpr(IHasValue <P1> p1, Func <P1, T> dlg, string shortName)
 {
     mP1 = p1;
     if (p1 != null)
     {
         p1.Subscribe(this, "p1");
     }
     System.Diagnostics.Debug.Assert(dlg != null);
     mDelegate  = dlg;
     mShortName = shortName;
 }
Exemplo n.º 14
0
        public void As_Struct()
        {
            int        expected = _random.Next();
            BespokeInt bespoke  = new BespokeInt(expected);

            Assert.IsNotInstanceOfType(bespoke, typeof(IHasValue));
            IHasValue duck = DuckType.As <IHasValue>(bespoke);

            Assert.IsInstanceOfType(duck, typeof(IHasValue));
            Assert.AreEqual(expected, duck.Value);
        }
Exemplo n.º 15
0
        public void As()
        {
            DateTime        expected = new DateTime(_random.Next(1900, 2000), _random.Next(1, 12), _random.Next(1, 28));
            BespokeDateTime bespoke  = new BespokeDateTime(expected);

            Assert.IsNotInstanceOfType(bespoke, typeof(IHasValue));
            IHasValue duck = DuckType.As <IHasValue>(bespoke);

            Assert.IsInstanceOfType(duck, typeof(IHasValue));
            Assert.AreEqual(expected, duck.Value);
        }
Exemplo n.º 16
0
        protected override IHasValue ParseLeft(Token token, ref IHasValue result)
        {
            switch (token.Type)
            {
            case TokenType.OpenBracket:
                return(ParseMatrix());

            default:
                return(base.ParseLeft(token, ref result));
            }
        }
Exemplo n.º 17
0
        public GetArrayEntryExpr(IHasValue array, List <IHasValue> @params)
        {
            array.Subscribe(this, "Array");
            Subscribe("index", @params);
            IHasValue[] newParams = @params.ToArray();
            int[]       newValues = new int[@params.Count];

            mArray            = array;
            mParams           = newParams;
            mValues           = newValues;
            mResultSystemType = array.ValueType.GetElementType();
        }
Exemplo n.º 18
0
        /// <inheritdoc />
        protected override IHasValue Calculate(IHasValue firstArg, IHasValue secondArg)
        {
            decimal firstValue  = firstArg.GetValue();
            decimal secondValue = secondArg.GetValue();

            if (secondValue == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(secondArg),
                                                      ResourceStore.GetExceptionMessage("DenominatorIsZero"));
            }

            return(_numberFactory.CreateNumber(firstValue / secondValue));
        }
Exemplo n.º 19
0
        // ToDo: Parametr 'baseObject' is never used.
        private Func <T> EmitGetMethod(IHasValue baseObject, MethodInfo mi)
        {
            // ToDo: variable 'paramTypes' is never used.
            var           paramTypes = (from p in mPreparedParams select p.ValueType).ToArray();
            DynamicMethod meth       = new DynamicMethod(
                "DynamicGetMethod",
                typeof(T),
                new Type[] { GetType() },
                GetType(),  // associate with a type
                true);
            ILGenerator il = meth.GetILGenerator();
            FieldInfo   fiPreparedParams = GetType().GetField("mPreparedParams", BindingFlags.Instance | BindingFlags.NonPublic);
            MethodInfo  miGetObjectValue = typeof(IHasValue).GetProperty("ObjectValue").GetGetMethod();

            if (!mi.IsStatic)
            {
                FieldInfo fiBaseObject = GetType().GetField("mBaseObject", BindingFlags.Instance | BindingFlags.NonPublic);
                il.Emit(OpCodes.Ldarg_0);             // this
                il.Emit(OpCodes.Ldfld, fiBaseObject); // this.mParams
                il.Emit(OpCodes.Callvirt, miGetObjectValue);
            }
            var methodParams = mi.GetParameters();

            for (int i = 0; i < mPreparedParams.Length; i++)
            {
                il.Emit(OpCodes.Ldarg_0);                 // this
                il.Emit(OpCodes.Ldfld, fiPreparedParams); // this.mParams
                il.Emit(OpCodes.Ldc_I4, i);               // i
                il.Emit(OpCodes.Ldelem_Ref);              // this.mParams[i]
                il.Emit(OpCodes.Callvirt, miGetObjectValue);
                var preparedType = mPreparedParams[i].ValueType;
                if (preparedType == typeof(string))
                {
                    throw new NotImplementedException("check this");
                }
                if (preparedType.IsValueType && methodParams[i].ParameterType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox_Any, preparedType);
                }
            }
            il.Emit(OpCodes.Call, mi);
            il.Emit(OpCodes.Ret);

            Delegate dlg = meth.CreateDelegate(typeof(Func <T>), this);

            return((Func <T>)dlg);
        }
Exemplo n.º 20
0
        private void EmitGetField(IHasValue baseObject, MemberInfo method)
        {
            Expression expr;

            if (baseObject != null)
            {
                var expectedType = typeof(IHasValue <>).MakeGenericType(baseObject.ValueType);
                expr = Expression.MakeMemberAccess(Expression.Constant(baseObject), expectedType.GetProperty("Value"));
                expr = Expression.Field(expr, (FieldInfo)method);
            }
            else
            {
                expr = Expression.Field(null, (FieldInfo)method);
            }
            var lambda = Expression.Lambda <Func <T> >(expr);

            mValueDelegate = lambda.Compile();
        }
Exemplo n.º 21
0
 public OperatorIfExpr(IHasValue ifExpr, IHasValue thenExpr, IHasValue elseExpr)
 {
     this.ifExpr = ifExpr;
     if (ifExpr != null)
     {
         ifExpr.Subscribe(this, "if");
     }
     this.thenExpr = thenExpr;
     if (thenExpr != null)
     {
         thenExpr.Subscribe(this, "then");
     }
     this.elseExpr = elseExpr;
     if (elseExpr != null)
     {
         elseExpr.Subscribe(this, "else");
     }
     mSystemType = thenExpr.ValueType;
 }
Exemplo n.º 22
0
        static void Main(string[] args)
        {
            IHasValue[] stuff = new IHasValue[]
            {
                new Building(5, "Cave", 20),
                new SettlerUnit(4, 1000),
                new MilitaryUnit(5, 200, 50),
                new Building(5000, "Apartement", 20),
                new Building(5000, "House", 20),
                new MilitaryUnit(3, 400, 500),
                new Building(5000, "ManiacMansion", 20),
            };
            IHasValue previous = null;

            foreach (IHasValue thing in stuff)
            {
                Console.Write(thing.GetType().Name + " ");
                Console.Write(thing.Equals(previous) ? "It's equal!" : "They're different!");
                previous = thing;
            }
        }
Exemplo n.º 23
0
        //internal override int GetPrecedence(BaseToken tk, bool unary)
        //{
        //    return 0;
        //}


        protected override void ParseRight(Token tk, int opPrecedence, IHasValue Acc, ref IHasValue valueLeft)
        {
            switch (tk.Type)
            {
            case TokenType.Custom:

                IHasValue valueRight;
                switch (tk.CustomType)
                {
                case VbTokenType.OperatorPercent:
                    NextToken();
                    if (EmitDelegateExpr(ref valueLeft, Acc, new Func <double, double, double>((a, b) => a * b / 100), "%"))
                    {
                        return;
                    }
                    break;

                case VbTokenType.IntegerDiv:
                    NextToken();
                    valueRight = ParseExpr(valueLeft, opPrecedence);
                    if (EmitDelegateExpr(ref valueLeft, valueRight, new Func <int, int, int>((a, b) => a / b), "\\"))
                    {
                        return;
                    }
                    break;
                }
                break;

            case TokenType.OperatorDivide:
                NextToken();
                valueRight = ParseExpr(valueLeft, opPrecedence);
                if (EmitDelegateExpr(ref valueLeft, valueRight, new Func <double, double, double>((a, b) => a / b), "/"))
                {
                    return;
                }
                break;
            }
            base.ParseRight(tk, opPrecedence, Acc, ref valueLeft);
        }
Exemplo n.º 24
0
        protected override void ParseRight(Token tk, int opPrecedence, IHasValue Acc, ref IHasValue valueLeft)
        {
            var tt = tk.Type;

            switch (tt)
            {
            case TokenType.OperatorIf:
                NextToken();
                IHasValue thenExpr = ParseExpr(null, 1);
                if (!Expect(TokenType.OperatorColon, "Missing : in ? expression test ? valueIfTrue : valueIfFalse.", ref valueLeft))
                {
                    return;
                }
                IHasValue elseExpr = ParseExpr(null, 1);
                var       t        = typeof(OperatorIfExpr <>).MakeGenericType(thenExpr.ValueType);

                valueLeft = (IHasValue)Activator.CreateInstance(t, valueLeft, thenExpr, elseExpr);
                break;

            default:
                base.ParseRight(tk, opPrecedence, Acc, ref valueLeft);
                break;
            }
        }
Exemplo n.º 25
0
 public static IDisposable Subscribe(this IHasValue source, string role, Action action)
 {
     return(source.Subscribe(new SimpleObserver(source, action), role));
 }
Exemplo n.º 26
0
 public LeftShiftLogicGate(IHasValue source, ushort bitsNumber)
     : base(source, bitsNumber)
 {
 }
Exemplo n.º 27
0
		static void Worker(IHasValue state)
		{
			Console.WriteLine("Worker runs on thread id {0}", Thread.CurrentThread.ManagedThreadId);
			Console.WriteLine("State value: {0}", state.Value.Text);
		}
        static void Worker(IHasValue state)
        {
            WriteLine($"Worker runs on thread id {CurrentThread.ManagedThreadId}.");

            WriteLine($"State value: {state.Value.Text}");
        }
Exemplo n.º 29
0
 protected BinaryLogicGate(IHasValue source1, IHasValue source2)
 {
     _source1 = source1;
     _source2 = source2;
 }
Exemplo n.º 30
0
 public UnaryLogicGate(IHasValue source)
 {
     _source = source;
 }
Exemplo n.º 31
0
 public NotLogicGate(IHasValue source)
     : base(source)
 {
 }
Exemplo n.º 32
0
 public void OnValueChanged(IHasValue value)
 {
     mAction();
 }
Exemplo n.º 33
0
 public SimpleObserver(IHasValue source, Action action)
 {
     mSource = source;
     mAction = action;
 }
Exemplo n.º 34
0
 public void OnValueChanged(IHasValue value)
 {
     mAction();
 }
Exemplo n.º 35
0
 protected ShiftGate(IHasValue source, ushort bitsNumber)
 {
     _source = source;
     _bitsNumber = bitsNumber;
 }
Exemplo n.º 36
0
 public AndLogicGate(IHasValue source1, IHasValue source2)
     : base(source1, source2)
 {
 }
Exemplo n.º 37
0
 public static double GetDebitValue(this IHasValue hasValue)
 {
     return(hasValue.Value <= 0 ? -1 * hasValue.Value : 0);
 }
Exemplo n.º 38
0
 protected ISet<HashedValue> FromArray(IHasValue[] values)
 {
     if (values == null) return null;
     var hashedValues = values.Select(value =>
         {
             return new HashedValue(value.Value);
         });
     var set = new SortedSet<HashedValue>((ICollection<HashedValue>)hashedValues.ToList());
     return set;
 }
Exemplo n.º 39
0
 public Wire(string name, IHasValue source)
     : this(name)
 {
     Source = source;
 }
Exemplo n.º 40
0
 public SimpleObserver(IHasValue source, Action action)
 {
     mSource = source;
     mAction = action;
 }
Exemplo n.º 41
0
 static void Worker(IHasValue state)
 {
     Console.WriteLine("Worker runs on thread id {0}", Thread.CurrentThread.ManagedThreadId);
     Console.WriteLine("State value: {0}", state.Value.Text);
 }