OFFIS DCMTK  Version 3.6.0
ofaptr.h
1 /*
2  *
3  * Copyright (C) 2009-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Uli Schlachter
17  *
18  * Purpose: Template class for automatically deleting pointers when they go out
19  * of scope.
20  *
21  * Last Update: $Author: uli $
22  * Update Date: $Date: 2010-11-01 09:38:19 $
23  * CVS/RCS Revision: $Revision: 1.9 $
24  * Status: $State: Exp $
25  *
26  * CVS/RCS Log at end of file
27  *
28  */
29 
30 #ifndef OFAPTR_H
31 #define OFAPTR_H
32 
33 #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
34 
35 #if defined(HAVE_STL) || defined(HAVE_STL_AUTO_PTR)
36 // std::auto_ptr has an identicaly interface so it can be used as an alternative
37 #include <memory>
38 #define OFauto_ptr std::auto_ptr
39 
40 #else
41 
42 #define INCLUDE_CSTDDEF /* For NULL */
43 #include "dcmtk/ofstd/ofstdinc.h"
44 
58 template <class T> class OFauto_ptr_ref
59 {
60 public:
62  T *ptr;
63 
67  explicit OFauto_ptr_ref(T* p) : ptr(p)
68  {
69  }
70 };
71 
76 template <class T> class OFauto_ptr
77 {
78  protected:
80  T *ptr;
81 
82  public:
87  explicit OFauto_ptr(T* p = NULL) : ptr(p)
88  {
89  }
90 
95  OFauto_ptr(OFauto_ptr<T>& other) : ptr(other.release())
96  {
97  }
98 
103  {
104  }
105 
110  {
111  reset();
112  }
113 
118  void reset(T* p = NULL)
119  {
120  if (this->ptr)
121  delete this->ptr;
122  this->ptr = p;
123  }
124 
128  T* get() const { return this->ptr; }
129 
133  T* operator->() const { return get(); }
134 
138  T& operator*() const { return *get(); }
139 
143  operator OFauto_ptr_ref<T>()
144  {
145  return OFauto_ptr_ref<T>(release());
146  }
147 
153  {
154  reset(other.release());
155  return *this;
156  }
157 
163  {
164  reset(other.ptr);
165  return *this;
166  }
167 
172  T* release()
173  {
174  T* tmp = this->ptr;
175  this->ptr = NULL;
176  return tmp;
177  }
178 };
179 
180 #endif
181 
182 #endif
183 
184 
185 /*
186 ** CVS/RCS Log:
187 ** $Log: ofaptr.h,v $
188 ** Revision 1.9 2010-11-01 09:38:19 uli
189 ** Fixed some compiler warnings reported by gcc with additional flags.
190 **
191 ** Revision 1.8 2010-10-14 13:15:49 joergr
192 ** Updated copyright header. Added reference to COPYRIGHT file.
193 **
194 ** Revision 1.7 2010-10-08 12:45:19 uli
195 ** Removed an invalid function which isn't part of std::auto_ptr.
196 **
197 ** Revision 1.6 2010-10-08 12:35:59 uli
198 ** Added macro HAVE_STL_AUTO_PTR which defines OFauto_ptr to std::auto_ptr.
199 **
200 ** Revision 1.5 2010-10-08 12:27:07 uli
201 ** Fixed all doxygen warnings for OFPair and OFauto_ptr.
202 **
203 ** Revision 1.4 2010-04-26 12:22:30 uli
204 ** Fixed a some minor doxygen warnings.
205 **
206 ** Revision 1.3 2010-03-01 09:08:50 uli
207 ** Removed some unnecessary include directives in the headers.
208 **
209 ** Revision 1.2 2009-09-15 15:20:51 joergr
210 ** Fixed issue with class OFauto_ptr: Default copy constructor and assignment
211 ** operator could lead to double deletion of objects.
212 **
213 ** Revision 1.1 2009-08-19 10:50:02 joergr
214 ** Added new class OFauto_ptr required for upcoming module "oflog".
215 **
216 **
217 */


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2