-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.TriBool.pas
More file actions
96 lines (76 loc) · 1.81 KB
/
Copy pathMaxLogic.TriBool.pas
File metadata and controls
96 lines (76 loc) · 1.81 KB
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
unit MaxLogic.TriBool;
{
inspired by:
https://stackoverflow.com/questions/19536331/three-valued-logic-in-delphi
Some sample usage:
var
x: Double;
tb1, tb2: TTriBool;
tb1 := True;
tb2 := x>3.0;
Writeln((tb1 or tb2).ToString);
tb1 := False;
tb2.Value := tbUnknown;
Writeln((tb1 or tb2).ToString);
which outputs:
True
Unknown
}
interface
type
TTriBool = record
public
type
TTriBoolEnum = (tbUnknown, tbFalse, tbTrue);
public
Value: TTriBoolEnum;
public
class operator Implicit(const Value: Boolean): TTriBool;
class operator Implicit(const Value: TTriBoolEnum): TTriBool;
class operator Implicit(const Value: TTriBool): TTriBoolEnum;
class operator Equal(const lhs, rhs: TTriBool): Boolean;
class operator LogicalOr(const lhs, rhs: TTriBool): TTriBool;
function ToString: string;
end;
implementation
class operator TTriBool.Implicit(const Value: Boolean): TTriBool;
begin
if Value then
Result.Value := tbTrue
else
Result.Value := tbFalse;
end;
class operator TTriBool.Implicit(const Value: TTriBoolEnum): TTriBool;
begin
Result.Value := Value;
end;
class operator TTriBool.Implicit(const Value: TTriBool): TTriBoolEnum;
begin
Result := Value.Value;
end;
class operator TTriBool.Equal(const lhs, rhs: TTriBool): Boolean;
begin
Result := lhs.Value = rhs.Value;
end;
class operator TTriBool.LogicalOr(const lhs, rhs: TTriBool): TTriBool;
begin
if (lhs.Value = tbTrue) or (rhs.Value = tbTrue) then
Result := tbTrue
else if (lhs.Value = tbFalse) and (rhs.Value = tbFalse) then
Result := tbFalse
else
Result := tbUnknown;
end;
function TTriBool.ToString: string;
begin
case Value of
tbFalse:
Result := 'False';
tbTrue:
Result := 'True';
// tbUnknown:
else
Result := 'Unknown';
end;
end;
end.