Home | History | Annotate | Download | only in stl
      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 
     26 /* NOTE: This is an internal header file, included by other STL headers.
     27  *   You should not attempt to use it directly.
     28  */
     29 
     30 #ifndef _STLP_INTERNAL_CONSTRUCT_H
     31 #define _STLP_INTERNAL_CONSTRUCT_H
     32 
     33 #if !defined (_STLP_DEBUG_UNINITIALIZED) && !defined (_STLP_INTERNAL_CSTRING)
     34 #  include <stl/_cstring.h>
     35 #endif
     36 
     37 #ifndef _STLP_INTERNAL_NEW
     38 #  include <stl/_new.h>
     39 #endif
     40 
     41 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
     42 #  include <stl/_iterator_base.h>
     43 #endif
     44 
     45 #ifndef _STLP_TYPE_TRAITS_H
     46 #  include <stl/type_traits.h>
     47 #endif
     48 
     49 #if !defined (_STLP_MOVE_CONSTRUCT_FWK_H) && !defined (_STLP_NO_MOVE_SEMANTIC)
     50 #  include <stl/_move_construct_fwk.h>
     51 #endif
     52 
     53 _STLP_BEGIN_NAMESPACE
     54 
     55 template <class _Tp>
     56 inline void __destroy_aux(_Tp* __pointer, const __false_type& /*_Trivial_destructor*/)
     57 { __pointer->~_Tp(); }
     58 
     59 template <class _Tp>
     60 inline void __destroy_aux(_Tp*, const __true_type& /*_Trivial_destructor*/) {}
     61 
     62 template <class _Tp>
     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));
     68 #endif
     69 }
     70 
     71 template <class _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));
     78 #  endif
     79 #else
     80   _Destroy(__pointer);
     81 #endif
     82 }
     83 
     84 #if defined (new)
     85 #  define _STLP_NEW_REDEFINE new
     86 #  undef new
     87 #endif
     88 
     89 template <class _T1>
     90 inline void _Construct_aux (_T1* __p, const __false_type&) {
     91   new(__p) _T1();
     92 }
     93 
     94 template <class _T1>
     95 inline void _Construct_aux (_T1* __p, const __true_type&) {
     96 #if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
     97   *__p = _T1(0);
     98 #else
     99   // We use binary copying for POD types since it results
    100   // in a considerably better code at least on MSVC.
    101   *__p = _T1();
    102 #endif /* _STLP_DEF_CONST_PLCT_NEW_BUG */
    103 }
    104 
    105 template <class _T1>
    106 inline void _Construct(_T1* __p) {
    107 #if defined (_STLP_DEBUG_UNINITIALIZED)
    108   memset((char*)__p, _STLP_SHRED_BYTE, sizeof(_T1));
    109 #endif
    110 #if defined (_STLP_DEF_CONST_PLCT_NEW_BUG)
    111   _Construct_aux (__p, _HasDefaultZeroValue(__p)._Answer());
    112 #else
    113   _Construct_aux (__p, _Is_POD(__p)._Answer());
    114 #endif /* _STLP_DEF_CONST_PLCT_NEW_BUG */
    115 }
    116 
    117 template <class _Tp>
    118 inline void _Copy_Construct_aux(_Tp* __p, const _Tp& __val, const __false_type&) {
    119   new(__p) _Tp(__val);
    120 }
    121 
    122 template <class _Tp>
    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.
    126   *__p = __val;
    127 }
    128 
    129 template <class _Tp>
    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));
    133 #endif
    134   _Copy_Construct_aux(__p, __val, _Is_POD(__p)._Answer());
    135 }
    136 
    137 template <class _T1, class _T2>
    138 inline void _Param_Construct_aux(_T1* __p, const _T2& __val, const __false_type&) {
    139   new(__p) _T1(__val);
    140 }
    141 
    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.
    146   *__p = _T1(__val);
    147 }
    148 
    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));
    153 #endif
    154   _Param_Construct_aux(__p, __val, _Is_POD(__p)._Answer());
    155 }
    156 
    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));
    161 #else
    162   _Param_Construct(__p, __val);
    163 #endif
    164 }
    165 
    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.
    170   *__p = _T1(__val);
    171 }
    172 
    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));
    177 #endif
    178   _Move_Construct_Aux(__p, __val, _Is_POD(__p)._Answer());
    179 }
    180 
    181 #if defined(_STLP_NEW_REDEFINE)
    182 #  if defined (DEBUG_NEW)
    183 #    define new DEBUG_NEW
    184 #  endif
    185 #  undef _STLP_NEW_REDEFINE
    186 #endif
    187 
    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));
    195 #endif
    196   }
    197 }
    198 
    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));
    205 }
    206 #else
    207 inline void
    208 __destroy_range_aux(_ForwardIterator, _ForwardIterator, _Tp*, const __true_type& /*_Trivial_destructor*/) {}
    209 #endif
    210 
    211 template <class _ForwardIterator, class _Tp>
    212 inline void
    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());
    216 }
    217 
    218 template <class _ForwardIterator>
    219 inline void _Destroy_Range(_ForwardIterator __first, _ForwardIterator __last) {
    220   __destroy_range(__first, __last, _STLP_VALUE_TYPE(__first, _ForwardIterator));
    221 }
    222 
    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*) {}
    227 #endif
    228 
    229 #if !defined (_STLP_NO_MOVE_SEMANTIC)
    230 template <class _ForwardIterator, class _Tp>
    231 inline void
    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());
    235 }
    236 #endif
    237 
    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)); }
    242 #else
    243 { _Destroy_Range(__first, __last); }
    244 #endif
    245 
    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
    249 template <class _Tp>
    250 inline _Tp __default_constructed_aux(_Tp*, const __false_type&) {
    251   return _Tp();
    252 }
    253 template <class _Tp>
    254 inline _Tp __default_constructed_aux(_Tp*, const __true_type&) {
    255   return _Tp(0);
    256 }
    257 
    258 template <class _Tp>
    259 inline _Tp __default_constructed(_Tp* __p) {
    260   return __default_constructed_aux(__p, _HasDefaultZeroValue(__p)._Answer());
    261 }
    262 
    263 #  define _STLP_DEFAULT_CONSTRUCTED(_TTp) __default_constructed((_TTp*)0)
    264 #else
    265 #  define _STLP_DEFAULT_CONSTRUCTED(_TTp) _TTp()
    266 #endif /* _STLP_DEF_CONST_DEF_PARAM_BUG */
    267 
    268 
    269 #if !defined (_STLP_NO_ANACHRONISMS)
    270 // --------------------------------------------------
    271 // Old names from the HP STL.
    272 
    273 template <class _T1, class _T2>
    274 inline void construct(_T1* __p, const _T2& __val) {_Param_Construct(__p, __val); }
    275 template <class _T1>
    276 inline void construct(_T1* __p) { _STLP_STD::_Construct(__p); }
    277 template <class _Tp>
    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 */
    282 
    283 _STLP_END_NAMESPACE
    284 
    285 #endif /* _STLP_INTERNAL_CONSTRUCT_H */
    286 
    287 // Local Variables:
    288 // mode:C++
    289 // End:
    290