-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathg3property.pas
802 lines (697 loc) · 26.7 KB
/
g3property.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
{
@abstract(Properties store class)
Entity properties, collection of properties
The contents of this file are subject to the license defined in the file licence.txt.
}
unit g3property;
{$I g3config.inc}
interface
uses
g3types, {!}Template, g3IO;
type
// Possible property types
TPropertyType = (
// Boolean value
ptBoolean,
// 32-bit integer value
ptInteger,
// 64-bit integer value
ptInt64,
// Single-precision floating-point value
ptSingle,
// Double-precision floating-point value
ptDouble,
// Short string value
ptShortString,
// Ansi string value
ptAnsiString,
// Unicode string value
ptString,
// Enumerated value
ptEnumeration,
// Set of numbers [0..31]
ptSet,
// Pointer value
ptPointer,
// A link to an object
ptObjectLink,
// Bynary data
ptBinary,
// Object value
ptObject,
// Class value
ptClass
);
{ Type for serializable binary data.
Published properties of descendant types will be included during serialization and deserialization.
Actual type of such property should be always equal to its declared type.
During destruction of an entity destructors of all published properties of descendant types will be called. }
TBinaryData = class
private
// Indicates whether this instance is bound to an entity or must be destroyed by containing TProperties instance
Bound: Boolean;
public
// Init instance with data from the given instance
procedure Assign(AData: TBinaryData); virtual; abstract;
// Reads binary data from input stream and returns True if success.
function Read(IStream: TInputStream): Boolean; virtual; abstract;
// Writes binary data to output stream and returns True if success.
function Write(OStream: TOutputStream): Boolean; virtual; abstract;
// Returns pointer to data
function GetData(): Pointer; virtual; abstract;
end;
// Serializable binary data metaclass
CBinaryData = class of TBinaryData;
// Dynamic array if byte based binary data implementation
TDynamicArray = class(TBinaryData)
public
Data: array of Byte;
destructor Destroy(); override;
procedure Assign(AData: TBinaryData); override;
function Read(IStream: TInputStream): Boolean; override;
function Write(OStream: TOutputStream): Boolean; override;
function GetData(): Pointer; override;
end;
// Pointer based binary data implementation
TPointerData = class(TBinaryData)
public
Data: Pointer;
Size: Integer;
destructor Destroy(); override;
procedure Assign(AData: TBinaryData); override;
function Read(IStream: TInputStream): Boolean; override;
function Write(OStream: TOutputStream): Boolean; override;
function GetData(): Pointer; override;
procedure Allocate(ASize: Integer);
end;
// Text based binary data implementation
TTextData = class(TBinaryData)
public
Data: AnsiString;
destructor Destroy(); override;
procedure Assign(AData: TBinaryData); override;
function Read(IStream: TInputStream): Boolean; override;
function Write(OStream: TOutputStream): Boolean; override;
function GetData(): Pointer; override;
function GetText(): AnsiString;
end;
PPropertyValue = ^TPropertyValue;
// Data structure representing a property value
TPropertyValue = packed record
AsUnicodeString: UnicodeString;
AsAnsiString: AnsiString;
// Property value as various type
case t: TPropertyType of
ptBoolean: (AsBoolean: Boolean);
ptInteger, ptEnumeration, ptSet: (AsInteger: Integer);
ptInt64: (AsInt64: Int64);
ptSingle: (AsSingle: Single);
ptDouble: (AsDouble: Double);
ptShortString: (AsShortString: ShortString);
ptObject: (AsObject: TObject);
ptClass: (AsClass: TClass);
ptPointer: (AsPointer: Pointer);
ptObjectLink: (Linked: TObject; LinkedClass: CAbstractEntity);
ptBinary: (AsData: TBinaryData; BinDataClass: CBinaryData);
//, ptMethod, ptVariant, ptInterface: ();
end;
PProperty = ^TProperty;
// Data structure representing a property description
TProperty = packed record
// Property name
Name: TPropertyName;
// Property type information
TypeId: TPropertyType;
end;
_VectorValueType = TProperty;
_PVectorValueType = PProperty;
{$MESSAGE 'Instantiating TPropertyList interface'}
{$I tpl_coll_vector.inc}
// Property list
TPropertyList = class(_GenVector)
private
function IndexOfName(const Name: TPropertyName): Integer; {$I inline.inc}
end;
TPropertyProcessor = function(const Name: TPropertyName; TypeId: TPropertyType; const Value: TPropertyValue): Boolean;
{ @Abstract(Property collection) }
TProperties = class
protected
FProperties: TPropertyList;
FValues: array of TPropertyValue;
function GetIndex(const Name: TPropertyName): Integer;
function GetPropByIndex(Index: Integer): PProperty;
function GetValueByIndex(Index: Integer): PPropertyValue;
function GetProperty(const Name: TPropertyName): TProperty;
procedure SetProperty(const Name: TPropertyName; const Prop: TProperty);
function GetValue(const Name: TPropertyName): PPropertyValue;
procedure SetValue(const Name: TPropertyName; const Value: PPropertyValue);
function GetCount: Integer;
public
// Creates an empty property collection
constructor Create(); overload;
// Destroys the property collection
destructor Destroy; override;
// Add all properties from the given Properties to this properties
procedure AddAll(const Properties: TProperties);
// Add a property or reset an existing one
function AddProp(const Name: TPropertyName; TypeId: TPropertyType): PPropertyValue;
procedure AddString(const Name: TPropertyName; const Value: string);
procedure AddAnsiString(const Name: TPropertyName; const Value: AnsiString);
procedure AddBoolean(const Name: TPropertyName; const Value: Boolean);
procedure AddInt(const Name: TPropertyName; const Value: Integer);
procedure AddInt64(const Name: TPropertyName; const Value: Int64);
procedure AddSingle(const Name: TPropertyName; const Value: Single);
// Calls the processor for each property. Stops if the processor returns False. Returns True if all properties are processed.
function ForEach(Processor: TPropertyProcessor): Boolean;
// Property definitions
property Prop[const Name: TPropertyName]: TProperty read GetProperty write SetProperty;
// Property values
property Value[const Name: TPropertyName]: PPropertyValue read GetValue write SetValue; default;
// Property definition by index
property PropByIndex[Index: Integer]: PProperty read GetPropByIndex;
// Number of properties
property Count: Integer read GetCount;
end;
// Abstract class responsible for properties serialization / deserialization
TPropertyFilerBase = class
public
{ Reads arbitrary list of property definitions and values from input stream and returns it.
Returns nil if error occured.
May raise EUnsupportedOperation if filer format doesn't support storage of property definitions. }
function ReadArbitrary(IStream: TInputStream): TProperties; virtual; abstract;
// Reads and fills values of specified in Properties list of properties from input stream and returns True if success.
function Read(IStream: TInputStream; Properties: TProperties): Boolean; virtual; abstract;
// Writes properties to output stream and returns True if success.
function Write(OStream: TOutputStream; Properties: TProperties): Boolean; virtual; abstract;
end;
{ Simple property filer implementation.
Doesn't store property definitions but only values and therefore doesn't support read of arbitrary list of properties. }
TSimplePropertyFiler = class(TPropertyFilerBase)
public
function ReadArbitrary(IStream: TInputStream): TProperties; override;
function Read(IStream: TInputStream; Properties: TProperties): Boolean; override;
function Write(OStream: TOutputStream; Properties: TProperties): Boolean; override;
end;
ECEPropertyError = class(EError);
// Builds list of property definitions for the given class using RTTI
function GetClassProperties(AClass: TClass): TProperties;
// Builds list of property definitions for the given class using RTTI and fills values in the given object instance
function GetClassPropertiesAndValues(AClass: TClass; AObj: TObject): TProperties;
// Sets property values of the specified instance from the Properties
procedure SetClassPropertiesAndValues(AObj: TObject; Properties: TProperties);
implementation
uses TypInfo, g3rtti;
const
SIMPLE_PROPERTIES_BEGIN_SIGNATURE: TSignature = (Bytes: (Ord('S'), Ord('P'), Ord('_'), Ord('B')));
SIMPLE_PROPERTIES_END_SIGNATURE: TSignature = (Bytes: (Ord('S'), Ord('P'), Ord('_'), Ord('E')));
function _VectorEquals(const v1, v2: TProperty): Boolean; {$I inline.inc}
begin
Result := v1.Name = v2.Name;
end;
{ TDynamicArray }
destructor TDynamicArray.Destroy;
begin
SetLength(Data, 0);
Data := nil;
inherited;
end;
procedure TDynamicArray.Assign(AData: TBinaryData);
begin
if AData <> nil then Data := TDynamicArray(AData).Data;
end;
function TDynamicArray.Read(IStream: TInputStream): Boolean;
var DataSize: Integer;
begin
Result := False;
if not IStream.ReadCheck(DataSize, SizeOf(DataSize)) then Exit;
SetLength(Data, DataSize);
if DataSize > 0 then
if not IStream.ReadCheck(Data[0], Length(Data)) then Exit;
Result := True;
end;
function TDynamicArray.Write(OStream: TOutputStream): Boolean;
var DataSize: Integer;
begin
Result := False;
DataSize := Length(Data);
if not OStream.WriteCheck(DataSize, SizeOf(DataSize)) then Exit;
if DataSize > 0 then
if not OStream.WriteCheck(Data[0], DataSize) then Exit;
Result := True;
end;
function TDynamicArray.GetData: Pointer;
begin
if Data <> nil then
Result := @Data[0]
else
Result := nil;
end;
{ TTextData }
destructor TTextData.Destroy;
begin
SetLength(Data, 0);
inherited;
end;
procedure TTextData.Assign(AData: TBinaryData);
begin
if not (AData is TTextData) then
raise EInvalidArgument.Create('TTextData.Assign: Invalid type');
Data := Copy(TTextData(AData).GetText(), STRING_INDEX_BASE, Length(TTextData(AData).GetText()));
end;
function TTextData.Read(IStream: TInputStream): Boolean;
begin
Result := g3IO.ReadAnsiString(IStream, Data);
end;
function TTextData.Write(OStream: TOutputStream): Boolean;
begin
Result := g3IO.WriteAnsiString(OStream, Data);
end;
function TTextData.GetData: Pointer;
begin
if Data <> '' then
Result := @Data[STRING_INDEX_BASE]
else
Result := nil;
end;
function TTextData.GetText: AnsiString;
begin
Result := Data;
end;
{ TPointerData }
destructor TPointerData.Destroy;
begin
if Assigned(Data) then FreeMem(Data, Size);
Data := nil;
inherited;
end;
procedure TPointerData.Assign(AData: TBinaryData);
begin
if AData <> nil then
begin
Allocate(TPointerData(AData).Size);
Move(TPointerData(AData).Data^, Data^, Size);
end;
end;
function TPointerData.Read(IStream: TInputStream): Boolean;
begin
Result := False;
if not IStream.ReadCheck(Size, SizeOf(Size)) then Exit;
Allocate(Size);
if Size > 0 then
if not IStream.ReadCheck(Data^, Size) then Exit;
Result := True;
end;
function TPointerData.Write(OStream: TOutputStream): Boolean;
begin
Result := False;
if not OStream.WriteCheck(Size, SizeOf(Size)) then Exit;
if Size > 0 then
if not OStream.WriteCheck(Data^, Size) then Exit;
Result := True;
end;
function TPointerData.GetData: Pointer;
begin
Result := Data;
end;
procedure TPointerData.Allocate(ASize: Integer);
begin
if Assigned(Data) then FreeMem(Data, Size);
Data := nil;
Size := ASize;
if Size > 0 then
GetMem(Data, Size);
end;
{ TPropertyList }
function TPropertyList.IndexOfName(const Name: TPropertyName): Integer;
begin
Result := FSize;
while (Result >= 0) and (FValues[Result].Name <> Name) do Dec(Result);
end;
{$MESSAGE 'Instantiating TPropertyList'}
{$I tpl_coll_vector.inc}
{ TProperties }
function TProperties.GetIndex(const Name: TPropertyName): Integer;
begin
Result := FProperties.Size-1;
while (Result >= 0) and (FProperties[Result].Name <> Name) do Dec(Result);
end;
function TProperties.GetPropByIndex(Index: Integer): PProperty;
begin
if Index >= 0 then
Result := FProperties.GetPtr(Index)
else
Result := nil;
end;
function TProperties.GetValueByIndex(Index: Integer): PPropertyValue;
begin
if Index >= 0 then
Result := @FValues[Index]
else
Result := nil;
end;
function TProperties.GetProperty(const Name: TPropertyName): TProperty;
begin
Result := GetPropByIndex(GetIndex(Name))^;
end;
procedure TProperties.SetProperty(const Name: TPropertyName; const Prop: TProperty);
begin
end;
function TProperties.GetValue(const Name: TPropertyName): PPropertyValue;
begin
Result := GetValueByIndex(GetIndex(Name));
end;
procedure TProperties.SetValue(const Name: TPropertyName; const Value: PPropertyValue);
begin
end;
function TProperties.GetCount: Integer;
begin
Result := FProperties.GetSize();
end;
constructor TProperties.Create;
begin
FProperties := TPropertyList.Create();
end;
destructor TProperties.Destroy;
var i: Integer;
begin
for i := 0 to FProperties.Size-1 do
if (FProperties[i].TypeId = ptBinary) and Assigned(FValues[i].AsData) and not FValues[i].AsData.Bound then
FValues[i].AsData.Free();
FProperties.Free();
FProperties := nil;
inherited;
end;
procedure TProperties.AddAll(const Properties: TProperties);
var
i: Integer;
begin
for i := 0 to Properties.FProperties.Size- 1 do
begin
AddProp(Properties.FProperties[i].Name, Properties.FProperties[i].TypeId);
FValues[FProperties.Size-1] := Properties.FValues[i];
end;
end;
function TProperties.AddProp(const Name: TPropertyName; TypeId: TPropertyType): PPropertyValue;
var
Index: Integer;
begin
Index := GetIndex(Name);
if Index = -1 then begin
Index := FProperties.Size;
FProperties.Size := Index + 1;
SetLength(FValues, FProperties.Size);
end else
Finalize(FValues[Index]); // reset existing values
Result := @FValues[Index];
FProperties.ValuesPtr[Index].Name := Name;
FProperties.ValuesPtr[Index].TypeId := TypeId;
end;
procedure TProperties.AddString(const Name: TPropertyName; const Value: string);
begin
AddProp(Name, ptString)^.AsUnicodeString := Value;
end;
procedure TProperties.AddAnsiString(const Name: TPropertyName; const Value: AnsiString);
begin
AddProp(Name, ptAnsiString)^.AsAnsiString := Value;
end;
procedure TProperties.AddBoolean(const Name: TPropertyName; const Value: Boolean);
begin
AddProp(Name, ptBoolean)^.AsBoolean := Value;
end;
procedure TProperties.AddInt(const Name: TPropertyName; const Value: Integer);
begin
AddProp(Name, ptInteger)^.AsInteger := Value;
end;
procedure TProperties.AddInt64(const Name: TPropertyName; const Value: Int64);
begin
AddProp(Name, ptInt64)^.AsInt64 := Value;
end;
procedure TProperties.AddSingle(const Name: TPropertyName; const Value: Single);
begin
AddProp(Name, ptSingle)^.AsSingle := Value;
end;
function TProperties.ForEach(Processor: TPropertyProcessor): Boolean;
var
i: Integer;
begin
i := 0;
while (i < FProperties.Size) and Processor(FProperties[i].Name, FProperties[i].TypeId, FValues[i]) do;
Result := i < FProperties.Size;
end;
{ TPropertyFiler }
function TSimplePropertyFiler.ReadArbitrary(IStream: TInputStream): TProperties;
begin
Result := nil;
raise EUnsupportedOperation.Create('Arbitrary properties deserialization not supported');
end;
function TSimplePropertyFiler.Read(IStream: TInputStream; Properties: TProperties): Boolean;
var
i: Integer;
Sign: TSignature;
Prop: PProperty;
Value: PPropertyValue;
begin
if Properties = nil then raise EInvalidArgument.Create('Properties argument is nil');
Result := false;
if not IStream.ReadCheck(Sign.DWord, SizeOf(SIMPLE_PROPERTIES_BEGIN_SIGNATURE.DWord)) then Exit;
if Sign.DWord <> SIMPLE_PROPERTIES_BEGIN_SIGNATURE.DWord then Exit;
for i := 0 to Properties.Count-1 do
begin
Prop := Properties.GetPropByIndex(i);
Value := Properties.GetValueByIndex(i);
case Prop^.TypeId of
ptBoolean, ptInteger,
ptEnumeration, ptSet: if not IStream.ReadCheck(Value^.AsInteger, SizeOf(Value^.AsInteger)) then Exit;
ptInt64: if not IStream.ReadCheck(Value^.AsInt64, SizeOf(Value^.AsInt64)) then Exit;
ptSingle: if not IStream.ReadCheck(Value^.AsSingle, SizeOf(Value^.AsSingle)) then Exit;
ptDouble: if not IStream.ReadCheck(Value^.AsDouble, SizeOf(Value^.AsDouble)) then Exit;
ptShortString: if not g3IO.ReadShortString(IStream, Value^.AsShortString) then Exit;
ptAnsiString: if not g3IO.ReadAnsiString(IStream, Value^.AsAnsiString) then Exit;
ptString: if not g3IO.ReadUnicodeString(IStream, Value^.AsUnicodeString) then Exit;
ptBinary: begin
if not Assigned(Value^.AsData) or Value^.AsData.Bound then
Value^.AsData := Value^.BinDataClass.Create();
if not Value^.AsData.Read(IStream) then Exit;
end;
ptObjectLink: if not g3IO.ReadAnsiString(IStream, Value^.AsAnsiString) then Exit;
else Assert(False, 'Invalid property type: ' + TypInfo.GetEnumName(TypeInfo(TPropertyType), Ord(Prop.TypeId)));
end;
end;
if not IStream.ReadCheck(Sign.DWord, SizeOf(SIMPLE_PROPERTIES_END_SIGNATURE.DWord)) then Exit;
if Sign.DWord <> SIMPLE_PROPERTIES_END_SIGNATURE.DWord then Exit;
Result := True;
end;
function TSimplePropertyFiler.Write(OStream: TOutputStream; Properties: TProperties): Boolean;
var
i: Integer;
Prop: PProperty;
Value: PPropertyValue;
begin
if Properties = nil then raise EInvalidArgument.Create('Properties argument is nil');
Result := False;
if not OStream.WriteCheck(SIMPLE_PROPERTIES_BEGIN_SIGNATURE.DWord, SizeOf(SIMPLE_PROPERTIES_BEGIN_SIGNATURE.DWord)) then Exit;
for i := 0 to Properties.Count-1 do
begin
Prop := Properties.GetPropByIndex(i);
Value := Properties.GetValueByIndex(i);
case Prop^.TypeId of
ptBoolean, ptInteger,
ptEnumeration, ptSet: if not OStream.WriteCheck(Value^.AsInteger, SizeOf(Value^.AsInteger)) then Exit;
ptInt64: if not OStream.WriteCheck(Value^.AsInt64, SizeOf(Value^.AsInt64)) then Exit;
ptSingle: if not OStream.WriteCheck(Value^.AsSingle, SizeOf(Value^.AsSingle)) then Exit;
ptDouble: if not OStream.WriteCheck(Value^.AsDouble, SizeOf(Value^.AsDouble)) then Exit;
ptShortString: if not g3IO.WriteShortString(OStream, Value^.AsShortString) then Exit;
ptAnsiString: if not g3IO.WriteAnsiString(OStream, Value^.AsAnsiString) then Exit;
ptString: if not g3IO.WriteUnicodeString(OStream, Value^.AsUnicodeString) then Exit;
ptBinary: if not Value^.AsData.Write(OStream) then Exit;
ptObjectLink: if not g3IO.WriteAnsiString(OStream, Value^.AsAnsiString) then Exit;
else Assert(False, 'Invalid property type: ' + TypInfo.GetEnumName(TypeInfo(TPropertyType), Ord(Prop.TypeId)));
end;
end;
if not OStream.WriteCheck(SIMPLE_PROPERTIES_END_SIGNATURE.DWord, SizeOf(SIMPLE_PROPERTIES_END_SIGNATURE.DWord)) then Exit;
Result := True;
end;
function GetClassProperties(AClass: TClass): TProperties;
begin
Result := GetClassPropertiesAndValues(AClass, nil);
end;
function GetClassPropertiesAndValues(AClass: TClass; AObj: TObject): TProperties;
var
PropInfos: PPropList;
PropInfo: PPropInfo;
Count, i: Integer;
Value: PPropertyValue;
OClass: TClass;
begin
Assert((AObj = nil) or (AObj.ClassType = AClass));
Result := TProperties.Create();
Count := g3rtti.GetClassPropList(AClass, PropInfos);
try
for i := 0 to Count - 1 do
begin
PropInfo := PropInfos^[i];
WriteLn('Prop: ', PropInfo^.Name, ', type: ', TypInfo.GetEnumName(TypeInfo(TTypeKind), Ord(PropInfo^.PropType^.Kind)), ', type name: ', PropInfo^.PropType^.Name);
case PropInfo^.PropType^.Kind of
{$IF Declared(tkBool)}
tkBool,
{$IFEND}
tkInteger:
if PropInfo^.PropType^.Name = 'Boolean' then
begin
Value := Result.AddProp(PropInfo^.Name, ptBoolean);
if Assigned(AObj) then
Value^.AsBoolean := TypInfo.GetOrdProp(AObj, PropInfo) = Ord(True);
end else begin
Value := Result.AddProp(PropInfo^.Name, ptInteger);
if Assigned(AObj) then
Value^.AsInteger := TypInfo.GetOrdProp(AObj, PropInfo);
end;
tkInt64: begin
Value := Result.AddProp(PropInfo^.Name, ptInt64);
if Assigned(AObj) then
Value^.AsInt64 := TypInfo.GetInt64Prop(AObj, PropInfo);
end;
tkFloat:
if PropInfo^.PropType^.Name = 'Single' then
begin
Value := Result.AddProp(PropInfo^.Name, ptSingle);
if Assigned(AObj) then
Value^.AsSingle := TypInfo.GetFloatProp(AObj, PropInfo);
end
else if PropInfo^.PropType^.Name = 'Double' then
begin
Value := Result.AddProp(PropInfo^.Name, ptDouble);
if Assigned(AObj) then
Value^.AsDouble := TypInfo.GetFloatProp(AObj, PropInfo);
end else
raise ECEPropertyError.Create('Unsupported property type: ' + string(PropInfo^.PropType^.Name));
tkEnumeration: begin
Value := Result.AddProp(PropInfo^.Name, ptEnumeration);
if Assigned(AObj) then
Value^.AsInteger := TypInfo.GetOrdProp(AObj, PropInfo);
end;
tkSet: begin
Value := Result.AddProp(PropInfo^.Name, ptSet);
if Assigned(AObj) then
Value^.AsInteger := TypInfo.GetOrdProp(AObj, PropInfo);
end;
{$IF Declared(tkAString)}tkAString, {$IFEND}
tkLString: begin
if PropInfo^.PropType^.Name = 'UTF8String' then
begin
Value := Result.AddProp(PropInfo^.Name, ptString);
if Assigned(AObj) then
Value^.AsUnicodeString := UnicodeString(GetAnsiStrProp(AObj, PropInfo));
end else begin
Value := Result.AddProp(PropInfo^.Name, ptAnsiString);
if Assigned(AObj) then
Value^.AsAnsiString := GetAnsiStrProp(AObj, PropInfo);
end;
end;
{$IF Declared(tkUString)}tkUString: begin
Value := Result.AddProp(PropInfo^.Name, ptString);
if Assigned(AObj) then
Value^.AsUnicodeString := UnicodeString(TypInfo.GetUnicodeStrProp(AObj, PropInfo));
end;
{$IFEND}
tkString: begin
if PropInfo^.PropType^.Name = 'ShortString' then
begin
Value := Result.AddProp(PropInfo^.Name, ptShortString);
if Assigned(AObj) then
Value^.AsShortString := ShortString(TypInfo.GetStrProp(AObj, PropInfo));
end else begin
{$IFDEF UNICODE}
Value := Result.AddProp(PropInfo^.Name, ptString);
if Assigned(AObj) then
Value^.AsUnicodeString := UnicodeString(TypInfo.GetUnicodeStrProp(AObj, PropInfo));
{$ELSE}
Value := Result.AddProp(PropInfo^.Name, ptAnsiString);
if Assigned(AObj) then
Value^.AsAnsiString := GetAnsiStrProp(AObj, PropInfo);
{$ENDIF}
end;
end;
tkWString: begin
Value := Result.AddProp(PropInfo^.Name, ptString);
if Assigned(AObj) then
Value^.AsUnicodeString := TypInfo.GetWideStrProp(AObj, PropInfo);
end;
tkClass: begin
OClass := g3rtti.GetObjectPropClass(AClass, PropInfo);
if OClass.InheritsFrom(TBinaryData) then // Binary data case
begin
Value := Result.AddProp(PropInfo^.Name, ptBinary);
Value^.BinDataClass := CBinaryData(OClass);
if Assigned(AObj) then
begin
Value^.AsData := Value^.BinDataClass.Create();
Value^.AsData.Assign(TBinaryData(TypInfo.GetObjectProp(AObj, PropInfo)));
end;
end else if OClass.InheritsFrom(TAbstractEntity) then // Object link case
begin
Value := Result.AddProp(PropInfo^.Name, ptObjectLink);
Value^.LinkedClass := CAbstractEntity(OClass);
if Assigned(AObj) then
begin
Value^.Linked := TypInfo.GetObjectProp(AObj, PropInfo);
Value^.AsAnsiString := TAbstractEntity(Value^.Linked).GetFullName();
end;
end else
raise ECEPropertyError.Create('Property of unsupported class: ' + string(OClass.ClassName));
end;
else
raise ECEPropertyError.Create('Unsupported property type: ' + string(PropInfo^.PropType^.Name));
end;
end;
finally
FreeMem(PropInfos);
end;
end;
{$WARNINGS OFF}
procedure SetClassPropertiesAndValues(AObj: TObject; Properties: TProperties);
var
i: Integer;
Prop: TProperty;
Value: TPropertyValue;
begin
for i := 0 to Properties.Count-1 do
begin
Prop := Properties.PropByIndex[i]^;
Value := Properties.GetValueByIndex(i)^;
case Prop.TypeId of
ptBoolean, ptInteger,
ptEnumeration, ptSet: TypInfo.SetOrdProp(AObj, Prop.Name, Value.AsInteger);
ptInt64: TypInfo.SetInt64Prop(AObj, Prop.Name, Value.AsInt64);
ptSingle: TypInfo.SetFloatProp(AObj, Prop.Name, Value.AsSingle);
ptDouble: TypInfo.SetFloatProp(AObj, Prop.Name, Value.AsDouble);
ptShortString: TypInfo.SetStrProp(AObj, Prop.Name, Value.AsShortString);
ptAnsiString: TypInfo.SetStrProp(AObj, Prop.Name, Value.AsAnsiString);
ptString: {$IFDEF UNICODE}
TypInfo.SetUnicodeStrProp(AObj, Prop.Name, Value.AsUnicodeString);
{$ELSE}
TypInfo.SetStrProp(AObj, Prop.Name, Value.AsAnsiString);
{$ENDIF}
ptPointer: ;
ptObjectLink: begin
if ((Value.LinkedClass <> nil) and (Value.Linked is Value.LinkedClass)) or (Value.Linked is TAbstractEntity) then
TypInfo.SetObjectProp(AObj, Prop.Name, Value.Linked)
else
if AObj is TAbstractEntity then
TAbstractEntity(AObj).SetObjectLink(Prop.Name, Value.AsAnsiString)
else
raise ECEPropertyError.Create('Class doesn''t support object links: ' + string(AObj.ClassName));
end;
ptBinary: begin
TypInfo.SetObjectProp(AObj, Prop.Name, Value.AsData);
Value.AsData.Bound := True;
end;
ptObject: ;
ptClass: ;
else
raise ECEPropertyError.Create('Unsupported property type: ' + TypInfo.GetEnumName(TypeInfo(TPropertyType), Ord(Prop.TypeId)));
end;
end;
end;
{$WARNINGS ON}
end.