home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / cwin / c.exe / $INSTDIR / include / c++ / memory < prev    next >
Encoding:
Text File  |  2003-12-15  |  11.1 KB  |  372 lines

  1. // <memory> -*- C++ -*-
  2.  
  3. // Copyright (C) 2001, 2002 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 along
  17. // with this library; see the file COPYING.  If not, write to the Free
  18. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. // 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. /*
  31.  * Copyright (c) 1997-1999
  32.  * Silicon Graphics Computer Systems, Inc.
  33.  *
  34.  * Permission to use, copy, modify, distribute and sell this software
  35.  * and its documentation for any purpose is hereby granted without fee,
  36.  * provided that the above copyright notice appear in all copies and
  37.  * that both that copyright notice and this permission notice appear
  38.  * in supporting documentation.  Silicon Graphics makes no
  39.  * representations about the suitability of this software for any
  40.  * purpose.  It is provided "as is" without express or implied warranty.
  41.  *
  42.  */
  43.  
  44. /** @file memory
  45.  *  This is a Standard C++ Library header.  You should @c #include this header
  46.  *  in your programs, rather than any of the "st[dl]_*.h" implementation files.
  47.  */
  48.  
  49. #ifndef _CPP_MEMORY
  50. #define _CPP_MEMORY 1
  51.  
  52. #pragma GCC system_header
  53.  
  54. #include <bits/stl_algobase.h>
  55. #include <bits/stl_alloc.h>
  56. #include <bits/stl_construct.h>
  57. #include <bits/stl_iterator_base_types.h> //for iterator_traits
  58. #include <bits/stl_uninitialized.h>
  59. #include <bits/stl_raw_storage_iter.h>
  60.  
  61. // Since this entire file is within namespace std, there's no reason to
  62. // waste two spaces along the left column.  Thus the leading indentation is
  63. // slightly violated from here on.
  64. namespace std
  65. {
  66. /**
  67.  *  @if maint
  68.  *  This is a helper function.  The unused second parameter exists to
  69.  *  permit the real get_temporary_buffer to use template parameter deduction.
  70.  *
  71.  *  XXX This should perhaps use the pool.
  72.  *  @endif
  73. */
  74. template <typename _Tp>
  75. pair<_Tp*, ptrdiff_t>
  76. __get_temporary_buffer(ptrdiff_t __len, _Tp*)
  77. {
  78.   if (__len > ptrdiff_t(INT_MAX / sizeof(_Tp)))
  79.     __len = INT_MAX / sizeof(_Tp);
  80.  
  81.   while (__len > 0) {
  82.     _Tp* __tmp = (_Tp*) std::malloc((std::size_t)__len * sizeof(_Tp));
  83.     if (__tmp != 0)
  84.       return pair<_Tp*, ptrdiff_t>(__tmp, __len);
  85.     __len /= 2;
  86.   }
  87.  
  88.   return pair<_Tp*, ptrdiff_t>((_Tp*)0, 0);
  89. }
  90.  
  91. /**
  92.  *  @brief This is a mostly-useless wrapper around malloc().
  93.  *  @param  len  The number of objects of type Tp.
  94.  *  @return   See full description.
  95.  *
  96.  *  Reinventing the wheel, but this time with prettier spokes!
  97.  *
  98.  *  This function tries to obtain storage for @c len adjacent Tp objects.
  99.  *  The objects themselves are not constructed, of course.  A pair<> is
  100.  *  returned containing "the buffer s address and capacity (in the units of
  101.  *  sizeof(Tp)), or a pair of 0 values if no storage can be obtained."
  102.  *  Note that the capacity obtained may be less than that requested if the
  103.  *  memory is unavailable; you should compare len with the .second return
  104.  *  value.
  105. */
  106. template<typename _Tp>
  107.   inline pair<_Tp*,ptrdiff_t>
  108.   get_temporary_buffer(ptrdiff_t __len)
  109.   {
  110.     return __get_temporary_buffer(__len, (_Tp*) 0);
  111.   }
  112.  
  113. /**
  114.  *  @brief The companion to get_temporary_buffer().
  115.  *  @param  p  A buffer previously allocated by get_temporary_buffer.
  116.  *  @return   None.
  117.  *
  118.  *  Frees the memory pointed to by p.
  119.  */
  120. template<typename _Tp>
  121.   void
  122.   return_temporary_buffer(_Tp* __p)
  123.   {
  124.     std::free(__p);
  125.   }
  126.  
  127.  
  128. /**
  129.  *  A wrapper class to provide auto_ptr with reference semantics.  For
  130.  *  example, an auto_ptr can be assigned (or constructed from) the result of
  131.  *  a function which returns an auto_ptr by value.
  132.  *
  133.  *  All the auto_ptr_ref stuff should happen behind the scenes.
  134. */
  135. template<typename _Tp1>
  136.   struct auto_ptr_ref
  137. {
  138.    _Tp1* _M_ptr;
  139.  
  140.    explicit
  141.    auto_ptr_ref(_Tp1* __p)
  142.    : _M_ptr(__p) {}
  143. };
  144.  
  145.  
  146. /**
  147.  *  @brief  A simple smart pointer providing strict ownership semantics.
  148.  *
  149.  *  The Standard says:
  150.  *  <pre>
  151.  *  An @c auto_ptr owns the object it holds a pointer to.  Copying an
  152.  *  @c auto_ptr copies the pointer and transfers ownership to the destination.
  153.  *  If more than one @c auto_ptr owns the same object at the same time the
  154.  *  behavior of the program is undefined.
  155.  *
  156.  *  The uses of @c auto_ptr include providing temporary exception-safety for
  157.  *  dynamically allocated memory, passing ownership of dynamically allocated
  158.  *  memory to a function, and returning dynamically allocated memory from a
  159.  *  function.  @c auto_ptr does not meet the CopyConstructible and Assignable
  160.  *  requirements for Standard Library <a href="tables.html#65">container</a>
  161.  *  elements and thus instantiating a Standard Library container with an
  162.  *  @c auto_ptr results in undefined behavior.
  163.  *  </pre>
  164.  *  Quoted from [20.4.5]/3.
  165.  *
  166.  *  Good examples of what can and cannot be done with auto_ptr can be found
  167.  *  in the libstdc++ testsuite.
  168.  *
  169.  *  @if maint
  170.  *  _GLIBCPP_RESOLVE_LIB_DEFECTS
  171.  *  127.  auto_ptr<> conversion issues
  172.  *  These resolutions have all been incorporated.
  173.  *  @endif
  174. */
  175. template<typename _Tp>
  176.   class auto_ptr
  177. {
  178. private:
  179.   _Tp* _M_ptr;
  180.  
  181. public:
  182.   /// The pointed-to type.
  183.   typedef _Tp element_type;
  184.  
  185.   /**
  186.    *  @brief  An %auto_ptr is usually constructed from a raw pointer.
  187.    *  @param  p  A pointer (defaults to NULL).
  188.    *
  189.    *  This object now @e owns the object pointed to by @a p.
  190.   */
  191.   explicit
  192.   auto_ptr(element_type* __p = 0) throw()
  193.   : _M_ptr(__p) { }
  194.  
  195.   /**
  196.    *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
  197.    *  @param  a  Another %auto_ptr of the same type.
  198.    *
  199.    *  This object now @e owns the object previously owned by @a a, which has
  200.    *  given up ownsership.
  201.   */
  202.   auto_ptr(auto_ptr& __a) throw()
  203.   : _M_ptr(__a.release()) { }
  204.  
  205.   /**
  206.    *  @brief  An %auto_ptr can be constructed from another %auto_ptr.
  207.    *  @param  a  Another %auto_ptr of a different but related type.
  208.    *
  209.    *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
  210.    *
  211.    *  This object now @e owns the object previously owned by @a a, which has
  212.    *  given up ownsership.
  213.   */
  214.   template<typename _Tp1>
  215.     auto_ptr(auto_ptr<_Tp1>& __a) throw()
  216.     : _M_ptr(__a.release()) { }
  217.  
  218.   /**
  219.    *  @brief  %auto_ptr assignment operator.
  220.    *  @param  a  Another %auto_ptr of the same type.
  221.    *
  222.    *  This object now @e owns the object previously owned by @a a, which has
  223.    *  given up ownsership.  The object that this one @e used to own and
  224.    *  track has been deleted.
  225.   */
  226.   auto_ptr&
  227.   operator=(auto_ptr& __a) throw()
  228.     {
  229.       reset(__a.release());
  230.       return *this;
  231.     }
  232.  
  233.   /**
  234.    *  @brief  %auto_ptr assignment operator.
  235.    *  @param  a  Another %auto_ptr of a different but related type.
  236.    *
  237.    *  A pointer-to-Tp1 must be convertible to a pointer-to-Tp/element_type.
  238.    *
  239.    *  This object now @e owns the object previously owned by @a a, which has
  240.    *  given up ownsership.  The object that this one @e used to own and
  241.    *  track has been deleted.
  242.   */
  243.   template <typename _Tp1>
  244.     auto_ptr&
  245.     operator=(auto_ptr<_Tp1>& __a) throw()
  246.     {
  247.       reset(__a.release());
  248.       return *this;
  249.     }
  250.  
  251.   /**
  252.    *  When the %auto_ptr goes out of scope, the object it owns is deleted.
  253.    *  If it no longer owns anything (i.e., @c get() is @c NULL), then this
  254.    *  has no effect.
  255.    *
  256.    *  @if maint
  257.    *  The C++ standard says there is supposed to be an empty throw
  258.    *  specification here, but omitting it is standard conforming.  Its
  259.    *  presence can be detected only if _Tp::~_Tp() throws, but this is
  260.    *  prohibited.  [17.4.3.6]/2
  261.    *  @end maint
  262.   */
  263.   ~auto_ptr() { delete _M_ptr; }
  264.  
  265.   /**
  266.    *  @brief  Smart pointer dereferencing.
  267.    *
  268.    *  If this %auto_ptr no longer owns anything, then this operation will
  269.    *  crash.  (For a smart pointer, "no longer owns anything" is the same as
  270.    *  being a null pointer, and you know what happens when you dereference
  271.    *  one of those...)
  272.   */
  273.   element_type&
  274.   operator*() const throw() { return *_M_ptr; }
  275.  
  276.   /**
  277.    *  @brief  Smart pointer dereferencing.
  278.    *
  279.    *  This returns the pointer itself, which the language then will
  280.    *  automatically cause to be dereferenced.
  281.   */
  282.   element_type*
  283.   operator->() const throw() { return _M_ptr; }
  284.  
  285.   /**
  286.    *  @brief  Bypassing the smart pointer.
  287.    *  @return  The raw pointer being managed.
  288.    *
  289.    *  You can get a copy of the pointer that this object owns, for
  290.    *  situations such as passing to a function which only accepts a raw
  291.    *  pointer.
  292.    *
  293.    *  @note  This %auto_ptr still owns the memory.
  294.   */
  295.   element_type*
  296.   get() const throw() { return _M_ptr; }
  297.  
  298.   /**
  299.    *  @brief  Bypassing the smart pointer.
  300.    *  @return  The raw pointer being managed.
  301.    *
  302.    *  You can get a copy of the pointer that this object owns, for
  303.    *  situations such as passing to a function which only accepts a raw
  304.    *  pointer.
  305.    *
  306.    *  @note  This %auto_ptr no longer owns the memory.  When this object
  307.    *  goes out of scope, nothing will happen.
  308.   */
  309.   element_type*
  310.   release() throw()
  311.     {
  312.       element_type* __tmp = _M_ptr;
  313.       _M_ptr = 0;
  314.       return __tmp;
  315.     }
  316.  
  317.   /**
  318.    *  @brief  Forcibly deletes the managed object.
  319.    *  @param  p  A pointer (defaults to NULL).
  320.    *
  321.    *  This object now @e owns the object pointed to by @a p.  The previous
  322.    *  object has been deleted.
  323.   */
  324.   void
  325.   reset(element_type* __p = 0) throw()
  326.     {
  327.       if (__p != _M_ptr)
  328.         {
  329.           delete _M_ptr;
  330.           _M_ptr = __p;
  331.         }
  332.     }
  333.  
  334.   /** @{
  335.    *  @brief  Automatic conversions
  336.    *
  337.    *  These operations convert an %auto_ptr into and from an auto_ptr_ref
  338.    *  automatically as needed.  This allows constructs such as
  339.    *  @code
  340.    *    auto_ptr<Derived>  func_returning_auto_ptr(.....);
  341.    *    ...
  342.    *    auto_ptr<Base> ptr = func_returning_auto_ptr(.....);
  343.    *  @endcode
  344.   */
  345.   auto_ptr(auto_ptr_ref<element_type> __ref) throw()
  346.     : _M_ptr(__ref._M_ptr) {}
  347.  
  348.   auto_ptr&
  349.   operator=(auto_ptr_ref<element_type> __ref) throw()
  350.     {
  351.       if (__ref._M_ptr != this->get())
  352.         {
  353.           delete _M_ptr;
  354.           _M_ptr = __ref._M_ptr;
  355.         }
  356.       return *this;
  357.     }
  358.  
  359.   template<typename _Tp1>
  360.     operator auto_ptr_ref<_Tp1>() throw()
  361.       { return auto_ptr_ref<_Tp1>(this->release()); }
  362.  
  363.   template<typename _Tp1>
  364.     operator auto_ptr<_Tp1>() throw()
  365.       { return auto_ptr<_Tp1>(this->release()); }
  366.   /** @}  */
  367. };
  368.  
  369. } // namespace std
  370.  
  371. #endif /* _CPP_MEMORY */
  372.