$extrastylesheet
auto_ptr.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) 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 GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 #ifndef LIBMESH_AUTO_PTR_H
00019 #define LIBMESH_AUTO_PTR_H
00020 
00021 #include "libmesh/libmesh_config.h"
00022 #include "libmesh_common.h" // for libmesh_deprecated()
00023 
00024 // LibMesh's AutoPtr is now libmesh_deprecated(), just like the
00025 // std::auto_ptr it is based on.
00026 //
00027 // New library code should use the UniquePtr typedef (which will
00028 // eventually be a C++11 alias declaration).  LibMesh's UniquePtr is
00029 // one of:
00030 // 1.) std::unique_ptr
00031 // 2.) Howard Hinnant's C++03 compatible boost::unique_ptr
00032 // 3.) The deprecated libMesh AutoPtr
00033 // in that order, depending on what your compiler supports.  If you
00034 // are using a compiler that cannot compile Howard Hinnant's
00035 // unique_ptr implementation, you should probably think about
00036 // upgrading.
00037 #ifdef LIBMESH_ENABLE_UNIQUE_PTR
00038   #ifdef LIBMESH_HAVE_CXX11_UNIQUE_PTR
00039   #  include <memory>
00040   #  define UniquePtr std::unique_ptr
00041   #elif LIBMESH_HAVE_HINNANT_UNIQUE_PTR
00042   #  include "libmesh/unique_ptr.hpp"
00043   #  define UniquePtr boost::unique_ptr
00044   #else
00045   #  define UniquePtr libMesh::AutoPtr
00046   #endif
00047 #else
00048   // libMesh was configured with --disable-unique-ptr, so we'll use
00049   // libMesh's AutoPtr class instead.
00050   #define UniquePtr libMesh::AutoPtr
00051 #endif
00052 
00053 namespace libMesh
00054 {
00055 
00063 template<typename Tp1>
00064 struct AutoPtrRef
00065 {
00069   Tp1* _ptr;
00070 
00074   explicit
00075   AutoPtrRef(Tp1* p)
00076     : _ptr(p) {}
00077 };
00078 
00079 
00147 template<typename Tp>
00148 class AutoPtr
00149 {
00150 private:
00151 
00155   Tp* _ptr;
00156 
00157 public:
00161   typedef Tp element_type;
00162 
00169   explicit
00170   AutoPtr(element_type* p = 0)
00171     : _ptr(p)
00172   {
00173     // Note: we can't call libmesh_deprecated() here, since global
00174     // AutoPtr variables are sometimes created before the libMesh::out
00175     // stream is ready.
00176   }
00177 
00185   AutoPtr(AutoPtr& a)
00186     : _ptr(a.release())
00187   {
00188   }
00189 
00199   template<typename Tp1>
00200   AutoPtr(AutoPtr<Tp1>& a)
00201     : _ptr(a.release())
00202   {
00203   }
00204 
00213   AutoPtr&
00214   operator=(AutoPtr& a)
00215   {
00216     reset(a.release());
00217     return *this;
00218   }
00219 
00230   template <typename Tp1>
00231   AutoPtr&
00232   operator=(AutoPtr<Tp1>& a)
00233   {
00234     reset(a.release());
00235     return *this;
00236   }
00237 
00250   ~AutoPtr()
00251   {
00252     if (!libMesh::warned_about_auto_ptr)
00253       {
00254         libMesh::warned_about_auto_ptr = true;
00255         libMesh::out << "*** Warning, AutoPtr is deprecated and will be removed in a future library version! "
00256                      << __FILE__ << ", line " << __LINE__ << ", compiled " << __DATE__ << " at " << __TIME__ << " ***" << std::endl;
00257       }
00258     delete _ptr;
00259   }
00260 
00269   element_type&
00270   operator*() const  { return *_ptr; }
00271 
00278   element_type*
00279   operator->() const  { return _ptr; }
00280 
00291   element_type*
00292   get() const  { return _ptr; }
00293 
00305   element_type*
00306   release()
00307   {
00308     element_type* tmp = _ptr;
00309     _ptr = 0;
00310     return tmp;
00311   }
00312 
00320   void
00321   reset(element_type* p = 0)
00322   {
00323     if (p != _ptr)
00324       {
00325         delete _ptr;
00326         _ptr = p;
00327       }
00328   }
00329 
00341   AutoPtr(AutoPtrRef<element_type> ref)
00342     : _ptr(ref._ptr) {}
00343 
00350   AutoPtr&
00351   operator=(AutoPtrRef<element_type> ref)
00352   {
00353     if (ref._ptr != this->get())
00354       {
00355         delete _ptr;
00356         _ptr = ref._ptr;
00357       }
00358     return *this;
00359   }
00360 
00364   template<typename Tp1>
00365   operator AutoPtrRef<Tp1>()
00366   { return AutoPtrRef<Tp1>(this->release()); }
00367 
00371   template<typename Tp1>
00372   operator AutoPtr<Tp1>()
00373   { return AutoPtr<Tp1>(this->release()); }
00374 };
00375 
00376 
00377 
00378 } // namespace libMesh
00379 
00380 #endif // LIBMESH_AUTO_PTR_H