-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtinyvariant.pas
208 lines (178 loc) · 3.53 KB
/
dtinyvariant.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
204
205
206
207
208
unit dtinyvariant;
// Tiny variant class. rlyeh, public domain | wtrmrkrlyeh
{$MODE OBJFPC}
{$MODESWITCH ADVANCEDRECORDS}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
type
Tvar = record
public type
TCallback = procedure;
procedure Init; overload;
procedure InitInt(value: PtrInt); overload;
procedure InitDouble(value: Double); overload;
procedure InitString(const value: AnsiString); overload;
procedure InitCallback(value: TCallback); overload;
procedure Init(const other: Tvar); overload;
procedure Done;
procedure cleanup;
procedure Assign(const other: Tvar);
procedure Call;
function ToString: AnsiString;
function IsInt: Boolean; inline;
function IsDouble: Boolean; inline;
function IsString: Boolean; inline;
function IsCallback: Boolean; inline;
{$IF FPC_FULLVERSION > 30101}
class operator Initialize(var v: Tvar);
class operator Finalize(var v: Tvar);
class operator Copy(constref Src: Tvar; var Dst: Tvar);
{$ENDIF}
public case _type: PtrInt of
0: (i: PtrInt);
1: (d: {$IF Defined(CPU32)}Single{$ELSE}Double{$ENDIF});
2: (s: PAnsiString);
3: (c: TCallback);
end;
operator = (const A, B: Tvar): Boolean;
operator := (const v: Tvar): PtrInt;
operator := (const v: Tvar): Double;
operator := (const v: Tvar): AnsiString;
operator := (const v: Tvar): Tvar.TCallback;
implementation
procedure Tvar.Init;
begin
_type := 0;
i := 0;
end;
procedure Tvar.InitInt(value: PtrInt);
begin
_type := 0;
i := value;
end;
procedure Tvar.InitDouble(value: Double);
begin
_type := 1;
d := value;
end;
procedure Tvar.InitString(const value: AnsiString);
begin
_type := 2;
New(s);
s^ := value;
end;
procedure Tvar.InitCallback(value: TCallback);
begin
_type := 3;
c := value;
end;
procedure Tvar.Init(const other: Tvar);
begin
Init;
Assign(other);
end;
procedure Tvar.Done;
begin
cleanup;
end;
procedure Tvar.cleanup;
begin
if _type = 2 then
Dispose(s);
_type := 0;
end;
procedure Tvar.Assign(const other: Tvar);
begin
if other <> Self then begin
cleanup();
_type := other._type;
case _type of
0: i := other.i;
1: d := other.d;
2: begin
New(s);
s^ := other.s^;
end;
3: c := other.c
end;
end;
end;
procedure Tvar.Call;
begin
if _type = 3 then
c();
end;
function Tvar.ToString: AnsiString;
begin
case _type of
0: str(i, Result);
1: str(d, Result);
2: Exit(s^);
3: Exit('$' + HexStr(PtrUInt(c), 2 * SizeOf(PtrUInt)));
else
Exit('undefined');
end;
end;
function Tvar.IsInt: Boolean;
begin
Exit(_type = 0);
end;
function Tvar.IsDouble: Boolean;
begin
Exit(_Type = 1);
end;
function Tvar.IsString: Boolean;
begin
Exit(_type = 2)
end;
function Tvar.IsCallback: Boolean;
begin
Exit(_type = 3)
end;
operator = (const A, B: Tvar): Boolean;
begin
if A._type = B._type then begin
case A._type of
0: Exit(A.i = B.i);
1: Exit(A.d = B.d);
2: Exit(A.s^ = B.s^);
3: Exit(A.c = B.c);
else
Exit(False);
end;
end else
Exit(False);
end;
operator := (const v: Tvar): PtrInt;
begin
Exit(v.i);
end;
operator := (const v: Tvar): Double;
begin
Exit(v.d);
end;
operator := (const v: Tvar): AnsiString;
begin
Exit(v.s^);
end;
operator := (const v: Tvar): Tvar.TCallback;
begin
Exit(v.c);
end;
{$IF FPC_FULLVERSION > 30101}
class operator Tvar.Initialize(var v: Tvar);
begin
v.Init;
end;
class operator Tvar.Finalize(var v: Tvar);
begin
v.Done;
end;
class operator Tvar.Copy(constref Src: Tvar; var Dst: Tvar);
begin
Dst.Assign(Src);
end;
{$ENDIF}
end.