]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/pointers/_tools.h
minor doc updates
[polintos/scott/priv.git] / include / c++ / stl / stl / pointers / _tools.h
1 /*
2  * Copyright (c) 2003
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_POINTERS_SPEC_TOOLS_H
21 #define _STLP_POINTERS_SPEC_TOOLS_H
22
23 #ifndef _STLP_TYPE_TRAITS_H
24 #  include <stl/type_traits.h>
25 #endif
26
27 _STLP_BEGIN_NAMESPACE
28
29 //Some usefull declarations:
30 template <class _Tp> struct less;
31
32 _STLP_MOVE_TO_PRIV_NAMESPACE
33
34 template <class _StorageT, class _ValueT, class _BinaryPredicate>
35 struct _BinaryPredWrapper;
36
37 /*
38  * Since the compiler only allows at most one non-trivial
39  * implicit conversion we can make use of a shim class to
40  * be sure that functions below doesn't accept classes with
41  * implicit pointer conversion operators
42  */
43 struct _ConstVolatileVoidPointerShim
44 { _ConstVolatileVoidPointerShim(const volatile void*); };
45
46 //The dispatch functions:
47 struct _VoidPointerShim
48 { _VoidPointerShim(void*); };
49 struct _ConstVoidPointerShim
50 { _ConstVoidPointerShim(const void*); };
51 struct _VolatileVoidPointerShim
52 { _VolatileVoidPointerShim(volatile void*); };
53
54 template <class _Tp>
55 char _UseVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&);
56 char _UseVoidPtrStorageType(const __true_type& /*POD*/, ...);
57 char* _UseVoidPtrStorageType(const __true_type& /*POD*/, _VoidPointerShim);
58
59 template <class _Tp>
60 char _UseConstVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&);
61 char _UseConstVoidPtrStorageType(const __true_type& /*POD*/, ...);
62 char* _UseConstVoidPtrStorageType(const __true_type& /*POD*/, _ConstVoidPointerShim);
63
64 template <class _Tp>
65 char _UseVolatileVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&);
66 char _UseVolatileVoidPtrStorageType(const __true_type& /*POD*/, ...);
67 char* _UseVolatileVoidPtrStorageType(const __true_type& /*POD*/, _VolatileVoidPointerShim);
68
69 template <class _Tp>
70 char _UseConstVolatileVoidPtrStorageType(const __false_type& /*POD*/, const _Tp&);
71 char _UseConstVolatileVoidPtrStorageType(const __true_type& /*POD*/, ...);
72 char* _UseConstVolatileVoidPtrStorageType(const __true_type& /*POD*/, _ConstVolatileVoidPointerShim);
73
74 template <class _Tp>
75 struct _StorageType {
76   typedef typename __type_traits<_Tp>::is_POD_type _PODType;
77   static _Tp __null_rep();
78
79   enum { use_void_ptr = (sizeof(_UseVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) };
80   enum { use_const_void_ptr = (sizeof(_UseConstVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) };
81   enum { use_volatile_void_ptr = (sizeof(_UseVolatileVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) };
82   enum { use_const_volatile_void_ptr = (sizeof(_UseConstVolatileVoidPtrStorageType(_PODType(), __null_rep())) == sizeof(char*)) };
83
84   typedef typename __select<!use_const_volatile_void_ptr,
85                             _Tp,
86           typename __select<use_void_ptr,
87                             void*,
88           typename __select<use_const_void_ptr,
89                             const void*,
90           typename __select<use_volatile_void_ptr,
91                             volatile void*,
92                             const volatile void*>::_Ret >::_Ret >::_Ret >::_Ret _QualifiedType;
93
94 #if !defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
95   /* If the compiler do not support the iterator_traits structure we cannot wrap
96    * iterators pass to container template methods. The iterator dereferenced value
97    * has to be storable without any cast in the chosen storage type. To guaranty
98    * that the void pointer has to be correctly qualified.
99    */
100   typedef _QualifiedType _Type;
101 #else
102   /* With iterator_traits we can wrap passed iterators and make the necessary casts.
103    * We can always use a simple void* storage type:
104    */
105   typedef typename __select<use_const_volatile_void_ptr,
106                             void*,
107                             _Tp>::_Ret _Type;
108 #endif
109 };
110
111 template <class _Tp, class _Compare>
112 struct _AssocStorageTypes {
113   typedef _StorageType<_Tp> _StorageTypeInfo;
114   typedef typename _StorageTypeInfo::_Type _SType;
115
116   //We need to also check that the comparison functor used to instanciate the assoc container
117   //is the default Standard less implementation:
118   typedef typename _IsSTLportClass<_Compare>::_Ret _STLportLess;
119   enum { is_default_less = __type2bool<_STLportLess>::_Ret };
120
121   typedef typename __select<is_default_less, _SType, _Tp>::_Ret _KeyStorageType;
122   enum { ptr_type = _StorageTypeInfo::use_const_volatile_void_ptr };
123   typedef typename __select<is_default_less && ptr_type,
124                             _BinaryPredWrapper<_KeyStorageType, _Tp, _Compare>,
125                             _Compare>::_Ret _CompareStorageType;
126 };
127
128
129 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
130 /*
131  * Base struct to deal with qualifiers
132  */
133 template <class _StorageT, class _QualifiedStorageT>
134 struct _VoidCastTraitsAux {
135   typedef _QualifiedStorageT void_cv_type;
136   typedef _StorageT void_type;
137
138   static void_type * uncv_ptr(void_cv_type *__ptr)
139   { return __ptr; }
140   static void_type const* uncv_cptr(void_cv_type const*__ptr)
141   { return __ptr; }
142   static void_type ** uncv_pptr(void_cv_type **__ptr)
143   { return __ptr; }
144   static void_type & uncv_ref(void_cv_type & __ref)
145   { return __ref; }
146   static void_type const& uncv_cref(void_cv_type const& __ref)
147   { return __ref; }
148   static void_cv_type* cv_ptr(void_type *__ptr)
149   { return __ptr; }
150   static void_cv_type const* cv_cptr(void_type const*__ptr)
151   { return __ptr; }
152   static void_cv_type ** cv_pptr(void_type **__ptr)
153   { return __ptr; }
154   static void_cv_type & cv_ref(void_type & __ref)
155   { return __ref; }
156   static void_cv_type const& cv_cref(void_type const& __ref)
157   { return __ref; }
158 };
159
160 template <class _VoidCVType>
161 struct _VoidCastTraitsAuxBase {
162   typedef _VoidCVType* void_cv_type;
163   typedef void* void_type;
164
165   static void_type* uncv_ptr(void_cv_type *__ptr)
166   { return __CONST_CAST(void_type*, __ptr); }
167   static void_type const* uncv_cptr(void_cv_type const*__ptr)
168   { return __CONST_CAST(void_type const*, __ptr); }
169   static void_type** uncv_pptr(void_cv_type **__ptr)
170   { return __CONST_CAST(void_type**, __ptr); }
171   static void_type& uncv_ref(void_cv_type &__ref)
172   { return __CONST_CAST(void_type&, __ref); }
173   static void_type const& uncv_cref(void_cv_type const& __ptr)
174   { return __CONST_CAST(void_type const&, __ptr); }
175   // The reverse versions
176   static void_cv_type * cv_ptr(void_type *__ptr)
177   { return __CONST_CAST(void_cv_type *, __ptr); }
178   static void_cv_type const* cv_cptr(void_type const*__ptr)
179   { return __CONST_CAST(void_cv_type const*, __ptr); }
180   static void_cv_type ** cv_pptr(void_type **__ptr)
181   { return __CONST_CAST(void_cv_type**, __ptr); }
182   static void_cv_type & cv_ref(void_type &__ref)
183   { return __CONST_CAST(void_cv_type &, __ref); }
184   static void_cv_type const& cv_cref(void_type const& __ref)
185   { return __CONST_CAST(void_cv_type const&, __ref); }
186 };
187
188 _STLP_TEMPLATE_NULL
189 struct _VoidCastTraitsAux<void*, const void*> : _VoidCastTraitsAuxBase<void const>
190 {};
191 _STLP_TEMPLATE_NULL
192 struct _VoidCastTraitsAux<void*, volatile void*> : _VoidCastTraitsAuxBase<void volatile>
193 {};
194 _STLP_TEMPLATE_NULL
195 struct _VoidCastTraitsAux<void*, const volatile void*> : _VoidCastTraitsAuxBase<void const volatile>
196 {};
197
198 template <class _StorageT, class _ValueT>
199 struct _CastTraits {
200   typedef _ValueT value_type;
201   typedef typename _StorageType<_ValueT>::_QualifiedType _QualifiedStorageT;
202   typedef _VoidCastTraitsAux<_StorageT, _QualifiedStorageT> cv_traits;
203   typedef typename cv_traits::void_type void_type;
204   typedef typename cv_traits::void_cv_type void_cv_type;
205
206   static value_type * to_value_type_ptr(void_type *__ptr)
207   { return __REINTERPRET_CAST(value_type *, cv_traits::cv_ptr(__ptr)); }
208   static value_type const* to_value_type_cptr(void_type const*__ptr)
209   { return __REINTERPRET_CAST(value_type const*, cv_traits::cv_cptr(__ptr)); }
210   static value_type ** to_value_type_pptr(void_type **__ptr)
211   { return __REINTERPRET_CAST(value_type **, cv_traits::cv_pptr(__ptr)); }
212   static value_type & to_value_type_ref(void_type &__ref)
213   { return __REINTERPRET_CAST(value_type &, cv_traits::cv_ref(__ref)); }
214   static value_type const& to_value_type_cref(void_type const& __ptr)
215   { return __REINTERPRET_CAST(value_type const&, cv_traits::cv_cref(__ptr)); }
216   // Reverse versions
217   static void_type * to_storage_type_ptr(value_type *__ptr)
218   { return cv_traits::uncv_ptr(__REINTERPRET_CAST(void_cv_type *, __ptr)); }
219   static void_type const* to_storage_type_cptr(value_type const*__ptr)
220   { return cv_traits::uncv_cptr(__REINTERPRET_CAST(void_cv_type const*, __ptr)); }
221   static void_type ** to_storage_type_pptr(value_type **__ptr)
222   { return cv_traits::uncv_pptr(__REINTERPRET_CAST(void_cv_type **, __ptr)); }
223   static void_type const& to_storage_type_cref(value_type const& __ref)
224   { return cv_traits::uncv_cref(__REINTERPRET_CAST(void_cv_type const&, __ref)); }
225
226   //Method used to treat set container template method extension
227   static void_type const& to_storage_type_crefT(value_type const& __ref)
228   { return to_storage_type_cref(__ref); }
229 };
230
231 template <class _Tp>
232 struct _CastTraits<_Tp, _Tp> {
233   typedef _Tp storage_type;
234   typedef _Tp value_type;
235
236   static value_type * to_value_type_ptr(storage_type *__ptr)
237   { return __ptr; }
238   static value_type const* to_value_type_cptr(storage_type const*__ptr)
239   { return __ptr; }
240   static value_type ** to_value_type_pptr(storage_type **__ptr)
241   { return __ptr; }
242   static value_type & to_value_type_ref(storage_type &__ref)
243   { return __ref; }
244   static value_type const& to_value_type_cref(storage_type const&__ref)
245   { return __ref; }
246   // Reverse versions
247   static storage_type * to_storage_type_ptr(value_type *__ptr)
248   { return __ptr; }
249   static storage_type const* to_storage_type_cptr(value_type const*__ptr)
250   { return __ptr; }
251   static storage_type ** to_storage_type_pptr(value_type **__ptr)
252   { return __ptr; }
253   static storage_type const& to_storage_type_cref(value_type const& __ref)
254   { return __ref; }
255
256   //Method used to treat set container template method extension
257   template <class _Tp1>
258   static _Tp1 const& to_storage_type_crefT(_Tp1 const& __ref)
259   { return __ref; }
260 };
261
262 #define _STLP_USE_ITERATOR_WRAPPER
263
264 template <class _StorageT, class _ValueT, class _Iterator>
265 struct _IteWrapper {
266   typedef _CastTraits<_StorageT, _ValueT> cast_traits;
267   typedef iterator_traits<_Iterator> _IteTraits;
268
269   typedef typename _IteTraits::iterator_category iterator_category;
270   typedef _StorageT value_type;
271   typedef typename _IteTraits::difference_type difference_type;
272   typedef value_type* pointer;
273   typedef value_type const& const_reference;
274   //This wrapper won't be used for input so to avoid surprise
275   //the reference type will be a const reference:
276   typedef const_reference reference;
277
278   typedef _IteWrapper<_StorageT, _ValueT, _Iterator> _Self;
279   typedef _Self _Ite;
280
281   _IteWrapper(_Iterator &__ite) : _M_ite(__ite) {}
282
283   const_reference operator*() const { return cast_traits::to_storage_type_cref(*_M_ite); }
284
285   _Self& operator= (_Self const& __rhs) {
286     _M_ite = __rhs._M_ite;
287     return *this;
288   }
289
290   _Self& operator++() {
291     ++_M_ite;
292     return *this;
293   }
294
295   _Self& operator--() {
296     --_M_ite;
297     return *this;
298   }
299
300   _Self& operator += (difference_type __offset) {
301     _M_ite += __offset;
302     return *this;
303   }
304   difference_type operator -(_Self const& __other) const
305   { return _M_ite - __other._M_ite; }
306
307   bool operator == (_Self const& __other) const
308   { return _M_ite == __other._M_ite; }
309
310   bool operator != (_Self const& __other) const
311   { return _M_ite != __other._M_ite; }
312
313   bool operator < (_Self const& __rhs) const
314   { return _M_ite < __rhs._M_ite; }
315
316 private:
317   _Iterator _M_ite;
318 };
319
320 template <class _Tp, class _Iterator>
321 struct _IteWrapper<_Tp, _Tp, _Iterator>
322 { typedef _Iterator _Ite; };
323
324 #else
325
326 /*
327  * In this config the storage type is qualified in respect of the
328  * value_type qualification. Simple reinterpret_cast is enough.
329  */
330 template <class _StorageT, class _ValueT>
331 struct _CastTraits {
332   typedef _StorageT storage_type;
333   typedef _ValueT value_type;
334
335   static value_type * to_value_type_ptr(storage_type *__ptr)
336   { return __REINTERPRET_CAST(value_type*, __ptr); }
337   static value_type const* to_value_type_cptr(storage_type const*__ptr)
338   { return __REINTERPRET_CAST(value_type const*, __ptr); }
339   static value_type ** to_value_type_pptr(storage_type **__ptr)
340   { return __REINTERPRET_CAST(value_type **, __ptr); }
341   static value_type & to_value_type_ref(storage_type &__ref)
342   { return __REINTERPRET_CAST(value_type&, __ref); }
343   static value_type const& to_value_type_cref(storage_type const&__ref)
344   { return __REINTERPRET_CAST(value_type const&, __ref); }
345   // Reverse versions
346   static storage_type * to_storage_type_ptr(value_type *__ptr)
347   { return __REINTERPRET_CAST(storage_type*, __ptr); }
348   static storage_type const* to_storage_type_cptr(value_type const*__ptr)
349   { return __REINTERPRET_CAST(storage_type const*, __ptr); }
350   static storage_type ** to_storage_type_pptr(value_type **__ptr)
351   { return __REINTERPRET_CAST(storage_type **, __ptr); }
352   static storage_type const& to_storage_type_cref(value_type const&__ref)
353   { return __REINTERPRET_CAST(storage_type const&, __ref); }
354   template <class _Tp1>
355   static _Tp1 const& to_storage_type_crefT(_Tp1 const& __ref)
356   { return __ref; }
357 };
358
359 #endif
360
361 //Wrapper functors:
362 template <class _StorageT, class _ValueT, class _UnaryPredicate>
363 struct _UnaryPredWrapper {
364   typedef _CastTraits<_StorageT, _ValueT> cast_traits;
365
366   _UnaryPredWrapper (_UnaryPredicate const& __pred) : _M_pred(__pred) {}
367
368   bool operator () (_StorageT const& __ref) const
369   { return _M_pred(cast_traits::to_value_type_cref(__ref)); }
370
371 private:
372   _UnaryPredicate _M_pred;
373 };
374
375 template <class _StorageT, class _ValueT, class _BinaryPredicate>
376 struct _BinaryPredWrapper {
377   typedef _CastTraits<_StorageT, _ValueT> cast_traits;
378
379   _BinaryPredWrapper () {}
380   _BinaryPredWrapper (_BinaryPredicate const& __pred) : _M_pred(__pred) {}
381
382   _BinaryPredicate get_pred() const { return _M_pred; }
383
384   bool operator () (_StorageT const& __fst, _StorageT const& __snd) const
385   { return _M_pred(cast_traits::to_value_type_cref(__fst), cast_traits::to_value_type_cref(__snd)); }
386
387   //Cast operator used to transparently access underlying predicate
388   //in set::key_comp() method
389   operator _BinaryPredicate() const
390   { return _M_pred; }
391
392 private:
393   _BinaryPredicate _M_pred;
394 };
395
396 _STLP_MOVE_TO_STD_NAMESPACE
397
398 _STLP_END_NAMESPACE
399
400 #endif /* _STLP_POINTERS_SPEC_TOOLS_H */