00001 #ifndef _LCORE_ScopedPtr_H_ // -*-c++-*- 00002 00003 #ifdef __GNUG__ 00004 # pragma interface 00005 #endif 00006 00007 /* 00008 * Copyright (C) 1991-9 Eric M. Hopper <hopper@omnifarious.mn.org> 00009 * 00010 * This program is free software; you can redistribute it and/or modify it 00011 * under the terms of the GNU Lesser General Public License as published 00012 * by the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, but 00016 * WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00023 */ 00024 00025 /* $Header: /home/hopper/src/cvs/C++/LCore/LCore/auto_ptr.h,v 1.4 2002/05/09 21:55:21 hopper Exp $ */ 00026 00027 // For a log, see ../ChangeLog 00028 // 00029 // Revision 1.2 1998/05/01 11:59:34 hopper 00030 // Changed bool to bool_val or bool_cst as appropriate so it will be easier to 00031 // port to platforms that don't support bool. 00032 // 00033 // Revision 1.1 1996/02/26 03:52:22 hopper 00034 // This will be part of the standard C++ library someday, but isn't in 00035 // libg++ 2.7.1 00036 // 00037 00038 #include <bool.h> 00039 00040 #define _LCORE_ScopedPtr_H_ 00041 00042 namespace strmod { 00043 namespace lcore { 00044 00045 template<class T> class ScopedPtr { 00046 public: 00047 ScopedPtr(T *ptr) : myptr(ptr) { } 00048 inline ~ScopedPtr(); 00049 00050 T *operator ->() { return(myptr); } 00051 const T *operator ->() const { return(myptr); } 00052 T &operator *() { return(*myptr); } 00053 const T &operator *() const { return(*myptr); } 00054 00055 T *operator T *() { return(myptr); } 00056 const T *operator const T *() const { return(myptr); } 00057 bool_cst operator bool_cst() const { return(myptr != 0); } 00058 00059 void Forget() { myptr = 0; } 00060 00061 private: 00062 T *myptr; 00063 }; 00064 00065 //-----------------------------inline functions-------------------------------- 00066 00067 template<class T> ScopedPtr<T>::~ScopedPtr() 00068 { 00069 if (myptr) 00070 delete myptr; 00071 } 00072 00073 } // namespace lcore 00074 } // namespace strmod 00075 00076 #endif
1.3-rc1