/home/ntakagi/work/STLport-5.1.5/stlport/stl/debug/_vector.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996,1997
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _STLP_INTERNAL_DBG_VECTOR_H
00031 #define _STLP_INTERNAL_DBG_VECTOR_H
00032 
00033 #ifndef _STLP_DBG_ITERATOR_H
00034 #  include <stl/debug/_iterator.h>
00035 #endif
00036 
00037 #define _STLP_NON_DBG_VECTOR _STLP_PRIV _STLP_NON_DBG_NAME(vector) <_Tp, _Alloc>
00038 
00039 _STLP_BEGIN_NAMESPACE
00040 
00041 #if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
00042 template <class _Tp, class _Alloc>
00043 inline _Tp*
00044 value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_VECTOR >&)
00045 { return (_Tp*)0; }
00046 template <class _Tp, class _Alloc>
00047 inline random_access_iterator_tag
00048 iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_VECTOR >&)
00049 { return random_access_iterator_tag(); }
00050 #endif
00051 
00052 _STLP_MOVE_TO_PRIV_NAMESPACE
00053 
00054 template <class _Tp, class _NcIt>
00055 struct _Vector_const_traits;
00056 
00057 template <class _Tp, class _NcIt>
00058 struct _Vector_nonconst_traits {
00059   typedef _Nonconst_traits<_Tp> _BaseT;
00060   typedef _Tp value_type;
00061   typedef _Tp& reference;
00062   typedef _Tp* pointer;
00063   typedef _Vector_const_traits<_Tp, _NcIt> _ConstTraits;
00064   typedef _Vector_nonconst_traits<_Tp, _NcIt> _NonConstTraits;
00065 };
00066 
00067 template <class _Tp, class _NcIt>
00068 struct _Vector_const_traits {
00069   typedef _Const_traits<_Tp> _BaseT;
00070   typedef _Tp value_type;
00071   typedef const _Tp& reference;
00072   typedef const _Tp* pointer;
00073   typedef _Vector_const_traits<_Tp, _NcIt> _ConstTraits;
00074   typedef _Vector_nonconst_traits<_Tp, _NcIt> _NonConstTraits;
00075 };
00076 
00077 _STLP_TEMPLATE_NULL
00078 struct _Vector_nonconst_traits<bool, _Bit_iterator> {
00079   typedef _Bit_iterator::value_type value_type;
00080   typedef _Bit_iterator::reference reference;
00081   typedef _Bit_iterator::pointer pointer;
00082   typedef _Vector_const_traits<bool, _Bit_iterator> _ConstTraits;
00083   typedef _Vector_nonconst_traits<bool, _Bit_iterator> _NonConstTraits;
00084 };
00085 
00086 _STLP_TEMPLATE_NULL
00087 struct _Vector_const_traits<bool, _Bit_iterator> {
00088   typedef _Bit_const_iterator::value_type value_type;
00089   typedef _Bit_const_iterator::reference reference;
00090   typedef _Bit_const_iterator::pointer pointer;
00091   typedef _Vector_const_traits<bool, _Bit_iterator> _ConstTraits;
00092   typedef _Vector_nonconst_traits<bool, _Bit_iterator> _NonConstTraits;
00093 };
00094 
00095 _STLP_MOVE_TO_STD_NAMESPACE
00096 
00097 template <class _Tp, _STLP_DBG_ALLOCATOR_SELECT(_Tp) >
00098 class vector :
00099 #if !defined (__DMC__)
00100              private
00101 #endif
00102                      _STLP_PRIV __construct_checker< _STLP_NON_DBG_VECTOR >
00103 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00104              , public __stlport_class<vector<_Tp, _Alloc> >
00105 #endif
00106 {
00107 private:
00108   typedef _STLP_NON_DBG_VECTOR _Base;
00109   typedef vector<_Tp, _Alloc> _Self;
00110   typedef _STLP_PRIV __construct_checker<_STLP_NON_DBG_VECTOR > _ConstructCheck;
00111   _Base _M_non_dbg_impl;
00112   _STLP_PRIV __owned_list _M_iter_list;
00113 
00114 public:
00115   __IMPORT_CONTAINER_TYPEDEFS(_Base)
00116 
00117   typedef _STLP_PRIV _DBG_iter<_Base,
00118     _STLP_PRIV _DbgTraits<_STLP_PRIV _Vector_nonconst_traits<value_type, typename _Base::iterator> > > iterator;
00119 
00120   typedef _STLP_PRIV _DBG_iter<_Base,
00121     _STLP_PRIV _DbgTraits<_STLP_PRIV _Vector_const_traits<value_type, typename _Base::iterator> > > const_iterator;
00122 
00123 private:
00124   void _Invalidate_all()
00125   { _M_iter_list._Invalidate_all(); }
00126   void _Invalidate_iterator(const iterator& __it)
00127   { _STLP_PRIV __invalidate_iterator(&_M_iter_list, __it); }
00128   void _Invalidate_iterators(const iterator& __first, const iterator& __last)
00129   { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); }
00130 
00131   void _Check_Overflow(size_type __nb) {
00132     if (size() + __nb > capacity())
00133       _Invalidate_all();
00134   }
00135   void _Compare_Capacity (size_type __old_capacity) {
00136     if (capacity() > __old_capacity) {
00137       _Invalidate_all();
00138     }
00139   }
00140 
00141 public:
00142   _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
00143 
00144   allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); }
00145 
00146   iterator begin()             { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
00147   const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
00148   iterator end()               { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
00149   const_iterator end() const   { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
00150 
00151   reverse_iterator rbegin()             { return reverse_iterator(end()); }
00152   const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
00153   reverse_iterator rend()               { return reverse_iterator(begin()); }
00154   const_reverse_iterator rend() const   { return const_reverse_iterator(begin()); }
00155 
00156   size_type size() const        { return _M_non_dbg_impl.size(); }
00157   size_type max_size() const    { return _M_non_dbg_impl.max_size(); }
00158   size_type capacity() const    { return _M_non_dbg_impl.capacity(); }
00159   bool empty() const            { return _M_non_dbg_impl.empty(); }
00160 
00161   reference operator[](size_type __n) {
00162     _STLP_VERBOSE_ASSERT(__n < size(), _StlMsg_OUT_OF_BOUNDS)
00163     return _M_non_dbg_impl[__n];
00164   }
00165 
00166   const_reference operator[](size_type __n) const {
00167     _STLP_VERBOSE_ASSERT(__n < size(), _StlMsg_OUT_OF_BOUNDS)
00168     return _M_non_dbg_impl[__n];
00169   }
00170 
00171   reference at(size_type __n) { return _M_non_dbg_impl.at(__n); }
00172   const_reference at(size_type __n) const { return _M_non_dbg_impl.at(__n); }
00173 
00174   explicit vector(const allocator_type& __a = allocator_type())
00175     : _M_non_dbg_impl(__a), _M_iter_list(&_M_non_dbg_impl)  {}
00176 
00177 #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
00178   explicit vector(size_type __n, const _Tp& __x = _Tp(),
00179 #else
00180   vector(size_type __n, const _Tp& __x,
00181 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
00182          const allocator_type& __a = allocator_type())
00183     : _M_non_dbg_impl(__n, __x, __a), _M_iter_list(&_M_non_dbg_impl) {}
00184 
00185 #if defined(_STLP_DONT_SUP_DFLT_PARAM)
00186   explicit vector(size_type __n)
00187     : _M_non_dbg_impl(__n), _M_iter_list(&_M_non_dbg_impl) {}
00188 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
00189 
00190   vector(const _Self& __x)
00191     : _ConstructCheck(__x), _M_non_dbg_impl(__x._M_non_dbg_impl), _M_iter_list(&_M_non_dbg_impl) {}
00192 
00193   vector(__move_source<_Self> src)
00194     : _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)),
00195       _M_iter_list(&_M_non_dbg_impl) {
00196 #if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
00197     src.get()._M_iter_list._Invalidate_all();
00198 #else
00199     src.get()._M_iter_list._Set_owner(_M_iter_list);
00200 #endif
00201   }
00202 
00203 #if defined (_STLP_MEMBER_TEMPLATES)
00204   template <class _InputIterator>
00205   vector(_InputIterator __first, _InputIterator __last,
00206          const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
00207     : _ConstructCheck(__first, __last),
00208       _M_non_dbg_impl(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last), __a),
00209       _M_iter_list(&_M_non_dbg_impl) {}
00210 
00211 #  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
00212   template <class _InputIterator>
00213   vector(_InputIterator __first, _InputIterator __last)
00214     : _ConstructCheck(__first, __last),
00215       _M_non_dbg_impl(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last)),
00216       _M_iter_list(&_M_non_dbg_impl) {}
00217 #  endif
00218 #else
00219   vector(const _Tp* __first, const _Tp* __last,
00220          const allocator_type& __a = allocator_type())
00221     : _ConstructCheck(__first, __last), _M_non_dbg_impl(__first, __last, __a),
00222     _M_iter_list(&_M_non_dbg_impl) {}
00223 
00224   // mysterious VC++ bug ?
00225   vector(const_iterator __first, const_iterator __last ,
00226          const allocator_type& __a = allocator_type())
00227     : _ConstructCheck(__first, __last),
00228       _M_non_dbg_impl(__first._M_iterator, __last._M_iterator, __a),
00229     _M_iter_list(&_M_non_dbg_impl) {}
00230 #endif /* _STLP_MEMBER_TEMPLATES */
00231 
00232   _Self& operator=(const _Self& __x) {
00233     if (this != &__x) {
00234       _Invalidate_all();
00235       _M_non_dbg_impl = __x._M_non_dbg_impl;
00236     }
00237     return *this;
00238   }
00239 
00240   void reserve(size_type __n) {
00241     if (capacity() < __n)
00242       _Invalidate_all();
00243     _M_non_dbg_impl.reserve(__n);
00244   }
00245 
00246   reference front() {
00247     _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
00248     return *begin();
00249   }
00250   const_reference front() const {
00251     _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
00252     return *begin();
00253   }
00254   reference back() {
00255     _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
00256     return *(--end());
00257   }
00258   const_reference back() const {
00259     _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
00260     return *(--end());
00261   }
00262 
00263   void swap(_Self& __x) {
00264     _M_iter_list._Swap_owners(__x._M_iter_list);
00265     _M_non_dbg_impl.swap(__x._M_non_dbg_impl);
00266   }
00267 
00268 #if !defined(_STLP_DONT_SUP_DFLT_PARAM)
00269   iterator insert(iterator __pos, const _Tp& __x = _Tp()) {
00270 #else
00271   iterator insert(iterator __pos, const _Tp& __x) {
00272 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
00273     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
00274     _Check_Overflow(1);
00275     return iterator(&_M_iter_list, _M_non_dbg_impl.insert(__pos._M_iterator, __x));
00276   }
00277 
00278 #if defined(_STLP_DONT_SUP_DFLT_PARAM)
00279   iterator insert(iterator __pos)
00280   { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
00281 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
00282 
00283 #if defined (_STLP_MEMBER_TEMPLATES)
00284   // Check whether it's an integral type.  If so, it's not an iterator.
00285   template <class _InputIterator>
00286   void insert(iterator __pos,
00287               _InputIterator __first, _InputIterator __last) {
00288     typedef typename _AreSameUnCVTypes<_InputIterator, iterator>::_Ret _IsNonConstIterator;
00289     typedef typename _AreSameUnCVTypes<_InputIterator, const_iterator>::_Ret _IsConstIterator;
00290     typedef typename _Lor2<_IsNonConstIterator, _IsConstIterator>::_Ret _DoCheck;
00291     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
00292     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
00293     //Sequence requirements 23.1.1 Table 67:
00294     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_not_owner(&_M_iter_list, __first, _DoCheck()));
00295     size_type __old_capacity = capacity();
00296     _M_non_dbg_impl.insert(__pos._M_iterator,
00297                            _STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
00298     _Compare_Capacity(__old_capacity);
00299   }
00300 #else
00301   void insert (iterator __pos,
00302                const value_type *__first, const value_type *__last) {
00303     _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__first,__last))
00304     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
00305     size_type __old_capacity = capacity();
00306     _M_non_dbg_impl.insert(__pos._M_iterator, __first, __last);
00307     _Compare_Capacity(__old_capacity);
00308   }
00309 
00310   void insert(iterator __pos,
00311               const_iterator __first, const_iterator __last) {
00312     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
00313     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
00314     //Sequence requirements 23.1.1 Table 67:
00315     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_not_owner(&_M_iter_list, __first, __true_type()));
00316     size_type __old_capacity = capacity();
00317     _M_non_dbg_impl.insert(__pos._M_iterator, __first._M_iterator, __last._M_iterator);
00318     _Compare_Capacity(__old_capacity);
00319 }
00320 #endif
00321 
00322   void insert (iterator __pos, size_type __n, const _Tp& __x){
00323     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
00324     _Check_Overflow(__n);
00325     _M_non_dbg_impl.insert(__pos._M_iterator, __n, __x);
00326   }
00327 
00328   void pop_back() {
00329     _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
00330     _Invalidate_iterator(end());
00331     _M_non_dbg_impl.pop_back();
00332   }
00333   iterator erase(iterator __pos) {
00334     _STLP_DEBUG_CHECK(_STLP_PRIV _Dereferenceable(__pos))
00335     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
00336     _Invalidate_iterators(__pos, end());
00337     return iterator(&_M_iter_list, _M_non_dbg_impl.erase(__pos._M_iterator));
00338   }
00339   iterator erase(iterator __first, iterator __last) {
00340     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last, begin(), end()))
00341     _Invalidate_iterators(__first, end());
00342     return iterator(&_M_iter_list, _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator));
00343   }
00344 
00345 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
00346   void resize(size_type __new_size, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
00347 #else
00348   void resize(size_type __new_size, const _Tp& __x) {
00349 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
00350     if (__new_size > capacity()) {
00351       _Invalidate_all();
00352     }
00353     else if (__new_size < size()) {
00354       _Invalidate_iterators(begin() + __new_size, end());
00355     }
00356     _M_non_dbg_impl.resize(__new_size, __x);
00357   }
00358 
00359 #if defined (_STLP_DONT_SUP_DFLT_PARAM)
00360   void resize(size_type __new_size) { resize(__new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
00361 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
00362 
00363 #if defined (_STLP_MEMBER_TEMPLATES)
00364 private:
00365   template <class _Integer>
00366   void _M_assign_dispatch(_Integer __n, _Integer __val,
00367                           const __true_type& /*_IsIntegral*/) {
00368     _M_check_assign(__n);
00369     _M_non_dbg_impl.assign(__n, __val);
00370   }
00371 
00372   template <class _InputIter>
00373   void _M_assign_dispatch(_InputIter __first, _InputIter __last,
00374                           const __false_type& /*_IsIntegral*/) {
00375     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
00376     size_type __old_size = size();
00377     size_type __old_capacity = capacity();
00378     iterator __old_end = end();
00379     _M_non_dbg_impl.assign(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
00380     if (__old_capacity != 0) {
00381       if (empty() || (capacity() > __old_capacity)) {
00382         _Invalidate_all();
00383       }
00384       else if (size() < __old_size) {
00385         _Invalidate_iterators(begin() + size(), __old_end);
00386       }
00387     }
00388   }
00389 
00390 public:
00391   template <class _InputIterator>
00392   void assign(_InputIterator __first, _InputIterator __last) {
00393     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
00394     _M_assign_dispatch(__first, __last, _Integral());
00395   }
00396 #else
00397 private:
00398   void _M_assign(const value_type *__first, const value_type *__last) {
00399     size_type __len = distance(__first, __last);
00400     _M_check_assign(__len);
00401     _M_non_dbg_impl.assign(__first, __last);
00402   }
00403 public:
00404   void assign(const value_type *__first, const value_type *__last) {
00405     _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__first,__last))
00406     _M_assign(__first, __last);
00407   }
00408 
00409   void assign(const_iterator __first, const_iterator __last) {
00410     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
00411     _M_assign(__first._M_iterator, __last._M_iterator);
00412   }
00413 #endif
00414 
00415 private:
00416   void _M_check_assign(size_type __n) {
00417     if (__n > capacity()) {
00418       _Invalidate_all();
00419     }
00420     else if (__n < size()) {
00421       _Invalidate_iterators(begin() + __n, end());
00422     }
00423   }
00424 
00425 public:
00426   void assign(size_type __n, const _Tp& __val) {
00427     _M_check_assign(__n);
00428     _M_non_dbg_impl.assign(__n, __val);
00429   }
00430 
00431   void clear() {
00432     _Invalidate_all();
00433     _M_non_dbg_impl.clear();
00434   }
00435   void push_back(const _Tp& __x) {
00436     _Check_Overflow(1);
00437     _M_non_dbg_impl.push_back(__x);
00438   }
00439 };
00440 
00441 _STLP_END_NAMESPACE
00442 
00443 #undef _STLP_NON_DBG_VECTOR
00444 
00445 #endif /* _STLP_DBG_VECTOR_H */
00446 
00447 // Local Variables:
00448 // mode:C++
00449 // End:



Generated on Mon Mar 10 15:32:46 2008 by  doxygen 1.5.1