-
Notifications
You must be signed in to change notification settings - Fork 1
/
genericutils.pas
90 lines (73 loc) · 2.5 KB
/
genericutils.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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit genericutils;
interface
type
generic DefaultUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
end;
generic DefaultUnorderedUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
end;
generic IncomparableUtils <T> = record
class function Equals(const A, B: T): Boolean; static; inline;
class function LessThan(const A, B: T): Boolean; static; inline;
class function GreaterThan(const A, B: T): Boolean; static; inline;
end;
TObjectUtils = specialize DefaultUnorderedUtils <TObject>;
CardinalUtils = specialize DefaultUtils <Cardinal>;
LongIntUtils = specialize DefaultUtils <LongInt>;
IntegerUtils = specialize DefaultUtils <Integer>;
PointerUtils = specialize DefaultUtils <Pointer>;
PtrUIntUtils = specialize DefaultUtils <PtrUInt>;
DoubleUtils = specialize DefaultUtils <Double>;
RawByteStringUtils = specialize DefaultUtils <RawByteString>;
// for UTF8StringUtils, see stringutils.pas
implementation
uses
sysutils;
class function DefaultUtils.Equals(const A, B: T): Boolean;
begin
Result := A = B;
end;
class function DefaultUtils.LessThan(const A, B: T): Boolean;
begin
Result := A < B;
end;
class function DefaultUtils.GreaterThan(const A, B: T): Boolean;
begin
Result := A > B;
end;
class function DefaultUnorderedUtils.Equals(const A, B: T): Boolean;
begin
Result := A = B;
end;
class function DefaultUnorderedUtils.LessThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function DefaultUnorderedUtils.GreaterThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function IncomparableUtils.Equals(const A, B: T): Boolean;
begin
Result := False;
end;
class function IncomparableUtils.LessThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
class function IncomparableUtils.GreaterThan(const A, B: T): Boolean;
begin
raise Exception.Create('tried to compare unordered data');
Result := False;
end;
end.