-
Notifications
You must be signed in to change notification settings - Fork 22
/
RealValueHolder.cs
47 lines (40 loc) · 1.68 KB
/
RealValueHolder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace Binject {
/// <summary>
/// Types implementing this will be responsible for saving a <see cref="ValueType"/> data inside and provide
/// external access to it. This interface makes sure a <see cref="System.Collections.Generic.List{T}"/> can use any
/// structs.
/// </summary>
internal interface IValueHolder {
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Type GetValueType();
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public object BoxAndGetValue();
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public void BoxAndSetValue(object value);
}
/// <summary>
/// Any data stored in this will be boxed, but it's useful for showing the data inside in Unity Editor.
/// </summary>
[Serializable]
internal class BoxedValueHolder : IValueHolder {
[SerializeReference] public object Value;
public BoxedValueHolder(object value) => Value = value;
public Type GetValueType() => Value.GetType();
public object BoxAndGetValue() => Value;
public void BoxAndSetValue(object value) => Value = value;
}
/// <summary>
/// This will store real data and provides direct access to it without boxing.
/// </summary>
[Serializable]
internal class RealValueHolder<T> : IValueHolder {
public T Value;
public RealValueHolder(T value) => Value = value;
public Type GetValueType() => typeof(T);
public object BoxAndGetValue() => Value;
public void BoxAndSetValue(object value) => Value = (T)value;
}
}