auto_ptr.h
Go to the documentation of this file.00001 // Copyright (C) 2001, 2002 Free Software Foundation, Inc. 00002 // 00003 // This file is part of the GNU ISO C++ Library. This library is free 00004 // software; you can redistribute it and/or modify it under the 00005 // terms of the GNU General Public License as published by the 00006 // Free Software Foundation; either version 2, or (at your option) 00007 // any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 00014 // You should have received a copy of the GNU General Public License along 00015 // with this library; see the file COPYING. If not, write to the Free 00016 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00017 // USA. 00018 00019 // As a special exception, you may use this file as part of a free software 00020 // library without restriction. Specifically, if other files instantiate 00021 // templates or use macros or inline functions from this file, or you compile 00022 // this file and link it with other files to produce an executable, this 00023 // file does not by itself cause the resulting executable to be covered by 00024 // the GNU General Public License. This exception does not however 00025 // invalidate any other reasons why the executable file might be covered by 00026 // the GNU General Public License. 00027 00028 /* 00029 * Copyright (c) 1997-1999 00030 * Silicon Graphics Computer Systems, Inc. 00031 * 00032 * Permission to use, copy, modify, distribute and sell this software 00033 * and its documentation for any purpose is hereby granted without fee, 00034 * provided that the above copyright notice appear in all copies and 00035 * that both that copyright notice and this permission notice appear 00036 * in supporting documentation. Silicon Graphics makes no 00037 * representations about the suitability of this software for any 00038 * purpose. It is provided "as is" without express or implied warranty. 00039 * 00040 */ 00041 00042 00043 00044 #ifndef LIBMESH_AUTO_PTR_H 00045 #define LIBMESH_AUTO_PTR_H 00046 00047 namespace libMesh 00048 { 00049 00057 template<typename Tp1> 00058 struct AutoPtrRef 00059 { 00063 Tp1* _ptr; 00064 00068 explicit 00069 AutoPtrRef(Tp1* p) 00070 : _ptr(p) {} 00071 }; 00072 00073 00101 template<typename Tp> 00102 class AutoPtr 00103 { 00104 private: 00105 00109 Tp* _ptr; 00110 00111 public: 00115 typedef Tp element_type; 00116 00123 explicit 00124 AutoPtr(element_type* p = 0) 00125 : _ptr(p) {} 00126 00134 AutoPtr(AutoPtr& a) 00135 : _ptr(a.release()) {} 00136 00146 template<typename Tp1> 00147 AutoPtr(AutoPtr<Tp1>& a) 00148 : _ptr(a.release()) {} 00149 00158 AutoPtr& 00159 operator=(AutoPtr& a) 00160 { 00161 reset(a.release()); 00162 return *this; 00163 } 00164 00175 template <typename Tp1> 00176 AutoPtr& 00177 operator=(AutoPtr<Tp1>& a) 00178 { 00179 reset(a.release()); 00180 return *this; 00181 } 00182 00195 ~AutoPtr() { delete _ptr; } 00196 00205 element_type& 00206 operator*() const { return *_ptr; } 00207 00214 element_type* 00215 operator->() const { return _ptr; } 00216 00227 element_type* 00228 get() const { return _ptr; } 00229 00241 element_type* 00242 release() 00243 { 00244 element_type* tmp = _ptr; 00245 _ptr = 0; 00246 return tmp; 00247 } 00248 00256 void 00257 reset(element_type* p = 0) 00258 { 00259 if (p != _ptr) 00260 { 00261 delete _ptr; 00262 _ptr = p; 00263 } 00264 } 00265 00277 AutoPtr(AutoPtrRef<element_type> ref) 00278 : _ptr(ref._ptr) {} 00279 00286 AutoPtr& 00287 operator=(AutoPtrRef<element_type> ref) 00288 { 00289 if (ref._ptr != this->get()) 00290 { 00291 delete _ptr; 00292 _ptr = ref._ptr; 00293 } 00294 return *this; 00295 } 00296 00300 template<typename Tp1> 00301 operator AutoPtrRef<Tp1>() 00302 { return AutoPtrRef<Tp1>(this->release()); } 00303 00307 template<typename Tp1> 00308 operator AutoPtr<Tp1>() 00309 { return AutoPtr<Tp1>(this->release()); } 00310 }; 00311 00312 00313 00314 } // namespace libMesh 00315 00316 #endif // LIBMESH_AUTO_PTR_H
Site Created By: libMesh Developers
Last modified: February 05 2013 19:54:45 UTC
Hosted By: