ScopedPtr.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_ScopedPtr_hxx
00024 #define __ccxx_ScopedPtr_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/NullPointerException.h++>
00028 
00029 namespace ccxx {
00030 
00037 template<class T> class ScopedPtr
00038 {
00039   public:
00040 
00042   ScopedPtr()
00043     : _object(NULL)
00044   { }
00045 
00047   explicit ScopedPtr(T *object)
00048     : _object(object)
00049   { }
00050 
00052   ~ScopedPtr()
00053   { delete _object; }
00054 
00059   T& operator*() const throw(NullPointerException)
00060   {
00061     if(! _object)
00062       throw NullPointerException();
00063 
00064     return(*_object);
00065   }
00066 
00071   T* operator->() const throw(NullPointerException)
00072   {
00073     if(! _object)
00074       throw NullPointerException();
00075 
00076     return(_object);
00077   }
00078 
00080   void reset(T *object = NULL)
00081   {
00082     if(_object != object)
00083     {
00084       delete _object;
00085       _object = object;
00086     }
00087   }
00088 
00090   T* get() const
00091   { return(_object); }
00092 
00094   T* release() throw()
00095   {
00096     T *tmp = _object;
00097     _object = NULL;
00098     return(tmp);
00099   }
00100 
00102   bool operator!() const
00103   { return(isNull()); }
00104 
00106   operator const void *() const
00107   { return(_object ? this : NULL); }
00108 
00110   bool isNull() const throw()
00111   { return(_object == NULL); }
00112 
00114   void swap(ScopedPtr &other) throw()
00115   {
00116     T *tmp = other._object;
00117     other._object = _object;
00118     _object = tmp;
00119   }
00120 
00121   private:
00122 
00123   T *_object;
00124 
00125   CCXX_COPY_DECLS(ScopedPtr);
00126 };
00127 
00130 template<class T> inline void swap(ScopedPtr<T> &a, ScopedPtr<T> &b)
00131   throw()
00132 { a.swap(b); }
00133 
00134 }; // namespace ccxx
00135 
00136 #endif // __ccxx_ScopedPtr_hxx
00137 
00138 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3