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>
|
template <class V>
|
||||||
struct ptr_iterator
|
struct ptr_iterator
|
||||||
: boost::iterator_adaptor<
|
: boost::iterator_adaptor<
|
||||||
ptr_iterator<V>,
|
ptr_iterator<V>
|
||||||
boost::iterator_traits_adaptor<V*,
|
, V*
|
||||||
V,
|
, V
|
||||||
boost::iterator_tag< boost::writable_lvalue_iterator_tag
|
, std::random_access_iterator_tag
|
||||||
,boost::random_access_traversal_tag > >
|
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551))
|
||||||
|
, V&, V*
|
||||||
|
#endif
|
||||||
>
|
>
|
||||||
{
|
{
|
||||||
private:
|
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:
|
public:
|
||||||
ptr_iterator() { }
|
ptr_iterator() { }
|
||||||
ptr_iterator(V* d) : super_t(d) { }
|
ptr_iterator(V* d) : super_t(d) { }
|
||||||
|
|
||||||
template <class V2>
|
template <class V2>
|
||||||
ptr_iterator(const ptr_iterator<V2>& x) // should assert is_same add_cv<V> add_cv<V2>
|
ptr_iterator(
|
||||||
: super_t(x.base()) { }
|
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
|
int
|
||||||
@@ -128,8 +178,7 @@ main()
|
|||||||
// Test the iterator_traits
|
// Test the iterator_traits
|
||||||
{
|
{
|
||||||
// Test computation of defaults
|
// Test computation of defaults
|
||||||
typedef boost::iterator_adaptor<int*, boost::default_iterator_policies,
|
typedef ptr_iterator<int> Iter1;
|
||||||
boost::value_type_is<int> > Iter1;
|
|
||||||
// don't use std::iterator_traits here to avoid VC++ problems
|
// 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::value_type, int>::value));
|
||||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::reference, 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
|
// Test computation of default when the Value is const
|
||||||
typedef boost::iterator_adaptor<std::list<int>::iterator,
|
typedef ptr_iterator<int const> Iter1;
|
||||||
boost::default_iterator_policies,
|
|
||||||
boost::value_type_is<const int> > Iter1;
|
|
||||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::value_type, int>::value));
|
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::reference, const int&>::value));
|
||||||
BOOST_STATIC_ASSERT((boost::is_same<Iter1::pointer, 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
|
// 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::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::random_access_iterator_test(j, N, array);
|
||||||
boost::const_nonconst_iterator_test(i, ++j);
|
boost::const_nonconst_iterator_test(i, ++j);
|
||||||
}
|
}
|
||||||
@@ -180,33 +207,19 @@ main()
|
|||||||
// check operator-> with a forward iterator
|
// check operator-> with a forward iterator
|
||||||
{
|
{
|
||||||
boost::forward_iterator_archetype<dummyT> forward_iter;
|
boost::forward_iterator_archetype<dummyT> forward_iter;
|
||||||
#if defined(__BORLANDC__)
|
|
||||||
typedef boost::iterator_adaptor<boost::forward_iterator_archetype<dummyT>,
|
typedef fwd_iterator<dummyT> adaptor_type;
|
||||||
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
|
|
||||||
adaptor_type i(forward_iter);
|
adaptor_type i(forward_iter);
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
if (zero) // don't do this, just make sure it compiles
|
if (zero) // don't do this, just make sure it compiles
|
||||||
assert((*i).m_x == i->foo());
|
assert((*i).m_x == i->foo());
|
||||||
}
|
}
|
||||||
|
|
||||||
// check operator-> with an input iterator
|
// check operator-> with an input iterator
|
||||||
{
|
{
|
||||||
boost::input_iterator_archetype<dummyT> input_iter;
|
boost::input_iterator_archetype<dummyT> input_iter;
|
||||||
typedef boost::iterator_adaptor<boost::input_iterator_archetype<dummyT>,
|
typedef in_iterator<dummyT> adaptor_type;
|
||||||
boost::default_iterator_policies,
|
|
||||||
dummyT, const dummyT&, const dummyT*,
|
|
||||||
std::input_iterator_tag, std::ptrdiff_t> adaptor_type;
|
|
||||||
adaptor_type i(input_iter);
|
adaptor_type i(input_iter);
|
||||||
int zero = 0;
|
int zero = 0;
|
||||||
if (zero) // don't do this, just make sure it compiles
|
if (zero) // don't do this, just make sure it compiles
|
||||||
|
Reference in New Issue
Block a user