File: strong.cpp

package info (click to toggle)
libloki 0.1.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,888 kB
  • ctags: 5,535
  • sloc: cpp: 22,174; ansic: 1,955; makefile: 359; php: 316; perl: 108
file content (954 lines) | stat: -rw-r--r-- 32,403 bytes parent folder | download
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
////////////////////////////////////////////////////////////////////////////////
// Test program for The Loki Library
// Copyright (c) 2006 Richard Sposato
// Permission to use, copy, modify, distribute and sell this software for any 
//     purpose is hereby granted without fee, provided that the above copyright 
//     notice appear in all copies and that both that copyright notice and this 
//     permission notice appear in supporting documentation.
// The authors make no representations about the 
//     suitability of this software for any purpose. It is provided "as is" 
//     without express or implied warranty.
////////////////////////////////////////////////////////////////////////////////

// $Header: /cvsroot/loki-lib/loki/test/SmartPtr/strong.cpp,v 1.6 2006/05/17 16:23:39 syntheticpp Exp $

// ----------------------------------------------------------------------------

#include <loki/StrongPtr.h>

#include <iostream>
#include <cassert>

#include "base.h"


// ----------------------------------------------------------------------------

using namespace std;
using namespace Loki;


// ----------------------------------------------------------------------------

/// Used to check if SmartPtr can be used with a forward-reference.
class Thingy;

typedef Loki::StrongPtr< Thingy, true, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    Thingy_DeleteSingle_ptr;

typedef Loki::StrongPtr< Thingy, true, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteUsingFree, DontPropagateConst >
    Thingy_DeleteUsingFree_ptr;

typedef Loki::StrongPtr< Thingy, true, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteNothing, DontPropagateConst >
    Thingy_DeleteNothing_ptr;


// ----------------------------------------------------------------------------

class Earth;
class Moon;

typedef Loki::StrongPtr< Earth, false, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    Earth_WeakPtr;

typedef Loki::StrongPtr< Earth, true, TwoRefCounts, DisallowConversion,
    AssertCheck, AllowReset, DeleteSingle, DontPropagateConst >
    Earth_StrongPtr;

typedef Loki::StrongPtr< Moon, false, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    Moon_WeakPtr;

typedef Loki::StrongPtr< Moon, true, TwoRefCounts, DisallowConversion,
    AssertCheck, AllowReset, DeleteSingle, DontPropagateConst >
    Moon_StrongPtr;

// ----------------------------------------------------------------------------

class Earth
{
public:

    Earth( void ) : m_moon()
    {
        s_constructions++;
    }

    ~Earth( void )
    {
        s_destructions++;
    }

    void SetMoon( const Moon_StrongPtr & p );

    void SetMoon( const Moon_WeakPtr & p );

    static inline bool AllDestroyed( void )
    {
        return ( s_constructions == s_destructions );
    }

    static inline bool ExtraConstructions( void )
    {
        return ( s_constructions > s_destructions );
    }

    static inline bool ExtraDestructions( void )
    {
        return ( s_constructions < s_destructions );
    }

    static inline unsigned int GetCtorCount( void )
    {
        return s_constructions;
    }

    static inline unsigned int GetDtorCount( void )
    {
        return s_destructions;
    }

private:
    /// Not implemented.
    Earth( const Earth & );
    /// Not implemented.
    Earth & operator = ( const Earth & );

    static unsigned int s_constructions;
    static unsigned int s_destructions;

    Moon_WeakPtr m_moon;
};

unsigned int Earth::s_constructions = 0;
unsigned int Earth::s_destructions = 0;

// ----------------------------------------------------------------------------

class Moon
{
public:

    Moon( void ) : m_earth()
    {
        s_constructions++;
    }

    ~Moon( void )
    {
        s_destructions++;
    }

    void SetEarth( const Earth_WeakPtr & p );

    void SetEarth( const Earth_StrongPtr & p );

    static inline bool AllDestroyed( void )
    {
        return ( s_constructions == s_destructions );
    }

    static inline bool ExtraConstructions( void )
    {
        return ( s_constructions > s_destructions );
    }

    static inline bool ExtraDestructions( void )
    {
        return ( s_constructions < s_destructions );
    }

    static inline unsigned int GetCtorCount( void )
    {
        return s_constructions;
    }

    static inline unsigned int GetDtorCount( void )
    {
        return s_destructions;
    }

private:
    /// Not implemented.
    Moon( const Moon & );
    /// Not implemented.
    Moon & operator = ( const Moon & );

    static unsigned int s_constructions;
    static unsigned int s_destructions;
    Earth_WeakPtr m_earth;
};

unsigned int Moon::s_constructions = 0;
unsigned int Moon::s_destructions = 0;

// ----------------------------------------------------------------------------

void Moon::SetEarth( const Earth_WeakPtr & p )
{
    m_earth = p;
}

// ----------------------------------------------------------------------------

void Moon::SetEarth( const Earth_StrongPtr & p )
{
    m_earth = p;
}

// ----------------------------------------------------------------------------

void Earth::SetMoon( const Moon_WeakPtr & p )
{
    m_moon = p;
}

// ----------------------------------------------------------------------------

void Earth::SetMoon( const Moon_StrongPtr & p )
{
    m_moon = p;
}

// ----------------------------------------------------------------------------

typedef Loki::StrongPtr< BaseClass, false, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr;

typedef Loki::StrongPtr< BaseClass, true, TwoRefCounts, DisallowConversion,
    NoCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr;

typedef Loki::StrongPtr< BaseClass, false, TwoRefLinks, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    NonConstBase_WeakLink_NoConvert_Assert_NoPropagate_ptr;

typedef Loki::StrongPtr< BaseClass, true, TwoRefLinks, DisallowConversion,
    NoCheck, CantResetWithStrong, DeleteSingle, DontPropagateConst >
    NonConstBase_StrongLink_NoConvert_NoCheck_NoPropagate_ptr;

typedef Loki::StrongPtr< BaseClass, false, TwoRefCounts, DisallowConversion,
    AssertCheck, AllowReset, DeleteSingle, DontPropagateConst >
    NonConstBase_WeakCount_NoConvert_Assert_Reset_NoPropagate_ptr;

typedef Loki::StrongPtr< BaseClass, true, TwoRefCounts, DisallowConversion,
    NoCheck, AllowReset, DeleteSingle, DontPropagateConst >
    NonConstBase_StrongCount_NoConvert_NoCheck_Reset_NoPropagate_ptr;

/// @note Used for const propagation tests.
typedef Loki::StrongPtr< const BaseClass, true, TwoRefCounts, DisallowConversion,
    NoCheck, CantResetWithStrong, DeleteSingle, PropagateConst >
    ConstBase_StrongCount_NoConvert_NoCheck_Propagate_ptr;

typedef Loki::StrongPtr< const BaseClass, false, TwoRefCounts, DisallowConversion,
    AssertCheck, CantResetWithStrong, DeleteSingle, PropagateConst >
    ConstBase_WeakCount_NoConvert_Assert_Propagate_ptr;

// ----------------------------------------------------------------------------

void DoStrongRefCountTests( void )
{

    BaseClass * pNull = NULL; (void)pNull;
    const unsigned int ctorCount = BaseClass::GetCtorCount(); (void)ctorCount;
    const unsigned int dtorCount = BaseClass::GetDtorCount(); (void)dtorCount;
    assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w0;
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s0;
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w1( w0 );

        // Copy from weak to strong is available.
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s1( w0 );
        // Assignment from weak to strong is available.
        s1 = w1;

        // Converting from strong to weak is available.
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w2( s0 );
        // Assignment from strong to weak is available.
        w1 = s1;

        assert( !s0 );
        assert( !s1 );
        assert( !w0 );
        assert( !w1 );
        assert( s1 == pNull );
        assert( s0 == pNull );
        assert( w1 == pNull );
        assert( w1 == pNull );
        assert( pNull == s0 );
        assert( pNull == s1 );
        assert( pNull == w0 );
        assert( pNull == w1 );
        assert( s0 == s0 );
        assert( s1 == s1 );
        assert( w0 == w0 );
        assert( w1 == w1 );
        assert( s1 == s0 );
        assert( s0 == s1 );
        assert( w0 == s0 );
        assert( s0 == w0 );
        assert( w0 == w1 );
        assert( w1 == w0 );
        assert( w0 == w1 );
        assert( w1 == s1 );
        assert( s1 == w1 );
        assert( s1 <= s0 );
        assert( s1 >= s0 );
        assert( s0 <= s1 );
        assert( s0 >= s1 );
        assert( w0 <= s0 );
        assert( w0 >= s0 );
        assert( s0 <= w0 );
        assert( s0 >= w0 );
        assert( w1 <= w0 );
        assert( w1 >= w0 );
        assert( w0 <= w1 );
        assert( w0 >= w1 );
        assert( !( s1 < s0 ) );
        assert( !( s1 > s0 ) );
        assert( !( s0 < s1 ) );
        assert( !( s0 > s1 ) );
        assert( !( w0 < s0 ) );
        assert( !( w0 > s0 ) );
        assert( !( s0 < w0 ) );
        assert( !( s0 > w0 ) );
        assert( !( w1 < w0 ) );
        assert( !( w1 > w0 ) );
        assert( !( w0 < w1 ) );
        assert( !( w0 > w1 ) );
        assert( !( w0 < pNull ) );
        assert( !( w0 > pNull ) );
        assert( !( pNull < w0 ) );
        assert( !( pNull > w0 ) );
    }
    assert( ctorCount == BaseClass::GetCtorCount() );
    assert( dtorCount == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w1( new BaseClass );
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w2( new BaseClass );
        assert( w1 != w2 );
        assert( w1 );
        assert( w2 );
        w1 = w2;
        assert( w1 == w2 );
        assert( w1 );
        assert( w2 );
        assert( dtorCount + 1 == BaseClass::GetDtorCount() );
    }
    assert( ctorCount + 2 == BaseClass::GetCtorCount() );
    assert( dtorCount + 2 == BaseClass::GetDtorCount() );

    {
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s1( new BaseClass );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s2( new BaseClass );
        assert( s1 != s2 );
        assert( s1 );
        assert( s2 );
        s1 = s2;
        assert( s1 == s2 );
        assert( s1 );
        assert( s2 );
        assert( dtorCount + 3 == BaseClass::GetDtorCount() );
    }
    assert( ctorCount + 4 == BaseClass::GetCtorCount() );
    assert( dtorCount + 4 == BaseClass::GetDtorCount() );
    assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w1( new BaseClass );
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w2( new BaseClass );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s1( w1 );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s2( w2 );

        // prove basic stuff.
        assert( w1 != w2 );
        assert( s1 != s2 );
        assert( s1 == w1 );
        assert( s2 == w2 );
        assert( s1 );
        assert( s2 );
        assert( w1 );
        assert( w2 );

        // prove a weak pointer can be re-assigned to another without affecting
        // any strong co-pointers. and that no objects were released.
        w1 = w2;  // w1 == w2 == s2   s1
        assert( w1 == w2 );
        assert( s1 != s2 );
        assert( s1 != w1 );
        assert( s1 != w2 );
        assert( s2 == w1 );
        assert( w1 );
        assert( w2 );
        assert( s1 );
        assert( s2 );
        assert( dtorCount + 4 == BaseClass::GetDtorCount() );

        // Prove they all point to same thing.
        s1 = s2;  // w1 == w2 == s2 == s1
        // and prove that one of them released the object.
        assert( dtorCount + 5 == BaseClass::GetDtorCount() );
        assert( w1 == w2 );
        assert( s1 == s2 );
        assert( s1 == w1 );
        assert( s1 == w2 );
        assert( s2 == w1 );
        assert( w1 );
        assert( w2 );
        assert( s1 );
        assert( s2 );
    }
    assert( ctorCount + 6 == BaseClass::GetCtorCount() );
    assert( dtorCount + 6 == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w1( new BaseClass );
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w2( new BaseClass );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s1( w1 );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s2( w2 );

        // prove basic stuff.  w1 == s1   w2 == s2
        assert( w1 != w2 );
        assert( s1 != s2 );
        assert( s1 == w1 );
        assert( s2 == w2 );
        assert( s1 );
        assert( s2 );
        assert( w1 );
        assert( w2 );

        // prove a strong pointer can be re-assigned to another weak pointer,
        // and that any weak co-pointers released the object.
        s1 = w2;  // s1 == w2 == s2   w1
        assert( w1 != w2 );
        assert( s1 == s2 );
        assert( s1 != w1 );
        assert( s1 == w2 );
        assert( s2 != w1 );
        assert( !w1 );
        assert( w2 );
        assert( s1 );
        assert( s2 );
        assert( dtorCount + 7 == BaseClass::GetDtorCount() );

        // Prove that when strong pointer is re-assigned, object
        // is not destroyed if another strong co-pointer exists.
        s1 = w1;  // w1 == s1   w2 == s2
        // and prove that none of them released an object.
        assert( dtorCount + 7 == BaseClass::GetDtorCount() );
        assert( w1 != w2 );
        assert( s1 != s2 );
        assert( s1 == w1 );
        assert( s2 == w2 );
        assert( !s1 );
        assert( s2 );
        assert( !w1 );
        assert( w2 );
    }
    assert( ctorCount + 8 == BaseClass::GetCtorCount() );
    assert( dtorCount + 8 == BaseClass::GetDtorCount() );
}

// ----------------------------------------------------------------------------

void DoStrongConstTests( void )
{

    const unsigned int ctorCount = BaseClass::GetCtorCount(); (void)ctorCount;
    const unsigned int dtorCount = BaseClass::GetDtorCount(); (void)dtorCount;
    assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w1( new BaseClass );
        const NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w2( w1 );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s1( w1 );
        const NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s2( w2 );

        const BaseClass & cbw1 = *w1;
        cbw1.DoThat();
        const BaseClass & cbw2 = *w2;
        cbw2.DoThat();
        const BaseClass & cbs1 = *s1;
        cbs1.DoThat();
        const BaseClass & cbs2 = *s2;
        cbs2.DoThat();

        BaseClass & bw1 = *w1;
        bw1.DoThat();
        BaseClass & bw2 = *w2;
        bw2.DoThat();
        BaseClass & bs1 = *s1;
        bs1.DoThat();
        BaseClass & bs2 = *s2;
        bs2.DoThat();
    }
    assert( ctorCount + 1 == BaseClass::GetCtorCount() );
    assert( dtorCount + 1 == BaseClass::GetDtorCount() );

    {
        ConstBase_WeakCount_NoConvert_Assert_Propagate_ptr w1( new BaseClass );
        const ConstBase_WeakCount_NoConvert_Assert_Propagate_ptr w2( w1 );
        ConstBase_StrongCount_NoConvert_NoCheck_Propagate_ptr s1( w1 );
        const ConstBase_StrongCount_NoConvert_NoCheck_Propagate_ptr s2( w2 );

        const BaseClass & cbw1 = *w1;
        cbw1.DoThat();
        const BaseClass & cbw2 = *w2;
        cbw2.DoThat();
        const BaseClass & cbs1 = *s1;
        cbs1.DoThat();
        const BaseClass & cbs2 = *s2;
        cbs2.DoThat();

        /** @note These are illegal because constness is propagated by the
         StrongPtr's policy.  Your compiler should produce error messages if
         you attempt to compile these lines.
         */
        //BaseClass & bw1 = *w1;
        //bw1.DoThat();
        //BaseClass & bw2 = *w2;
        //bw2.DoThat();
        //BaseClass & bs1 = *s1;
        //bs1.DoThat();
        //BaseClass & bs2 = *s2;
        //bs2.DoThat();
    }
    assert( ctorCount + 2 == BaseClass::GetCtorCount() );
    assert( dtorCount + 2 == BaseClass::GetDtorCount() );
}

// ----------------------------------------------------------------------------

void DoStrongReleaseTests( void )
{

    BaseClass * pNull = NULL; (void)pNull;
    const unsigned int ctorCount = BaseClass::GetCtorCount(); (void)ctorCount;
    const unsigned int dtorCount = BaseClass::GetDtorCount(); (void)dtorCount;
    assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );

    {
        // These are tests of pointers that don't allow reset or release if
        // there is at least 1 strong pointer.  Ironically, this means that
        // if only 1 strong pointer holds onto an object, and you call Release
        // using that strong pointer, it can't release itself.

        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w1( new BaseClass );
        NonConstBase_WeakCount_NoConvert_Assert_NoPropagate_ptr w2( new BaseClass );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s1( w1 );
        NonConstBase_StrongCount_NoConvert_NoCheck_NoPropagate_ptr s2( w2 );

        // Prove that neither weak nor strong pointers can be
        // released if any co-pointer is strong.
        bool released = ReleaseAll( w2, pNull );
        assert( !released );

        released = ReleaseAll( w1, pNull );
        assert( !released );

        released = ReleaseAll( s1, pNull );
        assert( !released );

        released = ReleaseAll( s2, pNull );
        assert( !released );

        // Prove that weak and strong pointers can be reset only
        // if stored pointer matches parameter pointer - or there
        // are no strong co-pointers.
        bool reset = ResetAll( w2, pNull );
        assert( !reset );

        reset = ResetAll( w1, pNull );
        assert( !reset );

        reset = ResetAll( s1, pNull );
        assert( !reset );

        reset = ResetAll( s2, pNull );
        assert( !reset );

        s2 = pNull;
        assert( dtorCount + 1 == BaseClass::GetDtorCount() );
        reset = ResetAll( w2, pNull );
        assert( reset );

        reset = ResetAll( w1, pNull );
        assert( !reset );

        reset = ResetAll( s1, pNull );
        assert( !reset );

        reset = ResetAll( s2, pNull );
        assert( reset );
    }
    assert( ctorCount + 2 == BaseClass::GetCtorCount() );
    assert( dtorCount + 2 == BaseClass::GetDtorCount() );

    {
        // These are tests of pointers that do allow reset and release even
        // if a strong pointer exists.

        NonConstBase_WeakCount_NoConvert_Assert_Reset_NoPropagate_ptr w1( new BaseClass );
        NonConstBase_StrongCount_NoConvert_NoCheck_Reset_NoPropagate_ptr w2( new BaseClass );
        NonConstBase_WeakCount_NoConvert_Assert_Reset_NoPropagate_ptr s1( w1 );
        NonConstBase_StrongCount_NoConvert_NoCheck_Reset_NoPropagate_ptr s2( w2 );

        BaseClass * thing = NULL;
        bool released = ReleaseAll( w2, thing );
        assert( released );
        assert( NULL != thing );
        delete thing;
        assert( dtorCount + 3 == BaseClass::GetDtorCount() );

        released = ReleaseAll( s1, thing );
        assert( released );
        assert( NULL != thing );
        delete thing;
        assert( dtorCount + 4 == BaseClass::GetDtorCount() );

        released = ReleaseAll( w1, thing );
        assert( released );
        assert( NULL == thing );

        released = ReleaseAll( s2, thing );
        assert( released );
        assert( NULL == thing );

        // Prove that weak and strong pointers can be reset
        // only if stored pointer matches parameter pointer
        // - even if there are strong co-pointers.
        bool reset = ResetAll( w2, pNull );
        assert( reset );

        reset = ResetAll( w1, pNull );
        assert( reset );

        reset = ResetAll( s1, pNull );
        assert( reset );

        reset = ResetAll( s2, pNull );
        assert( reset );
        assert( ctorCount + 4 == BaseClass::GetCtorCount() );
        assert( dtorCount + 4 == BaseClass::GetDtorCount() );

        s2 = new BaseClass;
        s1 = new BaseClass;
        reset = ResetAll( w2, pNull );
        assert( reset );

        reset = ResetAll( w1, pNull );
        assert( reset );
    }
    assert( ctorCount + 6 == BaseClass::GetCtorCount() );
    assert( dtorCount + 6 == BaseClass::GetDtorCount() );
}

// ----------------------------------------------------------------------------

void DoStrongRefLinkTests( void )
{

    BaseClass * pNull = NULL; (void)pNull;
    const unsigned int ctorCount = BaseClass::GetCtorCount(); (void)ctorCount;
    const unsigned int dtorCount = BaseClass::GetDtorCount(); (void)dtorCount;
    assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakLink_NoConvert_Assert_NoPropagate_ptr w0;
        NonConstBase_WeakLink_NoConvert_Assert_NoPropagate_ptr w1;
        NonConstBase_StrongLink_NoConvert_NoCheck_NoPropagate_ptr s0;
        NonConstBase_StrongLink_NoConvert_NoCheck_NoPropagate_ptr s1;
        assert( !s0 );
        assert( !s1 );
        assert( s0 == s1 );
        assert( s1 == pNull );
        assert( s0 == pNull );
        assert( pNull == s0 );
        assert( pNull == s1 );
        assert( s1 == s0 );
        assert( s1 == s1 );
        assert( s0 == s0 );
        assert( s0 == s1 );
        assert( s1 <= s0 );
        assert( s1 >= s0 );
        assert( s0 <= s1 );
        assert( s0 >= s1 );
        assert( !( s1 < s0 ) );
        assert( !( s1 > s0 ) );
        assert( !( s0 < s1 ) );
        assert( !( s0 > s1 ) );
        assert( !w0 );
        assert( !w1 );
        assert( w0 == pNull );
        assert( s0 == pNull );
        assert( pNull == s0 );
        assert( pNull == w0 );
        assert( w0 == s0 );
        assert( w0 == w0 );
        assert( s0 == s0 );
        assert( s0 == w0 );
        assert( w0 <= s0 );
        assert( w0 >= s0 );
        assert( s0 <= w0 );
        assert( s0 >= w0 );
        assert( !( w0 < s0 ) );
        assert( !( w0 > s0 ) );
        assert( !( s0 < w0 ) );
        assert( !( s0 > w0 ) );
        assert( w0 == w1 );
        assert( w1 == pNull );
        assert( w0 == pNull );
        assert( pNull == w0 );
        assert( pNull == w1 );
        assert( w1 == w0 );
        assert( w1 == w1 );
        assert( w0 == w0 );
        assert( w0 == w1 );
        assert( w1 <= w0 );
        assert( w1 >= w0 );
        assert( w0 <= w1 );
        assert( w0 >= w1 );
        assert( !( w1 < w0 ) );
        assert( !( w1 > w0 ) );
        assert( !( w0 < w1 ) );
        assert( !( w0 > w1 ) );
    }
    assert( ctorCount == BaseClass::GetCtorCount() );
    assert( dtorCount == BaseClass::GetDtorCount() );

    {
        NonConstBase_WeakLink_NoConvert_Assert_NoPropagate_ptr w3( new BaseClass );
        NonConstBase_WeakLink_NoConvert_Assert_NoPropagate_ptr w4( new BaseClass );
        assert( w3 != w4 );
        assert( w3 );
        assert( w4 );
        w3 = w4;
        assert( w3 == w4 );
        assert( w3 );
        assert( w4 );
        assert( dtorCount + 1 == BaseClass::GetDtorCount() );
    }
    assert( ctorCount + 2 == BaseClass::GetCtorCount() );
    assert( dtorCount + 2 == BaseClass::GetDtorCount() );

    {
        NonConstBase_StrongLink_NoConvert_NoCheck_NoPropagate_ptr s3( new BaseClass );
        NonConstBase_StrongLink_NoConvert_NoCheck_NoPropagate_ptr s4( new BaseClass );
        assert( s3 != s4 );
        assert( s3 );
        assert( s4 );
        s3 = s4;
        assert( s3 == s4 );
        assert( s3 );
        assert( s4 );
        assert( dtorCount + 3 == BaseClass::GetDtorCount() );
    }
    assert( ctorCount + 4 == BaseClass::GetCtorCount() );
    assert( dtorCount + 4 == BaseClass::GetDtorCount() );
    assert( BaseClass::GetCtorCount() == BaseClass::GetDtorCount() );

}

// ----------------------------------------------------------------------------

void DoWeakCycleTests( void )
{

    const unsigned int ctorCountMoon = Moon::GetCtorCount(); (void)ctorCountMoon;
    const unsigned int dtorCountMoon = Moon::GetDtorCount(); (void)dtorCountMoon;
    assert( Moon::AllDestroyed() );
    const unsigned int ctorCountEarth = Earth::GetCtorCount(); (void)ctorCountEarth;
    const unsigned int dtorCountEarth = Earth::GetDtorCount(); (void)dtorCountEarth;
    assert( Earth::AllDestroyed() );

    {
        Earth_WeakPtr ew0;
        Moon_WeakPtr mw0;
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon );
    assert( Moon::GetDtorCount() == dtorCountMoon );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth );
    assert( Earth::GetDtorCount() == dtorCountEarth );

    {
        Earth_WeakPtr ew1( new Earth );
    }
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+1 );
    assert( Earth::GetDtorCount() == dtorCountEarth+1 );

    {
        Moon_WeakPtr mw1( new Moon );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+1 );
    assert( Moon::GetDtorCount() == dtorCountMoon+1 );

    {
        Earth_WeakPtr ew1( new Earth );
        Moon_WeakPtr mw1( new Moon );
        ew1->SetMoon( mw1 );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+2 );
    assert( Moon::GetDtorCount() == dtorCountMoon+2 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+2 );
    assert( Earth::GetDtorCount() == dtorCountEarth+2 );

    {
        Earth_WeakPtr ew1( new Earth );
        Moon_WeakPtr mw1( new Moon );
        mw1->SetEarth( ew1 );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+3 );
    assert( Moon::GetDtorCount() == dtorCountMoon+3 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+3 );
    assert( Earth::GetDtorCount() == dtorCountEarth+3 );

    {
        Earth_WeakPtr ew1( new Earth );
        Moon_WeakPtr mw1( new Moon );
        ew1->SetMoon( mw1 );
        mw1->SetEarth( ew1 );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+4 );
    assert( Moon::GetDtorCount() == dtorCountMoon+4 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+4 );
    assert( Earth::GetDtorCount() == dtorCountEarth+4 );

    {
        Earth_StrongPtr es1( new Earth );
        Moon_StrongPtr ms1( new Moon );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+5 );
    assert( Moon::GetDtorCount() == dtorCountMoon+5 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+5 );
    assert( Earth::GetDtorCount() == dtorCountEarth+5 );

    {
        Earth_StrongPtr es1( new Earth );
        Moon_StrongPtr ms1( new Moon );
        es1->SetMoon( ms1 );
        ms1->SetEarth( es1 );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+6 );
    assert( Moon::GetDtorCount() == dtorCountMoon+6 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+6 );
    assert( Earth::GetDtorCount() == dtorCountEarth+6 );

    {
        Earth_StrongPtr es1( new Earth );
        Moon_StrongPtr ms1( new Moon );
        {
            Earth_WeakPtr ew1( es1 );
            Moon_WeakPtr mw1( ms1 );
            ew1->SetMoon( mw1 );
            mw1->SetEarth( ew1 );
        }
        // Note that dtor counts have not changed from previous test.
        assert( Moon::GetCtorCount() == ctorCountMoon+7 );
        assert( Moon::GetDtorCount() == dtorCountMoon+6 );
        assert( Earth::GetCtorCount() == ctorCountEarth+7 );
        assert( Earth::GetDtorCount() == dtorCountEarth+6 );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+7 );
    assert( Moon::GetDtorCount() == dtorCountMoon+7 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+7 );
    assert( Earth::GetDtorCount() == dtorCountEarth+7 );

    {
        Earth_StrongPtr es1;
        Moon_StrongPtr ms1;
        {
            Earth_WeakPtr ew1( new Earth );
            Moon_WeakPtr mw1( new Moon );
            ew1->SetMoon( mw1 );
            mw1->SetEarth( ew1 );
            es1 = ew1;
            ms1 = mw1;
        }
        // Note that dtor counts have not changed from previous test.
        assert( Moon::GetCtorCount() == ctorCountMoon+8 );
        assert( Moon::GetDtorCount() == dtorCountMoon+7 );
        assert( Earth::GetCtorCount() == ctorCountEarth+8 );
        assert( Earth::GetDtorCount() == dtorCountEarth+7 );
    }
    assert( Moon::AllDestroyed() );
    assert( Moon::GetCtorCount() == ctorCountMoon+8 );
    assert( Moon::GetDtorCount() == dtorCountMoon+8 );
    assert( Earth::AllDestroyed() );
    assert( Earth::GetCtorCount() == ctorCountEarth+8 );
    assert( Earth::GetDtorCount() == dtorCountEarth+8 );
}

// ----------------------------------------------------------------------------

void DoStrongForwardReferenceTest( void )
{
    /** @note These lines should cause the compiler to make a warning message
     about attempting to delete an undefined type.  They should also cause
     an error message about a negative subscript since 
     */
    //Thingy_DeleteSingle_ptr p1;
    //Thingy_DeleteSingle_ptr p2( p1 );
    //Thingy_DeleteSingle_ptr p3;
    //p3 = p1;

    /** @note These lines should cause the compiler to make an error message
     about attempting to call the destructor for an undefined type.
     */
    //Thingy_DeleteUsingFree_ptr p4;
    //Thingy_DeleteUsingFree_ptr p5( p4 );
    //Thingy_DeleteUsingFree_ptr p6;
    //p6 = p4;

    /** @note These lines should cause the compiler to make neither a warning
     nor an error message even though the type is undefined.
     */
    Thingy_DeleteNothing_ptr p7;
    Thingy_DeleteNothing_ptr p8( p7 );
    Thingy_DeleteNothing_ptr p9;
    p9 = p7;
}

// ----------------------------------------------------------------------------

// $Log: strong.cpp,v $
// Revision 1.6  2006/05/17 16:23:39  syntheticpp
// remove gcc warnings
//
// Revision 1.5  2006/04/19 01:04:25  rich_sposato
// Changed DeleteSingle and DeleteArray policy to not allow use of incomplete
// types.
//
// Revision 1.4  2006/04/18 07:29:41  lfittl
// - Various makefile improvements (better mingw support, easier to add new sources)
// - Include loki/StrongPtr.hpp, not Loki/StrongPtr.hpp (test/SmartPtr)
//
// Revision 1.3  2006/04/07 16:27:11  vizowl
// adding an XCode build project
//
// Revision 1.2  2006/04/06 18:19:58  rich_sposato
// Added CVS Log keyword.
//