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
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "gdcmSmartPointer.h"
#include "gdcmObject.h"
#include <iostream>
using gdcm::Object;
using gdcm::SmartPointer;
class Foo : public Object {
public:
void foo() { // Does exist in Object as far as I know :)
std::cout << "foo" << std::endl;
}
};
class Container {
public:
Container():Instance(nullptr) {}
SmartPointer<Object> Instance;
};
void Fill(SmartPointer<Foo> &p)
{
SmartPointer<Foo> in = new Foo;
// p = in;
Foo & rp = *in;
p = &rp;
}
static SmartPointer<Foo> gf;
SmartPointer<Foo> TestReturn(int i)
{
static int n = 0;
if( !n )
{
++n;
gf = new Foo;
}
if( i == 0 )
{
return gf;
}
else if( i == 1 )
{
SmartPointer<Foo> f = new Foo;
return f;
}
else if( i == 2 )
{
return new Foo;
}
return nullptr;
}
//class Object2 : public Foo {};
int TestSmartPointer(int, char *[])
{
SmartPointer<Object> p = new Object;
SmartPointer<Foo> p2 = new Foo;
p2->foo();
SmartPointer<Object> p3 = new Foo;
//p3->foo(); // should not compile
//std::cout << p << std::endl;
//std::cout << p2 << std::endl;
//std::cout << p3 << std::endl;
if( p == p2
|| p == p3
|| p2 == p3 )
{
return 1;
}
// SmartPointer
SmartPointer<Foo> p4 = p2;
SmartPointer<Object> p5 = p3;
// Pointer:
SmartPointer<Foo> p6 = &(*p2);
SmartPointer<Foo> p7;
Fill(p7);
Foo &foo = *p7;
foo.foo();
Container c1;
Container c2;
c2 = c1;
// TODO:
//SmartPointer<Object> s = new Foo;
//delete s;
for(int i = 0; i < 5; ++i)
{
SmartPointer<Foo> f = TestReturn(i);
if( f )
{
f->foo();
}
}
return 0;
}
|