summaryrefslogtreecommitdiffstats
blob: 5bb9c6269cf579af78260465714794a6d5564e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// (C) Copyright David Abrahams 2002.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// Boost versions of
//
//    std::iterator_traits<>::iterator_category
//    std::iterator_traits<>::difference_type
//    std::distance()
//
// ...for all compilers and iterators
//
// Additionally, if X is a pointer
//    std::iterator_traits<X>::pointer

// Otherwise, if partial specialization is supported or X is not a pointer
//    std::iterator_traits<X>::value_type
//    std::iterator_traits<X>::pointer
//    std::iterator_traits<X>::reference
//
// See http://www.boost.org for most recent version including documentation.

// Revision History
// 04 Mar 2001 - More attempted fixes for Intel C++ (David Abrahams)
// 03 Mar 2001 - Put all implementation into namespace
//               boost::detail::iterator_traits_. Some progress made on fixes
//               for Intel compiler. (David Abrahams)
// 02 Mar 2001 - Changed BOOST_MSVC to BOOST_MSVC_STD_ITERATOR in a few
//               places. (Jeremy Siek)
// 19 Feb 2001 - Improved workarounds for stock MSVC6; use yes_type and
//               no_type from type_traits.hpp; stopped trying to remove_cv
//               before detecting is_pointer, in honor of the new type_traits
//               semantics. (David Abrahams)
// 13 Feb 2001 - Make it work with nearly all standard-conforming iterators
//               under raw VC6. The one category remaining which will fail is
//               that of iterators derived from std::iterator but not
//               boost::iterator and which redefine difference_type.
// 11 Feb 2001 - Clean away code which can never be used (David Abrahams)
// 09 Feb 2001 - Always have a definition for each traits member, even if it
//               can't be properly deduced. These will be incomplete types in
//               some cases (undefined<void>), but it helps suppress MSVC errors
//               elsewhere (David Abrahams)
// 07 Feb 2001 - Support for more of the traits members where possible, making
//               this useful as a replacement for std::iterator_traits<T> when
//               used as a default template parameter.
// 06 Feb 2001 - Removed useless #includes of standard library headers
//               (David Abrahams)

#ifndef ITERATOR_DWA122600_HPP_
# define ITERATOR_DWA122600_HPP_

# include <boost/config.hpp>
# include <iterator>

// STLPort 4.0 and betas have a bug when debugging is enabled and there is no
// partial specialization: instead of an iterator_category typedef, the standard
// container iterators have _Iterator_category.
//
// Also, whether debugging is enabled or not, there is a broken specialization
// of std::iterator<output_iterator_tag,void,void,void,void> which has no
// typedefs but iterator_category.
# if defined(__SGI_STL_PORT)

#  if (__SGI_STL_PORT <= 0x410) && !defined(__STL_CLASS_PARTIAL_SPECIALIZATION) && defined(__STL_DEBUG)
#   define BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
#  endif

#  define BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION

# endif // STLPort <= 4.1b4 && no partial specialization

# if !defined(BOOST_NO_STD_ITERATOR_TRAITS)             \
  && !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) \
  && !defined(BOOST_MSVC_STD_ITERATOR)
    
namespace boost { namespace detail {

// Define a new template so it can be specialized
template <class Iterator>
struct iterator_traits
    : std::iterator_traits<Iterator>
{};
using std::distance;

}} // namespace boost::detail

# else

#  if  !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)  \
    && !defined(BOOST_MSVC_STD_ITERATOR)

// This is the case where everything conforms except BOOST_NO_STD_ITERATOR_TRAITS

namespace boost { namespace detail {

// Rogue Wave Standard Library fools itself into thinking partial
// specialization is missing on some platforms (e.g. Sun), so fails to
// supply iterator_traits!
template <class Iterator>
struct iterator_traits
{
    typedef typename Iterator::value_type value_type;
    typedef typename Iterator::reference reference;
    typedef typename Iterator::pointer pointer;
    typedef typename Iterator::difference_type difference_type;
    typedef typename Iterator::iterator_category iterator_category;
};

template <class T>
struct iterator_traits<T*>
{
    typedef T value_type;
    typedef T& reference;
    typedef T* pointer;
    typedef std::ptrdiff_t difference_type;
    typedef std::random_access_iterator_tag iterator_category;
};

template <class T>
struct iterator_traits<T const*>
{
    typedef T value_type;
    typedef T const& reference;
    typedef T const* pointer;
    typedef std::ptrdiff_t difference_type;
    typedef std::random_access_iterator_tag iterator_category;
};

}} // namespace boost::detail

#  else

# include <boost/type_traits/remove_const.hpp>
# include <boost/type_traits/detail/yes_no_type.hpp>
# include <boost/type_traits/is_pointer.hpp>

# ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
#  include <boost/type_traits/is_same.hpp>
#  include <boost/type_traits/remove_pointer.hpp>
# endif
# ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
#  include <boost/type_traits/is_base_and_derived.hpp>
# endif

# include <boost/mpl/if.hpp>
# include <boost/mpl/has_xxx.hpp>
# include <cstddef>

// should be the last #include
# include "boost/type_traits/detail/bool_trait_def.hpp"

namespace boost { namespace detail {

BOOST_MPL_HAS_XXX_TRAIT_DEF(value_type)
BOOST_MPL_HAS_XXX_TRAIT_DEF(reference)
BOOST_MPL_HAS_XXX_TRAIT_DEF(pointer)
BOOST_MPL_HAS_XXX_TRAIT_DEF(difference_type)
BOOST_MPL_HAS_XXX_TRAIT_DEF(iterator_category)

// is_mutable_iterator --
//
//   A metafunction returning true iff T is a mutable iterator type
//   with a nested value_type. Will only work portably with iterators
//   whose operator* returns a reference, but that seems to be OK for
//   the iterators supplied by Dinkumware. Some input iterators may
//   compile-time if they arrive here, and if the compiler is strict
//   about not taking the address of an rvalue.

// This one detects ordinary mutable iterators - the result of
// operator* is convertible to the value_type.
template <class T>
type_traits::yes_type is_mutable_iterator_helper(T const*, BOOST_DEDUCED_TYPENAME T::value_type*);

// Since you can't take the address of an rvalue, the guts of
// is_mutable_iterator_impl will fail if we use &*t directly.  This
// makes sure we can still work with non-lvalue iterators.
template <class T> T* mutable_iterator_lvalue_helper(T& x);
int mutable_iterator_lvalue_helper(...);


// This one detects output iterators such as ostream_iterator which
// return references to themselves.
template <class T>
type_traits::yes_type is_mutable_iterator_helper(T const*, T const*);

type_traits::no_type is_mutable_iterator_helper(...);

template <class T>
struct is_mutable_iterator_impl
{
    static T t;
    
    BOOST_STATIC_CONSTANT(
        bool, value = sizeof(
            detail::is_mutable_iterator_helper(
                (T*)0
              , mutable_iterator_lvalue_helper(*t) // like &*t
            ))
        == sizeof(type_traits::yes_type)
    );
};

BOOST_TT_AUX_BOOL_TRAIT_DEF1(
    is_mutable_iterator,T,::boost::detail::is_mutable_iterator_impl<T>::value)


// is_full_iterator_traits --
//
//   A metafunction returning true iff T has all the requisite nested
//   types to satisfy the requirements for a fully-conforming
//   iterator_traits implementation.
template <class T>
struct is_full_iterator_traits_impl
{
    enum { value = 
           has_value_type<T>::value 
           & has_reference<T>::value 
           & has_pointer<T>::value 
           & has_difference_type<T>::value
           & has_iterator_category<T>::value
    };
};

BOOST_TT_AUX_BOOL_TRAIT_DEF1(
    is_full_iterator_traits,T,::boost::detail::is_full_iterator_traits_impl<T>::value)


#   ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
BOOST_MPL_HAS_XXX_TRAIT_DEF(_Iterator_category)
    
// is_stlport_40_debug_iterator --
//
//   A metafunction returning true iff T has all the requisite nested
//   types to satisfy the requirements of an STLPort 4.0 debug iterator
//   iterator_traits implementation.
template <class T>
struct is_stlport_40_debug_iterator_impl
{
    enum { value = 
           has_value_type<T>::value 
           & has_reference<T>::value 
           & has_pointer<T>::value 
           & has_difference_type<T>::value
           & has__Iterator_category<T>::value
    };
};

BOOST_TT_AUX_BOOL_TRAIT_DEF1(
    is_stlport_40_debug_iterator,T,::boost::detail::is_stlport_40_debug_iterator_impl<T>::value)

template <class T>
struct stlport_40_debug_iterator_traits
{
    typedef typename T::value_type value_type;
    typedef typename T::reference reference;
    typedef typename T::pointer pointer;
    typedef typename T::difference_type difference_type;
    typedef typename T::_Iterator_category iterator_category;
};
#   endif // BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF 

template <class T> struct pointer_iterator_traits;

#   ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T>
struct pointer_iterator_traits<T*>
{
    typedef typename remove_const<T>::type value_type;
    typedef T* pointer;
    typedef T& reference;
    typedef std::random_access_iterator_tag iterator_category;
    typedef std::ptrdiff_t difference_type;
};
#   else

// In case of no template partial specialization, and if T is a
// pointer, iterator_traits<T>::value_type can still be computed.  For
// some basic types, remove_pointer is manually defined in
// type_traits/broken_compiler_spec.hpp. For others, do it yourself.

template<class P> class please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee;

template<class P>
struct pointer_value_type
  : mpl::if_<
        is_same<P, typename remove_pointer<P>::type>
      , please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee<P>
      , typename remove_const<
            typename remove_pointer<P>::type
        >::type
    >
{
};


template<class P>
struct pointer_reference
  : mpl::if_<
        is_same<P, typename remove_pointer<P>::type>
      , please_invoke_BOOST_TT_BROKEN_COMPILER_SPEC_on_cv_unqualified_pointee<P>
      , typename remove_pointer<P>::type&
    >
{
};

template <class T>
struct pointer_iterator_traits
{
    typedef T pointer;
    typedef std::random_access_iterator_tag iterator_category;
    typedef std::ptrdiff_t difference_type;

    typedef typename pointer_value_type<T>::type value_type;
    typedef typename pointer_reference<T>::type reference;
};

#   endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION

// We'll sort iterator types into one of these classifications, from which we
// can determine the difference_type, pointer, reference, and value_type
template <class Iterator>
struct standard_iterator_traits
{
    typedef typename Iterator::difference_type difference_type;
    typedef typename Iterator::value_type value_type;
    typedef typename Iterator::pointer pointer;
    typedef typename Iterator::reference reference;
    typedef typename Iterator::iterator_category iterator_category;
};

template <class Iterator>
struct msvc_stdlib_mutable_traits
    : std::iterator_traits<Iterator>
{
    typedef typename std::iterator_traits<Iterator>::distance_type difference_type;
    typedef typename std::iterator_traits<Iterator>::value_type* pointer;
    typedef typename std::iterator_traits<Iterator>::value_type& reference;
};

template <class Iterator>
struct msvc_stdlib_const_traits
    : std::iterator_traits<Iterator>
{
    typedef typename std::iterator_traits<Iterator>::distance_type difference_type;
    typedef const typename std::iterator_traits<Iterator>::value_type* pointer;
    typedef const typename std::iterator_traits<Iterator>::value_type& reference;
};

#   ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
template <class Iterator>
struct is_bad_output_iterator
    : is_base_and_derived<
        std::iterator<std::output_iterator_tag,void,void,void,void>
        , Iterator>
{
};

struct bad_output_iterator_traits
{
    typedef void value_type;
    typedef void difference_type;
    typedef std::output_iterator_tag iterator_category;
    typedef void pointer;
    typedef void reference;
};
#   endif

// If we're looking at an MSVC6 (old Dinkumware) ``standard''
// iterator, this will generate an appropriate traits class. 
template <class Iterator>
struct msvc_stdlib_iterator_traits
    : mpl::if_<
       is_mutable_iterator<Iterator>
       , msvc_stdlib_mutable_traits<Iterator>
       , msvc_stdlib_const_traits<Iterator>
      >::type
{};

template <class Iterator>
struct non_pointer_iterator_traits
    : mpl::if_<
        // if the iterator contains all the right nested types...
        is_full_iterator_traits<Iterator>
        // Use a standard iterator_traits implementation
        , standard_iterator_traits<Iterator>
#   ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
        // Check for STLPort 4.0 broken _Iterator_category type
        , mpl::if_<
             is_stlport_40_debug_iterator<Iterator>
             , stlport_40_debug_iterator_traits<Iterator>
#   endif
        // Otherwise, assume it's a Dinkum iterator
        , msvc_stdlib_iterator_traits<Iterator>
#   ifdef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
        >::type
#   endif 
    >::type
{
};

template <class Iterator>
struct iterator_traits_aux
    : mpl::if_<
        is_pointer<Iterator>
        , pointer_iterator_traits<Iterator>
        , non_pointer_iterator_traits<Iterator>
    >::type
{
};

template <class Iterator>
struct iterator_traits
{
    // Explicit forwarding from base class needed to keep MSVC6 happy
    // under some circumstances.
 private:
#   ifdef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION
    typedef 
    typename mpl::if_<
        is_bad_output_iterator<Iterator>
        , bad_output_iterator_traits
        , iterator_traits_aux<Iterator>
    >::type base;
#   else
    typedef iterator_traits_aux<Iterator> base;
#   endif
 public:
    typedef typename base::value_type value_type;
    typedef typename base::pointer pointer;
    typedef typename base::reference reference;
    typedef typename base::difference_type difference_type;
    typedef typename base::iterator_category iterator_category;
};

// This specialization cuts off ETI (Early Template Instantiation) for MSVC.
template <> struct iterator_traits<int>
{
    typedef int value_type;
    typedef int pointer;
    typedef int reference;
    typedef int difference_type;
    typedef int iterator_category;
};

}} // namespace boost::detail

#  endif // workarounds

namespace boost { namespace detail {

namespace iterator_traits_
{
  template <class Iterator, class Difference>
  struct distance_select
  {
      static Difference execute(Iterator i1, const Iterator i2, ...)
      {
          Difference result = 0;
          while (i1 != i2)
          {
              ++i1;
              ++result;
          }
          return result;
      }

      static Difference execute(Iterator i1, const Iterator i2, std::random_access_iterator_tag*)
      {
          return i2 - i1;
      }
  };
} // namespace boost::detail::iterator_traits_

template <class Iterator>
inline typename iterator_traits<Iterator>::difference_type
distance(Iterator first, Iterator last)
{
    typedef typename iterator_traits<Iterator>::difference_type diff_t;
    typedef typename ::boost::detail::iterator_traits<Iterator>::iterator_category iterator_category;
    
    return iterator_traits_::distance_select<Iterator,diff_t>::execute(
        first, last, (iterator_category*)0);
}

}}

# endif


# undef BOOST_BAD_CONTAINER_ITERATOR_CATEGORY_TYPEDEF
# undef BOOST_BAD_OUTPUT_ITERATOR_SPECIALIZATION

#endif // ITERATOR_DWA122600_HPP_