Added traits:

is_incrementable.hpp: checks whether ++x is well-formed

   pointee.hpp: value_type of iterators or smart pointers

   indirect_reference.hpp: reference type of iterators or smart pointers

indirect_iterator.hpp
indirect_iterator_member_types.cpp

   Use pointee/indirect_reference to select value/reference type.

iterator_concepts.hpp: Fixed interoperable test.  Hardly tests enough, but it's a start

minimum_category.hpp: Better error messages for vc6

indirect_iterator_test.cpp: Workarounds for compilers without SFINAE

static_assert_same.hpp: Informative error reports; added a macro.

zip_iterator_test.hpp: Added missing #include

Jamfile: made zip_iterator test pass with vc6/stlport


[SVN r21514]
This commit is contained in:
Dave Abrahams
2004-01-06 17:35:36 +00:00
parent f716d705c5
commit 20b31d1cca
9 changed files with 177 additions and 104 deletions

View File

@ -21,7 +21,13 @@ namespace boost { namespace detail {
// //
// //
template <bool GreaterEqual, bool LessEqual> template <bool GreaterEqual, bool LessEqual>
struct minimum_category_impl; struct minimum_category_impl
# if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
{
typedef void type;
}
# endif
;
template <class T1, class T2> template <class T1, class T2>
struct error_not_related_by_convertibility; struct error_not_related_by_convertibility;
@ -58,15 +64,9 @@ template <>
struct minimum_category_impl<false,false> struct minimum_category_impl<false,false>
{ {
template <class T1, class T2> struct apply template <class T1, class T2> struct apply
# if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
{
typedef void type;
};
# else
: error_not_related_by_convertibility<T1,T2> : error_not_related_by_convertibility<T1,T2>
{ {
}; };
# endif
}; };
template <class T1 = mpl::_1, class T2 = mpl::_2> template <class T1 = mpl::_1, class T2 = mpl::_2>

View File

@ -12,11 +12,15 @@
#include <boost/iterator.hpp> #include <boost/iterator.hpp>
#include <boost/iterator/iterator_adaptor.hpp> #include <boost/iterator/iterator_adaptor.hpp>
#include <boost/iterator/iterator_traits.hpp> #include <boost/pointee.hpp>
#include <boost/indirect_reference.hpp>
#include <boost/type_traits/remove_cv.hpp> #include <boost/detail/iterator.hpp>
#include <boost/python/detail/indirect_traits.hpp> #include <boost/python/detail/indirect_traits.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/mpl/bool.hpp> #include <boost/mpl/bool.hpp>
#include <boost/mpl/identity.hpp> #include <boost/mpl/identity.hpp>
#include <boost/mpl/apply_if.hpp> #include <boost/mpl/apply_if.hpp>
@ -39,43 +43,6 @@ namespace boost
namespace detail namespace detail
{ {
template <class T>
struct iterator_is_mutable
: mpl::not_<
boost::python::detail::is_reference_to_const<
typename iterator_reference<T>::type
>
>
{
};
// If the Value parameter is unspecified, we use this metafunction
// to deduce the default type
template <class Dereferenceable>
struct default_indirect_value
{
typedef typename mpl::if_<
iterator_is_mutable<Dereferenceable>
, typename iterator_value<Dereferenceable>::type
, typename iterator_value<Dereferenceable>::type const
>::type type;
};
// If the Reference parameter is unspecified, we use this metafunction
// to deduce the default type
template <class Dereferenceable, class Value>
struct default_indirect_reference
{
struct use_value_ref { typedef Value& type; };
typedef typename
mpl::apply_if<
is_same<Value, use_default>
, iterator_reference<Dereferenceable>
, use_value_ref
>::type type;
};
template <class Iter, class Value, class Category, class Reference, class Difference> template <class Iter, class Value, class Category, class Reference, class Difference>
struct indirect_base struct indirect_base
{ {
@ -85,11 +52,16 @@ namespace boost
indirect_iterator<Iter, Value, Category, Reference, Difference> indirect_iterator<Iter, Value, Category, Reference, Difference>
, Iter , Iter
, typename ia_dflt_help< , typename ia_dflt_help<
Value, default_indirect_value<dereferenceable> Value, pointee<dereferenceable>
>::type >::type
, Category , Category
, typename ia_dflt_help< , typename ia_dflt_help<
Reference, default_indirect_reference<dereferenceable, Value> Reference
, mpl::apply_if<
is_same<Value,use_default>
, indirect_reference<dereferenceable>
, add_reference<Value>
>
>::type >::type
, Difference , Difference
> type; > type;

View File

@ -346,35 +346,50 @@ namespace detail
} // namespace detail } // namespace detail
template <typename Iterator, typename ConstIterator> template <typename Iterator, typename ConstIterator>
class InteroperableConcept class InteroperableConcept
{ {
public: public:
typedef typename boost::iterator_traversal<Iterator>::type traversal_category; typedef typename boost::detail::pure_traversal_tag<
typedef typename boost::detail::iterator_traits<Iterator>::difference_type typename boost::iterator_traversal<
difference_type; Iterator
>::type
>::type traversal_category;
typedef typename
boost::detail::iterator_traits<Iterator>::difference_type
difference_type;
typedef typename boost::iterator_traversal<ConstIterator>::type typedef typename boost::detail::pure_traversal_tag<
const_traversal_category; typename boost::iterator_traversal<
typedef typename boost::detail::iterator_traits<ConstIterator>::difference_type ConstIterator
const_difference_type; >::type
>::type const_traversal_category;
typedef typename
boost::detail::iterator_traits<ConstIterator>::difference_type
const_difference_type;
void constraints() { void constraints()
BOOST_STATIC_ASSERT((boost::is_same< difference_type, {
const_difference_type>::value)); BOOST_STATIC_ASSERT(
BOOST_STATIC_ASSERT((boost::is_same< traversal_category, (boost::is_same< difference_type, const_difference_type>::value)
const_traversal_category>::value)); );
// ToDo check what the std really requires BOOST_STATIC_ASSERT(
(boost::is_same< traversal_category, const_traversal_category>::value)
// detail::Operations<traversal_category>::constraints(i, ci); );
ci = i; // ToDo check what the std really requires
} // detail::Operations<traversal_category>::constraints(i, ci);
Iterator i;
ConstIterator ci; ci = i;
};
}
Iterator i;
ConstIterator ci;
};
} // namespace boost_concepts } // namespace boost_concepts

40
include/boost/pointee.hpp Executable file
View File

@ -0,0 +1,40 @@
// Copyright David Abrahams 2004. Use, modification and distribution is
// subject to 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)
#ifndef POINTEE_DWA200415_HPP
# define POINTEE_DWA200415_HPP
// dereferenceable_traits provides access to the value_type and
// reference of a Dereferenceable type.
# include <boost/detail/is_incrementable.hpp>
# include <boost/iterator/iterator_traits.hpp>
# include <boost/type_traits/remove_cv.hpp>
# include <boost/mpl/apply_if.hpp>
namespace boost {
namespace detail
{
template <class P>
struct smart_ptr_value
{
typedef typename remove_cv<typename P::element_type>::type type;
};
}
template <class P>
struct pointee
{
typedef typename remove_cv<P>::type stripped;
typedef typename mpl::apply_if<
detail::is_incrementable<stripped>
, iterator_value<stripped>
, detail::smart_ptr_value<stripped>
>::type type;
};
} // namespace boost
#endif // POINTEE_DWA200415_HPP

View File

@ -18,7 +18,13 @@ test-suite iterator
# compilation problems. # compilation problems.
[ run is_convertible_fail.cpp ] [ run is_convertible_fail.cpp ]
[ run zip_iterator_test.cpp ] [ run zip_iterator_test.cpp
: : :
# stlport's debug mode generates long symbols which overwhelm
# vc6
<msvc-stlport><*><runtime-build>release
]
# These tests should work for just about everything. # These tests should work for just about everything.
[ compile is_lvalue_iterator.cpp ] [ compile is_lvalue_iterator.cpp ]

View File

@ -14,26 +14,44 @@
#include <boost/iterator/indirect_iterator.hpp> #include <boost/iterator/indirect_iterator.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include "static_assert_same.hpp"
#include <boost/type_traits/same_traits.hpp> #include <boost/type_traits/same_traits.hpp>
struct zow { }; struct zow { };
struct my_ptr { struct my_ptr {
typedef zow value_type; typedef zow const element_type;
typedef const zow& reference; // typedef const zow& reference;
typedef const zow* pointer; // typedef const zow* pointer;
typedef void difference_type; // typedef void difference_type;
typedef boost::no_traversal_tag iterator_category; // typedef boost::no_traversal_tag iterator_category;
}; };
BOOST_TT_BROKEN_COMPILER_SPEC(my_ptr)
BOOST_TT_BROKEN_COMPILER_SPEC(zow)
#ifndef BOOST_ITERATOR_REF_CONSTNESS_KILLS_WRITABILITY
# define STATIC_ASSERT_SAME_POINTER(P1, P2) \
STATIC_ASSERT_SAME( \
boost::remove_const<boost::remove_pointer<P1>::type>::type \
, boost::remove_const<boost::remove_pointer<P2>::type>::type \
)
#else
# define STATIC_ASSERT_SAME_POINTER(P1, P2) STATIC_ASSERT_SAME(P1,P2)
#endif
int main() int main()
{ {
{ {
typedef boost::indirect_iterator<int**> Iter; typedef boost::indirect_iterator<int**> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, int>::value)); STATIC_ASSERT_SAME(Iter::value_type, int);
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, int&>::value)); STATIC_ASSERT_SAME(Iter::reference, int&);
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, int*>::value)); STATIC_ASSERT_SAME_POINTER(Iter::pointer, int*);
BOOST_STATIC_ASSERT((boost::is_same<Iter::difference_type, std::ptrdiff_t>::value)); STATIC_ASSERT_SAME(Iter::difference_type, std::ptrdiff_t);
BOOST_STATIC_ASSERT((boost::is_convertible<Iter::iterator_category, BOOST_STATIC_ASSERT((boost::is_convertible<Iter::iterator_category,
std::random_access_iterator_tag>::value)); std::random_access_iterator_tag>::value));
@ -42,28 +60,34 @@ int main()
} }
{ {
typedef boost::indirect_iterator<int const**> Iter; typedef boost::indirect_iterator<int const**> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, int>::value)); STATIC_ASSERT_SAME(Iter::value_type, int);
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, const int&>::value)); STATIC_ASSERT_SAME(Iter::reference, const int&);
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, const int*>::value)); #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) // Borland drops const all over the place
STATIC_ASSERT_SAME_POINTER(Iter::pointer, const int*);
#endif
} }
{ {
typedef boost::indirect_iterator<int**, int> Iter; typedef boost::indirect_iterator<int**, int> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, int>::value)); STATIC_ASSERT_SAME(Iter::value_type, int);
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, int&>::value)); STATIC_ASSERT_SAME(Iter::reference, int&);
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, int*>::value)); STATIC_ASSERT_SAME_POINTER(Iter::pointer, int*);
} }
{ {
typedef boost::indirect_iterator<int**, const int> Iter; typedef boost::indirect_iterator<int**, const int> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, int>::value)); STATIC_ASSERT_SAME(Iter::value_type, int);
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, const int&>::value)); STATIC_ASSERT_SAME(Iter::reference, const int&);
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, const int*>::value)); #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) // Borland drops const all over the place
STATIC_ASSERT_SAME_POINTER(Iter::pointer, const int*);
#endif
} }
{ {
typedef boost::indirect_iterator<my_ptr*> Iter; typedef boost::indirect_iterator<my_ptr*> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, zow>::value)); STATIC_ASSERT_SAME(Iter::value_type, zow);
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, const zow&>::value)); #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) // Borland drops const all over the place
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, const zow*>::value)); STATIC_ASSERT_SAME(Iter::reference, const zow&);
BOOST_STATIC_ASSERT((boost::is_same<Iter::difference_type, std::ptrdiff_t>::value)); STATIC_ASSERT_SAME_POINTER(Iter::pointer, const zow*);
#endif
STATIC_ASSERT_SAME(Iter::difference_type, std::ptrdiff_t);
BOOST_STATIC_ASSERT((boost::is_convertible<Iter::iterator_category, BOOST_STATIC_ASSERT((boost::is_convertible<Iter::iterator_category,
std::random_access_iterator_tag>::value)); std::random_access_iterator_tag>::value));
@ -72,9 +96,10 @@ int main()
} }
{ {
typedef boost::indirect_iterator<char**, int, std::random_access_iterator_tag, long&, short> Iter; typedef boost::indirect_iterator<char**, int, std::random_access_iterator_tag, long&, short> Iter;
BOOST_STATIC_ASSERT((boost::is_same<Iter::value_type, int>::value)); STATIC_ASSERT_SAME(Iter::value_type, int);
BOOST_STATIC_ASSERT((boost::is_same<Iter::reference, long&>::value)); STATIC_ASSERT_SAME(Iter::reference, long&);
BOOST_STATIC_ASSERT((boost::is_same<Iter::pointer, int*>::value)); STATIC_ASSERT_SAME_POINTER(Iter::pointer, int*);
BOOST_STATIC_ASSERT((boost::is_same<Iter::difference_type, short>::value)); STATIC_ASSERT_SAME(Iter::difference_type, short);
} }
return 0;
} }

View File

@ -26,6 +26,8 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/mpl/aux_/has_xxx.hpp>
#include <boost/type_traits/broken_compiler_spec.hpp> #include <boost/type_traits/broken_compiler_spec.hpp>
#include <vector> #include <vector>
#include <stdlib.h> #include <stdlib.h>
@ -139,6 +141,10 @@ void more_indirect_iterator_tests()
assert(std::equal(db, de, store.begin())); assert(std::equal(db, de, store.begin()));
} }
// element_type detector; defaults to true so the test passes when
// has_xxx isn't implemented
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_element_type, element_type, true)
int int
main() main()
{ {
@ -146,6 +152,10 @@ main()
dummyT(3), dummyT(4), dummyT(5) }; dummyT(3), dummyT(4), dummyT(5) };
const int N = sizeof(array)/sizeof(dummyT); const int N = sizeof(array)/sizeof(dummyT);
# if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
boost::shared_ptr<dummyT> zz((dummyT*)0); // Why? I don't know, but it suppresses a bad instantiation.
# endif
typedef std::vector<boost::shared_ptr<dummyT> > shared_t; typedef std::vector<boost::shared_ptr<dummyT> > shared_t;
shared_t shared; shared_t shared;
@ -154,9 +164,8 @@ main()
typedef boost::indirect_iterator<shared_t::iterator> iter_t; typedef boost::indirect_iterator<shared_t::iterator> iter_t;
BOOST_STATIC_ASSERT( BOOST_STATIC_ASSERT(
boost::detail::has_element_type< has_element_type<
boost::shared_ptr<dummyT> boost::detail::iterator_traits<shared_t::iterator>::value_type
// std::iterator_traits<shared_t::iterator>::value_type
>::value >::value
); );

View File

@ -7,6 +7,7 @@
# define STATIC_ASSERT_SAME_DWA2003530_HPP # define STATIC_ASSERT_SAME_DWA2003530_HPP
# include <boost/type.hpp> # include <boost/type.hpp>
# include <boost/static_assert.hpp>
#ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template <class T, class U> template <class T, class U>
@ -32,4 +33,8 @@ struct static_assert_same
{}; {};
#endif #endif
#define STATIC_ASSERT_SAME( T1,T2 ) \
enum { BOOST_JOIN(boost_static_assert_enum_, __LINE__) \
= static_assert_same<T1,T2>::value }
#endif // STATIC_ASSERT_SAME_DWA2003530_HPP #endif // STATIC_ASSERT_SAME_DWA2003530_HPP

View File

@ -46,6 +46,7 @@
#include <vector> #include <vector>
#include <list> #include <list>
#include <set> #include <set>
#include <functional>
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
#include <boost/iterator/transform_iterator.hpp> #include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/is_readable_iterator.hpp> #include <boost/iterator/is_readable_iterator.hpp>