home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / memory < prev    next >
Text File  |  1998-06-16  |  5KB  |  195 lines

  1. // memory standard header
  2.  
  3. #if     _MSC_VER > 1000
  4. #pragma once
  5. #endif
  6.  
  7. #ifndef _MEMORY_
  8. #define _MEMORY_
  9. #include <xmemory>
  10.  
  11. #ifdef  _MSC_VER
  12. #pragma pack(push,8)
  13. #endif  /* _MSC_VER */
  14.         // TEMPLATE OPERATOR new
  15. template<class _Ty>
  16.     inline void *operator new(size_t _N, std::allocator<_Ty>& _Al)
  17.     {return (_Al._Charalloc(_N)); }
  18. _STD_BEGIN
  19.         // TEMPLATE FUNCTION get_temporary_buffer
  20. template<class _Ty> inline
  21.     pair<_Ty _FARQ *, _PDFT>
  22.         get_temporary_buffer(_PDFT _N, _Ty _FARQ *)
  23.     {_Ty _FARQ *_P;
  24.     for (_P = 0; 0 < _N; _N /= 2)
  25.         if ((_P = (_Ty _FARQ *)operator new(
  26.             (_SIZT)_N * sizeof (_Ty), nothrow)) != 0)
  27.             break;
  28.     return (pair<_Ty _FARQ *, _PDFT>(_P, _N)); }
  29.         // TEMPLATE FUNCTION return_temporary_buffer
  30. template<class _Ty> inline
  31.     void return_temporary_buffer(_Ty *_P)
  32.     {operator delete(_P); }
  33.         // TEMPLATE FUNCTION uninitialized_copy
  34. template<class _II, class _FI> inline
  35.     _FI uninitialized_copy(_II _F, _II _L, _FI _X)
  36.     {for (; _F != _L; ++_X, ++_F)
  37.         _Construct(&*_X, *_F);
  38.     return (_X); }
  39.         // TEMPLATE FUNCTION uninitialized_fill
  40. template<class _FI, class _Ty> inline
  41.     void uninitialized_fill(_FI _F, _FI _L, const _Ty& _X)
  42.     {for (; _F != _L; ++_F)
  43.         _Construct(&*_F, _X); }
  44.         // TEMPLATE FUNCTION uninitialized_fill_n
  45. template<class _FI, class _S, class _Ty> inline
  46.     void uninitialized_fill_n(_FI _F, _S _N, const _Ty& _X)
  47.     {for (; 0 < _N; --_N, ++_F)
  48.         _Construct(&*_F, _X); }
  49.         // TEMPLATE CLASS raw_storage_iterator
  50. template<class _OI, class _Ty>
  51.     class raw_storage_iterator
  52.         : public iterator<output_iterator_tag, void, void> {
  53. public:
  54.     typedef _OI iterator_type;
  55.     typedef _Ty element_type;
  56.     explicit raw_storage_iterator(_OI _X)
  57.         : _Next(_X) {}
  58.     raw_storage_iterator<_OI, _Ty>& operator*()
  59.         {return (*this); }
  60.     raw_storage_iterator<_OI, _Ty>& operator=(const _Ty& _X)
  61.         {_Construct(&*_Next, _X);
  62.         return (*this); }
  63.     raw_storage_iterator<_OI, _Ty>& operator++()
  64.         {++_Next;
  65.         return (*this); }
  66.     raw_storage_iterator<_OI, _Ty> operator++(int)
  67.         {raw_storage_iterator<_OI, _Ty> _Ans = *this;
  68.         ++_Next;
  69.         return (_Ans); }
  70. private:
  71.     _OI _Next;
  72.     };
  73.         // TEMPLATE CLASS _Temp_iterator
  74. template<class _Ty>
  75.     class _Temp_iterator
  76.         : public iterator<output_iterator_tag, void, void> {
  77. public:
  78.     typedef _Ty _FARQ *_Pty;
  79.     _Temp_iterator(_PDFT _N = 0)
  80.         {pair<_Pty, _PDFT> _Pair =
  81.             get_temporary_buffer(_N, (_Pty)0);
  82.         _Buf._Begin = _Pair.first;
  83.         _Buf._Cur = _Pair.first;
  84.         _Buf._Hiwater = _Pair.first;
  85.         _Buf._Len = _Pair.second;
  86.         _Pb = &_Buf; }
  87.     _Temp_iterator(const _Temp_iterator<_Ty>& _X)
  88.         {_Buf._Begin = 0;
  89.         _Buf._Cur = 0;
  90.         _Buf._Hiwater = 0;
  91.         _Buf._Len = 0;
  92.         *this = _X; }
  93.     ~_Temp_iterator()
  94.         {if (_Buf._Begin != 0)
  95.             {for (_Pty _F = _Buf._Begin;
  96.                 _F != _Buf._Hiwater; ++_F)
  97.                 _Destroy(&*_F);
  98.             return_temporary_buffer(_Buf._Begin); }}
  99.     _Temp_iterator<_Ty>& operator=(const _Temp_iterator<_Ty>& _X)
  100.         {_Pb = _X._Pb;
  101.         return (*this); }
  102.     _Temp_iterator<_Ty>& operator=(const _Ty& _V)
  103.         {if (_Pb->_Cur < _Pb->_Hiwater)
  104.             *_Pb->_Cur++ = _V;
  105.         else
  106.             {_Construct(&*_Pb->_Cur, _V);
  107.             _Pb->_Hiwater = ++_Pb->_Cur; }
  108.         return (*this); }
  109.     _Temp_iterator<_Ty>& operator*()
  110.         {return (*this); }
  111.     _Temp_iterator<_Ty>& operator++()
  112.         {return (*this); }
  113.     _Temp_iterator<_Ty>& operator++(int)
  114.         {return (*this); }
  115.     _Temp_iterator<_Ty>& _Init()
  116.         {_Pb->_Cur = _Pb->_Begin;
  117.         return (*this); }
  118.     _Pty _First() const
  119.         {return (_Pb->_Begin); }
  120.     _Pty _Last() const
  121.         {return (_Pb->_Cur); }
  122.     _PDFT _Maxlen() const
  123.         {return (_Pb->_Len); }
  124. private:
  125.     struct _Bufpar {
  126.         _Pty _Begin;
  127.         _Pty _Cur;
  128.         _Pty _Hiwater;
  129.         _PDFT _Len;
  130.         } _Buf, *_Pb;
  131.     };
  132.         // TEMPLATE CLASS auto_ptr
  133. template<class _Ty>
  134.     class auto_ptr {
  135. public:
  136.     typedef _Ty element_type;
  137.     explicit auto_ptr(_Ty *_P = 0) _THROW0()
  138.         : _Owns(_P != 0), _Ptr(_P) {}
  139.     auto_ptr(const auto_ptr<_Ty>& _Y) _THROW0()
  140.         : _Owns(_Y._Owns), _Ptr(_Y.release()) {}
  141.     auto_ptr<_Ty>& operator=(const auto_ptr<_Ty>& _Y) _THROW0()
  142.         {if (this != &_Y)
  143.             {if (_Ptr != _Y.get())
  144.                 {if (_Owns)
  145.                     delete _Ptr;
  146.                 _Owns = _Y._Owns; }
  147.             else if (_Y._Owns)
  148.                 _Owns = true;
  149.             _Ptr = _Y.release(); }
  150.         return (*this); }
  151.     ~auto_ptr()
  152.         {if (_Owns)
  153.             delete _Ptr; }
  154.     _Ty& operator*() const _THROW0()
  155.         {return (*get()); }
  156.     _Ty *operator->() const _THROW0()
  157.         {return (get()); }
  158.     _Ty *get() const _THROW0()
  159.         {return (_Ptr); }
  160.     _Ty *release() const _THROW0()
  161.         {((auto_ptr<_Ty> *)this)->_Owns = false;
  162.         return (_Ptr); }
  163. private:
  164.     bool _Owns;
  165.     _Ty *_Ptr;
  166.     };
  167. _STD_END
  168. #ifdef  _MSC_VER
  169. #pragma pack(pop)
  170. #endif  /* _MSC_VER */
  171.  
  172. #endif /* _MEMORY_ */
  173.  
  174. /*
  175.  * Copyright (c) 1995 by P.J. Plauger.  ALL RIGHTS RESERVED. 
  176.  * Consult your license regarding permissions and restrictions.
  177.  */
  178.  
  179. /*
  180.  * This file is derived from software bearing the following
  181.  * restrictions:
  182.  *
  183.  * Copyright (c) 1994
  184.  * Hewlett-Packard Company
  185.  *
  186.  * Permission to use, copy, modify, distribute and sell this
  187.  * software and its documentation for any purpose is hereby
  188.  * granted without fee, provided that the above copyright notice
  189.  * appear in all copies and that both that copyright notice and
  190.  * this permission notice appear in supporting documentation.
  191.  * Hewlett-Packard Company makes no representations about the
  192.  * suitability of this software for any purpose. It is provided
  193.  * "as is" without express or implied warranty.
  194.  */
  195.