Пример #1
0
        /// <summary>
        /// Registers the type of input device available.
        /// </summary>
        /// <param name="match"></param>
        /// <param name="creator"></param>
        public void RegisterInputDeviceType(MatchFunc match, CreatorFunc creator)
        {
            if (match == null)
            {
                throw new ArgumentNullException(nameof(match));
            }
            if (creator == null)
            {
                throw new ArgumentNullException(nameof(creator));
            }

            _specialDeviceCreators.Add(new SpecialDeviceCreator {
                Match = match, Creator = creator
            });

            // Reconnect any existing devices matching the predicate
            // List<string> matchingDevices = (from device in _inputDevices.Values where device.DeviceImp != null && match(device.DeviceImp) select device.Id).ToList();
            List <string> matchingDevices = new List <string>();

            foreach (var device in _inputDevices.Values)
            {
                if (device.DeviceImp != null && match(device.DeviceImp))
                {
                    matchingDevices.Add(device.Id);
                }
            }

            foreach (var devId in matchingDevices)
            {
                InputDevice dev = _inputDevices[devId];

                // Set device to disconnected state
                dev.Disconnect();

                // Inform interested users about disconnection
                InputDeviceDisconnected?.Invoke(this, new DeviceConnectionArgs {
                    InputDevice = dev
                });

                IInputDeviceImp inputDeviceImp = dev.DeviceImp;

                // Remove device from the list
                _inputDevices.Remove(devId);

                dev = creator(inputDeviceImp);
                _inputDevices[devId] = dev;

                // no need to call reconnect since device is constructed from scratch

                // Inform interested users about the newly connected device.
                InputDeviceConnected?.Invoke(this, new DeviceConnectionArgs {
                    InputDevice = dev
                });
            }
        }
Пример #2
0
 public Arrays <T> printMatchesElements(MatchFunc <T> isMatch)
 {
     for (int i = 0; i < arr.Length; i++)
     {
         if (isMatch(arr[i]))
         {
             Console.WriteLine(arr[i].ToString());
         }
     }
     return(this);
 }
        public void ShouldHandleNullPattern()
        {
            var func = new MatchFunc<string, string>
            {
                {"a", s => PatternAInvocation},
                {Match.Null, s => NullPatternInvocation}
            }.Func;

            state = func(null).Single();

            AssertStateIs(NullPatternInvocation);
        }
        public void ShouldEvaluatePredicateWhenMatchingPatterns()
        {
            var func = new MatchFunc<string, string>
            {
                {"a", s => PatternAInvocation},
                {s => s.StartsWith("d"), s => PredicatePatternInvocation},
                { Match.Default, s => DefaultInvocation }
            }.Func;

            state = func("dcx").Single();

            AssertStateIs(PredicatePatternInvocation);
        }
        public void ShouldCallProperFuncWhenPatternMatches()
        {
            var func = new MatchFunc<string, string>
            {
                {"a", s => PatternAInvocation},
                {"b", s => PatternBInvocation},
                {"c", s => PatternCInvocation}
            }.Func;

            state = func("a").Single();

            AssertStateIs(PatternAInvocation);
        }
        public void ShouldCallDefaultFuncWhenPatternDoesNotMatchAndDefaultIsSpecified()
        {
            var func = new MatchFunc<string, string>
            {
                {"a", s => PatternAInvocation},
                {"b", s => PatternBInvocation},
                { Match.Default, s => DefaultInvocation }
            }.Func;

            state = func("c").Single();

            AssertStateIs(DefaultInvocation);
        }
Пример #7
0
        /// <summary>
        /// Filter 関数。条件関数が真のものだけ要素として返す。
        /// </summary>
        /// <param name="arr">操作対象</param>
        /// <param name="f">判定に使う条件関数。</param>
        /// <returns></returns>
        static public ArrayList Filter(ArrayList arr, MatchFunc f)
        {
            ArrayList ret = new ArrayList();

            for (int i = 0; i < arr.Count; i++)
            {
                bool use = f(( T )arr[i]);
                if (use)
                {
                    ret.Add(arr[i]);
                }
            }
            return(ret);
        }
Пример #8
0
        public static async Task <T> FirstAsync <T>(this IEnumerable <Func <CancellationToken, Task <T> > > funcs, Predicate <T> pred)
        {
            var tasks = new List <Task>();

            using (MatchFunc <T> match = new MatchFunc <T>(pred))
            {
                foreach (Func <CancellationToken, Task <T> > func in funcs)
                {
                    if (match.Canceled)
                    {
                        break;
                    }

                    Task task = match.RunAsync(func);
                    tasks.Add(task);
                }

                await Task.WhenAll(tasks);

                return(match.Result);
            }
        }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DocumentTypeRouteConstraint"/> class.
 /// </summary>
 /// <param name="documentTypeAlias">
 /// The document type alias.
 /// </param>
 /// <param name="match">
 /// Additional instruction on how to match this route if omitted will only check that the controller match <paramref name="documentTypeAlias"/>.
 /// </param>
 public DocumentTypeRouteConstraint(string documentTypeAlias, MatchFunc match = null)
 {
     this.match        = match;
     DocumentTypeAlias = documentTypeAlias;
 }
        public void ShouldInvokeOnlyFirstMatch()
        {
            var func = new MatchFunc<string, string>
            {
                {"a", s => PatternAInvocation},
                {"a", s => PatternASecondInvocation},
                { Match.Default, s => DefaultInvocation }
            }.Func;

            state = func("a").Single();

            AssertStateIs(PatternAInvocation);
        }
        public void ShouldNotCallAnyFuncWhenPatternDoesNotMatch()
        {
            var func = new MatchFunc<string, string>
            {
                {"a", s => PatternAInvocation},
                {"b", s => PatternBInvocation}
            }.Func;

            var result = func("c");
            if (result.Any())
            {
                state = result.Single();
            }

            AssertStateIs(NotChangedByTest);
        }
Пример #12
0
 public PreviousDependantDefinition(MatchFunc Match)
 {
     this.Match = Match;
 }
Пример #13
0
 public MatchingTokenDefinition(MatchFunc Match)
 {
     this.Match = Match;
 }
Пример #14
0
 public Matcher()
 {
     _localMatchFunc = entity => true;
 }
Пример #15
0
 public Matcher(MatchFunc matchFunc)
 {
     _localMatchFunc = matchFunc;
 }