using System; using System.Linq.Expressions; namespace MonoGame.Extended.Tweening; /// /// Provides compiled arithmetic delegates for a value type , enabling /// type-agnostic linear interpolation over any struct that supports addition, subtraction, and /// scalar multiplication operators. The delegates are compiled once via expression trees and /// cached as static members. /// /// /// The value type to compile operations for. Must define +, -, and * /// operators, with * accepting a scalar as the right-hand operand. /// public class LinearOperations { static LinearOperations() { var a = Expression.Parameter(typeof(T)); var b = Expression.Parameter(typeof(T)); var c = Expression.Parameter(typeof(float)); Add = Expression.Lambda>(Expression.Add(a, b), a, b).Compile(); Subtract = Expression.Lambda>(Expression.Subtract(a, b), a, b).Compile(); Multiply = Expression.Lambda>(Expression.Multiply(a, c), a, c).Compile(); } /// /// Gets a compiled delegate that adds two values of type . /// public static Func Add { get; } /// /// Gets a compiled delegate that subtracts the second value from the first for type . /// public static Func Subtract { get; } /// /// Gets a compiled delegate that multiplies a value of type by a scalar. /// public static Func Multiply { get; } }