00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * 00004 * The IPPL Framework 00005 * 00006 * 00007 * Visit http://people.web.psi.ch/adelmann/ for more details 00008 * 00009 ***************************************************************************/ 00010 00011 #ifndef MY_AUTO_PTR_H 00012 #define MY_AUTO_PTR_H 00013 00015 /* 00016 A simple compliant implementation of auto_ptr. 00017 This is from Greg Colvin's implementation posted to comp.std.c++. 00018 00019 Instead of using mutable this casts away const in release. 00020 00021 We have to do this because we can't build containers of these 00022 things otherwise. 00023 */ 00025 00026 template<class X> 00027 class my_auto_ptr 00028 { 00029 X* px; 00030 public: 00031 my_auto_ptr() : px(0) {} 00032 my_auto_ptr(X* p) : px(p) {} 00033 my_auto_ptr(const my_auto_ptr<X>& r) : px(r.release()) {} 00034 my_auto_ptr& operator=(const my_auto_ptr<X>& r) 00035 { 00036 if (&r != this) 00037 { 00038 delete px; 00039 px = r.release(); 00040 } 00041 return *this; 00042 } 00043 ~my_auto_ptr() { delete px; } 00044 X& operator*() const { return *px; } 00045 X* operator->() const { return px; } 00046 X* get() const { return px; } 00047 X* release() const { X *p=px; ((my_auto_ptr<X>*)(this))->px=0; return p; } 00048 }; 00049 00050 #ifdef UNDEFINED 00051 template<class X> 00052 class my_auto_ptr 00053 { 00054 bool owner; 00055 X* px; 00056 public: 00057 my_auto_ptr() 00058 : owner(false), px(0) {} 00059 my_auto_ptr(X* p) 00060 : owner(p), px(p) {} 00061 my_auto_ptr(const my_auto_ptr<X>& r) 00062 : owner(r.owner), px(r.release()) {} 00063 my_auto_ptr& operator=(const my_auto_ptr<X>& r) 00064 { 00065 if (&r != this) 00066 { 00067 if (owner) 00068 delete px; 00069 owner = r.owner; 00070 px = r.release(); 00071 } 00072 return *this; 00073 } 00074 ~my_auto_ptr() { if (owner) delete px; } 00075 X& operator*() const { return *px; } 00076 X* operator->() const { return px; } 00077 X* get() const { return px; } 00078 X* release() const { ((my_auto_ptr<X>*)(this))->owner = false; return px;} 00079 }; 00080 #endif // UNDEFINED 00081 00082 #endif // MY_AUTO_PTR_H 00083 00084 /*************************************************************************** 00085 * $RCSfile: my_auto_ptr.h,v $ $Author: adelmann $ 00086 * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:34 $ 00087 * IPPL_VERSION_ID: $Id: my_auto_ptr.h,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $ 00088 ***************************************************************************/