rigged new iterator_traits for backward compatibility

[SVN r9577]
This commit is contained in:
Jeremy Siek
2001-03-18 18:37:49 +00:00
parent cbbe851adb
commit eb06c122d1
3 changed files with 205 additions and 153 deletions

View File

@ -3,6 +3,8 @@
#include <boost/concept_check.hpp> #include <boost/concept_check.hpp>
#include <boost/iterator_traits.hpp> #include <boost/iterator_traits.hpp>
#include <boost/type_traits/conversion_traits.hpp>
#include <boost/static_assert.hpp>
namespace boost_concepts { namespace boost_concepts {
// Used a different namespace here (instead of "boost") so that the // Used a different namespace here (instead of "boost") so that the
@ -11,6 +13,7 @@ namespace boost_concepts {
//=========================================================================== //===========================================================================
// Iterator Access Concepts
template <typename Iterator> template <typename Iterator>
class ReadableIteratorConcept { class ReadableIteratorConcept {
@ -23,11 +26,12 @@ namespace boost_concepts {
void constraints() { void constraints() {
boost::function_requires< boost::SGIAssignableConcept<Iterator> >(); boost::function_requires< boost::SGIAssignableConcept<Iterator> >();
boost::function_requires< boost::EqualityComparableConcept<Iterator> >(); boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
boost::function_requires< boost::DefaultConstructibleConcept<Iterator> >();
boost::function_requires< boost::function_requires<
boost::ConvertibleConcept<return_category, boost::readable_iterator_tag> >(); boost::DefaultConstructibleConcept<Iterator> >();
BOOST_STATIC_ASSERT((boost::is_convertible<return_category*,
boost::readable_iterator_tag*>::value));
reference r = *i; // or perhaps read(x) reference r = *i; // or perhaps read(x)
value_type v(r); value_type v(r);
boost::ignore_unused_variable_warning(v); boost::ignore_unused_variable_warning(v);
@ -44,10 +48,11 @@ namespace boost_concepts {
void constraints() { void constraints() {
boost::function_requires< boost::SGIAssignableConcept<Iterator> >(); boost::function_requires< boost::SGIAssignableConcept<Iterator> >();
boost::function_requires< boost::EqualityComparableConcept<Iterator> >(); boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
boost::function_requires< boost::DefaultConstructibleConcept<Iterator> >();
boost::function_requires< boost::function_requires<
boost::ConvertibleConcept<return_category, boost::writable_iterator_tag> >(); boost::DefaultConstructibleConcept<Iterator> >();
BOOST_STATIC_ASSERT((boost::is_convertible<return_category*,
boost::writable_iterator_tag*>::value));
*i = v; // an alternative could be something like write(x, v) *i = v; // an alternative could be something like write(x, v)
} }
@ -65,12 +70,12 @@ namespace boost_concepts {
void constraints() { void constraints() {
boost::function_requires< ReadableIteratorConcept<Iterator> >(); boost::function_requires< ReadableIteratorConcept<Iterator> >();
boost::function_requires<
boost::ConvertibleConcept<return_category,
boost::constant_lvalue_iterator_tag> >();
typedef typename boost::require_same<reference, const value_type&>::type req; BOOST_STATIC_ASSERT((boost::is_convertible<return_category*,
boost::constant_lvalue_iterator_tag*>::value));
BOOST_STATIC_ASSERT((boost::is_same<reference,
const value_type&>::value));
reference v = *i; reference v = *i;
boost::ignore_unused_variable_warning(v); boost::ignore_unused_variable_warning(v);
@ -88,13 +93,13 @@ namespace boost_concepts {
void constraints() { void constraints() {
boost::function_requires< ReadableIteratorConcept<Iterator> >(); boost::function_requires< ReadableIteratorConcept<Iterator> >();
boost::function_requires< WritableIteratorConcept<Iterator, value_type> >();
boost::function_requires< boost::function_requires<
boost::ConvertibleConcept<return_category, WritableIteratorConcept<Iterator, value_type> >();
boost::constant_lvalue_iterator_tag> >();
BOOST_STATIC_ASSERT((boost::is_convertible<return_category*,
boost::mutable_lvalue_iterator_tag*>::value));
typedef typename boost::require_same<reference, value_type&>::type req; BOOST_STATIC_ASSERT((boost::is_same<reference, value_type&>::value));
reference v = *i; reference v = *i;
boost::ignore_unused_variable_warning(v); boost::ignore_unused_variable_warning(v);
@ -103,24 +108,25 @@ namespace boost_concepts {
}; };
//=========================================================================== //===========================================================================
// Iterator Traversal Concepts
template <typename Iterator> template <typename Iterator>
class SinglePassIteratorConcept { class SinglePassIteratorConcept {
public: public:
typedef typename boost::iterator_traits<Iterator>::motion_category typedef typename boost::iterator_traits<Iterator>::traversal_category
motion_category; traversal_category;
typedef typename boost::iterator_traits<Iterator>::difference_type typedef typename boost::iterator_traits<Iterator>::difference_type
difference_type; difference_type;
void constraints() { void constraints() {
boost::function_requires< boost::SGIAssignableConcept<Iterator> >(); boost::function_requires< boost::SGIAssignableConcept<Iterator> >();
boost::function_requires< boost::EqualityComparableConcept<Iterator> >(); boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
boost::function_requires< boost::DefaultConstructibleConcept<Iterator> >();
boost::function_requires< boost::function_requires<
boost::ConvertibleConcept<motion_category, boost::DefaultConstructibleConcept<Iterator> >();
boost::single_pass_iterator_tag> >();
BOOST_STATIC_ASSERT((boost::is_convertible<traversal_category*,
boost::single_pass_iterator_tag*>::value));
// difference_type must be a signed integral type // difference_type must be a signed integral type
++i; ++i;
@ -133,30 +139,28 @@ namespace boost_concepts {
template <typename Iterator> template <typename Iterator>
class ForwardIteratorConcept { class ForwardIteratorConcept {
public: public:
typedef typename boost::iterator_traits<Iterator>::motion_category typedef typename boost::iterator_traits<Iterator>::traversal_category
motion_category; traversal_category;
void constraints() { void constraints() {
boost::function_requires< SinglePassIteratorConcept<Iterator> >(); boost::function_requires< SinglePassIteratorConcept<Iterator> >();
boost::function_requires< BOOST_STATIC_ASSERT((boost::is_convertible<traversal_category*,
boost::ConvertibleConcept<motion_category, boost::forward_iterator_tag*>::value));
boost::forward_iterator_tag> >();
} }
}; };
template <typename Iterator> template <typename Iterator>
class BidirectionalIteratorConcept { class BidirectionalIteratorConcept {
public: public:
typedef typename boost::iterator_traits<Iterator>::motion_category typedef typename boost::iterator_traits<Iterator>::traversal_category
motion_category; traversal_category;
void constraints() { void constraints() {
boost::function_requires< ForwardIteratorConcept<Iterator> >(); boost::function_requires< ForwardIteratorConcept<Iterator> >();
boost::function_requires< BOOST_STATIC_ASSERT((boost::is_convertible<traversal_category*,
boost::ConvertibleConcept<motion_category, boost::bidirectional_iterator_tag*>::value));
boost::bidirectional_iterator_tag> >();
--i; --i;
(void)i--; (void)i--;
@ -167,18 +171,17 @@ namespace boost_concepts {
template <typename Iterator> template <typename Iterator>
class RandomAccessIteratorConcept { class RandomAccessIteratorConcept {
public: public:
typedef typename boost::iterator_traits<Iterator>::motion_category typedef typename boost::iterator_traits<Iterator>::traversal_category
motion_category; traversal_category;
typedef typename boost::iterator_traits<Iterator>::difference_type typedef typename boost::iterator_traits<Iterator>::difference_type
difference_type; difference_type;
void constraints() { void constraints() {
boost::function_requires< BidirectionalIteratorConcept<Iterator> >(); boost::function_requires< BidirectionalIteratorConcept<Iterator> >();
boost::function_requires< BOOST_STATIC_ASSERT((boost::is_convertible<traversal_category*,
boost::ConvertibleConcept<motion_category, boost::random_access_iterator_tag*>::value));
boost::random_access_iterator_tag> >();
i += n; i += n;
i = i + n; i = i + n;
i = n + i; i = n + i;
@ -190,92 +193,6 @@ namespace boost_concepts {
Iterator i, j; Iterator i, j;
}; };
//===========================================================================
template <typename Iterator>
class ReadableRandomAccessIteratorConcept {
public:
typedef typename boost::iterator_traits<Iterator>::value_type value_type;
typedef typename boost::iterator_traits<Iterator>::reference reference;
typedef typename boost::iterator_traits<Iterator>::difference_type
difference_type;
void constraints() {
boost::function_requires< RandomAccessIteratorConcept<Iterator> >();
boost::function_requires< ReadableIteratorConcept<Iterator> >();
reference r = i[n];
value_type v(r);
boost::ignore_unused_variable_warning(v);
}
difference_type n;
Iterator i;
};
template <typename Iterator>
class WritableRandomAccessIteratorConcept {
public:
typedef typename boost::iterator_traits<Iterator>::value_type value_type;
typedef typename boost::iterator_traits<Iterator>::difference_type
difference_type;
void constraints() {
boost::function_requires< RandomAccessIteratorConcept<Iterator> >();
boost::function_requires< WritableIteratorConcept<Iterator, value_type> >();
i[n] = v;
boost::ignore_unused_variable_warning(v);
}
difference_type n;
value_type v;
Iterator i;
};
template <typename Iterator>
class ConstantLvalueRandomAccessIteratorConcept {
public:
typedef typename boost::iterator_traits<Iterator>::value_type value_type;
typedef typename boost::iterator_traits<Iterator>::reference reference;
typedef typename boost::iterator_traits<Iterator>::difference_type
difference_type;
void constraints() {
boost::function_requires< RandomAccessIteratorConcept<Iterator> >();
boost::function_requires< ReadableIteratorConcept<Iterator> >();
typedef typename boost::require_same<reference, const value_type&>::type req;
reference v = i[n];
boost::ignore_unused_variable_warning(v);
}
difference_type n;
value_type v;
Iterator i;
};
template <typename Iterator>
class MutableLvalueRandomAccessIteratorConcept {
public:
typedef typename boost::iterator_traits<Iterator>::value_type value_type;
typedef typename boost::iterator_traits<Iterator>::reference reference;
typedef typename boost::iterator_traits<Iterator>::difference_type
difference_type;
void constraints() {
boost::function_requires< RandomAccessIteratorConcept<Iterator> >();
boost::function_requires< WritableIteratorConcept<Iterator, value_type> >();
boost::function_requires< ReadableIteratorConcept<Iterator> >();
typedef typename boost::require_same<reference, value_type&>::type req;
reference v = i[n];
boost::ignore_unused_variable_warning(v);
}
difference_type n;
value_type v;
Iterator i;
};
} // namespace boost_concepts } // namespace boost_concepts

View File

@ -2,21 +2,14 @@
#define BOOST_ITERATOR_TRAITS_HPP #define BOOST_ITERATOR_TRAITS_HPP
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/type_traits.hpp> #include <boost/type_traits/conversion_traits.hpp>
#include <boost/type_traits/cv_traits.hpp>
#include <boost/pending/ct_if.hpp>
#include <boost/detail/iterator.hpp>
namespace boost { namespace boost {
template <typename 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::return_category return_category;
typedef typename Iterator::motion_category motion_category;
};
// Motion Categories // Traversal Categories
struct single_pass_iterator_tag { }; struct single_pass_iterator_tag { };
struct forward_iterator_tag : public single_pass_iterator_tag { }; struct forward_iterator_tag : public single_pass_iterator_tag { };
struct bidirectional_iterator_tag : public forward_iterator_tag { }; struct bidirectional_iterator_tag : public forward_iterator_tag { };
@ -28,29 +21,113 @@ namespace boost {
struct mutable_lvalue_iterator_tag : virtual public writable_iterator_tag, struct mutable_lvalue_iterator_tag : virtual public writable_iterator_tag,
virtual public readable_iterator_tag { }; virtual public readable_iterator_tag { };
struct constant_lvalue_iterator_tag : public readable_iterator_tag { }; struct constant_lvalue_iterator_tag : public readable_iterator_tag { };
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) struct error_iterator_tag { };
// Inherit from iterator_base if your iterator defines its own
// return_category and traversal_category. Otherwise, the "old style"
// iterator category will be mapped to the return_category and
// traversal_category.
struct new_iterator_base { };
namespace detail { namespace detail {
template <bool IsConst>
struct pointer_return_category { struct iter_traits_from_nested_types {
typedef constant_lvalue_iterator_tag type; template <typename Iterator> struct bind {
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::return_category return_category;
typedef typename Iterator::traversal_category traversal_category;
};
}; };
template <>
struct pointer_return_category<false> { template <typename ValueType>
typedef mutable_lvalue_iterator_tag type; struct choose_lvalue_return {
typedef typename ct_if<is_const<ValueType>::value,
boost::constant_lvalue_iterator_tag,
boost::mutable_lvalue_iterator_tag>::type type;
}; };
template <typename Category, typename ValueType>
struct iter_category_to_return {
typedef typename ct_if<
is_convertible<Category*, std::forward_iterator_tag*>::value,
typename choose_lvalue_return<ValueType>::type,
typename ct_if<
is_convertible<Category*, std::input_iterator_tag*>::value,
boost::readable_iterator_tag,
typename ct_if<
is_convertible<Category*, std::output_iterator_tag*>::value,
boost::writable_iterator_tag,
boost::error_iterator_tag
>::type
>::type
>::type type;
};
template <typename Category>
struct iter_category_to_traversal {
typedef typename ct_if<
is_convertible<Category*, std::random_access_iterator_tag*>::value,
boost::random_access_iterator_tag,
typename ct_if<
is_convertible<Category*, std::bidirectional_iterator_tag*>::value,
boost::bidirectional_iterator_tag,
typename ct_if<
is_convertible<Category*, std::forward_iterator_tag*>::value,
boost::forward_iterator_tag,
boost::single_pass_iterator_tag>::type
>::type
>::type type;
};
struct iter_traits_from_old_traits {
template <typename Iterator> class bind {
typedef boost::detail::iterator_traits<Iterator> OldTraits;
typedef typename OldTraits::iterator_category Cat;
public:
typedef typename OldTraits::value_type value_type;
typedef typename OldTraits::reference reference;
typedef typename OldTraits::pointer pointer;
typedef typename OldTraits::difference_type difference_type;
typedef iter_category_to_return<Cat,value_type>::type return_category;
typedef iter_category_to_traversal<Cat>::type traversal_category;
};
};
template <typename Iterator>
class choose_iter_traits {
typedef typename ct_if<is_convertible<Iterator*,
new_iterator_base*>::value,
iter_traits_from_nested_types,
iter_traits_from_old_traits>::type Choice;
public:
typedef typename Choice:: template bind<Iterator> type;
};
} // namespace detail } // namespace detail
template <typename Iterator>
class iterator_traits
: public detail::choose_iter_traits<Iterator>::type { };
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
template <typename T> template <typename T>
struct iterator_traits<T*> { struct iterator_traits<T*>
{
typedef T value_type; typedef T value_type;
typedef T& reference; typedef T& reference;
typedef T* pointer; typedef T* pointer;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
typedef typename detail::pointer_return_category<is_const<T>::value>::type typedef typename ct_if<is_const<T>::value,
boost::constant_lvalue_iterator_tag,
boost::mutable_lvalue_iterator_tag>::type
return_category; return_category;
typedef random_access_iterator_tag motion_category; typedef boost::random_access_iterator_tag traversal_category;
}; };
#endif #endif

View File

@ -1,13 +1,71 @@
#include <boost/iterator_concepts.hpp> #include <boost/iterator_concepts.hpp>
#include <boost/operators.hpp>
struct new_iterator
: public boost::iterator<std::random_access_iterator_tag, int>,
public boost::new_iterator_base
{
typedef boost::random_access_iterator_tag traversal_category;
typedef boost::mutable_lvalue_iterator_tag return_category;
int& operator*() const { return *m_x; }
new_iterator& operator++() { return *this; }
new_iterator operator++(int) { return *this; }
new_iterator& operator--() { return *this; }
new_iterator operator--(int) { return *this; }
new_iterator& operator+=(std::ptrdiff_t) { return *this; }
new_iterator operator+(std::ptrdiff_t) { return *this; }
new_iterator& operator-=(std::ptrdiff_t) { return *this; }
std::ptrdiff_t operator-(const new_iterator&) const { return 0; }
new_iterator operator-(std::ptrdiff_t) const { return *this; }
bool operator==(const new_iterator&) const { return false; }
bool operator!=(const new_iterator&) const { return false; }
bool operator<(const new_iterator&) const { return false; }
int* m_x;
};
new_iterator operator+(std::ptrdiff_t, new_iterator x) { return x; }
struct old_iterator
: public boost::iterator<std::random_access_iterator_tag, int>
{
int& operator*() const { return *m_x; }
old_iterator& operator++() { return *this; }
old_iterator operator++(int) { return *this; }
old_iterator& operator--() { return *this; }
old_iterator operator--(int) { return *this; }
old_iterator& operator+=(std::ptrdiff_t) { return *this; }
old_iterator operator+(std::ptrdiff_t) { return *this; }
old_iterator& operator-=(std::ptrdiff_t) { return *this; }
old_iterator operator-(std::ptrdiff_t) const { return *this; }
std::ptrdiff_t operator-(const old_iterator&) const { return 0; }
bool operator==(const old_iterator&) const { return false; }
bool operator!=(const old_iterator&) const { return false; }
bool operator<(const old_iterator&) const { return false; }
int* m_x;
};
old_iterator operator+(std::ptrdiff_t, old_iterator x) { return x; }
int int
main() main()
{ {
boost::function_requires< boost::function_requires<
boost_concepts::MutableLvalueRandomAccessIteratorConcept<int*> >(); boost_concepts::MutableLvalueIteratorConcept<int*> >();
boost::function_requires<
boost_concepts::RandomAccessIteratorConcept<int*> >();
boost::function_requires< boost::function_requires<
boost_concepts::ConstantLvalueRandomAccessIteratorConcept<const int*> >(); boost_concepts::ConstantLvalueIteratorConcept<const int*> >();
boost::function_requires<
boost_concepts::RandomAccessIteratorConcept<const int*> >();
boost::function_requires<
boost_concepts::MutableLvalueIteratorConcept<new_iterator> >();
boost::function_requires<
boost_concepts::RandomAccessIteratorConcept<new_iterator> >();
boost::function_requires<
boost_concepts::MutableLvalueIteratorConcept<old_iterator> >();
boost::function_requires<
boost_concepts::RandomAccessIteratorConcept<old_iterator> >();
return 0; return 0;
} }