My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
{***************************************************************************}
{ }
{ Spring Framework for Delphi }
{ }
{ Copyright (c) 2009-2012 Spring4D Team }
{ }
{ http://www.spring4d.org }
{ }
{***************************************************************************}
{ }
{ Licensed under the Apache License, Version 2.0 (the "License"); }
{ you may not use this file except in compliance with the License. }
{ You may obtain a copy of the License at }
{ }
{ http://www.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{***************************************************************************}

/// <summary>
/// Provides many easy to use class helpers &amp; record helpers that extend
/// some common classes in the VCL framework.
/// </summary>
/// <remarks>
/// <para>
/// Classes helpers and record helpers have been introduced since Delphi
/// 2007. The initial purpose is to allow developers to extend a class
/// without change the original structure.
/// </para>
/// <note type="note">
/// A class helper type may not declare instance data, but class fields are
/// allowed.
/// </note>
/// <note type="warning">
/// Class helpers and record helpers are not intended to be a design tool
/// in Delphi. It is some kind of "patching" technology.
/// </note>
/// <para>
/// If you want to use these helpers, just uses the <b>Spring.Helpers</b>nam
/// espace in the target unit.
/// </para>
/// </remarks>
/// <example>
/// See examples in the <see cref="TGuidHelper" />.
/// </example>
unit Spring.Helpers;

{$I Spring.inc}

interface

uses
Classes,
SysUtils,
Types,
TypInfo,
Rtti,
Spring,
Spring.Collections,
Spring.Reflection;

type
/// <summary>
/// Represents a record helper for the <c>System.TGuid</c> structure to
/// make it easy to use.
/// </summary>
/// <remarks>
/// <note type="tip">
/// You can use the equal ("=") or not equal ("&lt;&gt;") operator
/// loading in latest Delphi XE.
/// </note>
/// </remarks>
/// <example>
/// The following code demonstrates how to create a new guid and use it in
/// OO-style:
/// <code lang="Delphi">
/// procedure TestGuidHelper;
/// var
/// guid: TGuid;
/// begin
/// // generates a new guid.
/// guid := TGuid.NewGuid;
/// // this guid must not be empty.
/// Assert(not guid.IsEmpty);
/// // print the string representation
/// Writeln(guid.ToString);
/// // print the quoted string
/// Writeln(guid.ToQuotedString);
/// // This guid must equal to itself.
/// Assert(guid.Equals(guid));
/// end;</code>
/// </example>
TGuidHelper = record helper for TGuid
private
class function GetEmpty: TGuid; static;
function GetIsEmpty: Boolean;
public
/// <summary>
/// Creates a guid structure from the specified guid string.
/// </summary>
/// <param name="guidString">
/// the guid string.
/// </param>
class function Create(const guidString: string): TGuid; overload; static;
class function Create(const bytes: TBytes): TGuid; overload; static;
class function Create(a: Integer; b: SmallInt; c: SmallInt; const d: TBytes): TGuid; overload; static;
class function Create(a: Integer; b: SmallInt; c: SmallInt; d, e, f, g, h, i, j, k: Byte): TGuid; overload; static;
class function Create(a: Cardinal; b: Word; c: Word; d, e, f, g, h, i, j, k: Byte): TGuid; overload; static;

/// <summary>
/// Generates a new <c>TGuid</c> instance.
/// </summary>
class function NewGuid: TGuid; static;

function ToBytes: TBytes;

function ToByteArray: TBytes;

/// <summary>
/// Returns a string representation of the guid.
/// </summary>
function ToString: string;

/// <summary>
/// Determines whether the guid equals to another TGuid structure.
/// </summary>
function Equals(const guid: TGuid): Boolean;

/// <summary>
/// Returns the quoted string representation of the guid.
/// </summary>
function ToQuotedString: string;

/// <summary>
/// Gets a value which indicates whether the guid is empty (all zero).
/// </summary>
property IsEmpty: Boolean read GetIsEmpty;

/// <summary>
/// Gets the shared empty guid.
/// </summary>
/// <value>
/// The value of the empty guid is
/// <c>{00000000-0000-0000-0000-000000000000}</c>
/// </value>
class property Empty: TGuid read GetEmpty;
end;

/// <summary>
/// Provides a static method to create a TMethod structure with an instance
/// and a methodaddress.
/// </summary>
TMethodHelper = record helper for TMethod
public
class function Create(const instance, methodAddress: Pointer): TMethod; static;
end;

TStreamHelper = class helper for TStream
public
/// <summary>
/// Reads a value of a value type, which could be an Integer, record,
/// etc., from the stream.
/// </summary>
/// <remarks>
/// <note type="tip">
/// The generic argument could be omitted if the compiler can
/// automatically inreference the type.
/// </note>
/// </remarks>
/// <example>
/// <para>
/// The following example demonstrates how to use the generic
/// <c>ReadBuffer&lt;T&gt;</c> and <c>WriteBuffer&lt;T&gt;</c>methods.
/// </para>
/// <code lang="Delphi">
/// procedure TestStreamHelper;
/// var
/// stream: TStream;
/// value: Integer;
/// begin
/// stream := TMemoryStream.Create;
/// try
/// value := 2;
/// stream.WriteBuffer(value);
/// stream.Position := 0;
/// stream.ReadBuffer&lt;Integer&gt;(value);
/// finally
/// stream.Free;
/// end;
/// end;</code>
/// </example>
procedure ReadBuffer<T: record>(var value: T); overload;

/// <summary>
/// Writes a value of a value type to the stream.
/// </summary>
procedure WriteBuffer<T: record>(const value: T); overload;
end;

TStringsHelper = class helper for TStrings
private
function GetIsEmpty: Boolean;
public
/// <summary>
/// Add an array of string to the list.
/// </summary>
procedure AddStrings(const strings: array of string); overload;

/// <summary>
/// Adds or updates a name-value pair.
/// </summary>
/// <remarks>
/// <note type="warning">
/// There is a <c>Values[name: string]</c>property in the TStrings
/// class, but the entry will be removed if the value is empty.
/// </note>
/// </remarks>
procedure AddOrUpdate(const name, value: string);

// procedure Remove(const s: string);

/// <summary>
/// Executes a procedure during batch updating of the list.
/// </summary>
/// <exception cref="Spring|EArgumentNullException">
/// Raised if the<paramref name="strings" /> is nil or the
/// <paramref name="proc" /> is not assigned.
/// </exception>
procedure ExecuteUpdate(proc: TProc);

/// <summary>
/// Extract all name entries and add them to the
/// <paramref name="strings" /> list.
/// </summary>
/// <exception cref="Spring|EArgumentNullException">
/// Raised if the <paramref name="strings" /> is nil.
/// </exception>
/// <seealso cref="ExtractValues(TStrings)">
/// ExtractValues
/// </seealso>
procedure ExtractNames(strings: TStrings);

/// <summary>
/// Extract all value entries and add them to the
/// <paramref name="strings" /> list.
/// </summary>
/// <exception cref="Spring|EArgumentNullException">
/// Raised if the <paramref name="strings" /> is nil.
/// </exception>
/// <seealso cref="ExtractNames(TStrings)" />
procedure ExtractValues(strings: TStrings);

/// <summary>
/// Returns a string array that contains all the <b>name</b>entries in
/// the string list.
/// </summary>
function GetNames: TStringDynArray;

/// <summary>
/// Returns a string array that contains all the <b>value</b>entries in
/// the string list.
/// </summary>
function GetValues: TStringDynArray;

// function GetValue(const name: string): string; overload;
// function GetValue(const index: Integer): string; overload;

/// <summary>
/// Gets the corresponding value of the name entry if there is such an
/// entry and the value is not empty, otherwise, returns the default
/// value specified by the <paramref name="default" />param.
/// </summary>
function GetValueOrDefault<T>(const name: string; const default: T): T; experimental;

/// <summary>
/// Try finding a name entry in the list.
/// </summary>
function TryFindName(const name: string; var index: Integer): Boolean;

/// <summary>
/// Try finding a value entry in the list.
/// </summary>
function TryFindValue(const value: string; var index: Integer): Boolean;

/// <summary>
/// Try finding an object in the list.
/// </summary>
function TryFindObject(obj: TObject; var index: Integer): Boolean;

/// <summary>
/// Determines whether the list contains the specified name entry.
/// </summary>
function ContainsName(const name: string): Boolean;

/// <summary>
/// Determines whether the list contains the specified value entry.
/// </summary>
function ContainsValue(const value: string): Boolean;

/// <summary>
/// Determines whether the list contains the specified object.
/// </summary>
function ContainsObject(obj: TObject): Boolean;

/// <summary>
/// Converts the string list to a dynamic string array.
/// </summary>
function ToArray: TStringDynArray;

/// <summary>
/// Gets a value indicates whether the strings is empty.
/// </summary>
/// <value>
/// Returns true if the count of the list is zero, otherwise, returns
/// false.
/// </value>
property IsEmpty: Boolean read GetIsEmpty;
end;

TCollectionHelper = class helper for TCollection
public
/// <param name="proc">
/// the anonymous method that will be executed within the batch update.
/// </param>
procedure ExecuteUpdate(proc: TProc);
end;

// TPointHelper, TSizeHelper, TRectHelper

{TODO -oPaul -cGeneral : Add some non-generic implementation}

TRttiObjectHelper = class helper for TRttiObject
public
/// <summary>
/// Gets an array which contains all custom attribute types which the
/// type applies.
/// </summary>
function GetCustomAttributes<T: TCustomAttribute>: TArray<T>;

/// <summary>
/// Enumerates all applied custom attributes and returns the first one
/// which is/inherits the specified type.
/// </summary>
function GetCustomAttribute<T: TCustomAttribute>: T;

/// <summary>
/// Try getting a custom attribute class which is applied by the type.
/// </summary>
function TryGetCustomAttribute<T: TCustomAttribute>(out attribute: T): Boolean;

/// <summary>
/// Determines whether the type applies the specified custom attribute
/// class.
/// </summary>
function HasCustomAttribute<T: TCustomAttribute>: Boolean;
end;

TRttiClassType = TRttiInstanceType;

/// <preliminary />
TRttiTypeHelper = class helper for TRttiType
private
function GetAsInterface: TRttiInterfaceType;
function GetIsClass: Boolean;
function GetIsInterface: Boolean;
function GetIsClassOrInterface: Boolean;
function GetAsClass: TRttiInstanceType;
function GetIsGenericType: Boolean;
function InternalGetConstructors(enumerateBaseType: Boolean = True): IEnumerable<TRttiMethod>;
function InternalGetMethods(enumerateBaseType: Boolean = True): IEnumerable<TRttiMethod>;
function InternalGetProperties(enumerateBaseType: Boolean = True): IEnumerable<TRttiProperty>;
function InternalGetFields(enumerateBaseType: Boolean = True): IEnumerable<TRttiField>;
function GetConstructors: IEnumerable<TRttiMethod>;
function GetMethods: IEnumerable<TRttiMethod>;
function GetProperties: IEnumerable<TRttiProperty>;
function GetFields: IEnumerable<TRttiField>;
public
// function GetMembers: IEnumerable<TRttiMember>;

/// <summary>
/// Returns an enumerable collection which contains all the interface
/// Rtti types that the target type implements.
/// <note type="note">
/// Only Guid interfaces will be enumerated.
/// </note>
/// </summary>
/// <seealso cref="Spring.Collections|IEnumerable&lt;T&gt;" />
function GetInterfaces: IEnumerable<TRttiInterfaceType>;

/// <summary>
/// Gets an array of types which contains all generic arguments.
/// </summary>
/// <remarks>
/// This method extracts generic arguments from the name of the generic
/// type. e.g. Invoking the method on the type
/// <b><c>TDictionary&lt;Integer, string&gt;,</c></b> it will return an
/// array?which contains two types: <c>System.Integer</c> and
/// <c>System.String</c>.
/// </remarks>
function GetGenericArguments: TArray<TRttiType>;

/// <summary>
/// Gets an enumerable collection which contains all constructor methods
/// of the type, including inherited.
/// </summary>
/// <seealso cref="Methods" />
/// <seealso cref="Propoerties" />
/// <seealso cref="Fields" />
property Constructors: IEnumerable<TRttiMethod> read GetConstructors;

/// <summary>
/// Gets a enumerable collection which contains all methods that the type
/// contains, including inherited.
/// </summary>
/// <seealso cref="Constructors" />
/// <seealso cref="Propoerties" />
/// <seealso cref="Fields" />
property Methods: IEnumerable<TRttiMethod> read GetMethods;

/// <summary>
/// Gets a enumerable collection which contains all properties that the
/// type contains, including inherited.
/// </summary>
/// <seealso cref="Constructors" />
/// <seealso cref="Methods" />
/// <seealso cref="Fields" />
property Properties: IEnumerable<TRttiProperty> read GetProperties;

/// <summary>
/// Gets a enumerable collection which contains all fields that the type
/// contains, including inherited.
/// </summary>
/// <seealso cref="Constructors" />
/// <seealso cref="Methods" />
/// <seealso cref="Propoerties" />
property Fields: IEnumerable<TRttiField> read GetFields;

property AsClass: TRttiInstanceType read GetAsClass;
property AsInterface: TRttiInterfaceType read GetAsInterface;
property IsClass: Boolean read GetIsClass;
property IsInterface: Boolean read GetIsInterface;
property IsClassOrInterface: Boolean read GetIsClassOrInterface;

/// <summary>
/// Gets a value indicates whether the current type is generic.
/// </summary>
property IsGenericType: Boolean read GetIsGenericType;
end;

TRttiMemberHelper = class helper for TRttiMember
private
function GetIsPrivate: Boolean;
function GetIsProtected: Boolean;
function GetIsPublic: Boolean;
function GetIsPublished: Boolean;
function GetIsConstructor: Boolean;
function GetIsProperty: Boolean;
function GetIsMethod: Boolean;
function GetIsField: Boolean;
function GetAsMethod: TRttiMethod;
function GetAsProperty: TRttiProperty;
function GetAsField: TRttiField;
public
// procedure InvokeMember(instance: TValue; const arguments: array of TValue);
function GetValue(const instance: TValue): TValue; overload;
procedure SetValue(const instance: TValue; const value: TValue); overload;
property AsMethod: TRttiMethod read GetAsMethod;
property AsProperty: TRttiProperty read GetAsProperty;
property AsField: TRttiField read GetAsField;
property IsConstructor: Boolean read GetIsConstructor;
property IsProperty: Boolean read GetIsProperty;
property IsMethod: Boolean read GetIsMethod;
property IsField: Boolean read GetIsField;
property IsPrivate: Boolean read GetIsPrivate;
property IsProtected: Boolean read GetIsProtected;
property IsPublic: Boolean read GetIsPublic;
property IsPublished: Boolean read GetIsPublished;
end;

TRttiPropertyHelper = class helper for TRttiProperty
public
function GetValue(const instance: TValue): TValue; overload;
procedure SetValue(const instance: TValue; const value: TValue); overload;
end;

TRttiFieldHelper = class helper for TRttiField
public
function GetValue(const instance: TValue): TValue; overload;
procedure SetValue(const instance: TValue; const value: TValue); overload;
end;

TRttiInterfaceTypeHelper = class helper for TRttiInterfaceType
private
function GetHasGuid: Boolean;
public
/// <summary>
/// Gets a value indicates whether this interface type has a guid.
/// </summary>
property HasGuid: Boolean read GetHasGuid;
end;

implementation

uses
StrUtils,
Spring.ResourceStrings;

{$REGION 'TGuidHelper'}

class function TGuidHelper.Create(const guidString: string): TGuid;
begin
Result := StringToGUID(guidString);
end;

class function TGuidHelper.Create(const bytes: TBytes): TGuid;
begin
if Length(bytes) <> 16 then
raise EArgumentException.CreateResFmt(@SInvalidGuidArray, [16]);
Move(bytes[0], Result, SizeOf(Result));
end;

class function TGuidHelper.Create(a: Integer; b, c: SmallInt;
const d: TBytes): TGuid;
begin
if Length(d) <> 16 then
raise EArgumentException.CreateResFmt(@SInvalidGuidArray, [8]);
Result.D1 := LongWord(a);
Result.D2 := Word(b);
Result.D3 := Word(c);
Move(d[0], Result.D4, SizeOf(Result.D4));
end;

class function TGuidHelper.Create(a: Cardinal; b, c: Word; d, e, f, g, h, i, j,
k: Byte): TGuid;
begin
Result.D1 := LongWord(a);
Result.D2 := Word(b);
Result.D3 := Word(c);
Result.D4[0] := d;
Result.D4[1] := e;
Result.D4[2] := f;
Result.D4[3] := g;
Result.D4[4] := h;
Result.D4[5] := i;
Result.D4[6] := j;
Result.D4[7] := k;
end;

class function TGuidHelper.Create(a: Integer; b, c: SmallInt; d, e, f, g, h, i,
j, k: Byte): TGuid;
begin
Result.D1 := LongWord(a);
Result.D2 := Word(b);
Result.D3 := Word(c);
Result.D4[0] := d;
Result.D4[1] := e;
Result.D4[2] := f;
Result.D4[3] := g;
Result.D4[4] := h;
Result.D4[5] := i;
Result.D4[6] := j;
Result.D4[7] := k;
end;

class function TGuidHelper.NewGuid: TGuid;
begin
if CreateGUID(Result) <> S_OK then
RaiseLastOSError;
end;

function TGuidHelper.Equals(const guid: TGuid): Boolean;
begin
Result := SysUtils.IsEqualGUID(Self, guid);
end;

function TGuidHelper.GetIsEmpty: Boolean;
begin
{$WARNINGS OFF}
Result := Self.Equals(TGuid.Empty);
{$WARNINGS ON}
end;

function TGuidHelper.ToString: string;
begin
Result := SysUtils.GUIDToString(Self);
end;

function TGuidHelper.ToByteArray: TBytes;
begin
SetLength(Result, 16);
Move(D1, Result[0], SizeOf(Self));
end;

function TGuidHelper.ToBytes: TBytes;
begin
SetLength(Result, 16);
Move(D1, Result[0], SizeOf(Self));
end;

function TGuidHelper.ToQuotedString: string;
begin
Result := QuotedStr(Self.ToString);
end;

class function TGuidHelper.GetEmpty: TGuid;
begin
FillChar(Result, Sizeof(Result), 0);
end;

//class operator TGuidHelper.Equal(const left, right: TGuid) : Boolean;
//begin
// Result := left.Equals(right);
//end;

//class operator TGuidHelper.NotEqual(const left, right: TGuid) : Boolean;
//begin
// Result := not left.Equals(right);
//end;

{$ENDREGION}


{$REGION 'TMethodHelper'}

class function TMethodHelper.Create(const instance,
methodAddress: Pointer): TMethod;
begin
Result.Code := methodAddress;
Result.Data := instance;
end;

{$ENDREGION}


{$REGION 'Classes Helpers'}

{ TStreamHelper }

procedure TStreamHelper.ReadBuffer<T>(var value: T);
begin
ReadBuffer(value, SizeOf(T));
end;

procedure TStreamHelper.WriteBuffer<T>(const value: T);
begin
WriteBuffer(value, SizeOf(T));
end;

{ TStringsHelper }

procedure TStringsHelper.AddStrings(const strings: array of string);
var
s: string;
begin
BeginUpdate;
try
for s in strings do
begin
Add(s);
end;
finally
EndUpdate;
end;
end;

procedure TStringsHelper.AddOrUpdate(const name, value: string);
var
index: Integer;
begin
index := IndexOfName(name);
if index <> -1 then
begin
Strings[index] := name + NameValueSeparator + value;
end
else
begin
Add(name + NameValueSeparator + value);
end;
end;

procedure TStringsHelper.ExecuteUpdate(proc: TProc);
begin
TArgument.CheckNotNull(Assigned(proc), 'proc');

BeginUpdate;
try
Clear;
proc();
finally
EndUpdate;
end;
end;

procedure TStringsHelper.ExtractNames(strings: TStrings);
var
i: Integer;
begin
TArgument.CheckNotNull(strings, 'strings');

strings.BeginUpdate;
try
for i := 0 to Count - 1 do
begin
strings.Add(Self.Names[i]);
end;
finally
strings.EndUpdate;
end;
end;

procedure TStringsHelper.ExtractValues(strings: TStrings);
var
i: Integer;
begin
TArgument.CheckNotNull(strings, 'strings');

strings.BeginUpdate;
try
for i := 0 to Count - 1 do
begin
strings.Add(Self.ValueFromIndex[i]);
end;
finally
strings.EndUpdate;
end;
end;

function TStringsHelper.TryFindName(const name: string;
var index: Integer): Boolean;
begin
index := IndexOfName(name);
Result := index > -1;
end;

function TStringsHelper.TryFindValue(const value: string;
var index: Integer): Boolean;
var
v: string;
i: Integer;
begin
index := -1;
Result := False;
for i := 0 to Count - 1 do
begin
v := ValueFromIndex[i];
if SameText(v, value) then
begin
index := i;
Exit(True);
end;
end;
end;

function TStringsHelper.TryFindObject(obj: TObject;
var index: Integer): Boolean;
begin
index := IndexOfObject(obj);
Result := index > -1;
end;

function TStringsHelper.ContainsName(const name: string): Boolean;
begin
Result := IndexOfName(name) > -1;
end;

function TStringsHelper.ContainsValue(const value: string): Boolean;
var
index: Integer;
begin
Result := TryFindValue(value, index);
end;

function TStringsHelper.ContainsObject(obj: TObject): Boolean;
begin
Result := IndexOfObject(obj) > -1;
end;

function TStringsHelper.GetValueOrDefault<T>(const name: string;
const default: T): T;
var
index: Integer;
value: string;
begin
index := IndexOfName(name);
if index > -1 then
begin
value := ValueFromIndex[index];
end;
if value <> '' then
begin
Result := TValue.From<string>(value).AsType<T>; // TODO: Fix this ASAP because TValue.AsType<T> sucks...
end
else
begin
Result := default;
end;
end;

function TStringsHelper.GetNames: TStringDynArray;
var
i: Integer;
begin
SetLength(Result, Count);
for i := 0 to Count - 1 do
begin
Result[i] := Names[i];
end;
end;

function TStringsHelper.GetValues: TStringDynArray;
var
i: Integer;
begin
SetLength(Result, Count);
for i := 0 to Count - 1 do
begin
Result[i] := ValueFromIndex[i];
end;
end;

function TStringsHelper.ToArray: TStringDynArray;
var
i: Integer;
begin
SetLength(Result, Count);
for i := 0 to Count - 1 do
begin
Result[i] := Strings[i];
end;
end;

function TStringsHelper.GetIsEmpty: Boolean;
begin
Result := Count = 0;
end;

{ TCollectionHelper }

procedure TCollectionHelper.ExecuteUpdate(proc: TProc);
begin
TArgument.CheckNotNull(Assigned(proc), 'proc');

BeginUpdate;
try
Clear;
proc();
finally
EndUpdate;
end;
end;

{$ENDREGION}


{$REGION 'RTTI Class Helpers'}

{ TRttiObjectHelper }

function TRttiObjectHelper.TryGetCustomAttribute<T>(out attribute: T): Boolean;
begin
attribute := GetCustomAttribute<T>;
Result := attribute <> nil;
end;

function TRttiObjectHelper.GetCustomAttribute<T>: T;
var
attribute: TCustomAttribute;
begin
Result := Default(T);
for attribute in GetAttributes do
begin
if attribute.InheritsFrom(T) then
begin
Result := T(attribute);
Break;
end;
end;
end;

function TRttiObjectHelper.GetCustomAttributes<T>: TArray<T>;
var
attribute: TCustomAttribute;
begin
for attribute in GetAttributes do
begin
if attribute.InheritsFrom(T) then
begin
SetLength(Result, Length(Result)+1);
Result[Length(Result)-1] := T(attribute);
end;
end;
end;

function TRttiObjectHelper.HasCustomAttribute<T>: Boolean;
var
attribute: T;
begin
Result := TryGetCustomAttribute<T>(attribute);
end;

{ TRttiTypeHelper }

function TRttiTypeHelper.InternalGetConstructors(
enumerateBaseType: Boolean): IEnumerable<TRttiMethod>;
var
func: TGetRttiMembersFunc<TRttiMethod>;
begin
func :=
function(targetType: TRttiType): TArray<TRttiMethod>
begin
Result := targetType.GetDeclaredMethods;
end;
Result := TRttiMemberEnumerable<TRttiMethod>.Create(
Self,
func,
enumerateBaseType,
TMethodFilters.IsConstructor()
);
end;

function TRttiTypeHelper.InternalGetMethods(
enumerateBaseType: Boolean): IEnumerable<TRttiMethod>;
var
func: TGetRttiMembersFunc<TRttiMethod>;
begin
func :=
function(targetType: TRttiType): TArray<TRttiMethod>
begin
Result := targetType.GetDeclaredMethods;
end;
Result := TRttiMemberEnumerable<TRttiMethod>.Create(
Self,
func,
enumerateBaseType,
nil
);
end;

function TRttiTypeHelper.InternalGetProperties(
enumerateBaseType: Boolean): IEnumerable<TRttiProperty>;
var
func: TGetRttiMembersFunc<TRttiProperty>;
begin
func :=
function(targetType: TRttiType): TArray<TRttiProperty>
begin
Result := targetType.GetDeclaredProperties;
end;
Result := TRttiMemberEnumerable<TRttiProperty>.Create(
Self,
func,
enumerateBaseType,
nil
);
end;

function TRttiTypeHelper.InternalGetFields(
enumerateBaseType: Boolean): IEnumerable<TRttiField>;
var
func: TGetRttiMembersFunc<TRttiField>;
begin
func :=
function(targetType: TRttiType): TArray<TRttiField>
begin
Result := targetType.GetDeclaredFields;
end;
Result := TRttiMemberEnumerable<TRttiField>.Create(
Self,
func,
enumerateBaseType,
nil
);
end;

function TRttiTypeHelper.GetConstructors: IEnumerable<TRttiMethod>;
begin
Result := InternalGetConstructors;
end;

function TRttiTypeHelper.GetMethods: IEnumerable<TRttiMethod>;
begin
Result := InternalGetMethods;
end;

function TRttiTypeHelper.GetProperties: IEnumerable<TRttiProperty>;
begin
Result := InternalGetProperties;
end;

function TRttiTypeHelper.GetFields: IEnumerable<TRttiField>;
begin
Result := InternalGetFields;
end;

{$IFNDEF DelphiXE_UP}
function SplitString(const s: string; delimiter: Char): TStringDynArray;
var
list: TStrings;
i: Integer;
begin
list := TStringList.Create;
try
list.StrictDelimiter := True;
list.Delimiter := delimiter;
list.DelimitedText := s;
SetLength(Result, list.Count);
for i := 0 to list.Count - 1 do
Result[i] := list[i];
finally
list.Free;
end;
end;
{$ENDIF}

// Nullable<TDateTime>
// TDictionary<string, TObject>
// TDictionary<string, IDictionary<string, TObject>>
function TRttiTypeHelper.GetGenericArguments: TArray<TRttiType>; // TEMP
var
p1, p2: Integer;
args: string;
elements: TStringDynArray;
i: Integer;
begin
p1 := Pos('<', Name);
p2 := Pos('>', Name);
if (p1 = 0) or (p2 = 0) or (p1 > p2) then
begin
Exit(nil);
end;
args := MidStr(Name, p1+1, p2-p1-1);
elements := SplitString(args, ',');
SetLength(Result, Length(elements));
for i := 0 to High(elements) do
begin
Result[i] := TType.FindType(elements[i]);
end;
end;

function TRttiTypeHelper.GetAsClass: TRttiInstanceType;
begin
Result := Self as TRttiInstanceType;
end;

function TRttiTypeHelper.GetAsInterface: TRttiInterfaceType;
begin
Result := Self as TRttiInterfaceType;
end;

function TRttiTypeHelper.GetInterfaces: IEnumerable<TRttiInterfaceType>;
var
list: IDictionary<TGUID, TRttiInterfaceType>;
classType: TClass;
table: PInterfaceTable;
entry: TInterfaceEntry;
aType: TRttiInterfaceType;
i: Integer;
begin
if Self.IsClass then
begin
list := TCollections.CreateDictionary<TGUID, TRttiInterfaceType>;
classType := Self.AsInstance.MetaclassType;
while classType <> nil do
begin
table := classType.GetInterfaceTable;
if table <> nil then
begin
for i := 0 to table.EntryCount - 1 do
begin
entry := table.Entries[i];
if not list.ContainsKey(entry.IID) and
{$WARNINGS OFF}
not entry.IID.IsEmpty and
{$WARNINGS ON}
TType.TryGetInterfaceType(entry.IID, aType) then
begin
list[entry.IID] := aType;
end;
end;
end;
classType := classType.ClassParent;
end;
Result := list.Values;
end;
end;

function TRttiTypeHelper.GetIsClass: Boolean;
begin
Result := Self is TRttiInstanceType;
end;

function TRttiTypeHelper.GetIsClassOrInterface: Boolean;
begin
Result := Self.IsClass or Self.IsInterface;
end;

function TRttiTypeHelper.GetIsGenericType: Boolean;
begin
Result := (Pos('<', Name) > 0) and (Pos('>', Name) > 0);
end;

function TRttiTypeHelper.GetIsInterface: Boolean;
begin
Result := Self is TRttiInterfaceType;
end;

{ TRttiInterfaceTypeHelper }

function TRttiInterfaceTypeHelper.GetHasGuid: Boolean;
begin
Result := ifHasGuid in Self.IntfFlags;
end;

{ TRttiMemberHelper }

function TRttiMemberHelper.GetValue(const instance: TValue): TValue;
begin
if IsProperty then
begin
Result := AsProperty.GetValue(instance);
end
else if IsField then
begin
Result := AsField.GetValue(instance);
end
else
begin
raise EInvalidOperationException.CreateRes(@SInvalidOperation_GetValue);
end;
end;

procedure TRttiMemberHelper.SetValue(const instance, value: TValue);
begin
if IsProperty then
begin
AsProperty.SetValue(instance, value);
end
else if IsField then
begin
AsField.SetValue(instance, value);
end
else
begin
raise EInvalidOperationException.CreateRes(@SInvalidOperation_SetValue);
end;
end;

function TRttiMemberHelper.GetIsPrivate: Boolean;
begin
Result := Visibility = mvPrivate;
end;

function TRttiMemberHelper.GetIsProtected: Boolean;
begin
Result := Visibility = mvProtected;
end;

function TRttiMemberHelper.GetIsPublic: Boolean;
begin
Result := Visibility = mvPublic;
end;

function TRttiMemberHelper.GetIsPublished: Boolean;
begin
Result := Visibility = mvPublished;
end;

function TRttiMemberHelper.GetIsConstructor: Boolean;
begin
Result := (Self is TRttiMethod) and TRttiMethod(Self).IsConstructor;
end;

function TRttiMemberHelper.GetIsProperty: Boolean;
begin
Result := Self is TRttiProperty;
end;

function TRttiMemberHelper.GetIsMethod: Boolean;
begin
Result := Self is TRttiMethod;
end;

function TRttiMemberHelper.GetIsField: Boolean;
begin
Result := Self is TRttiField;
end;

function TRttiMemberHelper.GetAsMethod: TRttiMethod;
begin
Result := Self as TRttiMethod;
end;

function TRttiMemberHelper.GetAsProperty: TRttiProperty;
begin
Result := Self as TRttiProperty;
end;

function TRttiMemberHelper.GetAsField: TRttiField;
begin
Result := Self as TRttiField;
end;

{ TRttiPropertyHelper }

function TRttiPropertyHelper.GetValue(const instance: TValue): TValue;
begin
if instance.IsObject then
Result := GetValue(instance.AsObject)
else
Result := GetValue(instance.GetReferenceToRawData);
end;

procedure TRttiPropertyHelper.SetValue(const instance, value: TValue);
begin
if instance.IsObject then
SetValue(instance.AsObject, value)
else
SetValue(instance.GetReferenceToRawData, value);
end;

{ TRttiFieldHelper }

function TRttiFieldHelper.GetValue(const instance: TValue): TValue;
begin
if instance.IsObject then
Result := AsField.GetValue(instance.AsObject)
else
Result := AsField.GetValue(instance.GetReferenceToRawData);
end;

procedure TRttiFieldHelper.SetValue(const instance, value: TValue);
begin
if instance.IsObject then
SetValue(instance.AsObject, value)
else
SetValue(instance.GetReferenceToRawData, value);
end;

{$ENDREGION}

end.

Change log

r476 by baoquan.zuo on Mar 12, 2012   Diff
Formatted all XML Documentations
Go to: 
Sign in to write a code review

Older revisions

r471 by sglie...@aagon.com on Feb 24, 2012   Diff
renamed Spring.System to Spring.Base
r450 by baoquan.zuo on Feb 14, 2012   Diff
Breaking Changes:
1. Moved Cryptography & Utils to
Spring.Extensions (Some routines now
locates in Spring.pas)
2. Removed Reflection.ValueExpression,
...
r447 by baoquan.zuo on Feb 8, 2012   Diff
Changed EInvalidOperation to EInvalidO
perationException(SysUtils.EInvalidOpE
xception) due to redeclaration in
Classes.pas
All revisions of this file

File info

Size: 34763 bytes, 1246 lines
Powered by Google Project Hosting