4 * Hewlett-Packard Company
6 * Copyright (c) 1996,1997
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
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.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #ifndef _STLP_INTERNAL_CONSTRUCT_H
31 #define _STLP_INTERNAL_CONSTRUCT_H
33 #if !defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_INTERNAL_CSTRING)
34 # include <stl/_cstring.h>
37 #ifndef _STLP_INTERNAL_NEW
38 # include <stl/_new.h>
41 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
42 # include <stl/_iterator_base.h>
45 #ifndef _STLP_TYPE_TRAITS_H
46 # include <stl/type_traits.h>
49 #if !defined (_STLP_MOVE_CONSTRUCT_FWK_H) && !defined (_STLP_NO_MOVE_SEMANTIC)
50 # include <stl/_move_construct_fwk.h>
56 inline void __destroy_aux(_Tp* __pointer, const __false_type& /*_Trivial_destructor*/)
57 { __pointer->~_Tp(); }
60 inline void __destroy_aux(_Tp*, const __true_type& /*_Trivial_destructor*/) {}
63 inline void _Destroy(_Tp* __pointer) {
64 typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;
65 __destroy_aux(__pointer, _Trivial_destructor());
66 #if defined (_STLP_DEBUG_UNINITIALIZED)
67 memset(__REINTERPRET_CAST(char*, __pointer), _STLP_SHRED_BYTE, sizeof(_Tp));
72 inline void _Destroy_Moved(_Tp* __pointer) {
73 #if !defined (_STLP_NO_MOVE_SEMANTIC)
74 typedef typename __move_traits<_Tp>::complete _Trivial_destructor;
75 __destroy_aux(__pointer, _Trivial_destructor());
76 # if defined (_STLP_DEBUG_UNINITIALIZED)
77 memset((char*)__pointer, _STLP_SHRED_BYTE, sizeof(_Tp));
85 # define _STLP_NEW_REDEFINE new
90 inline void _Construct_aux (_T1* __p, const __false_type&) {
95 inline void _Construct_aux (_T1* __p, const __true_type&) {
96 #if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
99 // We use binary copying for POD types since it results
100 // in a considerably better code at least on MSVC.
102 #endif /* _STLP_DEF_CONST_PLCT_NEW_BUG */
106 inline void _Construct(_T1* __p) {
107 #if defined (_STLP_DEBUG_UNINITIALIZED)
108 memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
110 #if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
111 _Construct_aux (__p, _HasDefaultZeroValue(__p)._Answer());
113 _Construct_aux (__p, _Is_POD(__p)._Answer());
114 #endif /* _STLP_DEF_CONST_PLCT_NEW_BUG */
118 inline void _Copy_Construct_aux(_Tp* __p, const _Tp& __val, const __false_type&) {
123 inline void _Copy_Construct_aux(_Tp* __p, const _Tp& __val, const __true_type&) {
124 // We use binary copying for POD types since it results
125 // in a considerably better code at least on MSVC.
130 inline void _Copy_Construct(_Tp* __p, const _Tp& __val) {
131 #if defined (_STLP_DEBUG_UNINITIALIZED)
132 memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_Tp));
134 _Copy_Construct_aux(__p, __val, _Is_POD(__p)._Answer());
137 template <class _T1, class _T2>
138 inline void _Param_Construct_aux(_T1* __p, const _T2& __val, const __false_type&) {
142 template <class _T1, class _T2>
143 inline void _Param_Construct_aux(_T1* __p, const _T2& __val, const __true_type&) {
144 // We use binary copying for POD types since it results
145 // in a considerably better code at least on MSVC.
149 template <class _T1, class _T2>
150 inline void _Param_Construct(_T1* __p, const _T2& __val) {
151 #if defined (_STLP_DEBUG_UNINITIALIZED)
152 memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
154 _Param_Construct_aux(__p, __val, _Is_POD(__p)._Answer());
157 template <class _T1, class _T2>
158 inline void _Move_Construct_Aux(_T1* __p, _T2& __val, const __false_type& /*_IsPOD*/) {
159 #if !defined (_STLP_NO_MOVE_SEMANTIC)
160 new(__p) _T1(_STLP_PRIV _AsMoveSource(__val));
162 _Param_Construct(__p, __val);
166 template <class _T1, class _T2>
167 inline void _Move_Construct_Aux(_T1* __p, _T2& __val, const __true_type& /*_IsPOD*/) {
168 // We use binary copying for POD types since it results
169 // in a considerably better code at least on MSVC.
173 template <class _T1, class _T2>
174 inline void _Move_Construct(_T1* __p, _T2& __val) {
175 #if defined (_STLP_DEBUG_UNINITIALIZED)
176 memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
178 _Move_Construct_Aux(__p, __val, _Is_POD(__p)._Answer());
181 #if defined(_STLP_NEW_REDEFINE)
182 # if defined (DEBUG_NEW)
183 # define new DEBUG_NEW
185 # undef _STLP_NEW_REDEFINE
188 template <class _ForwardIterator, class _Tp>
189 _STLP_INLINE_LOOP void
190 __destroy_range_aux(_ForwardIterator __first, _ForwardIterator __last, _Tp*, const __false_type& /*_Trivial_destructor*/) {
191 for ( ; __first != __last; ++__first) {
192 __destroy_aux(&(*__first), __false_type());
193 #if defined (_STLP_DEBUG_UNINITIALIZED)
194 memset((char*)&(*__first), _STLP_SHRED_BYTE, sizeof(_Tp));
199 template <class _ForwardIterator, class _Tp>
200 #if defined (_STLP_DEBUG_UNINITIALIZED)
201 _STLP_INLINE_LOOP void
202 __destroy_range_aux(_ForwardIterator __first, _ForwardIterator __last, _Tp*, const __true_type& /*_Trivial_destructor*/) {
203 for ( ; __first != __last; ++__first)
204 memset((char*)&(*__first), _STLP_SHRED_BYTE, sizeof(_Tp));
208 __destroy_range_aux(_ForwardIterator, _ForwardIterator, _Tp*, const __true_type& /*_Trivial_destructor*/) {}
211 template <class _ForwardIterator, class _Tp>
213 __destroy_range(_ForwardIterator __first, _ForwardIterator __last, _Tp *__ptr) {
214 typedef typename __type_traits<_Tp>::has_trivial_destructor _Trivial_destructor;
215 __destroy_range_aux(__first, __last, __ptr, _Trivial_destructor());
218 template <class _ForwardIterator>
219 inline void _Destroy_Range(_ForwardIterator __first, _ForwardIterator __last) {
220 __destroy_range(__first, __last, _STLP_VALUE_TYPE(__first, _ForwardIterator));
223 inline void _Destroy_Range(char*, char*) {}
224 #if defined (_STLP_HAS_WCHAR_T) // dwa 8/15/97
225 inline void _Destroy_Range(wchar_t*, wchar_t*) {}
226 inline void _Destroy_Range(const wchar_t*, const wchar_t*) {}
229 #if !defined (_STLP_NO_MOVE_SEMANTIC)
230 template <class _ForwardIterator, class _Tp>
232 __destroy_mv_srcs(_ForwardIterator __first, _ForwardIterator __last, _Tp *__ptr) {
233 typedef typename __move_traits<_Tp>::complete _CompleteMove;
234 __destroy_range_aux(__first, __last, __ptr, _CompleteMove());
238 template <class _ForwardIterator>
239 inline void _Destroy_Moved_Range(_ForwardIterator __first, _ForwardIterator __last)
240 #if !defined (_STLP_NO_MOVE_SEMANTIC)
241 { __destroy_mv_srcs(__first, __last, _STLP_VALUE_TYPE(__first, _ForwardIterator)); }
243 { _Destroy_Range(__first, __last); }
246 #if defined (_STLP_DEF_CONST_DEF_PARAM_BUG)
247 // Those adaptors are here to fix common compiler bug regarding builtins:
248 // expressions like int k = int() should initialize k to 0
250 inline _Tp __default_constructed_aux(_Tp*, const __false_type&) {
254 inline _Tp __default_constructed_aux(_Tp*, const __true_type&) {
259 inline _Tp __default_constructed(_Tp* __p) {
260 return __default_constructed_aux(__p, _HasDefaultZeroValue(__p)._Answer());
263 # define _STLP_DEFAULT_CONSTRUCTED(_TTp) __default_constructed((_TTp*)0)
265 # define _STLP_DEFAULT_CONSTRUCTED(_TTp) _TTp()
266 #endif /* _STLP_DEF_CONST_DEF_PARAM_BUG */
269 #if !defined (_STLP_NO_ANACHRONISMS)
270 // --------------------------------------------------
271 // Old names from the HP STL.
273 template <class _T1, class _T2>
274 inline void construct(_T1* __p, const _T2& __val) {_Param_Construct(__p, __val); }
276 inline void construct(_T1* __p) { _STLP_STD::_Construct(__p); }
278 inline void destroy(_Tp* __pointer) { _STLP_STD::_Destroy(__pointer); }
279 template <class _ForwardIterator>
280 inline void destroy(_ForwardIterator __first, _ForwardIterator __last) { _STLP_STD::_Destroy_Range(__first, __last); }
281 #endif /* _STLP_NO_ANACHRONISMS */
285 #endif /* _STLP_INTERNAL_CONSTRUCT_H */