-
Notifications
You must be signed in to change notification settings - Fork 2
/
TransmitRecords.pas
142 lines (105 loc) · 3.04 KB
/
TransmitRecords.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
unit TransmitRecords;
interface
type
{$Z4} // intger enum
TxRectTypeEnum = (TxRectType_Point, TxRectType_Line, TxRectType_Arc, TxRectType_Polyline, TxRectType_GeometryList);
{$Z1}
const
TxRectType_Undefined = TxRectTypeEnum(-1);
Type
// The base memory block of all parameter recs, also serves as a signal parameter (no data transmitted)
PTxRec = ^TxRec;
TxRec = Record
Size: Cardinal; // UInt32
RecType: TxRectTypeEnum; // Integer
class function Default<T>: T; static;
End;
PointRec = Record
X, Y: double;
End;
PTxPointRec = ^TxPointRec;
TxPointRec = Record
// Common header
Size: Cardinal; // UInt32
RecType: TxRectTypeEnum;
// Point Specific
p: PointRec;
End;
PTxLineRec = ^TxLineRec;
TxLineRec = Record
// Common header
Size: Cardinal; // UInt32
RecType: TxRectTypeEnum;
// Line Specific
p1, p2: PointRec;
End;
PTxArcRec = ^TxArcRec;
TxArcRec = Record
// Common header
Size: Cardinal; // UInt32
RecType: TxRectTypeEnum;
// Arc Specific
p: PointRec;
CCW: Boolean; // in Delphi a Boolean has size of Byte
StartAngle, EndAngle: double;
End;
PTxPolyLineRec = ^TxPolyLineRec;
TxPolyLineRec = Record
// Common header
Size: Cardinal; // UInt32
RecType: TxRectTypeEnum;
// Polyline Specific
VertexCount: Uint32;
Vertices: TArray<PointRec>;
End;
PTxGeometryListRec = ^TxGeometryListRec;
TxGeometryListRec = Record
// Common header
Size: Cardinal; // UInt32
RecType: TxRectTypeEnum;
// PolyLineArcRec Specific
Geometry: TArray<PTxRec>; // since we have an array of pointers,
// this array can be null terminated.
End;
const
DefaultPointRec: TxPointRec = (
Size: SizeOf(TxPointRec);
RecType: TxRectType_Point
);
DefaultLineRec: TxLineRec = (
Size: SizeOf(TxPointRec);
RecType: TxRectType_Line
);
DefaultArcRec: TxArcRec = (
Size: SizeOf(TxArcRec);
RecType: TxRectType_Arc
);
DefaultPolyLineRec: TxPolyLineRec = (
Size: SizeOf(TxPolyLineRec);
RecType: TxRectType_Polyline
);
DefaultGeometryListRec: TxGeometryListRec = (
Size: SizeOf(TxGeometryListRec);
RecType: TxRectType_GeometryList
);
implementation
{ TxPointRec }
class function TxRec.Default<T>: T;
var
PT: ^T; // this will be a pointer to a const, do not modify values via this pointer
begin
if TypeInfo(T) = TypeInfo(TxPointRec) then
PT := @DefaultPointRec
else if TypeInfo(T) = TypeInfo(TxLineRec) then
PT := @DefaultLineRec
else if TypeInfo(T) = TypeInfo(TxArcRec) then
PT := @DefaultArcRec
else if TypeInfo(T) = TypeInfo(TxPolyLineRec) then
PT := @DefaultPolyLineRec
else if TypeInfo(T) = TypeInfo(TxGeometryListRec) then
PT := @DefaultGeometryListRec
else
PT := nil; // raise exception
result := PT^; // We Copy value, so the constant is not inadvertently modified
end;
end.