forked from boostorg/range
iterator_range interop with variant.
This commit is contained in:
@ -30,6 +30,7 @@
|
||||
#include <boost/range/functions.hpp>
|
||||
#include <boost/range/iterator.hpp>
|
||||
#include <boost/range/difference_type.hpp>
|
||||
#include <boost/range/has_range_iterator.hpp>
|
||||
#include <boost/range/algorithm/equal.hpp>
|
||||
#include <boost/range/detail/safe_bool.hpp>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
@ -380,6 +381,19 @@ public:
|
||||
BOOST_DEDUCED_TYPENAME iterator_traversal<IteratorT>::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:
|
||||
typedef iterator_range_detail::iterator_range_impl<IteratorT> impl;
|
||||
|
||||
@ -397,13 +411,23 @@ public:
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
}
|
||||
|
@ -33,12 +33,16 @@ namespace boost
|
||||
{
|
||||
|
||||
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 iterator_range< iterator_t > base;
|
||||
|
||||
typedef BOOST_DEDUCED_TYPENAME base::impl impl;
|
||||
|
||||
public:
|
||||
typedef BOOST_DEDUCED_TYPENAME range_value<ForwardRange>::type value_type;
|
||||
typedef BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type iterator;
|
||||
@ -47,14 +51,22 @@ namespace boost
|
||||
typedef BOOST_DEDUCED_TYPENAME range_size<ForwardRange>::type size_type;
|
||||
typedef BOOST_DEDUCED_TYPENAME base::reference reference;
|
||||
|
||||
public: // for return value of front/back
|
||||
typedef BOOST_DEDUCED_TYPENAME
|
||||
boost::mpl::if_< boost::is_reference<reference>,
|
||||
const BOOST_DEDUCED_TYPENAME boost::remove_reference<reference>::type&,
|
||||
reference >::type const_reference;
|
||||
private:
|
||||
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,
|
||||
iterator
|
||||
>
|
||||
{
|
||||
};
|
||||
|
||||
public:
|
||||
sub_range() : base()
|
||||
sub_range()
|
||||
{ }
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500) )
|
||||
@ -64,7 +76,12 @@ namespace boost
|
||||
#endif
|
||||
|
||||
template< class ForwardRange2 >
|
||||
sub_range( ForwardRange2& r ) :
|
||||
sub_range(
|
||||
ForwardRange2& r,
|
||||
BOOST_DEDUCED_TYPENAME enable_if<
|
||||
is_compatible_range<ForwardRange2>
|
||||
>::type* = 0
|
||||
) :
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, <= 800 )
|
||||
base( impl::adl_begin( r ), impl::adl_end( r ) )
|
||||
@ -74,7 +91,12 @@ namespace boost
|
||||
{ }
|
||||
|
||||
template< class ForwardRange2 >
|
||||
sub_range( const ForwardRange2& r ) :
|
||||
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 ) )
|
||||
@ -89,14 +111,22 @@ namespace boost
|
||||
{ }
|
||||
|
||||
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 );
|
||||
return *this;
|
||||
}
|
||||
|
||||
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 );
|
||||
return *this;
|
||||
@ -107,71 +137,8 @@ namespace boost
|
||||
base::operator=( static_cast<const base&>(r) );
|
||||
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'
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
|
||||
|
@ -150,6 +150,7 @@ test-suite range :
|
||||
[ range-test istream_range ]
|
||||
[ range-test iterator_pair ]
|
||||
[ range-test iterator_range ]
|
||||
[ range-test iterator_range_variant ]
|
||||
# [ range-test mfc : <include>$(VC71_ROOT)/atlmfc/include ]
|
||||
[ range-test join ]
|
||||
[ range-test partial_workaround ]
|
||||
|
60
test/iterator_range_variant.cpp
Normal file
60
test/iterator_range_variant.cpp
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user