-
Notifications
You must be signed in to change notification settings - Fork 186
/
Quick.Options.pas
754 lines (654 loc) · 21.7 KB
/
Quick.Options.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
{ ***************************************************************************
Copyright (c) 2015-2021 Kike Pérez
Unit : Quick.Options
Description : Configuration group settings
Author : Kike Pérez
Version : 1.0
Created : 18/10/2019
Modified : 15/12/2021
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
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.
*************************************************************************** }
unit Quick.Options;
{$i QuickLib.inc}
interface
uses
Classes,
RTTI,
System.TypInfo,
System.SysUtils,
System.Generics.Collections,
Quick.RTTI.Utils,
Quick.Commons,
Quick.FileMonitor;
type
Required = class(TCustomAttribute);
TValidationCustomAttribute = class(TCustomAttribute)
protected
fErrorMsg : string;
public
property ErrorMsg : string read fErrorMsg write fErrorMsg;
end;
Range = class(TValidationCustomAttribute)
private
fRangeMin : Double;
fRangeMax : Double;
public
constructor Create(aMin, aMax : Integer; const aErrorMsg : string = ''); overload;
constructor Create(aMin, aMax : Double; const aErrorMsg : string = ''); overload;
property Min : Double read fRangeMin write fRangeMax;
property Max : Double read fRangeMax write fRangeMax;
end;
StringLength = class(TValidationCustomAttribute)
private
fMaxLength : Integer;
public
constructor Create(aMaxLength : Integer; const aErrorMsg : string = '');
property MaxLength : Integer read fMaxLength write fMaxLength;
end;
TOptionsBase = class(TInterfacedObject)
end;
{$IFDEF DELPHIRX102_UP}
{$M+}
TOptions = class;
{$M-}
TConfigureOptionsProc<T : TOptions> = reference to procedure(aOptions : T);
{$ELSE}
TConfigureOptionsProc<T> = reference to procedure(aOptions : T);
{$ENDIF}
IOptionsValidator = interface
['{C6A09F78-8E34-4689-B943-83620437B9EF}']
procedure ValidateOptions;
end;
{$M+}
TOptions = class(TInterfacedObject,IOptionsValidator)
private
fName : string;
fHideOptions : Boolean;
procedure DoValidateOptions; virtual;
public
constructor Create; virtual;
property Name : string read fName write fName;
property HideOptions : Boolean read fHideOptions write fHideOptions;
procedure DefaultValues; virtual;
{$IFDEF DELPHIRX102_UP}
function ConfigureOptions<T : TOptions>(aOptionsFunc : TConfigureOptionsProc<T>) : IOptionsValidator;
{$ELSE}
function ConfigureOptions<T>(aOptionsFunc : TConfigureOptionsProc<T>) : IOptionsValidator;
{$ENDIF}
procedure ValidateOptions;
end;
{$M-}
TOptionsValidator = class(TInterfacedObject,IOptionsValidator)
private
fOptions : TOptions;
public
constructor Create(aOptions : TOptions);
procedure ValidateRequired(const aInstance : TObject; aProperty: TRttiProperty);
procedure ValidateStringLength(const aInstance : TObject; aProperty: TRttiProperty; aValidation : StringLength);
procedure ValidateRange(const aInstance : TObject; aProperty: TRttiProperty; aValidation : Range);
procedure ValidateObject(aObj : TObject);
procedure ValidateArray(aValue : TValue);
procedure ValidateOptions;
end;
TOptions<T : TOptions> = record
private
fOptions : T;
public
constructor Create(aOptions : T);
function ConfigureOptions(aOptionsFunc : TConfigureOptionsProc<T>) : IOptionsValidator;
end;
IOptions<T : TOptions> = interface
['{2779F946-2692-4F74-88AD-F35F5137057A}']
function GetSectionValue : T;
property Value : T read GetSectionValue;
end;
TOptionsClass = class of TOptions;
IOptionsContainer = interface
['{A593C8BB-53CF-4AA4-9641-BF974E45CBD1}']
function AddSection(aOption : TOptionsClass; const aOptionsName : string = '') : TOptions;
function GetOptions(aOptionClass : TOptionsClass): TOptions;
function GetSection(aOptionsSection : TOptionsClass; var vOptions : TOptions) : Boolean; overload;
procedure AddOption(aOption : TOptions);
function ExistsSection(aOption : TOptionsClass; const aSectionName : string = '') : Boolean;
end;
TSectionList = TObjectList<TOptions>;
IOptionsSerializer = interface
['{7DECE203-4AAE-4C9D-86C8-B3D583DF7C8B}']
function Load(aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean;
function LoadSection(aSections : TSectionList; aOptions: TOptions) : Boolean;
procedure Save(aSections : TSectionList);
function GetFileSectionNames(out oSections : TArray<string>) : Boolean;
function ConfigExists : Boolean;
end;
IFileOptionsSerializer = interface(IOptionsSerializer)
['{3417B142-2879-4DA6-86CA-19F0F427A92C}']
function GetFileName : string;
procedure SetFileName(const aFilename : string);
property Filename : string read GetFilename write SetFilename;
end;
TOptionsSerializer = class(TInterfacedObject,IOptionsSerializer)
public
function Load(aSections : TSectionList; aFailOnSectionNotExists : Boolean) : Boolean; virtual; abstract;
function LoadSection(aSections : TSectionList; aOptions: TOptions) : Boolean; virtual; abstract;
procedure Save(aSections : TSectionList); virtual; abstract;
function GetFileSectionNames(out oSections : TArray<string>) : Boolean; virtual; abstract;
function ConfigExists : Boolean; virtual; abstract;
end;
TOptionsFileSerializer = class(TOptionsSerializer,IFileOptionsSerializer)
private
fFilename : string;
function GetFileName : string;
procedure SetFileName(const aFilename : string);
public
property Filename : string read GetFilename write SetFilename;
end;
TFileModifiedEvent = reference to procedure;
TLoadConfigEvent = reference to procedure;
TOptionValue<T : TOptions> = class(TInterfacedObject,IOptions<T>)
private
fValue : T;
function GetSectionValue : T;
public
constructor Create(aValue : T);
property Value : T read GetSectionValue;
end;
TOptionsContainer = class(TInterfacedObject,IOptionsContainer)
private
fSerializer : IOptionsSerializer;
fSections : TSectionList;
fLoaded : Boolean;
fOnConfigLoaded : TLoadConfigEvent;
fOnConfigReloaded : TLoadConfigEvent;
function GetOptions(aOptionClass : TOptionsClass): TOptions; overload;
function GetOptions(aIndex : Integer) : TOptions; overload;
function GetSection(aOptionsSection : TOptionsClass; var vOptions : TOptions) : Boolean; overload;
public
constructor Create(aOptionsSerializer : IOptionsSerializer);
destructor Destroy; override;
property IsLoaded : Boolean read fLoaded;
function ExistsSection(aOption : TOptionsClass; const aSectionName : string = '') : Boolean; overload;
function ExistsSection<T : TOptions>(const aSectionName : string = '') : Boolean; overload;
property Items[aOptionClass : TOptionsClass] : TOptions read GetOptions; default;
property Items[aIndex : Integer] : TOptions read GetOptions; default;
property OnConfigLoaded : TLoadConfigEvent read fOnConfigLoaded write fOnConfigLoaded;
property OnConfigReloaded : TLoadConfigEvent read fOnConfigReloaded write fOnConfigReloaded;
function AddSection(aOption : TOptionsClass; const aSectionName : string = '') : TOptions; overload;
function AddSection<T : TOptions>(const aSectionName : string = '') : TOptions<T>; overload;
procedure AddOption(aOption : TOptions);
function GetSectionInterface<T : TOptions> : IOptions<T>;
function GetSection<T : TOptions>(const aSectionName : string = '') : T; overload;
function GetFileSectionNames(out oSections : TArray<string>) : Boolean;
function Count : Integer;
procedure Load(aFailOnSectionNotExists : Boolean = False); virtual;
procedure LoadSection(aOptions : TOptions);
procedure Save; virtual;
end;
TFileOptionsContainer = class(TOptionsContainer)
private
fFilename : string;
fFileMonitor : TFileMonitor;
fOnFileModified : TFileModifiedEvent;
fReloadIfFileChanged : Boolean;
procedure CreateFileMonitor;
procedure FileModifiedNotify(MonitorNotify : TMonitorNotify);
procedure SetReloadIfFileChanged(const Value: Boolean);
public
constructor Create(aOptionsSerializer : IFileOptionsSerializer; aReloadIfFileChanged : Boolean = False);
destructor Destroy; override;
property FileName : string read fFilename;
property ReloadIfFileChanged : Boolean read fReloadIfFileChanged write SetReloadIfFileChanged;
property OnFileModified : TFileModifiedEvent read fOnFileModified write fOnFileModified;
procedure Save; override;
function GetFileSectionNames(out oSections: TArray<string>): Boolean;
end;
IOptionsBuilder<T : TOptions> = interface
['{1A1DC9A9-7F2D-4CC4-A772-6C7DBAB34424}']
function Options : T;
end;
TOptionsBuilder<T : TOptions> = class(TInterfacedObject,IOptionsBuilder<T>)
protected
fOptions : T;
public
constructor Create;
function Options : T;
end;
EOptionConfigureError = class(Exception);
EOptionLoadError = class(Exception);
EOptionSaveError = class(Exception);
EOptionValidationError = class(Exception);
implementation
{ TCustomOptionsContainer}
function TOptionsContainer.ExistsSection(aOption: TOptionsClass;const aSectionName: string): Boolean;
var
option : TOptions;
begin
Result := False;
for option in fSections do
begin
if CompareText(option.ClassName,aOption.ClassName) = 0 then
begin
if (not aSectionName.IsEmpty) and (CompareText(option.Name,aSectionName) = 0) then Exit(True);
end;
end;
end;
function TOptionsContainer.ExistsSection<T>(const aSectionName: string): Boolean;
begin
Result := GetSection<T>(aSectionName) <> nil;
end;
procedure TOptionsContainer.AddOption(aOption: TOptions);
begin
if aOption.Name.IsEmpty then aOption.Name := Copy(aOption.ClassName,2,aOption.ClassName.Length);
fSections.Add(aOption);
end;
function TOptionsContainer.AddSection(aOption : TOptionsClass; const aSectionName : string = '') : TOptions;
var
option : TOptions;
begin
//if section already exists, returns it
option := Self.GetOptions(aOption);
if option <> nil then Exit(option);
option := aOption.Create;
if aSectionName.IsEmpty then option.Name := Copy(aOption.ClassName,2,aOption.ClassName.Length)
else option.Name := aSectionName;
fSections.Add(option);
Result := option;
end;
function TOptionsContainer.AddSection<T>(const aSectionName: string): TOptions<T>;
var
option : TOptions;
begin
//if section already exists, returns it
option := Self.GetSection<T>(aSectionName);
if option <> nil then Exit(TOptions<T>(option));
//new section
option := TRTTI.CreateInstance<T>;
if aSectionName.IsEmpty then option.Name := Copy(T.ClassName,2,T.ClassName.Length)
else option.Name := aSectionName;
fSections.Add(option);
Result.Create(option);
end;
function TOptionsContainer.Count: Integer;
begin
Result := fSections.Count;
end;
constructor TOptionsContainer.Create(aOptionsSerializer: IOptionsSerializer);
begin
fSerializer := aOptionsSerializer;
fSections := TSectionList.Create(False);
fLoaded := False;
end;
destructor TOptionsContainer.Destroy;
var
option : TOptions;
begin
fSerializer := nil;
for option in fSections do
begin
if option.RefCount = 0 then option.Free;
end;
fSections.Free;
inherited;
end;
function TOptionsContainer.GetFileSectionNames(out oSections: TArray<string>): Boolean;
begin
Result := fSerializer.GetFileSectionNames(oSections);
end;
function TOptionsContainer.GetOptions(aIndex: Integer): TOptions;
begin
Result := fSections[aIndex];
end;
function TOptionsContainer.GetSection(aOptionsSection: TOptionsClass; var vOptions: TOptions): Boolean;
var
option : TOptions;
begin
Result := False;
for option in fSections do
begin
if option is TOptionsClass then
begin
vOptions := option as TOptionsClass;
Exit;
end;
end;
end;
function TOptionsContainer.GetOptions(aOptionClass : TOptionsClass) : TOptions;
var
option : TOptions;
begin
Result := nil;
for option in fSections do
begin
if option is aOptionClass then Result := option as TOptionsClass;
end;
end;
function TOptionsContainer.GetSection<T>(const aSectionName : string = '') : T;
var
option : TOptions;
begin
Result := nil;
for option in fSections do
begin
if option is T then
begin
if (aSectionName.IsEmpty) or (CompareText(option.Name,aSectionName) = 0) then
begin
Result := option as T;
Exit;
end;
end;
end;
end;
function TOptionsContainer.GetSectionInterface<T>: IOptions<T>;
begin
Result := TOptionValue<T>.Create(Self.GetSection<T>);
end;
procedure TOptionsContainer.Load(aFailOnSectionNotExists : Boolean = False);
var
option : TOptions;
begin
if fSerializer.ConfigExists then
begin
if not fSerializer.Load(fSections,aFailOnSectionNotExists) then Save;
if not fLoaded then
begin
fLoaded := True;
if Assigned(fOnConfigLoaded) then fOnConfigLoaded;
end
else if Assigned(fOnConfigReloaded) then fOnConfigReloaded;
end
else
begin
//if config not exists get default values
for option in fSections do option.DefaultValues;
//saves default
Save;
end;
end;
procedure TOptionsContainer.LoadSection(aOptions : TOptions);
begin
if fSerializer.ConfigExists then
begin
if not fSerializer.LoadSection(fSections,aOptions) then Save;
end;
end;
procedure TOptionsContainer.Save;
begin
fSerializer.Save(fSections);
end;
{ TOptionsContainer }
constructor TFileOptionsContainer.Create(aOptionsSerializer : IFileOptionsSerializer; aReloadIfFileChanged : Boolean = False);
begin
inherited Create(aOptionsSerializer);
fFilename := aOptionsSerializer.Filename;
if aReloadIfFileChanged then CreateFileMonitor;
end;
procedure TFileOptionsContainer.Save;
var
laststate : Boolean;
begin
//disable filemonitor to avoid detect manual save as a external file change
if fReloadIfFileChanged then
begin
laststate := fFileMonitor.Enabled;
fFileMonitor.Enabled := False;
try
//save config file
inherited;
finally
//set last state
Sleep(0);
fFileMonitor.Enabled := laststate;
end;
end
else inherited;
end;
procedure TFileOptionsContainer.SetReloadIfFileChanged(const Value: Boolean);
begin
if Value = fReloadIfFileChanged then Exit;
fReloadIfFileChanged := Value;
if Assigned(fFileMonitor) then fFileMonitor.Free;
if fReloadIfFileChanged then CreateFileMonitor;
end;
procedure TFileOptionsContainer.CreateFileMonitor;
begin
fFileMonitor := TQuickFileMonitor.Create;
fFileMonitor.FileName := fFilename;
fFileMonitor.Interval := 2000;
fFileMonitor.Notifies := [TMonitorNotify.mnFileModified];
fFileMonitor.OnFileChange := FileModifiedNotify;
fFileMonitor.Enabled := True;
end;
destructor TFileOptionsContainer.Destroy;
begin
if Assigned(fFileMonitor) then fFileMonitor.Free;
inherited;
end;
procedure TFileOptionsContainer.FileModifiedNotify(MonitorNotify: TMonitorNotify);
begin
if MonitorNotify = TMonitorNotify.mnFileModified then
begin
if Assigned(fOnFileModified) then fOnFileModified;
if fReloadIfFileChanged then
begin
Load(False);
end;
end;
end;
function TFileOptionsContainer.GetFileSectionNames(out oSections : TArray<string>) : Boolean;
begin
Result := fSerializer.GetFileSectionNames(oSections);
end;
{ TOptions }
function TOptions.ConfigureOptions<T>(aOptionsFunc: TConfigureOptionsProc<T>): IOptionsValidator;
var
value : TValue;
begin
Result := TOptionsValidator.Create(Self);
if Assigned(aOptionsFunc) then
begin
value := Self;
aOptionsFunc(value.AsType<T>);
end;
end;
constructor TOptions.Create;
begin
fName := '';
fHideOptions := False;
end;
procedure TOptions.DefaultValues;
begin
//nothing
end;
procedure TOptions.DoValidateOptions;
var
ivalidator : IOptionsValidator;
begin
ivalidator := TOptionsValidator.Create(Self);
ivalidator.ValidateOptions;
end;
procedure TOptions.ValidateOptions;
begin
try
DoValidateOptions;
except
on E : Exception do
begin
raise EOptionConfigureError.CreateFmt('Validation Options Error : %s',[e.Message]);
end;
end;
end;
{ TOptionsValidator }
procedure TOptionsValidator.ValidateObject(aObj : TObject);
var
ctx : TRttiContext;
rtype : TRttiType;
rprop : TRttiProperty;
attrib : TCustomAttribute;
rvalue : TValue;
begin
rtype := ctx.GetType(aObj.ClassInfo);
for rprop in rtype.GetProperties do
begin
//check only published properties
if rprop.Visibility = TMemberVisibility.mvPublished then
begin
//check validation option attributes
for attrib in rprop.GetAttributes do
begin
if attrib is Required then ValidateRequired(aObj,rprop)
else if attrib is StringLength then ValidateStringLength(aObj,rprop,StringLength(attrib))
else if attrib is Range then ValidateRange(aObj,rprop,Range(attrib));
end;
rvalue := rprop.GetValue(aObj);
if not rvalue.IsEmpty then
begin
case rvalue.Kind of
tkClass : ValidateObject(rvalue.AsObject);
tkDynArray : ValidateArray(rvalue);
end;
end;
end;
end;
end;
constructor TOptionsValidator.Create(aOptions: TOptions);
begin
fOptions := aOptions;
end;
procedure TOptionsValidator.ValidateOptions;
begin
ValidateObject(fOptions);
end;
procedure TOptionsValidator.ValidateArray(aValue : TValue);
type
PPByte = ^PByte;
var
ctx : TRttiContext;
rDynArray : TRttiDynamicArrayType;
itvalue : TValue;
i : Integer;
begin
rDynArray := ctx.GetType(aValue.TypeInfo) as TRTTIDynamicArrayType;
for i := 0 to aValue.GetArrayLength - 1 do
begin
TValue.Make(PPByte(aValue.GetReferenceToRawData)^ + rDynArray.ElementType.TypeSize * i, rDynArray.ElementType.Handle,itvalue);
if not itvalue.IsEmpty then
begin
case itvalue.Kind of
tkClass : ValidateObject(itvalue.AsObject);
tkDynArray : ValidateArray(itvalue);
end;
end;
end;
end;
procedure TOptionsValidator.ValidateRange(const aInstance : TObject; aProperty: TRttiProperty; aValidation : Range);
var
value : TValue;
msg : string;
begin
value := aProperty.GetValue(aInstance);
if not value.IsEmpty then
begin
if value.Kind = tkFloat then
begin
if (value.AsExtended < aValidation.Min) or (value.AsExtended > aValidation.Max) then
begin
if aValidation.ErrorMsg.IsEmpty then msg := Format('Option %s "%s.%s" exceeds predefined range (%2f - %2f)',[fOptions.Name,aInstance.ClassName,aProperty.Name,aValidation.Min,aValidation.Max])
else msg := aValidation.ErrorMsg;
raise EOptionValidationError.Create(msg);
end;
end
else if value.Kind in [tkInteger,tkInt64] then
begin
if (value.AsInt64 < aValidation.Min) or (value.AsInt64 > aValidation.Max) then
begin
if aValidation.ErrorMsg.IsEmpty then msg := Format('Option %s "%s.%s" exceeds predefined range (%d - %d)',[fOptions.Name,aInstance.ClassName,aProperty.Name,Round(aValidation.Min),Round(aValidation.Max)])
else msg := aValidation.ErrorMsg;
raise EOptionValidationError.Create(msg);
end;
end;
end;
end;
procedure TOptionsValidator.ValidateRequired(const aInstance : TObject; aProperty: TRttiProperty);
begin
if aProperty.GetValue(aInstance).IsEmpty then raise EOptionValidationError.CreateFmt('Option %s "%s.%s" is required',[fOptions.Name,aInstance.ClassName,aProperty.Name]);
end;
procedure TOptionsValidator.ValidateStringLength(const aInstance : TObject; aProperty: TRttiProperty; aValidation : StringLength);
var
value : TValue;
msg : string;
begin
value := aProperty.GetValue(aInstance);
if (not value.IsEmpty) and (value.AsString.Length > aValidation.MaxLength) then
begin
if aValidation.ErrorMsg.IsEmpty then msg := Format('Option %s "%s.%s" exceeds max length (%d)',[fOptions.Name,aInstance.ClassName,aProperty.Name,aValidation.MaxLength])
else msg := aValidation.ErrorMsg;
raise EOptionValidationError.Create(msg);
end;
end;
{ Range }
constructor Range.Create(aMin, aMax: Integer; const aErrorMsg : string = '');
begin
fRangeMin := aMin;
fRangeMax := aMax;
fErrorMsg := aErrorMsg;
end;
constructor Range.Create(aMin, aMax: Double; const aErrorMsg: string);
begin
fRangeMin := aMin;
fRangeMax := aMax;
fErrorMsg := aErrorMsg;
end;
{ StringLength }
constructor StringLength.Create(aMaxLength: Integer; const aErrorMsg : string = '');
begin
fMaxLength := aMaxLength;
fErrorMsg := aErrorMsg;
end;
{ TOptionValue<T> }
constructor TOptionValue<T>.Create(aValue: T);
begin
fValue := aValue;
end;
function TOptionValue<T>.GetSectionValue: T;
begin
Result := fValue;
end;
{ TOptions<T> }
function TOptions<T>.ConfigureOptions(aOptionsFunc: TConfigureOptionsProc<T>): IOptionsValidator;
begin
if Assigned(aOptionsFunc) then Result := fOptions.ConfigureOptions<T>(aOptionsFunc)
else Result := TOptionsValidator.Create(fOptions);
end;
constructor TOptions<T>.Create(aOptions: T);
begin
fOptions := aOptions;
end;
{ TOptionsBuilder<T> }
constructor TOptionsBuilder<T>.Create;
begin
fOptions := (PTypeInfo(TypeInfo(T)).TypeData.ClassType.Create) as T;
end;
function TOptionsBuilder<T>.Options: T;
begin
Result := fOptions;
end;
{ TOptionsFileSerializer }
function TOptionsFileSerializer.GetFileName: string;
begin
Result := fFilename;
end;
procedure TOptionsFileSerializer.SetFileName(const aFilename: string);
begin
fFilename := aFilename;
end;
end.