memory

00001 // <memory> -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002 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 _CPP_MEMORY
00050 #define _CPP_MEMORY 1
00051 
00052 #pragma GCC system_header
00053 
00054 #include <bits/stl_algobase.h>
00055 #include <bits/stl_alloc.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 
00061 // Since this entire file is within namespace std, there's no reason to
00062 // waste two spaces along the left column.  Thus the leading indentation is
00063 // slightly violated from here on.
00064 namespace std
00065 {
00074 template <typename _Tp>
00075 pair<_Tp*, ptrdiff_t>
00076 __get_temporary_buffer(ptrdiff_t __len, _Tp*)
00077 {
00078   if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
00079     __len = INT_MAX / sizeof(_Tp);
00080 
00081   while (__len > 0) {
00082     _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
00083     if (__tmp != 0)
00084       return pair<_Tp*, ptrdiff_t>(__tmp, __len);
00085     __len /= 2;
00086   }
00087 
00088   return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
00089 }
00090 
00106 template<typename _Tp>
00107   inline pair<_Tp*,ptrdiff_t>
00108   get_temporary_buffer(ptrdiff_t __len)
00109   {
00110     return __get_temporary_buffer(__len, (_Tp*) 0);
00111   }
00112 
00120 template<typename _Tp>
00121   void
00122   return_temporary_buffer(_Tp* __p)
00123   {
00124     std::free(__p);
00125   }
00126 
00127 
00135 template<typename _Tp1>
00136   struct auto_ptr_ref
00137 {
00138    _Tp1* _M_ptr;
00139 
00140    explicit
00141    auto_ptr_ref(_Tp1* __p)
00142    : _M_ptr(__p) {}
00143 };
00144 
00145 
00175 template<typename _Tp>
00176   class auto_ptr
00177 {
00178 private:
00179   _Tp* _M_ptr;
00180 
00181 public:
00183   typedef _Tp element_type;
00184 
00191   explicit
00192   auto_ptr(element_type* __p = 0) throw()
00193   : _M_ptr(__p) { }
00194 
00202   auto_ptr(auto_ptr& __a) throw()
00203   : _M_ptr(__a.release()) { }
00204 
00214   template<typename _Tp1>
00215     auto_ptr(auto_ptr<_Tp1>& __a) throw()
00216     : _M_ptr(__a.release()) { }
00217 
00226   auto_ptr&
00227   operator=(auto_ptr& __a) throw()
00228     {
00229       reset(__a.release());
00230       return *this;
00231     }
00232 
00243   template <typename _Tp1>
00244     auto_ptr&
00245     operator=(auto_ptr<_Tp1>& __a) throw()
00246     {
00247       reset(__a.release());
00248       return *this;
00249     }
00250 
00263   ~auto_ptr() { delete _M_ptr; }
00264 
00273   element_type&
00274   operator*() const throw() { return *_M_ptr; }
00275 
00282   element_type*
00283   operator->() const throw() { return _M_ptr; }
00284 
00295   element_type*
00296   get() const throw() { return _M_ptr; }
00297 
00309   element_type*
00310   release() throw()
00311     {
00312       element_type* __tmp = _M_ptr;
00313       _M_ptr = 0;
00314       return __tmp;
00315     }
00316 
00324   void
00325   reset(element_type* __p = 0) throw()
00326     {
00327       if (__p != _M_ptr)
00328         {
00329           delete _M_ptr;
00330           _M_ptr = __p;
00331         }
00332     }
00333 
00345   auto_ptr(auto_ptr_ref<element_type> __ref) throw()
00346     : _M_ptr(__ref._M_ptr) {}
00347 
00348   auto_ptr&
00349   operator=(auto_ptr_ref<element_type> __ref) throw()
00350     {
00351       if (__ref._M_ptr != this->get())
00352         {
00353           delete _M_ptr;
00354           _M_ptr = __ref._M_ptr;
00355         }
00356       return *this;
00357     }
00358 
00359   template<typename _Tp1>
00360     operator auto_ptr_ref<_Tp1>() throw()
00361       { return auto_ptr_ref<_Tp1>(this->release()); }
00362 
00363   template<typename _Tp1>
00364     operator auto_ptr<_Tp1>() throw()
00365       { return auto_ptr<_Tp1>(this->release()); }
00367 };
00368 
00369 } // namespace std
00370 
00371 #endif /* _CPP_MEMORY */

Generated on Tue Dec 23 12:34:02 2003 for libstdc++-v3 Source by doxygen 1.3.4