Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
5.9 KiB

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