GDCM  2.4.5
gdcmSmartPointer.h
Go to the documentation of this file.
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 #ifndef GDCMSMARTPOINTER_H
15 #define GDCMSMARTPOINTER_H
16 
17 #include "gdcmObject.h"
18 
19 namespace gdcm
20 {
38 template<class ObjectType>
39 class SmartPointer
40 {
41 public:
42  SmartPointer():Pointer(0) {}
43  SmartPointer(const SmartPointer<ObjectType>& p):Pointer(p.Pointer)
44  { Register(); }
45  SmartPointer(ObjectType* p):Pointer(p)
46  { Register(); }
47  SmartPointer(ObjectType const & p)
48  {
49  Pointer = const_cast<ObjectType*>(&p);
50  Register();
51  }
53  UnRegister();
54  Pointer = 0;
55  }
56 
58  ObjectType *operator -> () const
59  { return Pointer; }
60 
61  ObjectType& operator * () const
62  { return *Pointer; }
63 
65  operator ObjectType * () const
66  { return Pointer; }
67 
70  { return operator = (r.Pointer); }
71 
73  SmartPointer &operator = (ObjectType *r)
74  {
75  // http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.22
76  // DO NOT CHANGE THE ORDER OF THESE STATEMENTS!
77  // (This order properly handles self-assignment)
78  // (This order also properly handles recursion, e.g., if a ObjectType contains SmartPointer<ObjectType>s)
79  if( Pointer != r )
80  {
81  ObjectType* old = Pointer;
82  Pointer = r;
83  Register();
84  if ( old ) { old->UnRegister(); }
85  }
86  return *this;
87  }
88 
89  SmartPointer &operator = (ObjectType const &r)
90  {
91  ObjectType* tmp = const_cast<ObjectType*>(&r);
92  return operator = (tmp);
93  }
94 
96  ObjectType *GetPointer() const
97  { return Pointer; }
98 
99 private:
100  void Register()
101  {
102  if(Pointer) Pointer->Register();
103  }
104 
105  void UnRegister()
106  {
107  if(Pointer) Pointer->UnRegister();
108  }
109 
110  ObjectType* Pointer;
111 };
112 
113 } // end namespace gdcm
114 
115 #endif //GDCMSMARTPOINTER_H
ObjectType & operator*() const
Definition: gdcmSmartPointer.h:61
ObjectType * operator->() const
Overload operator ->
Definition: gdcmSmartPointer.h:58
~SmartPointer()
Definition: gdcmSmartPointer.h:52
SmartPointer(ObjectType *p)
Definition: gdcmSmartPointer.h:45
SmartPointer(const SmartPointer< ObjectType > &p)
Definition: gdcmSmartPointer.h:43
SmartPointer()
Definition: gdcmSmartPointer.h:42
Class for Smart Pointer.
Definition: gdcmObject.h:26
SmartPointer & operator=(SmartPointer const &r)
Overload operator assignment.
Definition: gdcmSmartPointer.h:69
SmartPointer(ObjectType const &p)
Definition: gdcmSmartPointer.h:47
Definition: gdcmASN1.h:20
ObjectType * GetPointer() const
Explicit function to retrieve the pointer.
Definition: gdcmSmartPointer.h:96

Generated on Fri Sep 25 2015 17:58:24 for GDCM by doxygen 1.8.9.1
SourceForge.net Logo