_construct.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  29. #define _STLP_INTERNAL_CONSTRUCT_H
  30. #if !defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_INTERNAL_CSTRING)
  31. # include <stl/_cstring.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_NEW
  34. # include <stl/_new.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  37. # include <stl/_iterator_base.h>
  38. #endif
  39. #ifndef _STLP_TYPE_TRAITS_H
  40. # include <stl/type_traits.h>
  41. #endif
  42. #if !defined (_STLP_MOVE_CONSTRUCT_FWK_H) && !defined (_STLP_NO_MOVE_SEMANTIC)
  43. # include <stl/_move_construct_fwk.h>
  44. #endif
  45. _STLP_BEGIN_NAMESPACE
  46. template <class _Tp>
  47. inline void __destroy_aux(_Tp* __pointer, const __false_type& /*_Trivial_destructor*/)
  48. { __pointer->~_Tp(); }
  49. template <class _Tp>
  50. inline void __destroy_aux(_Tp*, const __true_type& /*_Trivial_destructor*/) {}
  51. template <class _Tp>
  52. inline void _Destroy(_Tp* __pointer) {
  53. typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;
  54. __destroy_aux(__pointer, _Trivial_destructor());
  55. #if defined (_STLP_DEBUG_UNINITIALIZED)
  56. memset(__REINTERPRET_CAST(char*, __pointer), _STLP_SHRED_BYTE, sizeof(_Tp));
  57. #endif
  58. }
  59. template <class _Tp>
  60. inline void _Destroy_Moved(_Tp* __pointer) {
  61. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  62. typedef typename __move_traits<_Tp>::complete _Trivial_destructor;
  63. __destroy_aux(__pointer, _Trivial_destructor());
  64. # if defined (_STLP_DEBUG_UNINITIALIZED)
  65. memset((char*)__pointer, _STLP_SHRED_BYTE, sizeof(_Tp));
  66. # endif
  67. #else
  68. _Destroy(__pointer);
  69. #endif
  70. }
  71. #if defined (new)
  72. # define _STLP_NEW_REDEFINE new
  73. # undef new
  74. #endif
  75. template <class _T1>
  76. inline void _Construct_aux (_T1* __p, const __false_type&) {
  77. new(__p) _T1();
  78. }
  79. template <class _T1>
  80. inline void _Construct_aux (_T1* __p, const __true_type&) {
  81. #if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
  82. *__p = _T1(0);
  83. #else
  84. // We use binary copying for POD types since it results
  85. // in a considerably better code at least on MSVC.
  86. *__p = _T1();
  87. #endif /* _STLP_DEF_CONST_PLCT_NEW_BUG */
  88. }
  89. template <class _T1>
  90. inline void _Construct(_T1* __p) {
  91. #if defined (_STLP_DEBUG_UNINITIALIZED)
  92. memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
  93. #endif
  94. #if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
  95. _Construct_aux (__p, _HasDefaultZeroValue(__p)._Answer());
  96. #else
  97. _Construct_aux (__p, _Is_POD(__p)._Answer());
  98. #endif /* _STLP_DEF_CONST_PLCT_NEW_BUG */
  99. }
  100. template <class _Tp>
  101. inline void _Copy_Construct_aux(_Tp* __p, const _Tp& __val, const __false_type&) {
  102. ::new(__p) _Tp(__val);
  103. }
  104. template <class _Tp>
  105. inline void _Copy_Construct_aux(_Tp* __p, const _Tp& __val, const __true_type&) {
  106. // We use binary copying for POD types since it results
  107. // in a considerably better code at least on MSVC.
  108. *__p = __val;
  109. }
  110. template <class _Tp>
  111. inline void _Copy_Construct(_Tp* __p, const _Tp& __val) {
  112. #if defined (_STLP_DEBUG_UNINITIALIZED)
  113. memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_Tp));
  114. #endif
  115. _Copy_Construct_aux(__p, __val, _Is_POD(__p)._Answer());
  116. }
  117. template <class _T1, class _T2>
  118. inline void _Param_Construct_aux(_T1* __p, const _T2& __val, const __false_type&) {
  119. new(__p) _T1(__val);
  120. }
  121. template <class _T1, class _T2>
  122. inline void _Param_Construct_aux(_T1* __p, const _T2& __val, const __true_type&) {
  123. // We use binary copying for POD types since it results
  124. // in a considerably better code at least on MSVC.
  125. *__p = _T1(__val);
  126. }
  127. template <class _T1, class _T2>
  128. inline void _Param_Construct(_T1* __p, const _T2& __val) {
  129. #if defined (_STLP_DEBUG_UNINITIALIZED)
  130. memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
  131. #endif
  132. _Param_Construct_aux(__p, __val, _Is_POD(__p)._Answer());
  133. }
  134. template <class _T1, class _T2>
  135. inline void _Move_Construct_Aux(_T1* __p, _T2& __val, const __false_type& /*_IsPOD*/) {
  136. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  137. new(__p) _T1(_STLP_PRIV _AsMoveSource(__val));
  138. #else
  139. _Param_Construct(__p, __val);
  140. #endif
  141. }
  142. template <class _T1, class _T2>
  143. inline void _Move_Construct_Aux(_T1* __p, _T2& __val, const __true_type& /*_IsPOD*/) {
  144. // We use binary copying for POD types since it results
  145. // in a considerably better code at least on MSVC.
  146. *__p = _T1(__val);
  147. }
  148. template <class _T1, class _T2>
  149. inline void _Move_Construct(_T1* __p, _T2& __val) {
  150. #if defined (_STLP_DEBUG_UNINITIALIZED)
  151. memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
  152. #endif
  153. _Move_Construct_Aux(__p, __val, _Is_POD(__p)._Answer());
  154. }
  155. #if defined(_STLP_NEW_REDEFINE)
  156. # if defined (DEBUG_NEW)
  157. # define new DEBUG_NEW
  158. # endif
  159. # undef _STLP_NEW_REDEFINE
  160. #endif
  161. template <class _ForwardIterator, class _Tp>
  162. _STLP_INLINE_LOOP void
  163. __destroy_range_aux(_ForwardIterator __first, _ForwardIterator __last, _Tp*, const __false_type& /*_Trivial_destructor*/) {
  164. for ( ; __first != __last; ++__first) {
  165. __destroy_aux(&(*__first), __false_type());
  166. #if defined (_STLP_DEBUG_UNINITIALIZED)
  167. memset((char*)&(*__first), _STLP_SHRED_BYTE, sizeof(_Tp));
  168. #endif
  169. }
  170. }
  171. template <class _ForwardIterator, class _Tp>
  172. #if defined (_STLP_DEBUG_UNINITIALIZED)
  173. _STLP_INLINE_LOOP void
  174. __destroy_range_aux(_ForwardIterator __first, _ForwardIterator __last, _Tp*, const __true_type& /*_Trivial_destructor*/) {
  175. for ( ; __first != __last; ++__first)
  176. memset((char*)&(*__first), _STLP_SHRED_BYTE, sizeof(_Tp));
  177. }
  178. #else
  179. inline void
  180. __destroy_range_aux(_ForwardIterator, _ForwardIterator, _Tp*, const __true_type& /*_Trivial_destructor*/) {}
  181. #endif
  182. template <class _ForwardIterator, class _Tp>
  183. inline void
  184. __destroy_range(_ForwardIterator __first, _ForwardIterator __last, _Tp *__ptr) {
  185. typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;
  186. __destroy_range_aux(__first, __last, __ptr, _Trivial_destructor());
  187. }
  188. template <class _ForwardIterator>
  189. inline void _Destroy_Range(_ForwardIterator __first, _ForwardIterator __last) {
  190. __destroy_range(__first, __last, _STLP_VALUE_TYPE(__first, _ForwardIterator));
  191. }
  192. inline void _Destroy_Range(char*, char*) {}
  193. #if defined (_STLP_HAS_WCHAR_T) // dwa 8/15/97
  194. inline void _Destroy_Range(wchar_t*, wchar_t*) {}
  195. inline void _Destroy_Range(const wchar_t*, const wchar_t*) {}
  196. #endif
  197. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  198. template <class _ForwardIterator, class _Tp>
  199. inline void
  200. __destroy_mv_srcs(_ForwardIterator __first, _ForwardIterator __last, _Tp *__ptr) {
  201. typedef typename __move_traits<_Tp>::complete _CompleteMove;
  202. __destroy_range_aux(__first, __last, __ptr, _CompleteMove());
  203. }
  204. #endif
  205. template <class _ForwardIterator>
  206. inline void _Destroy_Moved_Range(_ForwardIterator __first, _ForwardIterator __last)
  207. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  208. { __destroy_mv_srcs(__first, __last, _STLP_VALUE_TYPE(__first, _ForwardIterator)); }
  209. #else
  210. { _Destroy_Range(__first, __last); }
  211. #endif
  212. #if defined (_STLP_DEF_CONST_DEF_PARAM_BUG)
  213. // Those adaptors are here to fix common compiler bug regarding builtins:
  214. // expressions like int k = int() should initialize k to 0
  215. template <class _Tp>
  216. inline _Tp __default_constructed_aux(_Tp*, const __false_type&) {
  217. return _Tp();
  218. }
  219. template <class _Tp>
  220. inline _Tp __default_constructed_aux(_Tp*, const __true_type&) {
  221. return _Tp(0);
  222. }
  223. template <class _Tp>
  224. inline _Tp __default_constructed(_Tp* __p) {
  225. return __default_constructed_aux(__p, _HasDefaultZeroValue(__p)._Answer());
  226. }
  227. # define _STLP_DEFAULT_CONSTRUCTED(_TTp) __default_constructed((_TTp*)0)
  228. #else
  229. # define _STLP_DEFAULT_CONSTRUCTED(_TTp) _TTp()
  230. #endif /* _STLP_DEF_CONST_DEF_PARAM_BUG */
  231. #if !defined (_STLP_NO_ANACHRONISMS)
  232. // --------------------------------------------------
  233. // Old names from the HP STL.
  234. template <class _T1, class _T2>
  235. inline void construct(_T1* __p, const _T2& __val) {_Param_Construct(__p, __val); }
  236. template <class _T1>
  237. inline void construct(_T1* __p) { _STLP_STD::_Construct(__p); }
  238. template <class _Tp>
  239. inline void destroy(_Tp* __pointer) { _STLP_STD::_Destroy(__pointer); }
  240. template <class _ForwardIterator>
  241. inline void destroy(_ForwardIterator __first, _ForwardIterator __last) { _STLP_STD::_Destroy_Range(__first, __last); }
  242. #endif /* _STLP_NO_ANACHRONISMS */
  243. _STLP_END_NAMESPACE
  244. #endif /* _STLP_INTERNAL_CONSTRUCT_H */
  245. // Local Variables:
  246. // mode:C++
  247. // End: