forked from boostorg/iterator
More fun with iterator adaptors
[SVN r1139]
This commit is contained in:
@@ -86,23 +86,73 @@ struct my_gen
|
||||
template <class V>
|
||||
struct ptr_iterator
|
||||
: boost::iterator_adaptor<
|
||||
ptr_iterator<V>,
|
||||
boost::iterator_traits_adaptor<V*,
|
||||
V,
|
||||
boost::iterator_tag< boost::writable_lvalue_iterator_tag
|
||||
,boost::random_access_traversal_tag > >
|
||||
>
|
||||
ptr_iterator<V>
|
||||
, V*
|
||||
, V
|
||||
, std::random_access_iterator_tag
|
||||
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
||||
, V&, V*
|
||||
#endif
|
||||
>
|
||||
{
|
||||
private:
|
||||
typedef iterator_adaptor super_t;
|
||||
typedef boost::iterator_adaptor<
|
||||
ptr_iterator<V>
|
||||
, V*
|
||||
, V
|
||||
, std::random_access_iterator_tag
|
||||
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
||||
, V&, V*
|
||||
#endif
|
||||
> super_t;
|
||||
|
||||
public:
|
||||
ptr_iterator() { }
|
||||
ptr_iterator(V* d) : super_t(d) { }
|
||||
|
||||
template <class V2>
|
||||
ptr_iterator(const ptr_iterator<V2>& x) // should assert is_same add_cv<V> add_cv<V2>
|
||||
: super_t(x.base()) { }
|
||||
ptr_iterator(
|
||||
const ptr_iterator<V2>& x
|
||||
, typename boost::enable_if_convertible<V2, V>::type* = 0
|
||||
)
|
||||
: super_t(x.base())
|
||||
{}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct fwd_iterator
|
||||
: boost::iterator_adaptor<
|
||||
fwd_iterator<T>
|
||||
, boost::forward_iterator_archetype<T>
|
||||
>
|
||||
{
|
||||
private:
|
||||
typedef boost::iterator_adaptor<
|
||||
fwd_iterator<T>
|
||||
, boost::forward_iterator_archetype<T>
|
||||
> super_t;
|
||||
|
||||
public:
|
||||
fwd_iterator() { }
|
||||
fwd_iterator(boost::forward_iterator_archetype<T> d) : super_t(d) { }
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct in_iterator
|
||||
: boost::iterator_adaptor<
|
||||
in_iterator<T>
|
||||
, boost::input_iterator_archetype<T>
|
||||
>
|
||||
{
|
||||
private:
|
||||
typedef boost::iterator_adaptor<
|
||||
in_iterator<T>
|
||||
, boost::input_iterator_archetype<T>
|
||||
> super_t;
|
||||
|
||||
public:
|
||||
in_iterator() { }
|
||||
in_iterator(boost::input_iterator_archetype<T> d) : super_t(d) { }
|
||||
};
|
||||
|
||||
int
|
||||
@@ -128,8 +178,7 @@ main()
|
||||
// Test the iterator_traits
|
||||
{
|
||||
// Test computation of defaults
|
||||
typedef boost::iterator_adaptor<int*, boost::default_iterator_policies,
|
||||
boost::value_type_is<int> > Iter1;
|
||||
typedef ptr_iterator<int> Iter1;
|
||||
// don't use std::iterator_traits here to avoid VC++ problems
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::value_type, int>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, int&>::value));
|
||||
@@ -139,40 +188,18 @@ main()
|
||||
}
|
||||
{
|
||||
// Test computation of default when the Value is const
|
||||
typedef boost::iterator_adaptor<std::list<int>::iterator,
|
||||
boost::default_iterator_policies,
|
||||
boost::value_type_is<const int> > Iter1;
|
||||
typedef ptr_iterator<int const> Iter1;
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::value_type, int>::value));
|
||||
#if defined(__BORLANDC__) || defined(BOOST_MSVC) && BOOST_MSVC <= 1300
|
||||
// We currently don't know how to workaround this bug.
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, int&>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::pointer, int*>::value));
|
||||
#else
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, const int&>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::pointer, const int*>::value));
|
||||
#endif
|
||||
}
|
||||
{
|
||||
// Test with no defaults
|
||||
typedef boost::iterator_adaptor<int*, boost::default_iterator_policies,
|
||||
boost::reference_is<long>,
|
||||
boost::pointer_is<float*>,
|
||||
boost::value_type_is<char>,
|
||||
boost::iterator_category_is<std::input_iterator_tag>,
|
||||
boost::difference_type_is<int>
|
||||
> Iter1;
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::value_type, char>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, long>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::pointer, float*>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::difference_type, int>::value));
|
||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::iterator_category, std::input_iterator_tag>::value));
|
||||
}
|
||||
|
||||
// Test the iterator_adaptor
|
||||
{
|
||||
boost::iterator_adaptor<dummyT*, boost::default_iterator_policies, dummyT> i(array);
|
||||
ptr_iterator<dummyT> i(array);
|
||||
boost::random_access_iterator_test(i, N, array);
|
||||
|
||||
boost::iterator_adaptor<const dummyT*, boost::default_iterator_policies, const dummyT> j(array);
|
||||
ptr_iterator<const dummyT> j(array);
|
||||
boost::random_access_iterator_test(j, N, array);
|
||||
boost::const_nonconst_iterator_test(i, ++j);
|
||||
}
|
||||
@@ -180,33 +207,19 @@ main()
|
||||
// check operator-> with a forward iterator
|
||||
{
|
||||
boost::forward_iterator_archetype<dummyT> forward_iter;
|
||||
#if defined(__BORLANDC__)
|
||||
typedef boost::iterator_adaptor<boost::forward_iterator_archetype<dummyT>,
|
||||
boost::default_iterator_policies,
|
||||
dummyT, const dummyT&, const dummyT*,
|
||||
std::forward_iterator_tag, std::ptrdiff_t> adaptor_type;
|
||||
#else
|
||||
typedef boost::iterator_adaptor<boost::forward_iterator_archetype<dummyT>,
|
||||
boost::default_iterator_policies,
|
||||
boost::reference_is<const dummyT&>,
|
||||
boost::pointer_is<const dummyT*> ,
|
||||
boost::iterator_category_is<std::forward_iterator_tag>,
|
||||
boost::value_type_is<dummyT>,
|
||||
boost::difference_type_is<std::ptrdiff_t>
|
||||
> adaptor_type;
|
||||
#endif
|
||||
|
||||
typedef fwd_iterator<dummyT> adaptor_type;
|
||||
|
||||
adaptor_type i(forward_iter);
|
||||
int zero = 0;
|
||||
if (zero) // don't do this, just make sure it compiles
|
||||
assert((*i).m_x == i->foo());
|
||||
}
|
||||
|
||||
// check operator-> with an input iterator
|
||||
{
|
||||
boost::input_iterator_archetype<dummyT> input_iter;
|
||||
typedef boost::iterator_adaptor<boost::input_iterator_archetype<dummyT>,
|
||||
boost::default_iterator_policies,
|
||||
dummyT, const dummyT&, const dummyT*,
|
||||
std::input_iterator_tag, std::ptrdiff_t> adaptor_type;
|
||||
typedef in_iterator<dummyT> adaptor_type;
|
||||
adaptor_type i(input_iter);
|
||||
int zero = 0;
|
||||
if (zero) // don't do this, just make sure it compiles
|
||||
|
Reference in New Issue
Block a user