# auto_ptr.h

Go to the documentation of this file.

    00001 // auto_ptr implementation -*- C++ -*-
    00002
    00003 // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License
    00017 // along with this library; see the file COPYING.  If not, write to
    00018 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
    00019 // Boston, MA 02110-1301, USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 /** @file backward/auto_ptr.h
    00031  *  This is an internal header file, included by other library headers.
    00032  *  You should not attempt to use it directly.
    00033  */
    00034
    00035 #ifndef _STL_AUTO_PTR_H
    00036 #define _STL_AUTO_PTR_H 1
    00037
    00038 #include <bits/c++config.h>
    00039 #include <debug/debug.h>
    00040
    00041 _GLIBCXX_BEGIN_NAMESPACE(std)
    00042
    00043   /**
    00044    *  A wrapper class to provide auto_ptr with reference semantics.
    00045    *  For example, an auto_ptr can be assigned (or constructed from)
    00046    *  the result of a function which returns an auto_ptr by value.
    00047    *
    00048    *  All the auto_ptr_ref stuff should happen behind the scenes.
    00049    */
    00050   template<typename _Tp1>
    00051     struct auto_ptr_ref
    00052     {
    00053       _Tp1* _M_ptr;
    00054
    00055       explicit
    00056       auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
    00057     } _GLIBCXX_DEPRECATED_ATTR;
    00058
    00059
    00060   /**
    00061    *  @brief  A simple smart pointer providing strict ownership semantics.
    00062    *
    00063    *  The Standard says:
    00064    *  <pre>
    00065    *  An @c auto_ptr owns the object it holds a pointer to.  Copying
    00066    *  an @c auto_ptr copies the pointer and transfers ownership to the
    00067    *  destination.  If more than one @c auto_ptr owns the same object
    00068    *  at the same time the behavior of the program is undefined.
    00069    *
    00070    *  The uses of @c auto_ptr include providing temporary
    00071    *  exception-safety for dynamically allocated memory, passing
    00072    *  ownership of dynamically allocated memory to a function, and
    00073    *  returning dynamically allocated memory from a function.  @c
    00074    *  auto_ptr does not meet the CopyConstructible and Assignable
    00075    *  requirements for Standard Library <a
    00076    *  href="tables.html#65">container</a> elements and thus
    00077    *  instantiating a Standard Library container with an @c auto_ptr
    00078    *  results in undefined behavior.
    00079    *  </pre>
    00080    *  Quoted from [20.4.5]/3.
    00081    *
    00082    *  Good examples of what can and cannot be done with auto_ptr can
    00083    *  be found in the libstdc++ testsuite.
    00084    *
    00085    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
    00086    *  127.  auto_ptr<> conversion issues
    00087    *  These resolutions have all been incorporated.
    00088    */
    00089   template<typename _Tp>
    00090     class auto_ptr
    00091     {
    00092     private:
    00093       _Tp* _M_ptr;
    00094
    00095     public:
    00096       /// The pointed-to type.
    00097       typedef _Tp element_type;
    00098
    00099       /**
    00100        *  @brief  An %auto_ptr is usually constructed from a raw pointer.
    00101        *  @param  p  A pointer (defaults to NULL).
    00102        *
    00103        *  This object now @e owns the object pointed to by @a p.
    00104        */
    00105       explicit
    00106       auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
    00107
    00108       /**
    00109        *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
    00110        *  @param  a  Another %auto_ptr of the same type.
    00111        *
    00112        *  This object now @e owns the object previously owned by @a a,
    00113        *  which has given up ownership.
    00114        */
    00115       auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
    00116
    00117       /**
    00118        *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
    00119        *  @param  a  Another %auto_ptr of a different but related type.
    00120        *
    00121        *  A pointer-to-Tp1 must be convertible to a
    00122        *  pointer-to-Tp/element_type.
    00123        *
    00124        *  This object now @e owns the object previously owned by @a a,
    00125        *  which has given up ownership.
    00126        */
    00127       template<typename _Tp1>
    00128         auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
    00129
    00130       /**
    00131        *  @brief  %auto_ptr assignment operator.
    00132        *  @param  a  Another %auto_ptr of the same type.
    00133        *
    00134        *  This object now @e owns the object previously owned by @a a,
    00135        *  which has given up ownership.  The object that this one @e
    00136        *  used to own and track has been deleted.
    00137        */
    00138       auto_ptr&
    00139       operator=(auto_ptr& __a) throw()
    00140       {
    00141     reset(__a.release());
    00142     return *this;
    00143       }
    00144
    00145       /**
    00146        *  @brief  %auto_ptr assignment operator.
    00147        *  @param  a  Another %auto_ptr of a different but related type.
    00148        *
    00149        *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
    00150        *
    00151        *  This object now @e owns the object previously owned by @a a,
    00152        *  which has given up ownership.  The object that this one @e
    00153        *  used to own and track has been deleted.
    00154        */
    00155       template<typename _Tp1>
    00156         auto_ptr&
    00157         operator=(auto_ptr<_Tp1>& __a) throw()
    00158         {
    00159       reset(__a.release());
    00160       return *this;
    00161     }
    00162
    00163       /**
    00164        *  When the %auto_ptr goes out of scope, the object it owns is
    00165        *  deleted.  If it no longer owns anything (i.e., @c get() is
    00166        *  @c NULL), then this has no effect.
    00167        *
    00168        *  The C++ standard says there is supposed to be an empty throw
    00169        *  specification here, but omitting it is standard conforming.  Its
    00170        *  presence can be detected only if _Tp::~_Tp() throws, but this is
    00171        *  prohibited.  [17.4.3.6]/2
    00172        */
    00173       ~auto_ptr() { delete _M_ptr; }
    00174
    00175       /**
    00176        *  @brief  Smart pointer dereferencing.
    00177        *
    00178        *  If this %auto_ptr no longer owns anything, then this
    00179        *  operation will crash.  (For a smart pointer, "no longer owns
    00180        *  anything" is the same as being a null pointer, and you know
    00181        *  what happens when you dereference one of those...)
    00182        */
    00183       element_type&
    00184       operator*() const throw()
    00185       {
    00186     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
    00187     return *_M_ptr;
    00188       }
    00189
    00190       /**
    00191        *  @brief  Smart pointer dereferencing.
    00192        *
    00193        *  This returns the pointer itself, which the language then will
    00194        *  automatically cause to be dereferenced.
    00195        */
    00196       element_type*
    00197       operator->() const throw()
    00198       {
    00199     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
    00200     return _M_ptr;
    00201       }
    00202
    00203       /**
    00204        *  @brief  Bypassing the smart pointer.
    00205        *  @return  The raw pointer being managed.
    00206        *
    00207        *  You can get a copy of the pointer that this object owns, for
    00208        *  situations such as passing to a function which only accepts
    00209        *  a raw pointer.
    00210        *
    00211        *  @note  This %auto_ptr still owns the memory.
    00212        */
    00213       element_type*
    00214       get() const throw() { return _M_ptr; }
    00215
    00216       /**
    00217        *  @brief  Bypassing the smart pointer.
    00218        *  @return  The raw pointer being managed.
    00219        *
    00220        *  You can get a copy of the pointer that this object owns, for
    00221        *  situations such as passing to a function which only accepts
    00222        *  a raw pointer.
    00223        *
    00224        *  @note  This %auto_ptr no longer owns the memory.  When this object
    00225        *  goes out of scope, nothing will happen.
    00226        */
    00227       element_type*
    00228       release() throw()
    00229       {
    00230     element_type* __tmp = _M_ptr;
    00231     _M_ptr = 0;
    00232     return __tmp;
    00233       }
    00234
    00235       /**
    00236        *  @brief  Forcibly deletes the managed object.
    00237        *  @param  p  A pointer (defaults to NULL).
    00238        *
    00239        *  This object now @e owns the object pointed to by @a p.  The
    00240        *  previous object has been deleted.
    00241        */
    00242       void
    00243       reset(element_type* __p = 0) throw()
    00244       {
    00245     if (__p != _M_ptr)
    00246       {
    00247         delete _M_ptr;
    00248         _M_ptr = __p;
    00249       }
    00250       }
    00251
    00252       /**
    00253        *  @brief  Automatic conversions
    00254        *
    00255        *  These operations convert an %auto_ptr into and from an auto_ptr_ref
    00256        *  automatically as needed.  This allows constructs such as
    00257        *  @code
    00258        *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
    00259        *    ...
    00260        *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
    00261        *  @endcode
    00262        */
    00263       auto_ptr(auto_ptr_ref<element_type> __ref) throw()
    00264       : _M_ptr(__ref._M_ptr) { }
    00265
    00266       auto_ptr&
    00267       operator=(auto_ptr_ref<element_type> __ref) throw()
    00268       {
    00269     if (__ref._M_ptr != this->get())
    00270       {
    00271         delete _M_ptr;
    00272         _M_ptr = __ref._M_ptr;
    00273       }
    00274     return *this;
    00275       }
    00276
    00277       template<typename _Tp1>
    00278         operator auto_ptr_ref<_Tp1>() throw()
    00279         { return auto_ptr_ref<_Tp1>(this->release()); }
    00280
    00281       template<typename _Tp1>
    00282         operator auto_ptr<_Tp1>() throw()
    00283         { return auto_ptr<_Tp1>(this->release()); }
    00284     } _GLIBCXX_DEPRECATED_ATTR;
    00285
    00286   // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00287   // 541. shared_ptr template assignment and void
    00288   template<>
    00289     class auto_ptr<void>
    00290     {
    00291     public:
    00292       typedef void element_type;
    00293     } _GLIBCXX_DEPRECATED_ATTR;
    00294
    00295 _GLIBCXX_END_NAMESPACE
    00296
    00297 #endif /* _STL_AUTO_PTR_H */