LTP GCOV extension - code coverage report
Current view: | directory - usr/include/c++/4.3/backward - auto_ptr.h
Test: | lcov.info
Date: | 2008-08-14 |  | Instrumented lines: | 15
Code covered: | 100.0 % |  | Executed lines: | 15

           1                 : // auto_ptr implementation -*- C++ -*-
           2                 :
           3                 : // Copyright (C) 2007, 2008 Free Software Foundation, Inc.
           4                 : //
           5                 : // This file is part of the GNU ISO C++ Library.  This library is free
           6                 : // software; you can redistribute it and/or modify it under the
           7                 : // terms of the GNU General Public License as published by the
           8                 : // Free Software Foundation; either version 2, or (at your option)
           9                 : // any later version.
          10                 :
          11                 : // This library is distributed in the hope that it will be useful,
          12                 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
          13                 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
          14                 : // GNU General Public License for more details.
          15                 :
          16                 : // You should have received a copy of the GNU General Public License
          17                 : // along with this library; see the file COPYING.  If not, write to
          18                 : // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
          19                 : // Boston, MA 02110-1301, USA.
          20                 :
          21                 : // As a special exception, you may use this file as part of a free software
          22                 : // library without restriction.  Specifically, if other files instantiate
          23                 : // templates or use macros or inline functions from this file, or you compile
          24                 : // this file and link it with other files to produce an executable, this
          25                 : // file does not by itself cause the resulting executable to be covered by
          26                 : // the GNU General Public License.  This exception does not however
          27                 : // invalidate any other reasons why the executable file might be covered by
          28                 : // the GNU General Public License.
          29                 :
          30                 : /** @file backward/auto_ptr.h
          31                 :  *  This is an internal header file, included by other library headers.
          32                 :  *  You should not attempt to use it directly.
          33                 :  */
          34                 :
          35                 : #ifndef _STL_AUTO_PTR_H
          36                 : #define _STL_AUTO_PTR_H 1
          37                 :
          38                 : #include <bits/c++config.h>
          39                 : #include <debug/debug.h>
          40                 :
          41                 : _GLIBCXX_BEGIN_NAMESPACE(std)
          42                 :
          43                 :   /**
          44                 :    *  A wrapper class to provide auto_ptr with reference semantics.
          45                 :    *  For example, an auto_ptr can be assigned (or constructed from)
          46                 :    *  the result of a function which returns an auto_ptr by value.
          47                 :    *
          48                 :    *  All the auto_ptr_ref stuff should happen behind the scenes.
          49                 :    */
          50                 :   template<typename _Tp1>
          51                 :     struct auto_ptr_ref
          52                 :     {
          53                 :       _Tp1* _M_ptr;
          54                 :
          55                 :       explicit
          56             666 :       auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
          57                 :     } _GLIBCXX_DEPRECATED_ATTR;
          58                 :
          59                 :
          60                 :   /**
          61                 :    *  @brief  A simple smart pointer providing strict ownership semantics.
          62                 :    *
          63                 :    *  The Standard says:
          64                 :    *  <pre>
          65                 :    *  An @c auto_ptr owns the object it holds a pointer to.  Copying
          66                 :    *  an @c auto_ptr copies the pointer and transfers ownership to the
          67                 :    *  destination.  If more than one @c auto_ptr owns the same object
          68                 :    *  at the same time the behavior of the program is undefined.
          69                 :    *
          70                 :    *  The uses of @c auto_ptr include providing temporary
          71                 :    *  exception-safety for dynamically allocated memory, passing
          72                 :    *  ownership of dynamically allocated memory to a function, and
          73                 :    *  returning dynamically allocated memory from a function.  @c
          74                 :    *  auto_ptr does not meet the CopyConstructible and Assignable
          75                 :    *  requirements for Standard Library <a
          76                 :    *  href="tables.html#65">container</a> elements and thus
          77                 :    *  instantiating a Standard Library container with an @c auto_ptr
          78                 :    *  results in undefined behavior.
          79                 :    *  </pre>
          80                 :    *  Quoted from [20.4.5]/3.
          81                 :    *
          82                 :    *  Good examples of what can and cannot be done with auto_ptr can
          83                 :    *  be found in the libstdc++ testsuite.
          84                 :    *
          85                 :    *  _GLIBCXX_RESOLVE_LIB_DEFECTS
          86                 :    *  127.  auto_ptr<> conversion issues
          87                 :    *  These resolutions have all been incorporated.
          88                 :    */
          89                 :   template<typename _Tp>
          90                 :     class auto_ptr
          91                 :     {
          92                 :     private:
          93                 :       _Tp* _M_ptr;
          94                 :
          95                 :     public:
          96                 :       /// The pointed-to type.
          97                 :       typedef _Tp element_type;
          98                 :
          99                 :       /**
         100                 :        *  @brief  An %auto_ptr is usually constructed from a raw pointer.
         101                 :        *  @param  p  A pointer (defaults to NULL).
         102                 :        *
         103                 :        *  This object now @e owns the object pointed to by @a p.
         104                 :        */
         105                 :       explicit
         106             666 :       auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
         107                 :
         108                 :       /**
         109                 :        *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
         110                 :        *  @param  a  Another %auto_ptr of the same type.
         111                 :        *
         112                 :        *  This object now @e owns the object previously owned by @a a,
         113                 :        *  which has given up ownership.
         114                 :        */
         115             590 :       auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
         116                 :
         117                 :       /**
         118                 :        *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
         119                 :        *  @param  a  Another %auto_ptr of a different but related type.
         120                 :        *
         121                 :        *  A pointer-to-Tp1 must be convertible to a
         122                 :        *  pointer-to-Tp/element_type.
         123                 :        *
         124                 :        *  This object now @e owns the object previously owned by @a a,
         125                 :        *  which has given up ownership.
         126                 :        */
         127                 :       template<typename _Tp1>
         128                 :         auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
         129                 :
         130                 :       /**
         131                 :        *  @brief  %auto_ptr assignment operator.
         132                 :        *  @param  a  Another %auto_ptr of the same type.
         133                 :        *
         134                 :        *  This object now @e owns the object previously owned by @a a,
         135                 :        *  which has given up ownership.  The object that this one @e
         136                 :        *  used to own and track has been deleted.
         137                 :        */
         138                 :       auto_ptr&
         139                 :       operator=(auto_ptr& __a) throw()
         140                 :       {
         141                 :         reset(__a.release());
         142                 :         return *this;
         143                 :       }
         144                 :
         145                 :       /**
         146                 :        *  @brief  %auto_ptr assignment operator.
         147                 :        *  @param  a  Another %auto_ptr of a different but related type.
         148                 :        *
         149                 :        *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
         150                 :        *
         151                 :        *  This object now @e owns the object previously owned by @a a,
         152                 :        *  which has given up ownership.  The object that this one @e
         153                 :        *  used to own and track has been deleted.
         154                 :        */
         155                 :       template<typename _Tp1>
         156                 :         auto_ptr&
         157                 :         operator=(auto_ptr<_Tp1>& __a) throw()
         158                 :         {
         159                 :           reset(__a.release());
         160                 :           return *this;
         161                 :         }
         162                 :
         163                 :       /**
         164                 :        *  When the %auto_ptr goes out of scope, the object it owns is
         165                 :        *  deleted.  If it no longer owns anything (i.e., @c get() is
         166                 :        *  @c NULL), then this has no effect.
         167                 :        *
         168                 :        *  The C++ standard says there is supposed to be an empty throw
         169                 :        *  specification here, but omitting it is standard conforming.  Its
         170                 :        *  presence can be detected only if _Tp::~_Tp() throws, but this is
         171                 :        *  prohibited.  [17.4.3.6]/2
         172                 :        */
         173            1922 :       ~auto_ptr() { delete _M_ptr; }
         174                 :
         175                 :       /**
         176                 :        *  @brief  Smart pointer dereferencing.
         177                 :        *
         178                 :        *  If this %auto_ptr no longer owns anything, then this
         179                 :        *  operation will crash.  (For a smart pointer, "no longer owns
         180                 :        *  anything" is the same as being a null pointer, and you know
         181                 :        *  what happens when you dereference one of those...)
         182                 :        */
         183                 :       element_type&
         184                 :       operator*() const throw()
         185                 :       {
         186                 :         _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
         187                 :         return *_M_ptr;
         188                 :       }
         189                 :
         190                 :       /**
         191                 :        *  @brief  Smart pointer dereferencing.
         192                 :        *
         193                 :        *  This returns the pointer itself, which the language then will
         194                 :        *  automatically cause to be dereferenced.
         195                 :        */
         196                 :       element_type*
         197             590 :       operator->() const throw()
         198                 :       {
         199                 :         _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
         200             590 :         return _M_ptr;
         201                 :       }
         202                 :
         203                 :       /**
         204                 :        *  @brief  Bypassing the smart pointer.
         205                 :        *  @return  The raw pointer being managed.
         206                 :        *
         207                 :        *  You can get a copy of the pointer that this object owns, for
         208                 :        *  situations such as passing to a function which only accepts
         209                 :        *  a raw pointer.
         210                 :        *
         211                 :        *  @note  This %auto_ptr still owns the memory.
         212                 :        */
         213                 :       element_type*
         214            1256 :       get() const throw() { return _M_ptr; }
         215                 :
         216                 :       /**
         217                 :        *  @brief  Bypassing the smart pointer.
         218                 :        *  @return  The raw pointer being managed.
         219                 :        *
         220                 :        *  You can get a copy of the pointer that this object owns, for
         221                 :        *  situations such as passing to a function which only accepts
         222                 :        *  a raw pointer.
         223                 :        *
         224                 :        *  @note  This %auto_ptr no longer owns the memory.  When this object
         225                 :        *  goes out of scope, nothing will happen.
         226                 :        */
         227                 :       element_type*
         228            1256 :       release() throw()
         229                 :       {
         230            1256 :         element_type* __tmp = _M_ptr;
         231            1256 :         _M_ptr = 0;
         232            1256 :         return __tmp;
         233                 :       }
         234                 :
         235                 :       /**
         236                 :        *  @brief  Forcibly deletes the managed object.
         237                 :        *  @param  p  A pointer (defaults to NULL).
         238                 :        *
         239                 :        *  This object now @e owns the object pointed to by @a p.  The
         240                 :        *  previous object has been deleted.
         241                 :        */
         242                 :       void
         243                 :       reset(element_type* __p = 0) throw()
         244                 :       {
         245                 :         if (__p != _M_ptr)
         246                 :           {
         247                 :             delete _M_ptr;
         248                 :             _M_ptr = __p;
         249                 :           }
         250                 :       }
         251                 :
         252                 :       /**
         253                 :        *  @brief  Automatic conversions
         254                 :        *
         255                 :        *  These operations convert an %auto_ptr into and from an auto_ptr_ref
         256                 :        *  automatically as needed.  This allows constructs such as
         257                 :        *  @code
         258                 :        *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
         259                 :        *    ...
         260                 :        *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
         261                 :        *  @endcode
         262                 :        */
         263             666 :       auto_ptr(auto_ptr_ref<element_type> __ref) throw()
         264             666 :       : _M_ptr(__ref._M_ptr) { }
         265                 :
         266                 :       auto_ptr&
         267                 :       operator=(auto_ptr_ref<element_type> __ref) throw()
         268                 :       {
         269                 :         if (__ref._M_ptr != this->get())
         270                 :           {
         271                 :             delete _M_ptr;
         272                 :             _M_ptr = __ref._M_ptr;
         273                 :           }
         274                 :         return *this;
         275                 :       }
         276                 :
         277                 :       template<typename _Tp1>
         278             666 :         operator auto_ptr_ref<_Tp1>() throw()
         279             666 :         { return auto_ptr_ref<_Tp1>(this->release()); }
         280                 :
         281                 :       template<typename _Tp1>
         282                 :         operator auto_ptr<_Tp1>() throw()
         283                 :         { return auto_ptr<_Tp1>(this->release()); }
         284                 :     } _GLIBCXX_DEPRECATED_ATTR;
         285                 :
         286                 :   // _GLIBCXX_RESOLVE_LIB_DEFECTS
         287                 :   // 541. shared_ptr template assignment and void
         288                 :   template<>
         289                 :     class auto_ptr<void>
         290                 :     {
         291                 :     public:
         292                 :       typedef void element_type;
         293                 :     } _GLIBCXX_DEPRECATED_ATTR;
         294                 :
         295                 : _GLIBCXX_END_NAMESPACE
         296                 :
         297                 : #endif /* _STL_AUTO_PTR_H */

Generated by: LTP GCOV extension version 1.6