Skip to content

Commit bcfcd9d

Browse files
committed
Add base property wrappers.
1 parent 72d43ee commit bcfcd9d

19 files changed

+177
-97
lines changed

src/UnityMvvmToolkit.Core/Internal/ObjectHandlers/ObjectWrapperHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
139139
var args = new object[] { valueConverter };
140140

141141
var wrapperType = property is IProperty
142-
? typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType)
143-
: typeof(ReadOnlyPropertyWrapper<,>).MakeGenericType(sourceType, targetType);
142+
? typeof(PropertyConvertWrapper<,>).MakeGenericType(sourceType, targetType)
143+
: typeof(ReadOnlyPropertyConvertWrapper<,>).MakeGenericType(sourceType, targetType);
144144

145145
return (TProperty) ObjectWrapperHelper.CreatePropertyWrapper(wrapperType, args, converterId, property);
146146
}
@@ -281,7 +281,7 @@ private void CreatePropertyValueConverterInstances(int converterId, IPropertyVal
281281
var itemsQueue = new Queue<IObjectWrapper>();
282282

283283
var args = new object[] { converter };
284-
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(converter.SourceType, converter.TargetType);
284+
var wrapperType = typeof(PropertyConvertWrapper<,>).MakeGenericType(converter.SourceType, converter.TargetType);
285285

286286
for (var i = 0; i < capacity; i++)
287287
{

src/UnityMvvmToolkit.Core/Internal/ObjectWrappers/PropertyCastWrapper.TSource.TValue.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
1+
using System.Runtime.CompilerServices;
2+
3+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
24
{
35
internal sealed class PropertyCastWrapper<TSource, TValue> : PropertyWrapper<TSource, TValue>
46
{
5-
public PropertyCastWrapper() : base(default)
6-
{
7-
}
8-
7+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98
protected override TValue Convert(TSource value)
109
{
1110
return (TValue) (object) value;
1211
}
1312

13+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1414
protected override TSource ConvertBack(TValue value)
1515
{
1616
return (TSource) (object) value;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityMvvmToolkit.Core.Attributes;
3+
using UnityMvvmToolkit.Core.Interfaces;
4+
5+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
6+
{
7+
internal sealed class PropertyConvertWrapper<TSource, TValue> : PropertyWrapper<TSource, TValue>
8+
{
9+
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
10+
11+
[Preserve]
12+
public PropertyConvertWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
13+
{
14+
_valueConverter = valueConverter;
15+
}
16+
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
protected override TValue Convert(TSource value)
19+
{
20+
return _valueConverter.Convert(value);
21+
}
22+
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
protected override TSource ConvertBack(TValue value)
25+
{
26+
return _valueConverter.ConvertBack(value);
27+
}
28+
}
29+
}

src/UnityMvvmToolkit.Core/Internal/ObjectWrappers/PropertyWrapper.TSource.TValue.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77

88
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
99
{
10-
internal class PropertyWrapper<TSource, TValue> : IProperty<TValue>, IPropertyWrapper
10+
internal abstract class PropertyWrapper<TSource, TValue> : IProperty<TValue>, IPropertyWrapper
1111
{
12-
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
13-
1412
private int _converterId;
1513

1614
private TValue _value;
1715
private TSource _sourceValue;
1816
private IProperty<TSource> _property;
1917

2018
[Preserve]
21-
public PropertyWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
19+
protected PropertyWrapper()
2220
{
2321
_converterId = -1;
24-
_valueConverter = valueConverter;
2522
}
2623

2724
public int ConverterId => _converterId;
@@ -101,16 +98,10 @@ private void OnPropertyValueChanged(object sender, TSource sourceValue)
10198
}
10299

103100
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104-
protected virtual TValue Convert(TSource value)
105-
{
106-
return _valueConverter.Convert(value);
107-
}
101+
protected abstract TValue Convert(TSource value);
108102

109103
[MethodImpl(MethodImplOptions.AggressiveInlining)]
110-
protected virtual TSource ConvertBack(TValue value)
111-
{
112-
return _valueConverter.ConvertBack(value);
113-
}
104+
protected abstract TSource ConvertBack(TValue value);
114105

115106
void IProperty<TValue>.ForceSetValue(TValue value) => throw new NotImplementedException();
116107
}

src/UnityMvvmToolkit.Core/Internal/ObjectWrappers/ReadOnlyPropertyCastWrapper.TSource.TValue.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
1+
using System.Runtime.CompilerServices;
2+
3+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
24
{
35
internal sealed class ReadOnlyPropertyCastWrapper<TSource, TValue> : ReadOnlyPropertyWrapper<TSource, TValue>
46
{
5-
public ReadOnlyPropertyCastWrapper() : base(default)
6-
{
7-
}
8-
7+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98
protected override TValue Convert(TSource value)
109
{
1110
return (TValue) (object) value;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityMvvmToolkit.Core.Attributes;
3+
using UnityMvvmToolkit.Core.Interfaces;
4+
5+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
6+
{
7+
internal sealed class ReadOnlyPropertyConvertWrapper<TSource, TValue> : ReadOnlyPropertyWrapper<TSource, TValue>
8+
{
9+
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
10+
11+
[Preserve]
12+
public ReadOnlyPropertyConvertWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
13+
{
14+
_valueConverter = valueConverter;
15+
}
16+
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
protected override TValue Convert(TSource value)
19+
{
20+
return _valueConverter.Convert(value);
21+
}
22+
}
23+
}

src/UnityMvvmToolkit.Core/Internal/ObjectWrappers/ReadOnlyPropertyWrapper.TSource.TValue.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@
66

77
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
88
{
9-
internal class ReadOnlyPropertyWrapper<TSource, TValue> : IReadOnlyProperty<TValue>, IPropertyWrapper
9+
internal abstract class ReadOnlyPropertyWrapper<TSource, TValue> : IReadOnlyProperty<TValue>, IPropertyWrapper
1010
{
11-
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
12-
1311
private int _converterId;
1412
private bool _isInitialized;
1513

1614
private TValue _value;
1715

1816
[Preserve]
19-
public ReadOnlyPropertyWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
17+
protected ReadOnlyPropertyWrapper()
2018
{
2119
_converterId = -1;
22-
_valueConverter = valueConverter;
2320
}
2421

2522
public int ConverterId => _converterId;
@@ -67,9 +64,6 @@ public void Reset()
6764
}
6865

6966
[MethodImpl(MethodImplOptions.AggressiveInlining)]
70-
protected virtual TValue Convert(TSource value)
71-
{
72-
return _valueConverter.Convert(value);
73-
}
67+
protected abstract TValue Convert(TSource value);
7468
}
7569
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectHandlers/ObjectWrapperHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
139139
var args = new object[] { valueConverter };
140140

141141
var wrapperType = property is IProperty
142-
? typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType)
143-
: typeof(ReadOnlyPropertyWrapper<,>).MakeGenericType(sourceType, targetType);
142+
? typeof(PropertyConvertWrapper<,>).MakeGenericType(sourceType, targetType)
143+
: typeof(ReadOnlyPropertyConvertWrapper<,>).MakeGenericType(sourceType, targetType);
144144

145145
return (TProperty) ObjectWrapperHelper.CreatePropertyWrapper(wrapperType, args, converterId, property);
146146
}
@@ -281,7 +281,7 @@ private void CreatePropertyValueConverterInstances(int converterId, IPropertyVal
281281
var itemsQueue = new Queue<IObjectWrapper>();
282282

283283
var args = new object[] { converter };
284-
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(converter.SourceType, converter.TargetType);
284+
var wrapperType = typeof(PropertyConvertWrapper<,>).MakeGenericType(converter.SourceType, converter.TargetType);
285285

286286
for (var i = 0; i < capacity; i++)
287287
{

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/Core/Internal/ObjectWrappers/PropertyCastWrapper.TSource.TValue.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
1+
using System.Runtime.CompilerServices;
2+
3+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
24
{
35
internal sealed class PropertyCastWrapper<TSource, TValue> : PropertyWrapper<TSource, TValue>
46
{
5-
public PropertyCastWrapper() : base(default)
6-
{
7-
}
8-
7+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98
protected override TValue Convert(TSource value)
109
{
1110
return (TValue) (object) value;
1211
}
1312

13+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1414
protected override TSource ConvertBack(TValue value)
1515
{
1616
return (TSource) (object) value;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Runtime.CompilerServices;
2+
using UnityMvvmToolkit.Core.Attributes;
3+
using UnityMvvmToolkit.Core.Interfaces;
4+
5+
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
6+
{
7+
internal sealed class PropertyConvertWrapper<TSource, TValue> : PropertyWrapper<TSource, TValue>
8+
{
9+
private readonly IPropertyValueConverter<TSource, TValue> _valueConverter;
10+
11+
[Preserve]
12+
public PropertyConvertWrapper(IPropertyValueConverter<TSource, TValue> valueConverter)
13+
{
14+
_valueConverter = valueConverter;
15+
}
16+
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
protected override TValue Convert(TSource value)
19+
{
20+
return _valueConverter.Convert(value);
21+
}
22+
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
protected override TSource ConvertBack(TValue value)
25+
{
26+
return _valueConverter.ConvertBack(value);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)