Пример #1
0
        public void AddSignal(ISignal signal)
        {
            int count = Signals.Count(c => c.Name.Contains(signal.Name));

            signal.Name = count != 0 ? $"{ signal.Name}({count})" : signal.Name;
            if (signal.GetType() == typeof(SampledSignal))
            {
                SampledSignals.Add((SampledSignal)signal);
            }
            if (signal.GetType() == typeof(ContinuousSignal))
            {
                ContinuousSignals.Add((ContinuousSignal)signal);
            }
            Signals.Add(signal);
        }
Пример #2
0
        private Task PublishSignal(ISignal signal, CancellationToken cancellationToken = default)
        {
            var signalType = signal.GetType();
            var handler    = SignalHandlers.GetOrAdd(signalType,
                                                     t => (SignalHandlerWrapper)Activator.CreateInstance(typeof(SignalHandlerWrapperImpl <>).MakeGenericType(signalType)));

            return(handler.Handle(signal, cancellationToken, ServiceFactory, PublishCore));
        }
Пример #3
0
 public override bool Check(ISignal sig)
 {
     if (sig.GetType() != typeof(ASignalValue))
     {
         return(false);
     }
     return(((ASignalValue)sig).key == key && ((ASignalValue)sig).Value == Value);
 }
Пример #4
0
 public override bool Check(ISignal sig)
 {
     if (sig.GetType() != typeof(ASignalCode))
     {
         return(false);
     }
     return(((ASignalCode)sig).code == code);
 }
Пример #5
0
        /// <inheritdoc/>
        public Task SignalAsync(ISignal <TId, TData> signal, CancellationToken token)
        {
            if (!_signals.TryGetValue(signal.GetType(), out var handler))
            {
                return(Task.CompletedTask);
            }

            return(handler(signal, token));
        }
Пример #6
0
 /// Apply ListensTo delegates
 private void AssignDelegate(MonoBehaviour mediator, ISignal signal, MethodInfo method)
 {
     if (signal.GetType().BaseType.IsGenericType)
     {
         signal.listener = Delegate.CreateDelegate(signal.listener.GetType(), mediator, method);                 //e.g. Signal<T>, Signal<T,U> etc.
     }
     else
     {
         ((Signal)signal).AddListener((Action)Delegate.CreateDelegate(typeof(Action), mediator, method));                 //Assign and cast explicitly for Type == Signal case
     }
 }
Пример #7
0
 /// Apply ListensTo delegates
 private void AssignDelegate(MonoBehaviour mediator, ISignal signal, MethodInfo method)
 {
     if (signal.GetType().BaseType.IsGenericType)
     {
         signal.listener = Delegate.CreateDelegate(signal.listener.GetType(), mediator, method); //e.g. Signal<T>, Signal<T,U> etc.
     }
     else
     {
         ((Signal)signal).AddListener((Action)Delegate.CreateDelegate(typeof(Action), mediator, method)); //Assign and cast explicitly for Type == Signal case
     }
 }
Пример #8
0
 /// Remove any existing ListensTo Delegates
 protected void RemoveDelegate(object mediator, ISignal signal, MethodInfo method)
 {
     if (signal.GetType().BaseType.IsGenericType)             //e.g. Signal<T>, Signal<T,U> etc.
     {
         Delegate toRemove = Delegate.CreateDelegate(signal.listener.GetType(), mediator, method);
         signal.listener = Delegate.Remove(signal.listener, toRemove);
     }
     else
     {
         ((Signal)signal).RemoveListener((Action)Delegate.CreateDelegate(typeof(Action), mediator, method));                 //Assign and cast explicitly for Type == Signal case
     }
 }
Пример #9
0
 /// Apply ListensTo delegates
 protected void AssignDelegate(object mediator, ISignal signal, MethodInfo method)
 {
     if (signal.GetType().BaseType.IsGenericType)
     {
         var toAdd = Delegate.CreateDelegate(signal.listener.GetType(), mediator, method);                 //e.g. Signal<T>, Signal<T,U> etc.
         signal.listener = Delegate.Combine(signal.listener, toAdd);
     }
     else
     {
         ((Signal)signal).AddListener((Action)Delegate.CreateDelegate(typeof(Action), mediator, method));                 //Assign and cast explicitly for Type == Signal case
     }
 }
Пример #10
0
    public void SignalTriggered(ISignal iSignal)
    {
        Type type = iSignal.GetType();

        if (type != typeof(S))
        {
            Debug.LogWarning("Got wrong Signal handler(" + typeof(S) + ") for type (" + type + ")");
            return;
        }
        S signal = (S)iSignal;

        SignalTriggered(signal);
    }
Пример #11
0
 private MethodCallExpression BuildMethodCall(ISignal signal)
 => Expression.Call(Expression.Constant(signal),
                    signal.GetType().GetMethod(nameof(ISignal.Calculate), new Type[] { typeof(IReadOnlyList <Candle>), typeof(int) }),
                    _candles,
                    _index);
Пример #12
0
 public bool Assignable(ISignal other)
 {
     return(this.GetType() == other.GetType() &&
            this.size == ((StdLogicVector)other).size);
 }
Пример #13
0
 private void AssignDelegate(object model, ISignal signal, MethodInfo method)
 {
     try {
         if (signal.GetType().BaseType.IsGenericType) {
             Type genericActionType = typeof(Action<>).MakeGenericType(signal.GetType().BaseType.GetGenericArguments());
             signal.AddListener(Delegate.CreateDelegate(genericActionType, model, method));
         } else {
             signal.AddListener((Action)Delegate.CreateDelegate(typeof(Action), model, method));
         }
     } catch (Exception e) {
         Debug.LogError(e.GetType() + " " + e.Message + " " + signal.GetType().Name + " " + method.Name);
     }
 }
Пример #14
0
        /// <summary>
        /// Inspects the listenerObject for methods decorated with the [ListenTo] attribute.
        /// Then, Creates a delegate for each of those methods and binds it to the corresponding
        /// Signal instance registered with the SignalContext.
        /// Objects that autobind their listeners this way must call UnbindSignals()
        /// before they are destroyed.
        /// </summary>
        protected void BindSignals(System.Object listenerObject)
        {
            // Try to retrieve MethodInfo's for the targetObject from a cache first, cuz reflection is slow
            IEnumerable <MethodInfo> methodInfos = null;
            Type listenerObjectType = listenerObject.GetType();

            if (this.methodInfoCache.TryGetValue(listenerObjectType, out methodInfos) == false)
            {
                // If you REEAALLLLY have to, use reflection to look up the methodInfos and cache them
                methodInfos = listenerObjectType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
                this.methodInfoCache[listenerObjectType] = methodInfos;
            }

            // Loop though all the methodInfos on the targetObject...
            foreach (MethodInfo methodInfo in methodInfos)
            {
                // Identify listener methods by retrieving a SignalListenerAttribute associated with the methodInfo
                ListenTo[] listenerBindings = null;
                listenerBindings = (ListenTo[])methodInfo.GetCustomAttributes(typeof(ListenTo), false);

                // Should be only one ListenToAttribute since they are exclusive (if there is one at all)
                foreach (ListenTo listenTo in listenerBindings)
                {
                    // Debug.Log("ListenToAttribute:" + listenTo.SignalType);
                    // Get an ISignal reference from the signalContext based on the SignalType
                    ISignal signal = this.SignalContext.GetSignal(listenTo.SignalType);
                    if (signal != null)
                    {
                        var methodParameterCount = methodInfo.GetParameters().Length;
                        if (methodParameterCount != signal.ParameterCount)
                        {
                            throw new ArgumentException($"Incorrect number of parameters found when binding [ListenTo(typeof({ signal.GetType().Name })]. Expected to find {signal.ParameterCount} parameter(s) but found { methodParameterCount }.");
                        }

                        Delegate d = null;
                        try
                        {
                            // Create a callback listener for this methodInfo
                            d = Delegate.CreateDelegate(signal.GetListenerType(), listenerObject, methodInfo);
                        }
                        catch (ArgumentException)
                        {
                            var parameters     = methodInfo.GetParameters();
                            var parameterTypes = new Type [parameters.Length];
                            for (int i = 0; i < parameters.Length; i++)
                            {
                                parameterTypes[i] = parameters[i].ParameterType;
                            }

                            var typeMissmatchMessage = string.Empty;
                            var parameterIndex       = 0;
                            if (signal.TryGetParameterErrorMessage(parameterTypes, out typeMissmatchMessage, out parameterIndex))
                            {
                                throw new ArgumentException($"Incorrect parameter type while binding listener method `{ methodInfo.DeclaringType.Name }.{ methodInfo.Name }`. { typeMissmatchMessage } The { ToOrdinal(parameterIndex+1) } parameter in the listener method does not match what is defined by the Signal.");
                            }
                            else
                            {
                                throw;
                            }
                        }

                        signal.AddListener(d, listenTo.ListenerType);

                        // Add the signal listener to the internal list of delegates associated with the listenerObject
                        // ( so we can easily unbind all the signal listeners later. )
                        List <SignalListener> delegateList = null;
                        if (this.signalListenersByObject.TryGetValue(listenerObject, out delegateList) == false)
                        {
                            //Debug.Log("adding signal listener:" + methodInfo.Name + " for " + listenerObjectType);
                            delegateList = new List <SignalListener>();
                            this.signalListenersByObject[listenerObject] = delegateList;
                        }
                        delegateList.Add(new SignalListener(signal, d));
                    }
                    else
                    {
                        throw new InvalidOperationException($"Unable to Bind Singals for an instance of '{ listenerObjectType }'. The Signal '{ listenTo.SignalType }' is not registered with the SignalManager.");
                    }
                }
            }
        }
Пример #15
0
 public bool Assignable(ISignal other)
 {
     return(this.GetType() == other.GetType());
 }