| | | 1 | | namespace pva.SuperV.Engine.Processing |
| | | 2 | | { |
| | | 3 | | /// <summary> |
| | | 4 | | /// Base class for value processing of a <see cref="Field{T}"/> trigerring field |
| | | 5 | | /// </summary> |
| | | 6 | | /// <typeparam name="T"></typeparam> |
| | | 7 | | public abstract class FieldValueProcessing<T> : IFieldValueProcessing |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gets or sets the name. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <value> |
| | | 13 | | /// The name. |
| | | 14 | | /// </value> |
| | 960 | 15 | | public string Name { get; set; } = String.Empty; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets or sets the constructor arguments. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <value> |
| | | 21 | | /// The constructor arguments. |
| | | 22 | | /// </value> |
| | 4754 | 23 | | public List<object> CtorArguments { get; set; } = []; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the field definition which triggers the processing. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <value> |
| | | 29 | | /// The trigerring field definition. |
| | | 30 | | /// </value> |
| | 470 | 31 | | public IFieldDefinition? TrigerringFieldDefinition { get; set; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="FieldValueProcessing{T}"/> class. Ctor used for deserialization |
| | | 35 | | /// </summary> |
| | 3 | 36 | | protected FieldValueProcessing() |
| | 3 | 37 | | { |
| | 3 | 38 | | } |
| | | 39 | | |
| | 447 | 40 | | protected FieldValueProcessing(string name) |
| | 447 | 41 | | { |
| | 447 | 42 | | Name = name; |
| | 447 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Builds the field value processing from the <see cref="CtorArguments" /> after deserialization. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="clazz">The clazz.</param> |
| | | 49 | | public abstract void BuildAfterDeserialization(Project project, Class clazz); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets a ctor argument. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <typeparam name="T1">The type of the argument.</typeparam> |
| | | 55 | | /// <param name="index">The index of argument.</param> |
| | | 56 | | /// <returns></returns> |
| | | 57 | | /// <exception cref="ArgumentOutOfRangeException">index</exception> |
| | | 58 | | public T1 GetCtorArgument<T1>(int index) |
| | 29 | 59 | | { |
| | 29 | 60 | | ArgumentOutOfRangeException.ThrowIfGreaterThan(index, CtorArguments.Count); |
| | 29 | 61 | | return (T1)CtorArguments[index]; |
| | 29 | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Processes the value change. |
| | | 66 | | /// </summary> |
| | | 67 | | /// <param name="instance">Instance on which the triggering field changed.</param> |
| | | 68 | | /// <param name="changedField">The <see cref="Field{T}"/> which changed.</param> |
| | | 69 | | /// <param name="valueChanged">If <c>true</c>, indicates that the trigerring field value changed.</param> |
| | | 70 | | /// <param name="previousValue">The previous value of field.</param> |
| | | 71 | | /// <param name="currentValue">The current value of field.</param> |
| | | 72 | | public abstract void ProcessValue(IInstance instance, Field<T> changedField, bool valueChanged, T previousValue, |
| | | 73 | | |
| | | 74 | | public abstract bool IsFieldUsed(string fieldName); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets a field definition from a class. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="clazz">The class from which to get field definition.</param> |
| | | 80 | | /// <param name="fieldName">Name of the field.</param> |
| | | 81 | | /// <returns><see cref="IFieldDefinition"/></returns> |
| | | 82 | | protected static IFieldDefinition? GetFieldDefinition(Class clazz, string? fieldName) |
| | 1908 | 83 | | { |
| | 1908 | 84 | | return string.IsNullOrEmpty(fieldName) |
| | 1908 | 85 | | ? null |
| | 1908 | 86 | | : clazz.GetField(fieldName); |
| | 1908 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets a field definition from a class. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <typeparam name="T1">The type of the field definition.</typeparam> |
| | | 93 | | /// <param name="clazz">The class from which to get field definition.</param> |
| | | 94 | | /// <param name="fieldName">Name of the field.</param> |
| | | 95 | | /// <returns><see cref="FieldDefinition{T}"/></returns> |
| | | 96 | | protected static FieldDefinition<T1>? GetFieldDefinition<T1>(Class clazz, string? fieldName) |
| | 1514 | 97 | | { |
| | 1514 | 98 | | return string.IsNullOrEmpty(fieldName) |
| | 1514 | 99 | | ? null |
| | 1514 | 100 | | : clazz.GetField<T1>(fieldName); |
| | 1514 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Converts a field definition to a specific type. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <typeparam name="T1">The type of the field definition.</typeparam> |
| | | 107 | | /// <param name="fieldDefinition">The field definition to convert.</param> |
| | | 108 | | /// <param name="fieldName">Name of the field.</param> |
| | | 109 | | /// <returns><see cref="FieldDefinition{T}"/></returns> |
| | | 110 | | protected static FieldDefinition<T1>? ConvertFieldDefinition<T1>(IFieldDefinition fieldDefinition) |
| | 0 | 111 | | { |
| | 0 | 112 | | return fieldDefinition as FieldDefinition<T1>; |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Gets a field from an instance. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="instance">The instance from which to get the field.</param> |
| | | 119 | | /// <param name="fieldName">Name of the field.</param> |
| | | 120 | | /// <returns><see cref="IField"/></returns> |
| | | 121 | | protected static IField? GetInstanceField(IInstance instance, string? fieldName) |
| | 118 | 122 | | { |
| | 118 | 123 | | return string.IsNullOrEmpty(fieldName) |
| | 118 | 124 | | ? null |
| | 118 | 125 | | : instance.GetField(fieldName); |
| | 118 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Gets a field from an instance. |
| | | 130 | | /// </summary> |
| | | 131 | | /// <typeparam name="T1">The type of the field.</typeparam> |
| | | 132 | | /// <param name="instance">The instance from which to get the field.</param> |
| | | 133 | | /// <param name="fieldName">Name of the field.</param> |
| | | 134 | | /// <returns><see cref="Field{T}"/></returns> |
| | | 135 | | protected static Field<T1>? GetInstanceField<T1>(IInstance instance, string? fieldName) |
| | 120 | 136 | | { |
| | 120 | 137 | | return string.IsNullOrEmpty(fieldName) |
| | 120 | 138 | | ? null |
| | 120 | 139 | | : instance.GetField<T1>(fieldName); |
| | 120 | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |