-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimpleNamedValues.pas
1577 lines (1342 loc) · 55.3 KB
/
SimpleNamedValues.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
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
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
Simple Named Values
This library provides a class that implements a list of name-value pairs.
It is intended for passing data to objects or functions when the type and
amount of data cannot be known at compile-time, or there is too many
arguments to be passed as function parameters. It is similar to passing
an array of variants, but is more structured and reliable, as the data do
not need to be ordered.
It should NOT be used for long-term data storage or when the data are
frequently accessed (access is not optimized for speed).
Only some basic types are implemented, but by using typecasting and dynamic
memory allocation, practically any data can be stored.
Note on type nvtBuffer (general memory buffers):
Values of this type are stored and accessed using structure TSNVBuffer
and are copy-on-assign.
When you assign buffer to a named value, it is not directly stored,
instead a copy is made and this copy is stored. This means you can then
free or otherwise invalidate the source.
When retrieving this value type, again a copy is made and this copy is
then returned. You have to free the returned buffer using function
FreeNamedValueBuffer after you are done using it.
Internal storage of the buffer is managed automatically but can be
directly accessed using properties BufferValueMemory and BufferValueSize
(both read-only).
The named values list can be used in two ways - as a normal object instance,
where you are responsible for creation and destruction of the object, or
in mode in-here called transient instancing.
Normal mode is clear enough, typical use should be:
- create an instance of TSimpleNamedValues
- fill it with required data (values)
- pass it to function/class that requires this data
- the function/class should copy the passed data to a local storage
and/or use them immediately
- after return from the function/method, free the instance and with it
the data
In transient instancing, the object is created internally by function
TransientNamedValues, containing values passed to this function, and
only special interface (ITransientSimpleNamedValues) is returned.
This interface is passed to a function/method requiring the data. This
function will use method Implementor, defined by the interface, to obtain
the actual named value list (object of type TSimpleNamedValues) and
continue to use this list the same way as in normal mode.
Since the implementor is reference counted, it will be, thanks to automatic
release of interfaces, freed after the function accepting the interface
exits (note that the destruction might not happed right after the exit, but
after all variables, including implicit/hidden ones, will go out of scope).
Therefore, in this mode, you are not responsible for managing instances of
the named value list.
Version 1.3.5 (2024-05-03)
Last change 2024-05-03
©2020-2024 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.SimpleNamedValues
Dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
Library AuxExceptions is required only when rebasing local exception classes
(see symbol SimpleNamedValues_UseAuxExceptions for details).
Library AuxExceptions might also be required as an indirect dependency.
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit SimpleNamedValues;
{
SimpleNamedValues_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
SimpleNamedValues_UseAuxExceptions to achieve this.
}
{$IF Defined(SimpleNamedValues_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils,
AuxTypes, AuxClasses{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ESNVException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
ESNVIndexOutOfBounds = class(ESNVException);
ESNVInvalidValue = class(ESNVException);
ESNVUnknownNamedValue = class(ESNVException);
ESNVValueTypeMismatch = class(ESNVException);
ESNVDuplicateValue = class(ESNVException);
ESNVInvalidValueType = class(ESNVException);
{===============================================================================
--------------------------------------------------------------------------------
TSimpleNamedValues
--------------------------------------------------------------------------------
===============================================================================}
type
TSNVBuffer = record
Memory: Pointer;
Size: TMemSize;
end;
Function NamedValueBuffer(Memory: Pointer; Size: TMemSize): TSNVBuffer;
procedure FreeNamedValueBuffer(var Buffer: TSNVBuffer);
//------------------------------------------------------------------------------
type
TSNVNamedValueType = (nvtBool,nvtInteger,nvtInt64,nvtFloat,nvtDateTime,
nvtCurrency,nvtString,nvtPointer,nvtGUID,nvtBuffer);
TSNVNamedValue = record
Name: String;
Changed: Boolean; // internal field
case ValueType: TSNVNamedValueType of
nvtBool: (BoolValue: Boolean);
nvtInteger: (IntegerValue: Integer);
nvtInt64: (Int64Value: Int64);
nvtFloat: (FloatValue: Extended);
nvtDateTime: (DateTimeValue: TDateTime);
nvtCurrency: (CurrencyValue: Currency);
nvtString: (StringValue: PChar);
nvtPointer: (PointerValue: Pointer);
nvtGUID: (GUIDValue: TGUID);
nvtBuffer: (BufferValue: TSNVBuffer)
end;
{===============================================================================
TSimpleNamedValues - class declaration
===============================================================================}
type
TSimpleNamedValues = class(TCustomListObject)
protected
fValues: array of TSNVNamedValue;
fCount: Integer;
fUpdateCounter: Integer;
fChanged: Boolean;
fOnChangeEvent: TNotifyEvent;
fOnChangeCallback: TNotifyCallback;
fOnValueChangeEvent: TIntegerEvent;
fOnValueChangeCallback: TIntegerCallback;
// getters/setters
Function GetValue(Index: Integer): TSNVNamedValue; virtual;
// value getters/setters
Function GetBoolValue(const Name: String): Boolean; virtual;
procedure SetBoolValue(const Name: String; Value: Boolean); virtual;
Function GetIntegerValue(const Name: String): Integer; virtual;
procedure SetIntegerValue(const Name: String; Value: Integer); virtual;
Function GetInt64Value(const Name: String): Int64; virtual;
procedure SetInt64Value(const Name: String; Value: Int64); virtual;
Function GetFloatValue(const Name: String): Extended; virtual;
procedure SetFloatValue(const Name: String; Value: Extended); virtual;
Function GetDateTimeValue(const Name: String): TDateTime; virtual;
procedure SetDateTimeValue(const Name: String; Value: TDateTime); virtual;
Function GetCurrencyValue(const Name: String): Currency; virtual;
procedure SetCurrencyValue(const Name: String; Value: Currency); virtual;
Function GetStringValue(const Name: String): String; virtual;
procedure SetStringValue(const Name: String; const Value: String); virtual;
Function GetPointerValue(const Name: String): Pointer; virtual;
procedure SetPointerValue(const Name: String; Value: Pointer); virtual;
Function GetGUIDValue(const Name: String): TGUID; virtual;
procedure SetGUIDValue(const Name: String; Value: TGUID); virtual;
Function GetBufferValue(const Name: String): TSNVBuffer; virtual;
procedure SetBufferValue(const Name: String; Value: TSNVBuffer); virtual;
Function GetBufferValueMemory(const Name: String): Pointer; virtual;
Function GetBufferValueSize(const Name: String): TMemSize; virtual;
// list methods
Function GetCapacity: Integer; override;
procedure SetCapacity(Value: Integer); override;
Function GetCount: Integer; override;
procedure SetCount(Value: Integer); override;
// change reporting
procedure DoChange; virtual;
procedure DoValueChange(Index: Integer); virtual;
// utility
Function PrepareValue(const Name: String; ValueType: TSNVNamedValueType): Integer; virtual;
class procedure InitializeNamedValue(var NamedValue: TSNVNamedValue); virtual;
class procedure FinalizeNamedValue(var NamedValue: TSNVNamedValue); virtual;
public
constructor Create;
destructor Destroy; override;
procedure BeginUpdate; virtual;
procedure EndUpdate; virtual;
Function LowIndex: Integer; override;
Function HighIndex: Integer; override;
Function IndexOf(const Name: String): Integer; overload; virtual;
Function IndexOf(const Name: String; ValueType: TSNVNamedValueType): Integer; overload; virtual;
Function Find(const Name: String; out Index: Integer): Boolean; overload; virtual;
Function Find(const Name: String; ValueType: TSNVNamedValueType; out Index: Integer): Boolean; overload; virtual;
Function Exists(const Name: String): Boolean; overload; virtual;
Function Exists(const Name: String; ValueType: TSNVNamedValueType): Boolean; overload; virtual;
Function Add(const Name: String; ValueType: TSNVNamedValueType): Integer; virtual;
procedure Append(Values: TSimpleNamedValues); virtual;
procedure Insert(Index: Integer; const Name: String; ValueType: TSNVNamedValueType); virtual;
procedure Move(SrcIdx,DstIdx: Integer); virtual;
procedure Exchange(Idx1,Idx2: Integer); virtual;
Function Remove(const Name: String): Integer; virtual;
procedure Delete(Index: Integer); virtual;
procedure Clear; virtual;
property Values[Index: Integer]: TSNVNamedValue read GetValue; default;
// values access
property BoolValue[const Name: String]: Boolean read GetBoolValue write SetBoolValue;
property IntegerValue[const Name: String]: Integer read GetIntegerValue write SetIntegerValue;
property Int64Value[const Name: String]: Int64 read GetInt64Value write SetInt64Value;
property FloatValue[const Name: String]: Extended read GetFloatValue write SetFloatValue;
property DateTimeValue[const Name: String]: TDateTime read GetDateTimeValue write SetDateTimeValue;
property CurrencyValue[const Name: String]: Currency read GetCurrencyValue write SetCurrencyValue;
property StringValue[const Name: String]: String read GetStringValue write SetStringValue;
property PointerValue[const Name: String]: Pointer read GetPointerValue write SetPointerValue;
property GUIDValue[const Name: String]: TGUID read GetGUIDValue write SetGUIDValue;
property BufferValue[const Name: String]: TSNVBuffer read GetBufferValue write SetBufferValue;
property BufferValueMemory[const Name: String]: Pointer read GetBufferValueMemory;
property BufferValueSize[const Name: String]: TMemSize read GetBufferValueSize;
// events, callbacks
property OnChange: TNotifyEvent read fOnChangeEvent write fOnChangeEvent;
property OnChangeEvent: TNotifyEvent read fOnChangeEvent write fOnChangeEvent;
property OnChangeCallback: TNotifyCallback read fOnChangeCallback write fOnChangeCallback;
property OnValueChange: TIntegerEvent read fOnValueChangeEvent write fOnValueChangeEvent;
property OnValueChangeEvent: TIntegerEvent read fOnValueChangeEvent write fOnValueChangeEvent;
property OnValueChangeCallback: TIntegerCallback read fOnValueChangeCallback write fOnValueChangeCallback;
end;
{===============================================================================
--------------------------------------------------------------------------------
Transient instancing
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Transient instancing - declaration
===============================================================================}
const
IID_TransientSimpleNamedValues: TGUID = '{E0AD5430-0B30-49C6-8EBC-E28310DFF011}';
type
ITransientSimpleNamedValues = interface(IInterface)
['{E0AD5430-0B30-49C6-8EBC-E28310DFF011}']
Function Implementor: TSimpleNamedValues;
end;
//------------------------------------------------------------------------------
{
TSNVNamedValueContainer and following functions should only be used to pass
values to function TransientNamedValues, nowhere else.
}
type
TSNVNamedValueContainer = record
Name: String;
StringValue: String;
case ValueType: TSNVNamedValueType of
nvtBool: (BoolValue: Boolean);
nvtInteger: (IntegerValue: Integer);
nvtInt64: (Int64Value: Int64);
nvtFloat: (FloatValue: Extended);
nvtDateTime: (DateTimeValue: TDateTime);
nvtCurrency: (CurrencyValue: Currency);
nvtPointer: (PointerValue: Pointer);
nvtGUID: (GUIDValue: TGUID);
nvtBuffer: (BufferValue: TSNVBuffer)
end;
Function BoolNamedValue(const Name: String; const Value: Boolean): TSNVNamedValueContainer;
Function IntegerNamedValue(const Name: String; const Value: Integer): TSNVNamedValueContainer;
Function Int64NamedValue(const Name: String; const Value: Int64): TSNVNamedValueContainer;
Function FloatNamedValue(const Name: String; const Value: Extended): TSNVNamedValueContainer;
Function DateTimeNamedValue(const Name: String; const Value: TDateTime): TSNVNamedValueContainer;
Function CurrencyNamedValue(const Name: String; const Value: Currency): TSNVNamedValueContainer;
Function StringNamedValue(const Name: String; const Value: String): TSNVNamedValueContainer;
Function PointerNamedValue(const Name: String; const Value: Pointer): TSNVNamedValueContainer;
Function GUIDNamedValue(const Name: String; const Value: TGUID): TSNVNamedValueContainer;
Function BufferNamedValue(const Name: String; const Value: TSNVBuffer): TSNVNamedValueContainer;
//------------------------------------------------------------------------------
Function TransientNamedValues(const NamedValues: array of TSNVNamedValueContainer): ITransientSimpleNamedValues; overload;
Function TransientNamedValues(const NamedValue: TSNVNamedValueContainer): ITransientSimpleNamedValues; overload;
{===============================================================================
--------------------------------------------------------------------------------
Named values access
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Named values access - declaration
===============================================================================}
{
Following functions are here to simplify access to named values.
Each function checks whether the NamedValues list is assigned. When it is,
it then checks existence of requested named value and, when found, assigns
its value to a passed variable.
If the value is assigned, the function returns true.
When the list is not assigned or the requested value is not present, the
function will return false and the passed variable is left unchanged (its
value is preserved).
}
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Boolean): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Integer): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Int64): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Extended): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: TDateTime): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Currency): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: String): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Pointer): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: TGUID): Boolean; overload;
Function GetNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: TSNVBuffer): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Int8): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: UInt8): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Int16): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: UInt16): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Int32): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: UInt32): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Int64): Boolean; overload;
Function GetIntegerNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: UInt64): Boolean; overload;
Function GetFloatNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Single): Boolean; overload;
Function GetFloatNamedValue(NamedValues: TSimpleNamedValues; const Name: String; var Value: Double): Boolean; overload;
implementation
uses
{$IF not Defined(FPC) and Defined(Windows)}Windows,{$IFEND} Variants;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5024:={$WARN 5024 OFF}} // Parameter "$1" not used
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TSimpleNamedValues
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSimpleNamedValues - auxiliary functions
===============================================================================}
Function NamedValueBuffer(Memory: Pointer; Size: TMemSize): TSNVBuffer;
begin
Result.Memory := Memory;
Result.Size := Size;
end;
//------------------------------------------------------------------------------
procedure FreeNamedValueBuffer(var Buffer: TSNVBuffer);
begin
FreeMem(Buffer.Memory,Buffer.Size);
Buffer.Memory := nil;
Buffer.Size := 0;
end;
{===============================================================================
TSimpleNamedValues - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSimpleNamedValues - protected methods
-------------------------------------------------------------------------------}
Function TSimpleNamedValues.GetValue(Index: Integer): TSNVNamedValue;
begin
If CheckIndex(Index) then
Result := fValues[Index]
else
raise ESNVIndexOutOfBounds.CreateFmt('TSimpleNamedValues.GetValue: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetBoolValue(const Name: String): Boolean;
var
Index: Integer;
begin
If Find(Name,nvtBool,Index) then
Result := fValues[Index].BoolValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetBool: Unknown bool value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetBoolValue(const Name: String; Value: Boolean);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtBool);
fValues[Index].BoolValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetIntegerValue(const Name: String): Integer;
var
Index: Integer;
begin
If Find(Name,nvtInteger,Index) then
Result := fValues[Index].IntegerValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetIntegerValue: Unknown integer value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetIntegerValue(const Name: String; Value: Integer);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtInteger);
fValues[Index].IntegerValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetInt64Value(const Name: String): Int64;
var
Index: Integer;
begin
If Find(Name,nvtInt64,Index) then
Result := fValues[Index].Int64Value
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetInt64Value: Unknown int64 value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetInt64Value(const Name: String; Value: Int64);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtInt64);
fValues[Index].Int64Value := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetFloatValue(const Name: String): Extended;
var
Index: Integer;
begin
If Find(Name,nvtFloat,Index) then
Result := fValues[Index].FloatValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetFloatValue: Unknown float value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetFloatValue(const Name: String; Value: Extended);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtFloat);
fValues[Index].FloatValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetDateTimeValue(const Name: String): TDateTime;
var
Index: Integer;
begin
If Find(Name,nvtDateTime,Index) then
Result := fValues[Index].DateTimeValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetDateTimeValue: Unknown datetime value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetDateTimeValue(const Name: String; Value: TDateTime);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtDateTime);
fValues[Index].DateTimeValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetCurrencyValue(const Name: String): Currency;
var
Index: Integer;
begin
If Find(Name,nvtCurrency,Index) then
Result := fValues[Index].CurrencyValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetCurrencyValue: Unknown currency value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetCurrencyValue(const Name: String; Value: Currency);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtCurrency);
fValues[Index].CurrencyValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetStringValue(const Name: String): String;
var
Index: Integer;
begin
If Find(Name,nvtString,Index) then
Result := fValues[Index].StringValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetStringValue: Unknown textual value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetStringValue(const Name: String; const Value: String);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtString);
with fValues[Index] do
begin
If Assigned(StringValue) then
StrDispose(StringValue);
StringValue := StrNew(PChar(Value));
end;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetPointerValue(const Name: String): Pointer;
var
Index: Integer;
begin
If Find(Name,nvtPointer,Index) then
Result := fValues[Index].PointerValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetPointerValue: Unknown pointer value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetPointerValue(const Name: String; Value: Pointer);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtPointer);
fValues[Index].PointerValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetGUIDValue(const Name: String): TGUID;
var
Index: Integer;
begin
If Find(Name,nvtGUID,Index) then
Result := fValues[Index].GUIDValue
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetGUIDValue: Unknown guid value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetGUIDValue(const Name: String; Value: TGUID);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtGUID);
fValues[Index].GUIDValue := Value;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetBufferValue(const Name: String): TSNVBuffer;
var
Index: Integer;
begin
If Find(Name,nvtBuffer,Index) then
begin
Result.Size := fValues[Index].BufferValue.Size;
GetMem(Result.Memory,Result.Size);
System.Move(fValues[Index].BufferValue.Memory^,Result.Memory^,Result.Size);
end
else raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetBufferValue: Unknown buffer value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetBufferValue(const Name: String; Value: TSNVBuffer);
var
Index: Integer;
begin
Index := PrepareValue(Name,nvtBuffer);
with fValues[Index] do
begin
If Assigned(BufferValue.Memory) then
FreeNamedValueBuffer(BufferValue);
BufferValue.Size := Value.Size;
GetMem(BufferValue.Memory,BufferValue.Size);
System.Move(Value.Memory^,BufferValue.Memory^,BufferValue.Size);
end;
DoValueChange(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetBufferValueMemory(const Name: String): Pointer;
var
Index: Integer;
begin
If Find(Name,nvtBuffer,Index) then
Result := fValues[Index].BufferValue.Memory
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetBufferValueMemory: Unknown buffer value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetBufferValueSize(const Name: String): TMemSize;
var
Index: Integer;
begin
If Find(Name,nvtBuffer,Index) then
Result := fValues[Index].BufferValue.Size
else
raise ESNVUnknownNamedValue.CreateFmt('TSimpleNamedValues.GetBufferValueSize: Unknown buffer value "%s".',[Name]);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetCapacity: Integer;
begin
Result := Length(fValues);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.SetCapacity(Value: Integer);
var
i: Integer;
begin
If Value >= 0 then
begin
If Value <> Length(fValues) then
begin
If Value < fCount then
For i := Value to HighIndex do
FinalizeNamedValue(fValues[i]);
SetLength(fValues,Value);
If Value < fCount then
begin
fCount := Value;
DoChange;
end;
end;
end
else raise ESNVInvalidValue.CreateFmt('TSimpleNamedValues.SetCapacity: Invalid capacity value (%d).',[Value]);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.GetCount: Integer;
begin
Result := fCount;
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5024{$ENDIF}
procedure TSimpleNamedValues.SetCount(Value: Integer);
begin
// do nothing
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.DoChange;
begin
fChanged := True;
If fUpdateCounter <= 0 then
begin
If Assigned(fOnChangeEvent) then
fOnChangeEvent(Self)
else If Assigned(fOnChangeCallback) then
fOnChangeCallback(Self);
end
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.DoValueChange(Index: Integer);
var
i: Integer;
begin
If CheckIndex(Index) then
begin
If fUpdateCounter <= 0 then
begin
If Assigned(fOnValueChangeEvent) then
fOnValueChangeEvent(Self,Index)
else If Assigned(fOnValueChangeCallback) then
fOnValueChangeCallback(Self,Index);
end
else fValues[Index].Changed := True;
end
else
begin
// report all changed values
If (fUpdateCounter <= 0) and (Assigned(fOnValueChangeEvent) or Assigned(fOnValueChangeCallback)) then
For i := LowIndex to HighIndex do
If fValues[i].Changed then
begin
If Assigned(fOnValueChangeEvent) then
fOnValueChangeEvent(Self,i);
If Assigned(fOnValueChangeCallback) then
fOnValueChangeCallback(Self,i);
fValues[i].Changed := False;
end;
end;
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.PrepareValue(const Name: String; ValueType: TSNVNamedValueType): Integer;
begin
// do create-on-write
If Find(Name,Result) then
begin
// value with a proper name was found, check type
If fValues[Result].ValueType <> ValueType then
raise ESNVValueTypeMismatch.CreateFmt('TSimpleNamedValues.PrepareValue: Wrong value type (%d) for "%s".',[Ord(ValueType),Name]);
end
// value not found, add it
else Result := Add(Name,ValueType);
end;
//------------------------------------------------------------------------------
class procedure TSimpleNamedValues.InitializeNamedValue(var NamedValue: TSNVNamedValue);
begin
NamedValue.Name := '';
FillChar(NamedValue,SizeOf(TSNVNamedValue),0);
end;
//------------------------------------------------------------------------------
class procedure TSimpleNamedValues.FinalizeNamedValue(var NamedValue: TSNVNamedValue);
begin
NamedValue.Name := '';
If NamedValue.ValueType = nvtString then
begin
StrDispose(NamedValue.StringValue);
NamedValue.StringValue := nil;
end;
If NamedValue.ValueType = nvtBuffer then
FreeNamedValueBuffer(NamedValue.BufferValue);
end;
{-------------------------------------------------------------------------------
TSimpleNamedValues - public methods
-------------------------------------------------------------------------------}
constructor TSimpleNamedValues.Create;
begin
inherited Create;
SetLength(fValues,0);
fCount := 0;
fUpdateCounter := 0;
fChanged := False;
fOnChangeEvent := nil;
fOnChangeCallback := nil;
fOnValueChangeEvent := nil;
fOnValueChangeCallback := nil;
end;
//------------------------------------------------------------------------------
destructor TSimpleNamedValues.Destroy;
begin
// prevent change reporting
fOnChangeEvent := nil;
fOnChangeCallback := nil;
fOnValueChangeEvent := nil;
fOnValueChangeCallback := nil;
Clear;
inherited;
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.BeginUpdate;
begin
If fUpdateCounter <= 0 then
fChanged := False;
Inc(fUpdateCounter);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.EndUpdate;
begin
Dec(fUpdateCounter);
If fUpdateCounter <= 0 then
begin
fUpdateCounter := 0;
If fChanged then
DoChange;
DoValueChange(-1);
fChanged := False;
end;
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.LowIndex: Integer;
begin
Result := Low(fValues);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.HighIndex: Integer;
begin
Result := Pred(fCount);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.IndexOf(const Name: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := LowIndex to HighIndex do
If AnsiSameText(Name,fValues[i].Name) then
begin
Result := i;
Break{For i};
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TSimpleNamedValues.IndexOf(const Name: String; ValueType: TSNVNamedValueType): Integer;
begin
Result := IndexOf(Name);
If CheckIndex(Result) then
If fValues[Result].ValueType <> ValueType then
Result := -1;
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.Find(const Name: String; out Index: Integer): Boolean;
begin
Index := IndexOf(Name);
Result := CheckIndex(Index);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TSimpleNamedValues.Find(const Name: String; ValueType: TSNVNamedValueType; out Index: Integer): Boolean;
begin
Index := IndexOf(Name,ValueType);
Result := CheckIndex(Index);
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.Exists(const Name: String): Boolean;
begin
Result := CheckIndex(IndexOf(Name));
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function TSimpleNamedValues.Exists(const Name: String; ValueType: TSNVNamedValueType): Boolean;
begin
Result := CheckIndex(IndexOf(Name,ValueType));
end;
//------------------------------------------------------------------------------
Function TSimpleNamedValues.Add(const Name: String; ValueType: TSNVNamedValueType): Integer;
begin
If not Find(Name,Result) then
begin
Grow;
InitializeNamedValue(fValues[fCount]);
fValues[fCount].Name := Name;
fValues[fCount].ValueType := ValueType;
Result := fCount;
Inc(fCount);
DoChange;
end
else raise ESNVDuplicateValue.CreateFmt('TSimpleNamedValues.Add: Value "%s" already exists.',[Name]);
end;
//------------------------------------------------------------------------------
procedure TSimpleNamedValues.Append(Values: TSimpleNamedValues);
var
i: Integer;
Index: Integer;
begin
// first check for duplicitites
For i := Values.LowIndex to Values.HighIndex do
If Find(Values[i].Name,Index) then
raise ESNVDuplicateValue.CreateFmt('TSimpleNamedValues.Add: Value "%s" already exists.',[Values[i].Name]);
// add values
For i := Values.LowIndex to Values.HighIndex do
begin
Index := Add(Values[i].Name,Values[i].ValueType);
If CheckIndex(Index) then
case fValues[Index].ValueType of
nvtBool: fValues[Index].BoolValue := Values[i].BoolValue;
nvtInteger: fValues[Index].IntegerValue := Values[i].IntegerValue;
nvtInt64: fValues[Index].Int64Value := Values[i].Int64Value;
nvtFloat: fValues[Index].FloatValue := Values[i].FloatValue;
nvtDateTime: fValues[Index].DateTimeValue := Values[i].DateTimeValue;
nvtCurrency: fValues[Index].CurrencyValue := Values[i].CurrencyValue;
nvtString: fValues[Index].StringValue := StrNew(PChar(Values[i].StringValue));
nvtPointer: fValues[Index].PointerValue := Values[i].PointerValue;
nvtGUID: fValues[Index].GUIDValue := Values[i].GUIDValue;
nvtBuffer: begin
fValues[Index].BufferValue.Size := Values[i].BufferValue.Size;
GetMem(fValues[Index].BufferValue.Memory,fValues[Index].BufferValue.Size);
System.Move(Values[i].BufferValue.Memory^,
fValues[Index].BufferValue.Memory^,
fValues[Index].BufferValue.Size);
end;
else
raise ESNVInvalidValueType.CreateFmt('TSimpleNamedValues.Add: Invalid value type (%d).',[Ord(fValues[Index].ValueType)]);