_vector.h revision e46c9386c4f79aa40185f79a19fc5b2a7ef528b3
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_DBG_VECTOR_H
31#define _STLP_INTERNAL_DBG_VECTOR_H
32
33#ifndef _STLP_DBG_ITERATOR_H
34#  include <stl/debug/_iterator.h>
35#endif
36
37#define _STLP_NON_DBG_VECTOR _STLP_PRIV _STLP_NON_DBG_NAME(vector) <_Tp, _Alloc>
38
39_STLP_BEGIN_NAMESPACE
40
41#if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
42template <class _Tp, class _Alloc>
43inline _Tp*
44value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_VECTOR >&)
45{ return (_Tp*)0; }
46template <class _Tp, class _Alloc>
47inline random_access_iterator_tag
48iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_VECTOR >&)
49{ return random_access_iterator_tag(); }
50#endif
51
52_STLP_MOVE_TO_PRIV_NAMESPACE
53
54template <class _Tp, class _NcIt>
55struct _Vector_const_traits;
56
57template <class _Tp, class _NcIt>
58struct _Vector_nonconst_traits {
59  typedef _Nonconst_traits<_Tp> _BaseT;
60  typedef _Tp value_type;
61  typedef _Tp& reference;
62  typedef _Tp* pointer;
63  typedef _Vector_const_traits<_Tp, _NcIt> _ConstTraits;
64  typedef _Vector_nonconst_traits<_Tp, _NcIt> _NonConstTraits;
65};
66
67template <class _Tp, class _NcIt>
68struct _Vector_const_traits {
69  typedef _Const_traits<_Tp> _BaseT;
70  typedef _Tp value_type;
71  typedef const _Tp& reference;
72  typedef const _Tp* pointer;
73  typedef _Vector_const_traits<_Tp, _NcIt> _ConstTraits;
74  typedef _Vector_nonconst_traits<_Tp, _NcIt> _NonConstTraits;
75};
76
77_STLP_TEMPLATE_NULL
78struct _Vector_nonconst_traits<bool, _Bit_iterator> {
79  typedef _Bit_iterator::value_type value_type;
80  typedef _Bit_iterator::reference reference;
81  typedef _Bit_iterator::pointer pointer;
82  typedef _Vector_const_traits<bool, _Bit_iterator> _ConstTraits;
83  typedef _Vector_nonconst_traits<bool, _Bit_iterator> _NonConstTraits;
84};
85
86_STLP_TEMPLATE_NULL
87struct _Vector_const_traits<bool, _Bit_iterator> {
88  typedef _Bit_const_iterator::value_type value_type;
89  typedef _Bit_const_iterator::reference reference;
90  typedef _Bit_const_iterator::pointer pointer;
91  typedef _Vector_const_traits<bool, _Bit_iterator> _ConstTraits;
92  typedef _Vector_nonconst_traits<bool, _Bit_iterator> _NonConstTraits;
93};
94
95_STLP_MOVE_TO_STD_NAMESPACE
96
97template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
98class vector :
99#if !defined (__DMC__)
100             private
101#endif
102                     _STLP_PRIV __construct_checker< _STLP_NON_DBG_VECTOR >
103#if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
104             , public __stlport_class<vector<_Tp, _Alloc> >
105#endif
106{
107private:
108  typedef _STLP_NON_DBG_VECTOR _Base;
109  typedef vector<_Tp, _Alloc> _Self;
110  typedef _STLP_PRIV __construct_checker<_STLP_NON_DBG_VECTOR > _ConstructCheck;
111  _Base _M_non_dbg_impl;
112  _STLP_PRIV __owned_list _M_iter_list;
113
114public:
115  __IMPORT_CONTAINER_TYPEDEFS(_Base)
116
117  typedef _STLP_PRIV _DBG_iter<_Base,
118    _STLP_PRIV _DbgTraits<_STLP_PRIV _Vector_nonconst_traits<value_type, typename _Base::iterator> > > iterator;
119
120  typedef _STLP_PRIV _DBG_iter<_Base,
121    _STLP_PRIV _DbgTraits<_STLP_PRIV _Vector_const_traits<value_type, typename _Base::iterator> > > const_iterator;
122
123private:
124  void _Invalidate_all()
125  { _M_iter_list._Invalidate_all(); }
126  void _Invalidate_iterator(const iterator& __it)
127  { _STLP_PRIV __invalidate_iterator(&_M_iter_list, __it); }
128  void _Invalidate_iterators(const iterator& __first, const iterator& __last)
129  { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); }
130
131  void _Check_Overflow(size_type __nb) {
132    if (size() + __nb > capacity())
133      _Invalidate_all();
134  }
135  void _Compare_Capacity (size_type __old_capacity) {
136    if (capacity() > __old_capacity) {
137      _Invalidate_all();
138    }
139  }
140
141public:
142  _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
143
144  allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); }
145
146  iterator begin()             { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
147  const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
148  iterator end()               { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
149  const_iterator end() const   { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
150
151  reverse_iterator rbegin()             { return reverse_iterator(end()); }
152  const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
153  reverse_iterator rend()               { return reverse_iterator(begin()); }
154  const_reverse_iterator rend() const   { return const_reverse_iterator(begin()); }
155
156  size_type size() const        { return _M_non_dbg_impl.size(); }
157  size_type max_size() const    { return _M_non_dbg_impl.max_size(); }
158  size_type capacity() const    { return _M_non_dbg_impl.capacity(); }
159  bool empty() const            { return _M_non_dbg_impl.empty(); }
160
161  reference operator[](size_type __n) {
162    _STLP_VERBOSE_ASSERT(__n < size(), _StlMsg_OUT_OF_BOUNDS)
163    return _M_non_dbg_impl[__n];
164  }
165
166  const_reference operator[](size_type __n) const {
167    _STLP_VERBOSE_ASSERT(__n < size(), _StlMsg_OUT_OF_BOUNDS)
168    return _M_non_dbg_impl[__n];
169  }
170
171  reference at(size_type __n) { return _M_non_dbg_impl.at(__n); }
172  const_reference at(size_type __n) const { return _M_non_dbg_impl.at(__n); }
173
174  explicit vector(const allocator_type& __a = allocator_type())
175    : _M_non_dbg_impl(__a), _M_iter_list(&_M_non_dbg_impl)  {}
176
177#if !defined(_STLP_DONT_SUP_DFLT_PARAM)
178  explicit vector(size_type __n, const _Tp& __x = _Tp(),
179#else
180  vector(size_type __n, const _Tp& __x,
181#endif
182         const allocator_type& __a = allocator_type())
183    : _M_non_dbg_impl(__n, __x, __a), _M_iter_list(&_M_non_dbg_impl) {}
184
185#if defined(_STLP_DONT_SUP_DFLT_PARAM)
186  explicit vector(size_type __n)
187    : _M_non_dbg_impl(__n), _M_iter_list(&_M_non_dbg_impl) {}
188#endif
189
190  vector(const _Self& __x)
191    : _ConstructCheck(__x), _M_non_dbg_impl(__x._M_non_dbg_impl), _M_iter_list(&_M_non_dbg_impl) {}
192
193#if !defined (_STLP_NO_MOVE_SEMANTIC)
194  vector(__move_source<_Self> src)
195    : _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)),
196      _M_iter_list(&_M_non_dbg_impl) {
197#  if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
198    src.get()._M_iter_list._Invalidate_all();
199#  else
200    src.get()._M_iter_list._Set_owner(_M_iter_list);
201#  endif
202  }
203#endif
204
205#if defined (_STLP_MEMBER_TEMPLATES)
206  template <class _InputIterator>
207  vector(_InputIterator __first, _InputIterator __last,
208         const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
209    : _ConstructCheck(__first, __last),
210      _M_non_dbg_impl(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last), __a),
211      _M_iter_list(&_M_non_dbg_impl) {}
212
213#  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
214  template <class _InputIterator>
215  vector(_InputIterator __first, _InputIterator __last)
216    : _ConstructCheck(__first, __last),
217      _M_non_dbg_impl(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last)),
218      _M_iter_list(&_M_non_dbg_impl) {}
219#  endif
220#else
221  vector(const _Tp* __first, const _Tp* __last,
222         const allocator_type& __a = allocator_type())
223    : _ConstructCheck(__first, __last), _M_non_dbg_impl(__first, __last, __a),
224    _M_iter_list(&_M_non_dbg_impl) {}
225
226  // mysterious VC++ bug ?
227  vector(const_iterator __first, const_iterator __last ,
228         const allocator_type& __a = allocator_type())
229    : _ConstructCheck(__first, __last),
230      _M_non_dbg_impl(__first._M_iterator, __last._M_iterator, __a),
231    _M_iter_list(&_M_non_dbg_impl) {}
232#endif /* _STLP_MEMBER_TEMPLATES */
233
234  _Self& operator=(const _Self& __x) {
235    if (this != &__x) {
236      _Invalidate_all();
237      _M_non_dbg_impl = __x._M_non_dbg_impl;
238    }
239    return *this;
240  }
241
242  void reserve(size_type __n) {
243    if (capacity() < __n)
244      _Invalidate_all();
245    _M_non_dbg_impl.reserve(__n);
246  }
247
248  reference front() {
249    _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
250    return *begin();
251  }
252  const_reference front() const {
253    _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
254    return *begin();
255  }
256  reference back() {
257    _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
258    return *(--end());
259  }
260  const_reference back() const {
261    _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
262    return *(--end());
263  }
264
265  void swap(_Self& __x) {
266    _M_iter_list._Swap_owners(__x._M_iter_list);
267    _M_non_dbg_impl.swap(__x._M_non_dbg_impl);
268  }
269#if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
270  void _M_swap_workaround(_Self& __x) { swap(__x); }
271#endif
272
273#if !defined(_STLP_DONT_SUP_DFLT_PARAM)
274  iterator insert(iterator __pos, const _Tp& __x = _Tp()) {
275#else
276  iterator insert(iterator __pos, const _Tp& __x) {
277#endif /*_STLP_DONT_SUP_DFLT_PARAM*/
278    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
279    _Check_Overflow(1);
280    return iterator(&_M_iter_list, _M_non_dbg_impl.insert(__pos._M_iterator, __x));
281  }
282
283#if defined(_STLP_DONT_SUP_DFLT_PARAM)
284  iterator insert(iterator __pos)
285  { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
286#endif /*_STLP_DONT_SUP_DFLT_PARAM*/
287
288#if defined (_STLP_MEMBER_TEMPLATES)
289  // Check whether it's an integral type.  If so, it's not an iterator.
290  template <class _InputIterator>
291  void insert(iterator __pos,
292              _InputIterator __first, _InputIterator __last) {
293    _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last))
294    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
295    size_type __old_capacity = capacity();
296    _M_non_dbg_impl.insert(__pos._M_iterator,
297                           _STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
298    _Compare_Capacity(__old_capacity);
299  }
300#endif
301#if !defined (_STLP_MEMBER_TEMPLATES)
302  void insert (iterator __pos,
303               const value_type *__first, const value_type *__last) {
304    _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__first,__last))
305    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
306    size_type __old_capacity = capacity();
307    _M_non_dbg_impl.insert(__pos._M_iterator, __first, __last);
308    _Compare_Capacity(__old_capacity);
309  }
310#endif
311
312#if !defined (_STLP_MEMBER_TEMPLATES) || !defined (_STLP_NO_METHOD_SPECIALIZATION)
313  void insert(iterator __pos,
314              const_iterator __first, const_iterator __last) {
315    _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
316    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
317    //Sequence requirements 23.1.1 Table 67:
318    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_not_owner(&_M_iter_list, __first));
319    size_type __old_capacity = capacity();
320    _M_non_dbg_impl.insert(__pos._M_iterator, __first._M_iterator, __last._M_iterator);
321    _Compare_Capacity(__old_capacity);
322  }
323  void insert(iterator __pos,
324              iterator __first, iterator __last) {
325    _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
326    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
327    //Sequence requirements 23.1.1 Table 67:
328    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_not_owner(&_M_iter_list, __first));
329    size_type __old_capacity = capacity();
330    _M_non_dbg_impl.insert(__pos._M_iterator, __first._M_iterator, __last._M_iterator);
331    _Compare_Capacity(__old_capacity);
332  }
333#endif
334
335  void insert (iterator __pos, size_type __n, const _Tp& __x){
336    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
337    _Check_Overflow(__n);
338    _M_non_dbg_impl.insert(__pos._M_iterator, __n, __x);
339  }
340
341  void pop_back() {
342    _STLP_VERBOSE_ASSERT(!empty(), _StlMsg_EMPTY_CONTAINER)
343    _Invalidate_iterator(end());
344    _M_non_dbg_impl.pop_back();
345  }
346  iterator erase(iterator __pos) {
347    _STLP_DEBUG_CHECK(_STLP_PRIV _Dereferenceable(__pos))
348    _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __pos))
349    _Invalidate_iterators(__pos, end());
350    return iterator(&_M_iter_list, _M_non_dbg_impl.erase(__pos._M_iterator));
351  }
352  iterator erase(iterator __first, iterator __last) {
353    _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last, begin(), end()))
354    _Invalidate_iterators(__first, end());
355    return iterator(&_M_iter_list, _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator));
356  }
357
358#if !defined (_STLP_DONT_SUP_DFLT_PARAM)
359  void resize(size_type __new_size, const _Tp& __x = _STLP_DEFAULT_CONSTRUCTED(_Tp)) {
360#else
361  void resize(size_type __new_size, const _Tp& __x) {
362#endif /*_STLP_DONT_SUP_DFLT_PARAM*/
363    if (__new_size > capacity()) {
364      _Invalidate_all();
365    }
366    else if (__new_size < size()) {
367      _Invalidate_iterators(begin() + __new_size, end());
368    }
369    _M_non_dbg_impl.resize(__new_size, __x);
370  }
371
372#if defined (_STLP_DONT_SUP_DFLT_PARAM)
373  void resize(size_type __new_size) { resize(__new_size, _STLP_DEFAULT_CONSTRUCTED(_Tp)); }
374#endif
375
376#if defined (_STLP_MEMBER_TEMPLATES)
377  template <class _InputIterator>
378  void assign(_InputIterator __first, _InputIterator __last) {
379    _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
380    _Invalidate_all();
381    _M_non_dbg_impl.assign(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
382  }
383#else
384private:
385  void _M_assign(const value_type *__first, const value_type *__last) {
386    _Invalidate_all();
387    _M_non_dbg_impl.assign(__first, __last);
388  }
389public:
390  void assign(const value_type *__first, const value_type *__last) {
391    _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__first,__last))
392    _M_assign(__first, __last);
393  }
394
395  void assign(const_iterator __first, const_iterator __last) {
396    _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first,__last))
397    _M_assign(__first._M_iterator, __last._M_iterator);
398  }
399#endif
400
401public:
402  void assign(size_type __n, const _Tp& __val) {
403    _Invalidate_all();
404    _M_non_dbg_impl.assign(__n, __val);
405  }
406
407  void clear() {
408    _Invalidate_all();
409    _M_non_dbg_impl.clear();
410  }
411  void push_back(const _Tp& __x) {
412    _Check_Overflow(1);
413    _M_non_dbg_impl.push_back(__x);
414  }
415};
416
417_STLP_END_NAMESPACE
418
419#undef _STLP_NON_DBG_VECTOR
420
421#endif /* _STLP_DBG_VECTOR_H */
422
423// Local Variables:
424// mode:C++
425// End:
426