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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 
00049 #ifndef _GLIBCXX_MEMORY
00050 #define _GLIBCXX_MEMORY 1
00051 
00052 #pragma GCC system_header
00053 
00054 #include <bits/stl_algobase.h>
00055 #include <bits/allocator.h>
00056 #include <bits/stl_construct.h>
00057 #include <bits/stl_iterator_base_types.h> //for iterator_traits
00058 #include <bits/stl_uninitialized.h>
00059 #include <bits/stl_raw_storage_iter.h>
00060 #include <debug/debug.h>
00061 #include <limits>
00062 
00063 namespace std
00064 {
00073   template<typename _Tp>
00074     pair<_Tp*, ptrdiff_t>
00075     __get_temporary_buffer(ptrdiff_t __len, _Tp*)
00076     {
00077       const ptrdiff_t __max = numeric_limits<ptrdiff_t>::max() / sizeof(_Tp);
00078       if (__len > __max)
00079     __len = __max;
00080       
00081       while (__len > 0) 
00082     {
00083       _Tp* __tmp = static_cast<_Tp*>(::operator new(__len * sizeof(_Tp), 
00084                             nothrow));
00085       if (__tmp != 0)
00086         return pair<_Tp*, ptrdiff_t>(__tmp, __len);
00087       __len /= 2;
00088     }
00089       return pair<_Tp*, ptrdiff_t>(static_cast<_Tp*>(0), 0);
00090     }
00091 
00109   template<typename _Tp>
00110     inline pair<_Tp*, ptrdiff_t>
00111     get_temporary_buffer(ptrdiff_t __len)
00112     { return std::__get_temporary_buffer(__len, static_cast<_Tp*>(0)); }
00113 
00121   template<typename _Tp>
00122     void
00123     return_temporary_buffer(_Tp* __p)
00124     { ::operator delete(__p, nothrow); }
00125 
00133   template<typename _Tp1>
00134     struct auto_ptr_ref
00135     {
00136       _Tp1* _M_ptr;
00137       
00138       explicit
00139       auto_ptr_ref(_Tp1* __p): _M_ptr(__p) { }
00140     };
00141 
00142 
00174   template<typename _Tp>
00175     class auto_ptr
00176     {
00177     private:
00178       _Tp* _M_ptr;
00179       
00180     public:
00182       typedef _Tp element_type;
00183       
00190       explicit
00191       auto_ptr(element_type* __p = 0) throw() : _M_ptr(__p) { }
00192 
00200       auto_ptr(auto_ptr& __a) throw() : _M_ptr(__a.release()) { }
00201 
00212       template<typename _Tp1>
00213         auto_ptr(auto_ptr<_Tp1>& __a) throw() : _M_ptr(__a.release()) { }
00214 
00223       auto_ptr&
00224       operator=(auto_ptr& __a) throw()
00225       {
00226     reset(__a.release());
00227     return *this;
00228       }
00229 
00240       template<typename _Tp1>
00241         auto_ptr&
00242         operator=(auto_ptr<_Tp1>& __a) throw()
00243         {
00244       reset(__a.release());
00245       return *this;
00246     }
00247 
00260       ~auto_ptr() { delete _M_ptr; }
00261       
00270       element_type&
00271       operator*() const throw() 
00272       {
00273     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
00274     return *_M_ptr; 
00275       }
00276       
00283       element_type*
00284       operator->() const throw() 
00285       {
00286     _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
00287     return _M_ptr; 
00288       }
00289       
00300       element_type*
00301       get() const throw() { return _M_ptr; }
00302       
00314       element_type*
00315       release() throw()
00316       {
00317     element_type* __tmp = _M_ptr;
00318     _M_ptr = 0;
00319     return __tmp;
00320       }
00321       
00329       void
00330       reset(element_type* __p = 0) throw()
00331       {
00332     if (__p != _M_ptr)
00333       {
00334         delete _M_ptr;
00335         _M_ptr = __p;
00336       }
00337       }
00338       
00350       auto_ptr(auto_ptr_ref<element_type> __ref) throw()
00351       : _M_ptr(__ref._M_ptr) { }
00352       
00353       auto_ptr&
00354       operator=(auto_ptr_ref<element_type> __ref) throw()
00355       {
00356     if (__ref._M_ptr != this->get())
00357       {
00358         delete _M_ptr;
00359         _M_ptr = __ref._M_ptr;
00360       }
00361     return *this;
00362       }
00363       
00364       template<typename _Tp1>
00365         operator auto_ptr_ref<_Tp1>() throw()
00366         { return auto_ptr_ref<_Tp1>(this->release()); }
00367 
00368       template<typename _Tp1>
00369         operator auto_ptr<_Tp1>() throw()
00370         { return auto_ptr<_Tp1>(this->release()); }
00372   };
00373 } // namespace std
00374 
00375 #endif /* _GLIBCXX_MEMORY */

Generated on Tue Jan 30 17:31:51 2007 for GNU C++ STL by doxygen 1.3.6