forked from izrik/FbxSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFbxPropertyT.cs
85 lines (71 loc) · 2.1 KB
/
FbxPropertyT.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
namespace FbxSharp
{
public class FbxPropertyT<T> : FbxProperty
{
public FbxPropertyT(string name="")
: base(name)
{
}
public FbxPropertyT(string name, T initialValue)
: base(name)
{
Value = initialValue;
}
public override Type PropertyDataType { get { return typeof(T); } }
public T Value { get; set; }
//FbxPropertyT & Set (const T &pValue)
public void Set(T value)
{
Value = value;
}
public T Get()
{
return Value;
}
public override U Get<U>()
{
if (!(Value is U))
{
var tuple = new Tuple<Type, Type>(typeof(T), typeof(U));
if (Converters.ContainsKey(tuple))
{
var converter = Converters[tuple];
return (U)converter(Value);
}
throw new InvalidCastException(); // maybe find a better exception to throw
}
return (U)(object)Value;
}
public override object GetValue()
{
return Value;
}
public override bool Set<U>(U value)
{
// if U can be assigned to a prop/field of type T,
// then do so
// else if there is a converter available
// then use that
// else
// throw
if ((typeof(U).IsAssignableFrom(typeof(T))))
{
Value = (T)(object)value;
return true;
}
var tuple = new Tuple<Type, Type>(typeof(U), typeof(T));
if (Converters.ContainsKey(tuple))
{
var converter = Converters[tuple];
Value = (T)converter(value);
return true;
}
throw new InvalidCastException(); // maybe find a better exception to throw
}
public override bool Set(object value)
{
return Set<object>(value);
}
}
}