using System; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; namespace Artemis.Core; /// /// Represents an output pin containing a value of type on a /// public sealed class OutputPin : Pin { #region Constructors [JsonConstructor] internal OutputPin(INode node, string name) : base(node, name) { _value = default; IsNumeric = typeof(T) == typeof(Numeric); } #endregion #region Properties & Fields /// public override Type Type { get; } = typeof(T); /// public override object? PinValue => Value; /// public override PinDirection Direction => PinDirection.Output; private T? _value; /// /// Gets or sets the value of the output pin /// public T? Value { get { if (!IsEvaluated) Node?.Evaluate(); return _value; } set { _value = value; IsEvaluated = true; OnPropertyChanged(nameof(PinValue)); } } #endregion } /// /// Represents an output pin on a /// public sealed class OutputPin : Pin { #region Constructors internal OutputPin(INode node, Type type, string name) : base(node, name) { _type = type; _value = type.GetDefault(); IsNumeric = type == typeof(Numeric); } #endregion #region Methods /// /// Changes the type of this pin, disconnecting any pins that are incompatible with the new type. /// /// The new type of the pin. public void ChangeType(Type type) { if (type == _type) return; base.ChangeType(type, ref _type); Value = type.GetDefault(); } #endregion #region Properties & Fields /// public override Type Type => _type; /// public override object? PinValue => Value; /// public override PinDirection Direction => PinDirection.Output; private object? _value; private Type _type; /// /// Gets or sets the value of the output pin /// public object? Value { get { if (!IsEvaluated) Node?.Evaluate(); return _value; } set { if (Type.IsValueType && value == null) // We can't take null for value types so set it to the default value for that type _value = Type.GetDefault(); else if (value != null) // If a value was given make sure it matches if (!Type.IsInstanceOfType(value)) throw new ArgumentException($"Value of type '{value.GetType().Name}' can't be assigned to a pin of type {Type.Name}."); // Otherwise we're good and we can put a null here if it happens to be that _value = value; IsEvaluated = true; OnPropertyChanged(nameof(PinValue)); } } #endregion }