memory

Go to the documentation of this file.
00001 // <memory> -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2004 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 along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // 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 /*
00031  * Copyright (c) 1997-1999
00032  * Silicon Graphics Computer Systems, Inc.
00033  *
00034  * Permission to use, copy, modify, distribute and sell this software
00035  * and its documentation for any purpose is hereby granted without fee,
00036  * provided that the above copyright notice appear in all copies and
00037  * that both that copyright notice and this permission notice appear
00038  * in supporting documentation.  Silicon Graphics makes no
00039  * representations about the suitability of this software for any
00040  * purpose.  It is provided "as is" without express or implied warranty.
00041  *
00042  */
00043 
00048 #ifndef _GLIBCXX_MEMORY
00049 #define _GLIBCXX_MEMORY 1
00050 
00051 #pragma GCC system_header
00052 
00053 #include <bits/stl_algobase.h>
00054 #include <bits/allocator.h>
00055 #include <bits/stl_construct.h>
00056 #include <bits/stl_iterator_base_types.h> //for iterator_traits
00057 #include <bits/stl_uninitialized.h>
00058 #include <bits/stl_raw_storage_iter.h>
00059 #include <debug/debug.h>
00060 #include <limits>
00061 
00062 namespace std
00063 {
00072   template<typename _Tp>
00073     pair<_Tp*, ptrdiff_t>
00074     __get_temporary_buffer(ptrdiff_t __len, _Tp*)
00075     {
00076       const ptrdiff_t __max = numeric_limits<ptrdiff_t>::max() / sizeof(_Tp);
00077       if (__len > __max)
00078     __len = __max;
00079       
00080       while (__len > 0) 
00081     {
00082       _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 
00083                             nothrow));
00084       if (__tmp != 0)
00085         return pair<_Tp*, ptrdiff_t>(__tmp, __len);
00086       __len /= 2;
00087     }
00088       return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
00089     }
00090 
00108   template<typename _Tp>
00109     inline pair<_Tp*, ptrdiff_t>
00110     get_temporary_buffer(ptrdiff_t __len)
00111     { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); }
00112 
00120   template<typename _Tp>
00121     void
00122     return_temporary_buffer(_Tp* __p)
00123     { ::operator delete(__p, nothrow); }
00124 
00132   template<typename _Tp1>
00133     struct auto_ptr_ref
00134     {
00135       _Tp1* _M_ptr;
00136       
00137       explicit
00138       auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
00139     };
00140 
00141 
00173   template<typename _Tp>
00174     class auto_ptr
00175     {
00176     private:
00177       _Tp* _M_ptr;
00178       
00179     public:
00181       typedef _Tp element_type;
00182       
00189       explicit
00190       auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
00191 
00199       auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
00200 
00211       template<typename _Tp1>
00212         auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
00213 
00222       auto_ptr&
00223       operator=(auto_ptr& __a) throw()
00224       {
00225     reset(__a.release());
00226     return *this;
00227       }
00228 
00239       template<typename _Tp1>
00240         auto_ptr&
00241         operator=(auto_ptr<_Tp1>& __a) throw()
00242         {
00243       reset(__a.release());
00244       return *this;
00245     }
00246 
00259       ~auto_ptr() { delete _M_ptr; }
00260       
00269       element_type&
00270       operator*() const throw() 
00271       {
00272     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
00273     return *_M_ptr; 
00274       }
00275       
00282       element_type*
00283       operator->() const throw() 
00284       {
00285     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
00286     return _M_ptr; 
00287       }
00288       
00299       element_type*
00300       get() const throw() { return _M_ptr; }
00301       
00313       element_type*
00314       release() throw()
00315       {
00316     element_type* __tmp = _M_ptr;
00317     _M_ptr = 0;
00318     return __tmp;
00319       }
00320       
00328       void
00329       reset(element_type* __p = 0) throw()
00330       {
00331     if (__p != _M_ptr)
00332       {
00333         delete _M_ptr;
00334         _M_ptr = __p;
00335       }
00336       }
00337       
00349       auto_ptr(auto_ptr_ref<element_type> __ref) throw()
00350       : _M_ptr(__ref._M_ptr) { }
00351       
00352       auto_ptr&
00353       operator=(auto_ptr_ref<element_type> __ref) throw()
00354       {
00355     if (__ref._M_ptr != this->get())
00356       {
00357         delete _M_ptr;
00358         _M_ptr = __ref._M_ptr;
00359       }
00360     return *this;
00361       }
00362       
00363       template<typename _Tp1>
00364         operator auto_ptr_ref<_Tp1>() throw()
00365         { return auto_ptr_ref<_Tp1>(this->release()); }
00366 
00367       template<typename _Tp1>
00368         operator auto_ptr<_Tp1>() throw()
00369         { return auto_ptr<_Tp1>(this->release()); }
00370   };
00371 } // namespace std
00372 
00373 #endif /* _GLIBCXX_MEMORY */

Generated on Tue Feb 2 16:56:16 2010 for GNU C++ STL by  doxygen 1.4.7