Esempio n. 1
0
        static void Main(string[] args)
        {
            // CalcTester tester = new CalcTester();

            UserInput     input  = new UserInput();
            LiberalParser parser = new LiberalParser();
            // Example of creating a delegate and assigning it to a
            // local variable
            OperatorDelegate addFunc = delegate(double num1, double num2) {
                return(num1 + num2);
            };

            Calculator calc = new Calculator(input, parser);

            // example of using local variable as the delegate function
            calc.Operators.Add(new DelegatedOperator("Add", "+", addFunc));

            calc.Operators.Add(new SubtractOperator());
            calc.Operators.Add(new MultiplyOperator());
            calc.Operators.Add(new DivisionOperator());

            // Example of pointing a delegate to an existing function
            calc.Operators.Add(new DelegatedOperator("Mod", "%", Modulus));

            // Example of passing in anonymous function as a delegate
            calc.Operators.Add(new DelegatedOperator("Pow", "^",
                                                     delegate(double num1, double num2)
            {
                return(Math.Pow(num1, num2));
            }
                                                     ));

            calc.RunCalculator();
        }
Esempio n. 2
0
 public OperatorType(string _operator,
                     OperatorDelegate _delegate,
                     bool _allowFloatingPoint)
 {
     this.Operator           = _operator;
     this.Delegate           = _delegate;
     this.AllowFloatingPoint = _allowFloatingPoint;
 }
Esempio n. 3
0
 internal Operator AddFunction(ValueType type1, ValueType returnType, OperatorDelegate fkt)
 {
     Functions.Add((uint)type1,
        new OperatorProc()
        {
            ReturnType = returnType,
            Function = fkt
        });
     return this;
 }
Esempio n. 4
0
 internal Operator AddFunction(ValueType type1, ValueType type2, ValueType returnType, OperatorDelegate fkt)
 {
     _is2ArgOp = true;
     Functions.Add((uint)((ushort)type1 + ((ushort)type2 << 16)),
        new OperatorProc()
        {
            ReturnType = returnType,
            Function = fkt
        });
     return this;
 }
Esempio n. 5
0
        public static object Operation <T>(List <object> args, OperatorDelegate <T> del)
        {
            if (args.Count == 2)
            {
                var left  = (T)args[0];
                var right = (T)args[1];

                return(del(left, right));
            }
            else
            {
                return(args.Cast <T>().Aggregate((left, right) => del(left, right)));
            }
        }
Esempio n. 6
0
        static void Main()
        {
            //Har soigneret outputtet

            Arithmetic m = new Arithmetic();

            // Delegate instantiation.
            OperatorDelegate mathOpr = m.Plus;

            mathOpr += m.Gange;
            mathOpr += m.Minus;
            mathOpr += m.Dele;

            // Invoke the delegate object.
            mathOpr(16, 2);       //Multicast -alle 4 regningsarter. Output -> 18, 32, 14, 8
            mathOpr.Invoke(8, 2); //Ingen forskel ift ovenst., bortset fra output --> 10, 16, 6, 4

            Console.WriteLine("Brug af foreach:");
            double nonsenseTotal = 0; //akkumulerer alle resultaterne fra de 4 regningsarter. Hvorfor: for at demonstrere outputtet fra delegate item

            foreach (Delegate item in mathOpr.GetInvocationList())
            {
                //Looper alle delegates i multicast igennem og eksekverer dem
                nonsenseTotal += (double)item.DynamicInvoke(20, 2);
            }
            Console.WriteLine("Nonsense total på alle: " + nonsenseTotal);
            //filtrer: kun addition og multiplikation må eksekveres

            Console.WriteLine("Filtreret på Plus og Gange:");
            nonsenseTotal = 0;
            double arg1 = 20, arg2 = 5;

            foreach (Delegate item in mathOpr.GetInvocationList())
            {
                //Filtrer: kun addition og multiplikation må eksekveres
                if (item.Method.Name == "Plus")
                {
                    nonsenseTotal += (double)item.DynamicInvoke(arg1, arg2);
                }

                if (item.Method.Name == "Gange")
                {
                    nonsenseTotal += (double)item.DynamicInvoke(arg1, arg2);  //redundant, burde refaktoreres
                }
            }
            Console.WriteLine("Nonsense total på plus og gange: " + nonsenseTotal);
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Esempio n. 7
0
        public void SendDownstream(TupleStream tuple)
        {
            // Console.WriteLine("Remote synchronous and asynchronous delegates.");
            // Console.WriteLine(new String('-', 80));
            // Console.WriteLine();
            Console.WriteLine(this.OutputReplicas.Count);
            foreach (Replica rep in this.OutputReplicas)
            {
                Uri nodeUri = rep.resolve(tuple);
                Console.WriteLine(String.Format("Sending {0} to {1}", tuple.Elems[0], nodeUri));
                IDictionary prop = new Hashtable();
                prop["name"]            = Guid.NewGuid().ToString();
                prop["typeFilterLevel"] = TypeFilterLevel.Full;
                TcpChannel channel = new TcpChannel(prop, null, null);
                ChannelServices.RegisterChannel(channel, false);

                Node op = (Node)Activator.GetObject(
                    typeof(Node),
                    nodeUri.ToString() + "/op");

                op.Execute(tuple);
                // This delegate is an asynchronous delegate. Two delegates must
                // be created. The first is the system-defined AsyncCallback
                // delegate, which references the method that the remote type calls
                // back when the remote method is done.

                AsyncCallback RemoteCallback = new AsyncCallback(this.RemoteCallBack);

                // Create the delegate to the remote method you want to use
                // asynchronously.
                OperatorDelegate RemoteDel = new OperatorDelegate(op.Execute);

                // Start the method call. Note that execution on this
                // thread continues immediately without waiting for the return of
                // the method call.
                IAsyncResult RemAr = RemoteDel.BeginInvoke(tuple, RemoteCallback, null);
            }
            // WaitHandle.WaitAll(AsyncHandles);
            // handleNum = 0;
        }
Esempio n. 8
0
 static bool cond_or(OperatorDelegate a, OperatorDelegate b, double indicator, double thresholdA, double thresholdB) => a(indicator, thresholdA) || b(indicator, thresholdB);
Esempio n. 9
0
 public DelegatedOperator(String name, string symbol, OperatorDelegate del)
 {
     this.Name      = name;
     this.Symbol    = symbol;
     this._delegate = del;
 }
Esempio n. 10
0
 public Operator(OperatorDelegate op)
 {
     Operation = op;
 }