1 /*
2  * Copyright (c) 2004
3  * Francois Dumont
4  *
5  * This material is provided "as is", with absolutely no warranty expressed
6  * or implied. Any use is at your own risk.
7  *
8  * Permission to use or copy this software for any purpose is hereby granted
9  * without fee, provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  *
14  */
15 
16 /* NOTE: This is an internal header file, included by other STL headers.
17  *   You should not attempt to use it directly.
18  */
19 
20 #ifndef _STLP_SPECIALIZED_DEQUE_H
21 #define _STLP_SPECIALIZED_DEQUE_H
22 
23 #ifndef _STLP_POINTERS_SPEC_TOOLS_H
24 #  include <stl/pointers/_tools.h>
25 #endif
26 
27 _STLP_BEGIN_NAMESPACE
28 _STLP_MOVE_TO_PRIV_NAMESPACE
29 
30 /*
31  * struct helper to cast deque iterators:
32  */
33 template <class _StorageT, class _ValueT>
34 struct _DequeIteCast {
35   typedef _Deque_iterator<_ValueT, _Nonconst_traits<_ValueT> > iterator;
36   typedef _Deque_iterator<_ValueT, _Const_traits<_ValueT> >    const_iterator;
37   typedef _Deque_iterator<_StorageT, _Nonconst_traits<_StorageT> > storage_iterator;
38   typedef _Deque_iterator<_StorageT, _Const_traits<_StorageT> > const_storage_iterator;
39   typedef _CastTraits<_StorageT, _ValueT> cast_traits;
40 
to_value_type_ite_DequeIteCast41   static iterator to_value_type_ite (storage_iterator const& __ite) {
42     iterator tmp;
43     tmp._M_cur = cast_traits::to_value_type_ptr(__ite._M_cur);
44     tmp._M_first = cast_traits::to_value_type_ptr(__ite._M_first);
45     tmp._M_last = cast_traits::to_value_type_ptr(__ite._M_last);
46     tmp._M_node = cast_traits::to_value_type_pptr(__ite._M_node);
47     return tmp;
48   }
to_storage_type_ite_DequeIteCast49   static storage_iterator to_storage_type_ite (iterator const& __ite) {
50     storage_iterator tmp;
51     tmp._M_cur = cast_traits::to_storage_type_ptr(__ite._M_cur);
52     tmp._M_first = cast_traits::to_storage_type_ptr(__ite._M_first);
53     tmp._M_last = cast_traits::to_storage_type_ptr(__ite._M_last);
54     tmp._M_node = cast_traits::to_storage_type_pptr(__ite._M_node);
55     return tmp;
56   }
57 
to_value_type_cite_DequeIteCast58   static const_iterator to_value_type_cite (const_storage_iterator const& __ite) {
59     const_iterator tmp;
60     tmp._M_cur = cast_traits::to_value_type_ptr(__ite._M_cur);
61     tmp._M_first = cast_traits::to_value_type_ptr(__ite._M_first);
62     tmp._M_last = cast_traits::to_value_type_ptr(__ite._M_last);
63     tmp._M_node = cast_traits::to_value_type_pptr(__ite._M_node);
64     return tmp;
65   }
66 
to_storage_type_cite_DequeIteCast67   static const_storage_iterator to_storage_type_cite (const_iterator const& __ite) {
68     const_storage_iterator tmp;
69     tmp._M_cur = cast_traits::to_storage_type_ptr(__ite._M_cur);
70     tmp._M_first = cast_traits::to_storage_type_ptr(__ite._M_first);
71     tmp._M_last = cast_traits::to_storage_type_ptr(__ite._M_last);
72     tmp._M_node = cast_traits::to_storage_type_pptr(__ite._M_node);
73     return tmp;
74   }
75 };
76 
77 #define DEQUE_IMPL _STLP_PTR_IMPL_NAME(deque)
78 #if defined (__BORLANDC__) || defined (__DMC__)
79 #  define typename
80 #endif
81 
82 #if defined (_STLP_USE_TEMPLATE_EXPORT) && !defined (_STLP_USE_MSVC6_MEM_T_BUG_WORKAROUND)
83 _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<size_t, void*,  allocator<void*> >;
84 _STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<void***, void**,  allocator<void**> >;
85 _STLP_EXPORT template struct _STLP_CLASS_DECLSPEC _Deque_iterator<void*, _Nonconst_traits<void*> >;
86 _STLP_EXPORT_TEMPLATE_CLASS _Deque_base<void*,allocator<void*> >;
87 _STLP_EXPORT_TEMPLATE_CLASS DEQUE_IMPL<void*,allocator<void*> >;
88 #endif
89 
90 #if defined (_STLP_DEBUG)
91 #  define deque _STLP_NON_DBG_NAME(deque)
92 #else
93 _STLP_MOVE_TO_STD_NAMESPACE
94 #endif
95 
96 template <class _Tp, _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Tp>) >
97 class deque
98 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (deque)
99             : public __stlport_class<deque<_Tp, _Alloc> >
100 #endif
101 {
102   typedef _STLP_TYPENAME _STLP_PRIV _StorageType<_Tp>::_Type _StorageType;
103   typedef typename _Alloc_traits<_StorageType, _Alloc>::allocator_type _StorageTypeAlloc;
104   typedef _STLP_PRIV DEQUE_IMPL<_StorageType, _StorageTypeAlloc> _Base;
105   typedef deque<_Tp, _Alloc> _Self;
106 
107   typedef _STLP_PRIV _CastTraits<_StorageType, _Tp> cast_traits;
108   typedef _STLP_PRIV _DequeIteCast<_StorageType, _Tp> ite_cast_traits;
109 
110 public:
111   typedef _Tp value_type;
112   typedef value_type* pointer;
113   typedef const value_type* const_pointer;
114   typedef value_type& reference;
115   typedef const value_type& const_reference;
116   typedef size_t size_type;
117   typedef ptrdiff_t difference_type;
118   typedef random_access_iterator_tag _Iterator_category;
119   _STLP_FORCE_ALLOCATORS(value_type, _Alloc)
120   typedef typename _Alloc_traits<value_type, _Alloc>::allocator_type allocator_type;
121   typedef _STLP_PRIV _Deque_iterator<value_type, _Nonconst_traits<value_type> > iterator;
122   typedef _STLP_PRIV _Deque_iterator<value_type, _Const_traits<value_type> >    const_iterator;
123 
124   _STLP_DECLARE_RANDOM_ACCESS_REVERSE_ITERATORS;
125 
126 public:                         // Basic accessors
127   iterator begin() { return ite_cast_traits::to_value_type_ite(_M_impl.begin()); }
128   iterator end()   { return ite_cast_traits::to_value_type_ite(_M_impl.end()); }
129   const_iterator begin() const { return ite_cast_traits::to_value_type_cite(_M_impl.begin()); }
130   const_iterator end() const   { return ite_cast_traits::to_value_type_cite(_M_impl.end()); }
131 
132   reverse_iterator rbegin() { return reverse_iterator(end()); }
133   reverse_iterator rend()   { return reverse_iterator(begin()); }
134   const_reverse_iterator rbegin() const
135   { return const_reverse_iterator(end()); }
136   const_reverse_iterator rend() const
137   { return const_reverse_iterator(begin()); }
138 
139   reference operator[](size_type __n)
140   { return cast_traits::to_value_type_ref(_M_impl[__n]); }
141   const_reference operator[](size_type __n) const
142   { return cast_traits::to_value_type_cref(_M_impl[__n]); }
143 
144   reference at(size_type __n)
145   { return cast_traits::to_value_type_ref(_M_impl.at(__n)); }
146   const_reference at(size_type __n) const
147   { return cast_traits::to_value_type_cref(_M_impl.at(__n)); }
148 
149   reference front() { return cast_traits::to_value_type_ref(_M_impl.front()); }
150   reference back()  { return cast_traits::to_value_type_ref(_M_impl.back()); }
151   const_reference front() const { return cast_traits::to_value_type_cref(_M_impl.front()); }
152   const_reference back() const  { return cast_traits::to_value_type_cref(_M_impl.back()); }
153 
154   size_type size() const     { return _M_impl.size(); }
155   size_type max_size() const { return _M_impl.max_size(); }
156   bool empty() const         { return _M_impl.empty(); }
157   allocator_type get_allocator() const { return _STLP_CONVERT_ALLOCATOR(_M_impl.get_allocator(), value_type); }
158 
159   explicit deque(const allocator_type& __a = allocator_type())
160     : _M_impl(_STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
161 
162   deque(const _Self& __x) : _M_impl(__x._M_impl) {}
163 
164 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
165   explicit deque(size_type __n, const value_type& __val = _STLP_DEFAULT_CONSTRUCTED(value_type),
166 #else
167   deque(size_type __n, const value_type& __val,
168 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
169         const allocator_type& __a = allocator_type())
170     : _M_impl(__n, cast_traits::to_storage_type_cref(__val), _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
171   // int,long variants may be needed
172 #if defined (_STLP_DONT_SUP_DFLT_PARAM)
173   explicit deque(size_type __n) : _M_impl(__n) {}
174 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
175 
176 #if defined (_STLP_MEMBER_TEMPLATES)
177   template <class _InputIterator>
178   deque(_InputIterator __first, _InputIterator __last,
179         const allocator_type& __a _STLP_ALLOCATOR_TYPE_DFL)
180 #if !defined (_STLP_USE_ITERATOR_WRAPPER)
181   : _M_impl(__first, __last,
182             _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {
183 #else
184   : _M_impl(_STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {
185 #endif
186 #if defined (_STLP_USE_ITERATOR_WRAPPER)
187     insert(end(), __first, __last);
188 #endif
189   }
190 
191 #  if defined (_STLP_NEEDS_EXTRA_TEMPLATE_CONSTRUCTORS)
192   template <class _InputIterator>
193   deque(_InputIterator __first, _InputIterator __last)
194 #    if !defined (_STLP_USE_ITERATOR_WRAPPER)
195     : _M_impl(__first, __last) {}
196 #    else
197   { insert(end(), __first, __last); }
198 #    endif
199 #  endif
200 
201 #else
202   deque(const_pointer __first, const_pointer __last,
203         const allocator_type& __a = allocator_type() )
204     : _M_impl(cast_traits::to_storage_type_cptr(__first),
205               cast_traits::to_storage_type_cptr(__last),
206               _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
207 
208   deque(const_iterator __first, const_iterator __last,
209         const allocator_type& __a = allocator_type() )
210     : _M_impl(ite_cast_traits::to_storage_type_cite(__first),
211               ite_cast_traits::to_storage_type_cite(__last),
212               _STLP_CONVERT_ALLOCATOR(__a, _StorageType)) {}
213 #endif /* _STLP_MEMBER_TEMPLATES */
214 
215 #if !defined (_STLP_NO_MOVE_SEMANTIC)
216   deque(__move_source<_Self> src)
217     : _M_impl(__move_source<_Base>(src.get()._M_impl)) {}
218 #endif
219 
220   _Self& operator= (const _Self& __x) { _M_impl = __x._M_impl; return *this; }
221 
222   void swap(_Self& __x) { _M_impl.swap(__x._M_impl); }
223 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
224   void _M_swap_workaround(_Self& __x) { swap(__x); }
225 #endif
226 
227   void assign(size_type __n, const value_type& __val) {
228     _M_impl.assign(__n, cast_traits::to_storage_type_cref(__val));
229   }
230 
231 #if defined (_STLP_MEMBER_TEMPLATES)
232 #  if defined (_STLP_USE_ITERATOR_WRAPPER)
233 private:
234   template <class _Integer>
235   void _M_assign_dispatch(_Integer __n, _Integer __val,
236                           const __true_type&)
237   { _M_impl.assign(__n, __val); }
238 
239   template <class _InputIterator>
240   void _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
241                           const __false_type&) {
242     _M_impl.assign(_STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__first),
243                    _STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__last));
244   }
245 
246 public:
247 #  endif
248   template <class _InputIterator>
249   void assign(_InputIterator __first, _InputIterator __last) {
250 #  if defined (_STLP_USE_ITERATOR_WRAPPER)
251     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
252     _M_assign_dispatch(__first, __last, _Integral());
253 #  else
254     _M_impl.assign(__first, __last);
255 #  endif
256   }
257 #else
258   void assign(const_pointer __first, const_pointer __last)
259   { _M_impl.assign(cast_traits::to_storage_type_cptr(__first),
260                    cast_traits::to_storage_type_cptr(__last)); }
261   void assign(const_iterator __first, const_iterator __last)
262   { _M_impl.assign(ite_cast_traits::to_storage_type_cite(__first),
263                    ite_cast_traits::to_storage_type_cite(__last)); }
264 #endif /* _STLP_MEMBER_TEMPLATES */
265 
266 #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
267   void push_back(const value_type& __t = _STLP_DEFAULT_CONSTRUCTED(value_type))
268 #else
269   void push_back(const value_type& __t)
270 #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
271   { _M_impl.push_back(cast_traits::to_storage_type_cref(__t)); }
272 
273 #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
274   void push_front(const value_type& __t = _STLP_DEFAULT_CONSTRUCTED(value_type))
275 #else
276   void push_front(const value_type& __t)
277 #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
278   { _M_impl.push_front(cast_traits::to_storage_type_cref(__t)); }
279 
280 # if defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
281   void push_back()  { _M_impl.push_back(); }
282   void push_front() { _M_impl.push_front(); }
283 # endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
284 
285   void pop_back()  { _M_impl.pop_back(); }
286   void pop_front() { _M_impl.pop_front(); }
287 
288 #if !defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
289   iterator insert(iterator __pos, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
290 #else
291   iterator insert(iterator __pos, const value_type& __x)
292 #endif /*!_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
293   { return ite_cast_traits::to_value_type_ite(_M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
294                                                              cast_traits::to_storage_type_cref(__x))); }
295 
296 #if defined (_STLP_DONT_SUP_DFLT_PARAM) && !defined (_STLP_NO_ANACHRONISMS)
297   iterator insert(iterator __pos) { return insert(__pos, _STLP_DEFAULT_CONSTRUCTED(value_type)); }
298 #endif /*_STLP_DONT_SUP_DFLT_PARAM && !_STLP_NO_ANACHRONISMS*/
299 
300   void insert(iterator __pos, size_type __n, const value_type& __x)
301   { _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos), __n, cast_traits::to_storage_type_cref(__x)); }
302 
303 #if defined (_STLP_MEMBER_TEMPLATES)
304 #  if defined (_STLP_USE_ITERATOR_WRAPPER)
305 private:
306   template <class _Integer>
307   void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
308                           const __true_type&) {
309     _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos), __n, __val);
310   }
311 
312   template <class _InputIterator>
313   void _M_insert_dispatch(iterator __pos,
314                           _InputIterator __first, _InputIterator __last,
315                           const __false_type&) {
316     _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
317                    _STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__first),
318                    _STLP_TYPENAME _STLP_PRIV _IteWrapper<_StorageType, _Tp, _InputIterator>::_Ite(__last));
319   }
320 
321 public:
322 #  endif
323 
324   template <class _InputIterator>
325   void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
326 #  if defined (_STLP_USE_ITERATOR_WRAPPER)
327     // Check whether it's an integral type.  If so, it's not an iterator.
328     typedef typename _IsIntegral<_InputIterator>::_Ret _Integral;
329     _M_insert_dispatch(__pos, __first, __last, _Integral());
330 #  else
331     _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos), __first, __last);
332 #  endif
333   }
334 
335 #else /* _STLP_MEMBER_TEMPLATES */
336   void insert(iterator __pos,
337               const_pointer __first, const_pointer __last) {
338     _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
339                    cast_traits::to_storage_type_cptr(__first),
340                    cast_traits::to_storage_type_cptr(__last));
341   }
342   void insert(iterator __pos,
343               const_iterator __first, const_iterator __last) {
344     _M_impl.insert(ite_cast_traits::to_storage_type_ite(__pos),
345                    ite_cast_traits::to_storage_type_cite(__first),
346                    ite_cast_traits::to_storage_type_cite(__last));
347   }
348 
349 #endif /* _STLP_MEMBER_TEMPLATES */
350 
351 #if !defined (_STLP_DONT_SUP_DFLT_PARAM)
352   void resize(size_type __new_size, const value_type& __x = _STLP_DEFAULT_CONSTRUCTED(value_type))
353 #else
354   void resize(size_type __new_size, const value_type& __x)
355 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
356   { _M_impl.resize(__new_size, cast_traits::to_storage_type_cref(__x)); }
357 
358 #if defined (_STLP_DONT_SUP_DFLT_PARAM)
359   void resize(size_type __new_size) { _M_impl.resize(__new_size); }
360 #endif /*_STLP_DONT_SUP_DFLT_PARAM*/
361 
362   iterator erase(iterator __pos)
363   { return ite_cast_traits::to_value_type_ite(_M_impl.erase(ite_cast_traits::to_storage_type_ite(__pos))); }
364 
365   iterator erase(iterator __first, iterator __last)
366   { return ite_cast_traits::to_value_type_ite(_M_impl.erase(ite_cast_traits::to_storage_type_ite(__first),
367                                                             ite_cast_traits::to_storage_type_ite(__last))); }
368   void clear() { _M_impl.clear(); }
369 
370 private:
371   _Base _M_impl;
372 };
373 
374 #if defined (deque)
375 #  undef deque
376 _STLP_MOVE_TO_STD_NAMESPACE
377 #endif
378 
379 #undef DEQUE_IMPL
380 #if defined (__BORLANDC__) || defined (__DMC__)
381 #  undef typename
382 #endif
383 
384 _STLP_END_NAMESPACE
385 
386 #endif /* _STLP_SPECIALIZED_DEQUE_H */
387 
388 // Local Variables:
389 // mode:C++
390 // End:
391