Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
CastInstruction.cs
Go to the documentation of this file.
2
4
5internal abstract class CastInstruction : Instruction
6{
7 private sealed class CastInstructionT<T> : CastInstruction
8 {
9 public override int Run(InterpretedFrame frame)
10 {
11 object obj = frame.Pop();
12 frame.Push((T)obj);
13 return 1;
14 }
15 }
16
17 private abstract class CastInstructionNoT : CastInstruction
18 {
19 private sealed class Ref : CastInstructionNoT
20 {
21 public Ref(Type t)
22 : base(t)
23 {
24 }
25
26 protected override void ConvertNull(InterpretedFrame frame)
27 {
28 frame.Push(null);
29 }
30 }
31
32 private sealed class Value : CastInstructionNoT
33 {
34 public Value(Type t)
35 : base(t)
36 {
37 }
38
39 protected override void ConvertNull(InterpretedFrame frame)
40 {
41 throw new NullReferenceException();
42 }
43 }
44
45 private readonly Type _t;
46
48 {
49 _t = t;
50 }
51
52 public new static CastInstruction Create(Type t)
53 {
54 if (t.IsValueType && !t.IsNullableType())
55 {
56 return new Value(t);
57 }
58 return new Ref(t);
59 }
60
61 public override int Run(InterpretedFrame frame)
62 {
63 object obj = frame.Pop();
64 if (obj != null)
65 {
66 Type type = obj.GetType();
67 if (!type.HasReferenceConversionTo(_t) && !type.HasIdentityPrimitiveOrNullableConversionTo(_t))
68 {
69 throw new InvalidCastException();
70 }
72 {
73 throw new InvalidCastException();
74 }
75 frame.Push(obj);
76 }
77 else
78 {
79 ConvertNull(frame);
80 }
81 return 1;
82 }
83
84 protected abstract void ConvertNull(InterpretedFrame frame);
85 }
86
87 private static CastInstruction s_Boolean;
88
89 private static CastInstruction s_Byte;
90
91 private static CastInstruction s_Char;
92
94
95 private static CastInstruction s_Decimal;
96
97 private static CastInstruction s_Double;
98
99 private static CastInstruction s_Int16;
100
101 private static CastInstruction s_Int32;
102
103 private static CastInstruction s_Int64;
104
105 private static CastInstruction s_SByte;
106
107 private static CastInstruction s_Single;
108
109 private static CastInstruction s_String;
110
111 private static CastInstruction s_UInt16;
112
113 private static CastInstruction s_UInt32;
114
115 private static CastInstruction s_UInt64;
116
117 public override int ConsumedStack => 1;
118
119 public override int ProducedStack => 1;
120
121 public override string InstructionName => "Cast";
122
123 public static Instruction Create(Type t)
124 {
125 return t.GetTypeCode() switch
126 {
127 TypeCode.Boolean => s_Boolean ?? (s_Boolean = new CastInstructionT<bool>()),
128 TypeCode.Byte => s_Byte ?? (s_Byte = new CastInstructionT<byte>()),
129 TypeCode.Char => s_Char ?? (s_Char = new CastInstructionT<char>()),
131 TypeCode.Decimal => s_Decimal ?? (s_Decimal = new CastInstructionT<decimal>()),
132 TypeCode.Double => s_Double ?? (s_Double = new CastInstructionT<double>()),
133 TypeCode.Int16 => s_Int16 ?? (s_Int16 = new CastInstructionT<short>()),
134 TypeCode.Int32 => s_Int32 ?? (s_Int32 = new CastInstructionT<int>()),
135 TypeCode.Int64 => s_Int64 ?? (s_Int64 = new CastInstructionT<long>()),
136 TypeCode.SByte => s_SByte ?? (s_SByte = new CastInstructionT<sbyte>()),
137 TypeCode.Single => s_Single ?? (s_Single = new CastInstructionT<float>()),
138 TypeCode.String => s_String ?? (s_String = new CastInstructionT<string>()),
139 TypeCode.UInt16 => s_UInt16 ?? (s_UInt16 = new CastInstructionT<ushort>()),
140 TypeCode.UInt32 => s_UInt32 ?? (s_UInt32 = new CastInstructionT<uint>()),
141 TypeCode.UInt64 => s_UInt64 ?? (s_UInt64 = new CastInstructionT<ulong>()),
142 _ => CastInstructionNoT.Create(t),
143 };
144 }
145}
virtual bool IsAssignableFrom([NotNullWhen(true)] Type? c)
Definition Type.cs:1561
bool IsValueType
Definition Type.cs:234
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static TypeCode GetTypeCode(Type? type)
Definition Type.cs:919
TypeCode
Definition TypeCode.cs:4