iterator_range interop with variant.

This commit is contained in:
Neil Groves
2014-03-02 22:50:24 +00:00
parent 79d2a66831
commit 8b60f2c768
4 changed files with 144 additions and 92 deletions

View File

@ -30,6 +30,7 @@
#include <boost/range/functions.hpp> #include <boost/range/functions.hpp>
#include <boost/range/iterator.hpp> #include <boost/range/iterator.hpp>
#include <boost/range/difference_type.hpp> #include <boost/range/difference_type.hpp>
#include <boost/range/has_range_iterator.hpp>
#include <boost/range/algorithm/equal.hpp> #include <boost/range/algorithm/equal.hpp>
#include <boost/range/detail/safe_bool.hpp> #include <boost/range/detail/safe_bool.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
@ -380,6 +381,19 @@ public:
BOOST_DEDUCED_TYPENAME iterator_traversal<IteratorT>::type BOOST_DEDUCED_TYPENAME iterator_traversal<IteratorT>::type
> base_type; > base_type;
template<class Source>
struct is_compatible_range
: is_convertible<
BOOST_DEDUCED_TYPENAME mpl::eval_if<
has_range_iterator<Source>,
range_iterator<Source>,
mpl::identity<void>
>::type,
BOOST_DEDUCED_TYPENAME base_type::iterator
>
{
};
protected: protected:
typedef iterator_range_detail::iterator_range_impl<IteratorT> impl; typedef iterator_range_detail::iterator_range_impl<IteratorT> impl;
@ -397,13 +411,23 @@ public:
} }
template<class SinglePassRange> template<class SinglePassRange>
iterator_range(const SinglePassRange& r) iterator_range(
const SinglePassRange& r,
BOOST_DEDUCED_TYPENAME enable_if<
is_compatible_range<const SinglePassRange>
>::type* = 0
)
: base_type(impl::adl_begin(r), impl::adl_end(r)) : base_type(impl::adl_begin(r), impl::adl_end(r))
{ {
} }
template<class SinglePassRange> template<class SinglePassRange>
iterator_range(SinglePassRange& r) iterator_range(
SinglePassRange& r,
BOOST_DEDUCED_TYPENAME enable_if<
is_compatible_range<SinglePassRange>
>::type* = 0
)
: base_type(impl::adl_begin(r), impl::adl_end(r)) : base_type(impl::adl_begin(r), impl::adl_end(r))
{ {
} }

View File

@ -31,14 +31,18 @@
namespace boost namespace boost
{ {
template< class ForwardRange > template< class ForwardRange >
class sub_range : public iterator_range< BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type > class sub_range
: public iterator_range<
BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type
>
{ {
typedef BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type iterator_t; typedef BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type iterator_t;
typedef iterator_range< iterator_t > base; typedef iterator_range< iterator_t > base;
typedef BOOST_DEDUCED_TYPENAME base::impl impl; typedef BOOST_DEDUCED_TYPENAME base::impl impl;
public: public:
typedef BOOST_DEDUCED_TYPENAME range_value<ForwardRange>::type value_type; typedef BOOST_DEDUCED_TYPENAME range_value<ForwardRange>::type value_type;
typedef BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type iterator; typedef BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type iterator;
@ -46,17 +50,25 @@ namespace boost
typedef BOOST_DEDUCED_TYPENAME range_difference<ForwardRange>::type difference_type; typedef BOOST_DEDUCED_TYPENAME range_difference<ForwardRange>::type difference_type;
typedef BOOST_DEDUCED_TYPENAME range_size<ForwardRange>::type size_type; typedef BOOST_DEDUCED_TYPENAME range_size<ForwardRange>::type size_type;
typedef BOOST_DEDUCED_TYPENAME base::reference reference; typedef BOOST_DEDUCED_TYPENAME base::reference reference;
public: // for return value of front/back private:
typedef BOOST_DEDUCED_TYPENAME template<class Source>
boost::mpl::if_< boost::is_reference<reference>, struct is_compatible_range
const BOOST_DEDUCED_TYPENAME boost::remove_reference<reference>::type&, : is_convertible<
reference >::type const_reference; BOOST_DEDUCED_TYPENAME mpl::eval_if<
has_range_iterator<Source>,
range_iterator<Source>,
mpl::identity<void>
>::type,
iterator
>
{
};
public: public:
sub_range() : base() sub_range()
{ } { }
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500) ) #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500) )
sub_range( const sub_range& r ) sub_range( const sub_range& r )
: base( static_cast<const base&>( r ) ) : base( static_cast<const base&>( r ) )
@ -64,39 +76,57 @@ namespace boost
#endif #endif
template< class ForwardRange2 > template< class ForwardRange2 >
sub_range( ForwardRange2& r ) : sub_range(
ForwardRange2& r,
#if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 800 ) BOOST_DEDUCED_TYPENAME enable_if<
base( impl::adl_begin( r ), impl::adl_end( r ) ) is_compatible_range<ForwardRange2>
#else >::type* = 0
base( r ) ) :
#endif
{ }
template< class ForwardRange2 >
sub_range( const ForwardRange2& r ) :
#if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 800 ) #if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 800 )
base( impl::adl_begin( r ), impl::adl_end( r ) ) base( impl::adl_begin( r ), impl::adl_end( r ) )
#else #else
base( r ) base( r )
#endif #endif
{ }
template< class ForwardRange2 >
sub_range(
const ForwardRange2& r,
BOOST_DEDUCED_TYPENAME enable_if<
is_compatible_range<const ForwardRange2>
>::type* = 0
) :
#if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 800 )
base( impl::adl_begin( r ), impl::adl_end( r ) )
#else
base( r )
#endif
{ } { }
template< class Iter > template< class Iter >
sub_range( Iter first, Iter last ) : sub_range( Iter first, Iter last ) :
base( first, last ) base( first, last )
{ } { }
template< class ForwardRange2 > template<class ForwardRange2>
sub_range& operator=( ForwardRange2& r ) BOOST_DEDUCED_TYPENAME enable_if<
is_compatible_range<ForwardRange2>,
sub_range&
>::type
operator=(ForwardRange2& r)
{ {
base::operator=( r ); base::operator=( r );
return *this; return *this;
} }
template< class ForwardRange2 > template<class ForwardRange2>
sub_range& operator=( const ForwardRange2& r ) BOOST_DEDUCED_TYPENAME enable_if<
is_compatible_range<const ForwardRange2>,
sub_range&
>::type
operator=( const ForwardRange2& r )
{ {
base::operator=( r ); base::operator=( r );
return *this; return *this;
@ -107,71 +137,8 @@ namespace boost
base::operator=( static_cast<const base&>(r) ); base::operator=( static_cast<const base&>(r) );
return *this; return *this;
} }
public:
iterator begin() { return base::begin(); }
const_iterator begin() const { return base::begin(); }
iterator end() { return base::end(); }
const_iterator end() const { return base::end(); }
difference_type size() const { return base::size(); }
public: // convenience
reference front()
{
return base::front();
}
const_reference front() const
{
return base::front();
}
reference back()
{
return base::back();
}
const_reference back() const
{
return base::back();
}
reference operator[]( difference_type sz )
{
return base::operator[](sz);
}
const_reference operator[]( difference_type sz ) const
{
return base::operator[](sz);
}
}; };
template< class ForwardRange, class ForwardRange2 >
inline bool operator==( const sub_range<ForwardRange>& l,
const sub_range<ForwardRange2>& r )
{
return boost::equal( l, r );
}
template< class ForwardRange, class ForwardRange2 >
inline bool operator!=( const sub_range<ForwardRange>& l,
const sub_range<ForwardRange2>& r )
{
return !boost::equal( l, r );
}
template< class ForwardRange, class ForwardRange2 >
inline bool operator<( const sub_range<ForwardRange>& l,
const sub_range<ForwardRange2>& r )
{
return iterator_range_detail::less_than( l, r );
}
} // namespace 'boost' } // namespace 'boost'
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500)) #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))

View File

@ -150,6 +150,7 @@ test-suite range :
[ range-test istream_range ] [ range-test istream_range ]
[ range-test iterator_pair ] [ range-test iterator_pair ]
[ range-test iterator_range ] [ range-test iterator_range ]
[ range-test iterator_range_variant ]
# [ range-test mfc : <include>$(VC71_ROOT)/atlmfc/include ] # [ range-test mfc : <include>$(VC71_ROOT)/atlmfc/include ]
[ range-test join ] [ range-test join ]
[ range-test partial_workaround ] [ range-test partial_workaround ]

View File

@ -0,0 +1,60 @@
// Boost.Range library
//
// Copyright Neil Groves 2014. 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)
//
// For more information, see http://www.boost.org/libs/range/
//
#include <boost/detail/workaround.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/range/functions.hpp>
#include <boost/range/as_literal.hpp>
#include <boost/variant.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include <string>
namespace
{
enum E
{
e1, e2, e3
};
void test_variant_report()
{
typedef boost::mpl::vector<
E,
std::string,
boost::iterator_range<std::string::iterator>
>::type args;
typedef boost::make_variant_over<args>::type variant_t;
variant_t v;
std::string s;
v = boost::iterator_range<std::string::iterator>(s.begin(), s.end());
v = e2;
v = std::string();
// Rationale:
// This is cast to const char* to guard against ambiguity in the case
// where std::string::iterator it a char*
v = static_cast<const char*>("");
}
}
boost::unit_test::test_suite* init_unit_test_suite( int argc, char* argv[] )
{
boost::unit_test::test_suite* test =
BOOST_TEST_SUITE("iterator range and variant interoperability");
test->add(BOOST_TEST_CASE(&test_variant_report));
return test;
}