-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVarPtr.pas
203 lines (176 loc) · 5.44 KB
/
VarPtr.pas
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
unit VarPtr;
////////////////////////////////////////////////////////////////////////////////
//
// The VarPointer-type is primarily intended to facilitate the AssignToVar-methods
// in ArrayHlp.pas and Parse.pas
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils;
Type
TVarType = (vtUnTyped,vtByte,vtInt16,vtInt32,vtInt64,vtFloat32,vtFloat64);
TVarPointer = record
private
Var
FVarType: TVarType;
VarPointer: Pointer;
Function GetAsByte: Byte;
Function GetAsInt16: Int16;
Function GetAsInt32: Int32;
Function GetAsInt64: Int64;
Function GetAsFloat32: Float32;
Function GetAsFloat64: Float64;
Procedure SetAsByte(AsByte: Byte);
Procedure SetAsInt16(AsInt: Int16);
Procedure SetAsInt32(AsInt: Int32);
Procedure SetAsInt64(AsInt: Int64);
Procedure SetAsFloat32(AsFloat: Float32);
Procedure SetAsFloat64(AsFloat: Float64);
public
Class Operator Initialize(out VarRef: TVarPointer);
Class Operator Implicit(const [ref] IntVar: Byte): TVarPointer;
Class Operator Implicit(const [ref] IntVar: Int16): TVarPointer;
Class Operator Implicit(const [ref] IntVar: Int32): TVarPointer;
Class Operator Implicit(const [ref] IntVar: Int64): TVarPointer;
Class Operator Implicit(const [ref] FloatVar: Float32): TVarPointer;
Class Operator Implicit(const [ref] FloatVar: Float64): TVarPointer;
public
Property VarType: TVarType read FVarType;
Property AsByte: Byte read GetAsByte write SetAsByte;
Property AsInt16: Int16 read GetAsInt16 write SetAsInt16;
Property AsInt32: Int32 read GetAsInt32 write SetAsInt32;
Property AsInt64: Int64 read GetAsInt64 write SetAsInt64;
Property AsFloat32: Float32 read GetAsFloat32 write SetAsFloat32;
Property AsFloat64: Float64 read GetAsFloat64 write SetAsFloat64;
end;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Class Operator TVarPointer.Initialize(out varRef: TVarPointer);
begin
VarRef.FVarType := vtUnTyped;
end;
Class Operator TVarPointer.Implicit(const [ref] IntVar: Byte): TVarPointer;
begin
Result.FVarType := vtByte;
Result.VarPointer := @IntVar;
end;
Class Operator TVarPointer.Implicit(const [ref] IntVar: Int16): TVarPointer;
begin
Result.FVarType := vtInt16;
Result.VarPointer := @IntVar;
end;
Class Operator TVarPointer.Implicit(const [ref] IntVar: Int32): TVarPointer;
begin
Result.FVarType := vtInt32;
Result.VarPointer := @IntVar;
end;
Class Operator TVarPointer.Implicit(const [ref] IntVar: Int64): TVarPointer;
begin
Result.FVarType := vtInt64;
Result.VarPointer := @IntVar;
end;
Class Operator TVarPointer.Implicit(const [ref] FloatVar: Float32): TVarPointer;
begin
Result.FVarType := vtFloat32;
Result.VarPointer := @FloatVar;
end;
Class Operator TVarPointer.Implicit(const [ref] FloatVar: Float64): TVarPointer;
begin
Result.FVarType := vtFloat64;
Result.VarPointer := @FloatVar;
end;
Function TVarPointer.GetAsByte: Byte;
begin
if FVarType = vtByte then
Result := PByte(VarPointer)^
else
raise Exception.Create('Invalid type');
end;
Function TVarPointer.GetAsInt16: Int16;
begin
if FVarType = vtInt16 then
Result := PSmallInt(VarPointer)^
else
raise Exception.Create('Invalid type');
end;
Function TVarPointer.GetAsInt32: Int32;
begin
if FVarType = vtInt32 then
Result := PInteger(VarPointer)^
else
raise Exception.Create('Invalid type');
end;
Function TVarPointer.GetAsInt64: Int64;
begin
if FVarType = vtInt64 then
Result := PInt64(VarPointer)^
else
raise Exception.Create('Invalid type');
end;
Function TVarPointer.GetAsFloat32: Float32;
begin
if FVarType = vtFloat32 then
Result := PSingle(VarPointer)^
else
raise Exception.Create('Invalid type');
end;
Function TVarPointer.GetAsFloat64: Float64;
begin
if FVarType = vtFloat64 then
Result := PDouble(VarPointer)^
else
raise Exception.Create('Invalid type');
end;
Procedure TVarPointer.SetAsByte(AsByte: Byte);
begin
if FVarType = vtByte then
PByte(VarPointer)^ := AsByte
else
raise Exception.Create('Invalid type');
end;
Procedure TVarPointer.SetAsInt16(AsInt: Int16);
begin
if FVarType = vtInt16 then
PSmallInt(VarPointer)^ := AsInt
else
raise Exception.Create('Invalid type');
end;
Procedure TVarPointer.SetAsInt32(AsInt: Int32);
begin
if FVarType = vtInt32 then
PInteger(VarPointer)^ := AsInt
else
raise Exception.Create('Invalid type');
end;
Procedure TVarPointer.SetAsInt64(AsInt: Int64);
begin
if FVarType = vtInt64 then
PInt64(VarPointer)^ := AsInt
else
raise Exception.Create('Invalid type');
end;
Procedure TVarPointer.SetAsFloat32(AsFloat: Float32);
begin
if FVarType = vtFloat32 then
PSingle(VarPointer)^ := AsFloat
else
raise Exception.Create('Invalid type');
end;
Procedure TVarPointer.SetAsFloat64(AsFloat: Float64);
begin
if FVarType = vtFloat64 then
PDouble(VarPointer)^ := AsFloat
else
raise Exception.Create('Invalid type');
end;
end.