1 /*=========================================================================
2 
3   Program: GDCM (Grassroots DICOM). A DICOM library
4 
5   Copyright (c) 2006-2011 Mathieu Malaterre
6   All rights reserved.
7   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 
9      This software is distributed WITHOUT ANY WARRANTY; without even
10      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11      PURPOSE.  See the above copyright notice for more information.
12 
13 =========================================================================*/
14 #include "gdcmSmartPointer.h"
15 #include "gdcmObject.h"
16 
17 #include <iostream>
18 
19 using gdcm::Object;
20 using gdcm::SmartPointer;
21 
22 class Foo : public Object {
23   public:
foo()24    void foo() { // Does exist in Object as far as I know :)
25      std::cout << "foo" << std::endl;
26    }
27 };
28 
29 class Containter {
30 public:
Containter()31   Containter():Instance(nullptr) {}
32   SmartPointer<Object> Instance;
33 };
34 
Fill(SmartPointer<Foo> & p)35 void Fill(SmartPointer<Foo> &p)
36 {
37   SmartPointer<Foo> in = new Foo;
38   // p = in;
39   Foo & rp = *in;
40   p = &rp;
41 }
42 
43 SmartPointer<Foo> gf;
44 
TestReturn(int i)45 SmartPointer<Foo> TestReturn(int i)
46 {
47   static int n = 0;
48   if( !n )
49     {
50     ++n;
51     gf = new Foo;
52     }
53 
54   if( i == 0 )
55     {
56     return gf;
57     }
58   else if( i == 1 )
59     {
60     SmartPointer<Foo> f = new Foo;
61     return f;
62     }
63   else if( i == 2 )
64     {
65     return new Foo;
66     }
67   return nullptr;
68 }
69 
70 //class Object2 : public Foo {};
71 
TestSmartPointer(int,char * [])72 int TestSmartPointer(int, char *[])
73 {
74   SmartPointer<Object> p = new Object;
75   SmartPointer<Foo> p2 = new Foo;
76   p2->foo();
77   SmartPointer<Object> p3 = new Foo;
78   //p3->foo(); // should not compile
79 
80   //std::cout << p << std::endl;
81   //std::cout << p2 << std::endl;
82   //std::cout << p3 << std::endl;
83 
84   if( p == p2
85    || p == p3
86    || p2 == p3 )
87     {
88     return 1;
89     }
90 
91   // SmartPointer
92   SmartPointer<Foo> p4 = p2;
93   SmartPointer<Object> p5 = p3;
94 
95   // Pointer:
96   SmartPointer<Foo> p6 = &(*p2);
97 
98   SmartPointer<Foo> p7;
99   Fill(p7);
100   Foo &foo = *p7;
101   foo.foo();
102 
103   Containter c1;
104   Containter c2;
105   c2 = c1;
106 
107   // TODO:
108   //SmartPointer<Object> s = new Foo;
109   //delete s;
110 
111   for(int i = 0; i < 5; ++i)
112     {
113     SmartPointer<Foo> f = TestReturn(i);
114     if( f )
115       {
116       f->foo();
117       }
118     }
119 
120   return 0;
121 }
122