ofstd/include/dcmtk/ofstd/ofaptr.h

00001 /*
00002  *
00003  *  Copyright (C) 2009-2010, OFFIS e.V.
00004  *  All rights reserved.  See COPYRIGHT file for details.
00005  *
00006  *  This software and supporting documentation were developed by
00007  *
00008  *    OFFIS e.V.
00009  *    R&D Division Health
00010  *    Escherweg 2
00011  *    D-26121 Oldenburg, Germany
00012  *
00013  *
00014  *  Module:  ofstd
00015  *
00016  *  Author:  Uli Schlachter
00017  *
00018  *  Purpose: Template class for automatically deleting pointers when they go out
00019  *           of scope.
00020  *
00021  *  Last Update:      $Author: uli $
00022  *  Update Date:      $Date: 2010-11-01 09:38:19 $
00023  *  CVS/RCS Revision: $Revision: 1.9 $
00024  *  Status:           $State: Exp $
00025  *
00026  *  CVS/RCS Log at end of file
00027  *
00028  */
00029 
00030 #ifndef OFAPTR_H
00031 #define OFAPTR_H
00032 
00033 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
00034 
00035 #if defined(HAVE_STL) || defined(HAVE_STL_AUTO_PTR)
00036 // std::auto_ptr has an identicaly interface so it can be used as an alternative
00037 #include <memory>
00038 #define OFauto_ptr std::auto_ptr
00039 
00040 #else
00041 
00042 #define INCLUDE_CSTDDEF               /* For NULL */
00043 #include "dcmtk/ofstd/ofstdinc.h"
00044 
00058 template <class T> class OFauto_ptr_ref
00059 {
00060 public:
00062     T *ptr;
00063 
00067     explicit OFauto_ptr_ref(T* p) : ptr(p)
00068     {
00069     }
00070 };
00071 
00076 template <class T> class OFauto_ptr
00077 {
00078   protected:
00080     T *ptr;
00081 
00082   public:
00087     explicit OFauto_ptr(T* p = NULL) : ptr(p)
00088     {
00089     }
00090 
00095     OFauto_ptr(OFauto_ptr<T>& other) : ptr(other.release())
00096     {
00097     }
00098 
00102     OFauto_ptr(OFauto_ptr_ref<T> other) : ptr(other.ptr)
00103     {
00104     }
00105 
00109     ~OFauto_ptr()
00110     {
00111         reset();
00112     }
00113 
00118     void reset(T* p = NULL)
00119     {
00120         if (this->ptr)
00121             delete this->ptr;
00122         this->ptr = p;
00123     }
00124 
00128     T* get() const { return this->ptr; }
00129 
00133     T* operator->() const { return get(); }
00134 
00138     T& operator*() const { return *get(); }
00139 
00143     operator OFauto_ptr_ref<T>()
00144     {
00145         return OFauto_ptr_ref<T>(release());
00146     }
00147 
00152     OFauto_ptr& operator=(OFauto_ptr<T>& other)
00153     {
00154         reset(other.release());
00155         return *this;
00156     }
00157 
00162     OFauto_ptr& operator=(OFauto_ptr_ref<T> other)
00163     {
00164         reset(other.ptr);
00165         return *this;
00166     }
00167 
00172     T* release()
00173     {
00174         T* tmp = this->ptr;
00175         this->ptr = NULL;
00176         return tmp;
00177     }
00178 };
00179 
00180 #endif
00181 
00182 #endif
00183 
00184 
00185 /*
00186 ** CVS/RCS Log:
00187 ** $Log: ofaptr.h,v $
00188 ** Revision 1.9  2010-11-01 09:38:19  uli
00189 ** Fixed some compiler warnings reported by gcc with additional flags.
00190 **
00191 ** Revision 1.8  2010-10-14 13:15:49  joergr
00192 ** Updated copyright header. Added reference to COPYRIGHT file.
00193 **
00194 ** Revision 1.7  2010-10-08 12:45:19  uli
00195 ** Removed an invalid function which isn't part of std::auto_ptr.
00196 **
00197 ** Revision 1.6  2010-10-08 12:35:59  uli
00198 ** Added macro HAVE_STL_AUTO_PTR which defines OFauto_ptr to std::auto_ptr.
00199 **
00200 ** Revision 1.5  2010-10-08 12:27:07  uli
00201 ** Fixed all doxygen warnings for OFPair and OFauto_ptr.
00202 **
00203 ** Revision 1.4  2010-04-26 12:22:30  uli
00204 ** Fixed a some minor doxygen warnings.
00205 **
00206 ** Revision 1.3  2010-03-01 09:08:50  uli
00207 ** Removed some unnecessary include directives in the headers.
00208 **
00209 ** Revision 1.2  2009-09-15 15:20:51  joergr
00210 ** Fixed issue with class OFauto_ptr: Default copy constructor and assignment
00211 ** operator could lead to double deletion of objects.
00212 **
00213 ** Revision 1.1  2009-08-19 10:50:02  joergr
00214 ** Added new class OFauto_ptr required for upcoming module "oflog".
00215 **
00216 **
00217 */


Generated on 6 Jan 2011 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.5.1