aboutsummaryrefslogtreecommitdiffstats
path: root/Lua/src/lib/collections/CollWrappers.pas
blob: 513103a238c41e223ac4fce1e2dcb9e6471f9ff1 (plain) (blame)
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
unit CollWrappers;

(*****************************************************************************
 * Copyright 2003 by Matthew Greet
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details. (http://opensource.org/licenses/lgpl-license.php)
 *
 * See http://www.warmachine.u-net.com/delphi_collections for updates and downloads.
 *
 * $Version: v1.0.3 $
 * $Revision: 1.1.1.1 $
 * $Log: D:\QVCS Repositories\Delphi Collections\CollWrappers.qbt $
 * 
 *   Various primitive type wrappers, adapters and abstract base classes for
 *   natural items.
 * 
 * Revision 1.1.1.1  by: Matthew Greet  Rev date: 24/10/03 16:48:16
 *   v1.0 branch.
 * 
 * Revision 1.1  by: Matthew Greet  Rev date: 06/04/03 10:51:04
 *   Primitive type wrapper interfaces added. 
 *   Abstract, template classes added. 
 *   All classes implement reference counting by descending from
 *   TInterfacedObject. 
 *   
 * 
 * Revision 1.0  by: Matthew Greet  Rev date: 01/03/03 10:50:02
 *   Initial revision.
 * 
 * FPC compatibility fixes by: UltraStar Deluxe Team
 *
 * $Endlog$
 *****************************************************************************)

{$IFDEF FPC}
  {$MODE Delphi}{$H+}
{$ENDIF}
 
interface

uses
    SysUtils,
    Collections;

type
    IAssociationWrapper = interface
        ['{54DF42E0-64F2-11D7-8120-0002E3165EF8}']
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetKey: ICollectable;
        function GetValue: TObject;
        property AutoDestroy: Boolean read GetAutoDestroy write SetAutoDestroy;
        property Key: ICollectable read GetKey;
        property Value: TObject read GetValue;
    end;

    IBoolean = interface
        ['{62D1D160-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: Boolean;
        property Value: Boolean read GetValue;
    end;

    ICardinal = interface
        ['{6AF7B1C0-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: Cardinal;
        property Value: Cardinal read GetValue;
    end;

    IChar = interface
        ['{73AD00E0-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: Char;
        property Value: Char read GetValue;
    end;

    IClass = interface
        ['{7A84B660-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: TClass;
        property Value: TClass read GetValue;
    end;

    IDouble = interface
        ['{815C6BE0-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: Double;
        property Value: Double read GetValue;
    end;

    IInteger = interface
        ['{88ECC300-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: Integer;
        property Value: Integer read GetValue;
    end;

    IIntegerAssociationWrapper = interface
        ['{8F582220-64F2-11D7-8120-0002E3165EF8}']
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetKey: Integer;
        function GetValue: TObject;
        property AutoDestroy: Boolean read GetAutoDestroy write SetAutoDestroy;
        property Key: Integer read GetKey;
        property Value: TObject read GetValue;
    end;

    IInterfaceWrapper = interface
        ['{962E5100-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: IUnknown;
        property Value: IUnknown read GetValue;
    end;

    IObject = interface
        ['{9C675580-64F2-11D7-8120-0002E3165EF8}']
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetValue: TObject;
        property Value: TObject read GetValue;
    end;

    IString = interface
        ['{A420DF80-64F2-11D7-8120-0002E3165EF8}']
        function GetValue: String;
        property Value: String read GetValue;
    end;

    IStringAssociationWrapper = interface
        ['{AB98CCA0-64F2-11D7-8120-0002E3165EF8}']
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetKey: String;
        function GetValue: TObject;
        property AutoDestroy: Boolean read GetAutoDestroy write SetAutoDestroy;
        property Key: String read GetKey;
        property Value: TObject read GetValue;
    end;

    TAbstractItem = class(TInterfacedObject, ICollectable)
    public
        function GetInstance: TObject; virtual;
    end;

    TAbstractIntegerMappable = class(TAbstractItem, IEquatable, IIntegerMappable)
    private
        FKey: Integer;
    protected
        function MakeKey: Integer; virtual; abstract;
    public
        procedure AfterConstruction; override;
        function Equals(const Item: ICollectable): Boolean; virtual;
        function GetKey: Integer; virtual;
    end;

    TAbstractMappable = class(TAbstractItem, IEquatable, IMappable)
    private
        FKey: ICollectable;
    protected
        function MakeKey: ICollectable; virtual; abstract;
    public
        destructor Destroy; override;
        procedure AfterConstruction; override;
        function Equals(const Item: ICollectable): Boolean; virtual;
        function GetKey: ICollectable; virtual;
    end;

    TAbstractStringMappable = class(TAbstractItem, IEquatable, IStringMappable)
    private
        FKey: String;
    protected
        function MakeKey: String; virtual; abstract;
    public
        procedure AfterConstruction; override;
        function Equals(const Item: ICollectable): Boolean; virtual;
        function GetKey: String; virtual;
    end;

    TAssociationWrapper = class(TAbstractItem, IEquatable, IMappable, IAssociationWrapper)
    private
        FAutoDestroy: Boolean;
        FKey: ICollectable;
        FValue: TObject;
    public
        constructor Create(const Key: ICollectable; Value: TObject); overload;
        constructor Create(Key: Integer; Value: TObject); overload;
        constructor Create(Key: String; Value: TObject); overload;
        constructor Create(Key, Value: TObject; AutoDestroyKey: Boolean = true); overload;
        destructor Destroy; override;
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetKey: ICollectable;
        function GetValue: TObject;
        function Equals(const Item: ICollectable): Boolean;
        property AutoDestroy: Boolean read GetAutoDestroy write SetAutoDestroy;
        property Key: ICollectable read GetKey;
        property Value: TObject read GetValue;
    end;

    TBooleanWrapper = class(TAbstractItem, IEquatable, IHashable, IComparable, IBoolean)
    private
        FValue: Boolean;
    public
        constructor Create(Value: Boolean);
        function GetValue: Boolean;
        function CompareTo(const Item: ICollectable): Integer;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        property Value: Boolean read GetValue;
    end;

    TCardinalWrapper = class(TAbstractItem, IEquatable, IHashable, IComparable, ICardinal)
    private
        FValue: Cardinal;
    public
        constructor Create(Value: Cardinal);
        function GetValue: Cardinal;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        function CompareTo(const Item: ICollectable): Integer;
        property Value: Cardinal read GetValue;
    end;

    TCharWrapper = class(TAbstractItem, IEquatable, IHashable, IComparable, IChar)
    private
        FValue: Char;
    public
        constructor Create(Value: Char);
        function GetValue: Char;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        function CompareTo(const Item: ICollectable): Integer;
        property Value: Char read GetValue;
    end;

    TClassWrapper = class(TAbstractItem, IEquatable, IHashable, IClass)
    private
        FValue: TClass;
    public
        constructor Create(Value: TClass);
        function GetValue: TClass;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        property Value: TClass read GetValue;
    end;

    TDoubleWrapper = class(TAbstractItem, IEquatable, IHashable, IComparable, IDouble)
    private
        FValue: Double;
    public
        constructor Create(Value: Double);
        function GetValue: Double;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        function CompareTo(const Item: ICollectable): Integer;
        property Value: Double read GetValue;
    end;

    TIntegerWrapper = class(TAbstractItem, IEquatable, IHashable, IComparable, IInteger)
    private
        FValue: Integer;
    public
        constructor Create(Value: Integer);
        function GetValue: Integer;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        function CompareTo(const Item: ICollectable): Integer;
        property Value: Integer read GetValue;
    end;

    TIntegerAssociationWrapper = class(TAbstractItem, IEquatable, IIntegerMappable, IIntegerAssociationWrapper)
    private
        FAutoDestroy: Boolean;
        FKey: Integer;
        FValue: TObject;
    public
        constructor Create(const Key: Integer; Value: TObject); overload;
        destructor Destroy; override;
        function Equals(const Item: ICollectable): Boolean;
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetKey: Integer;
        function GetValue: TObject;
        property AutoDestroy: Boolean read GetAutoDestroy write SetAutoDestroy;
        property Key: Integer read GetKey;
        property Value: TObject read GetValue;
    end;

    TInterfaceWrapper = class(TAbstractItem, IHashable, IEquatable, IInterfaceWrapper)
    private
        FValue: IUnknown;
    public
        constructor Create(const Value: IUnknown);
        destructor Destroy; override;
        function GetValue: IUnknown;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        property Value: IUnknown read GetValue;
    end;

    TObjectWrapper = class(TAbstractItem, IEquatable, IComparable, IHashable, IObject)
    private
        FAutoDestroy: Boolean;
        FValue: TObject;
    public
        constructor Create(Value: TObject); overload;
        destructor Destroy; override;
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetValue: TObject;
        function CompareTo(const Item: ICollectable): Integer;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        property AutoDestroy: Boolean read FAutoDestroy write FAutoDestroy;
        property Value: TObject read GetValue;
    end;

    TStringWrapper = class(TAbstractItem, IEquatable, IHashable, IComparable, IString)
    private
        FValue: String;
    public
        constructor Create(Value: String);
        function GetValue: String;
        function Equals(const Item: ICollectable): Boolean;
        function HashCode: Integer;
        function CompareTo(const Item: ICollectable): Integer;
        property Value: String read FValue;
    end;

    TStringAssociationWrapper = class(TAbstractItem, IEquatable, IStringMappable, IStringAssociationWrapper)
    private
        FAutoDestroy: Boolean;
        FKey: String;
        FValue: TObject;
    public
        constructor Create(const Key: String; Value: TObject); overload;
        destructor Destroy; override;
        function GetAutoDestroy: Boolean;
        procedure SetAutoDestroy(Value: Boolean);
        function GetKey: String;
        function GetValue: TObject;
        function Equals(const Item: ICollectable): Boolean;
        property AutoDestroy: Boolean read GetAutoDestroy write SetAutoDestroy;
        property Key: String read GetKey;
        property Value: TObject read GetValue;
    end;

implementation

{ TAbstractItem }
function TAbstractItem.GetInstance: TObject;
begin
    Result := Self;
end;


{ TAbstractIntegerMappable }
procedure TAbstractIntegerMappable.AfterConstruction;
begin
    inherited AfterConstruction;
    FKey := MakeKey;
end;

function TAbstractIntegerMappable.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self = Item.GetInstance);
end;

function TAbstractIntegerMappable.GetKey: Integer;
begin
    Result := FKey;
end;

{ TAbstractMappable }
destructor TAbstractMappable.Destroy;
begin
    FKey := nil;
    inherited Destroy;
end;

procedure TAbstractMappable.AfterConstruction;
begin
    inherited AfterConstruction;
    FKey := MakeKey;
end;

function TAbstractMappable.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self = Item.GetInstance);
end;

function TAbstractMappable.GetKey: ICollectable;
begin
    Result := FKey;
end;

{ TAbstractStringMappable }
procedure TAbstractStringMappable.AfterConstruction;
begin
    inherited AfterConstruction;
    FKey := MakeKey;
end;

function TAbstractStringMappable.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self = Item.GetInstance);
end;

function TAbstractStringMappable.GetKey: String;
begin
    Result := FKey;
end;

{ TAssociationWrapper }
constructor TAssociationWrapper.Create(const Key: ICollectable; Value: TObject);
begin
    inherited Create;
    FAutoDestroy := true;
    FKey := Key;
    FValue := Value;
end;

constructor TAssociationWrapper.Create(Key: Integer; Value: TObject);
begin
    Create(TIntegerWrapper.Create(Key) as ICollectable, Value);
end;

constructor TAssociationWrapper.Create(Key: String; Value: TObject);
begin
    Create(TStringWrapper.Create(Key) as ICollectable, Value);
end;

constructor TAssociationWrapper.Create(Key, Value: TObject; AutoDestroyKey: Boolean);
var
    KeyWrapper: TObjectWrapper;
begin
    KeyWrapper := TObjectWrapper.Create(Key);
    KeyWrapper.AutoDestroy := AutoDestroyKey;
    Create(KeyWrapper as ICollectable, Value);
end;

destructor TAssociationWrapper.Destroy;
begin
    if FAutoDestroy then
        FValue.Free;
    FKey := nil;
    inherited Destroy;
end;

function TAssociationWrapper.GetAutoDestroy: Boolean;
begin
    Result := FAutoDestroy;
end;

procedure TAssociationWrapper.SetAutoDestroy(Value: Boolean);
begin
    FAutoDestroy := Value;
end;

function TAssociationWrapper.GetKey: ICollectable;
begin
    Result := FKey;
end;

function TAssociationWrapper.GetValue: TObject;
begin
    Result := FValue;
end;

function TAssociationWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TAssociationWrapper).Value)
end;

{ TCardinalWrapper }
constructor TCardinalWrapper.Create(Value: Cardinal);
begin
    inherited Create;
    FValue := Value;
end;

function TCardinalWrapper.GetValue: Cardinal;
begin
    Result := FValue;
end;

function TCardinalWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TCardinalWrapper).Value)
end;

function TCardinalWrapper.HashCode: Integer;
begin
    Result := FValue;
end;

function TCardinalWrapper.CompareTo(const Item: ICollectable): Integer;
var
    Value2: Cardinal;
begin
    Value2 := (Item.GetInstance as TCardinalWrapper).Value;
    if Value < Value2 then
        Result := -1
    else if Value > Value2 then
        Result := 1
    else
        Result := 0;
end;

{ TBooleanWrapper }
constructor TBooleanWrapper.Create(Value: Boolean);
begin
    inherited Create;
    FValue := Value;
end;

function TBooleanWrapper.GetValue: Boolean;
begin
    Result := FValue;
end;

function TBooleanWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TBooleanWrapper).Value)
end;

function TBooleanWrapper.HashCode: Integer;
begin
    Result := Ord(FValue);
end;

function TBooleanWrapper.CompareTo(const Item: ICollectable): Integer;
var
    Value2: Boolean;
begin
    Value2 := (Item.GetInstance as TBooleanWrapper).Value;
    if not Value and Value2 then
        Result := -1
    else if Value and not Value2 then
        Result := 1
    else
        Result := 0;
end;

{ TCharWrapper }
constructor TCharWrapper.Create(Value: Char);
begin
    inherited Create;
    FValue := Value;
end;

function TCharWrapper.GetValue: Char;
begin
    Result := FValue;
end;

function TCharWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TCharWrapper).Value)
end;

function TCharWrapper.HashCode: Integer;
begin
    Result := Integer(FValue);
end;

function TCharWrapper.CompareTo(const Item: ICollectable): Integer;
var
    Value2: Char;
begin
    Value2 := (Item.GetInstance as TCharWrapper).Value;
    if Value < Value2 then
        Result := -1
    else if Value > Value2 then
        Result := 1
    else
        Result := 0;
end;

{ TClassWrapper }
constructor TClassWrapper.Create(Value: TClass);
begin
    inherited Create;
    FValue := Value;
end;

function TClassWrapper.GetValue: TClass;
begin
    Result := FValue;
end;

function TClassWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TClassWrapper).Value)
end;

function TClassWrapper.HashCode: Integer;
begin
    Result := Integer(FValue.ClassInfo);
end;

{ TDoubleWrapper }
constructor TDoubleWrapper.Create(Value: Double);
begin
    inherited Create;
    FValue := Value;
end;

function TDoubleWrapper.GetValue: Double;
begin
    Result := FValue;
end;

function TDoubleWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TDoubleWrapper).Value)
end;

function TDoubleWrapper.HashCode: Integer;
var
    DblAsInt: array[0..1] of Integer;
begin
    Double(DblAsInt) := Value;
    Result := DblAsInt[0] xor DblAsInt[1];
end;

function TDoubleWrapper.CompareTo(const Item: ICollectable): Integer;
var
    Value2: Double;
begin
    Value2 := (Item.GetInstance as TDoubleWrapper).Value;
    if Value < Value2 then
        Result := -1
    else if Value > Value2 then
        Result := 1
    else
        Result := 0;
end;

{ TIntegerWrapper }
constructor TIntegerWrapper.Create(Value: Integer);
begin
    inherited Create;
    FValue := Value;
end;

function TIntegerWrapper.GetValue: Integer;
begin
    Result := FValue;
end;

function TIntegerWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TIntegerWrapper).Value)
end;

function TIntegerWrapper.HashCode: Integer;
begin
    Result := FValue;
end;

function TIntegerWrapper.CompareTo(const Item: ICollectable): Integer;
var
    Value2: Integer;
begin
    Value2 := (Item.GetInstance as TIntegerWrapper).Value;
    if Value < Value2 then
        Result := -1
    else if Value > Value2 then
        Result := 1
    else
        Result := 0;
end;

{ TIntegerAssociationWrapper }
constructor TIntegerAssociationWrapper.Create(const Key: Integer; Value: TObject);
begin
    inherited Create;
    FAutoDestroy := true;
    FKey := Key;
    FValue := Value;
end;

destructor TIntegerAssociationWrapper.Destroy;
begin
    if FAutoDestroy then
        FValue.Free;
    inherited Destroy;
end;

function TIntegerAssociationWrapper.GetAutoDestroy: Boolean;
begin
    Result := FAutoDestroy;
end;

procedure TIntegerAssociationWrapper.SetAutoDestroy(Value: Boolean);
begin
    FAutoDestroy := Value;
end;

function TIntegerAssociationWrapper.GetValue: TObject;
begin
    Result := FValue;
end;

function TIntegerAssociationWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TIntegerAssociationWrapper).Value)
end;

function TIntegerAssociationWrapper.GetKey: Integer;
begin
    Result := FKey;
end;

{ TStringAssociationWrapper }
constructor TStringAssociationWrapper.Create(const Key: String; Value: TObject);
begin
    inherited Create;
    FAutoDestroy := true;
    FKey := Key;
    FValue := Value;
end;

destructor TStringAssociationWrapper.Destroy;
begin
    if FAutoDestroy then
        FValue.Free;
    inherited Destroy;
end;

function TStringAssociationWrapper.GetAutoDestroy: Boolean;
begin
    Result := FAutoDestroy;
end;

procedure TStringAssociationWrapper.SetAutoDestroy(Value: Boolean);
begin
    FAutoDestroy := Value;
end;

function TStringAssociationWrapper.GetValue: TObject;
begin
    Result := FValue;
end;

function TStringAssociationWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TStringAssociationWrapper).Value)
end;

function TStringAssociationWrapper.GetKey: String;
begin
    Result := FKey;
end;

{ TInterfaceWrapper }
constructor TInterfaceWrapper.Create(const Value: IUnknown);
begin
    inherited Create;
    FValue := Value;
end;

destructor TInterfaceWrapper.Destroy;
begin
    FValue := nil;
    inherited Destroy;
end;

function TInterfaceWrapper.GetValue: IUnknown;
begin
    Result := FValue;
end;

function TInterfaceWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TInterfaceWrapper).Value)
end;

function TInterfaceWrapper.HashCode: Integer;
begin
    Result := Integer(Pointer(FValue));
end;

{ TObjectWrapper }
constructor TObjectWrapper.Create(Value: TObject);
begin
    inherited Create;
    FAutoDestroy := true;
    FValue := Value;
end;

destructor TObjectWrapper.Destroy;
begin
    if FAutoDestroy then
        FValue.Free;
    inherited Destroy;
end;

function TObjectWrapper.GetAutoDestroy: Boolean;
begin
    Result := FAutoDestroy;
end;

procedure TObjectWrapper.SetAutoDestroy(Value: Boolean);
begin
    FAutoDestroy := Value;
end;

function TObjectWrapper.GetValue: TObject;
begin
    Result := FValue;
end;

function TObjectWrapper.CompareTo(const Item: ICollectable): Integer;
var
    Value1, Value2: Integer;
begin
    Value1 := Integer(Pointer(Self));
    if Item <> nil then
        Value2 := Integer(Pointer(Item))
    else
        Value2 := Low(Integer);
    if (Value1 < Value2) then
        Result := -1
    else if (Value1 > Value2) then
        Result := 1
    else
        Result := 0;
end;

function TObjectWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TObjectWrapper).Value)
end;

function TObjectWrapper.HashCode: Integer;
begin
    Result := Integer(Pointer(FValue));
end;

{ TStringWrapper }
constructor TStringWrapper.Create(Value: String);
begin
    inherited Create;
    FValue := Value;
end;

function TStringWrapper.GetValue: String;
begin
    Result := FValue;
end;

function TStringWrapper.Equals(const Item: ICollectable): Boolean;
begin
    Result := (Self.Value = (Item.GetInstance as TStringWrapper).Value)
end;

function TStringWrapper.HashCode: Integer;
var
    I: Integer;
begin
    Result := 0;
    for I := 1 to Length(FValue) do
        Result := (Result shl 1) xor Ord(FValue[I]);
end;

function TStringWrapper.CompareTo(const Item: ICollectable): Integer;
begin
    Result := CompareStr(Self.Value, (Item.GetInstance as TStringWrapper).Value)
end;


end.