mirror of
https://github.com/boostorg/range.git
synced 2025-07-29 12:27:42 +02:00
More cleanups
[SVN r31860]
This commit is contained in:
200
doc/example.cpp
200
doc/example.cpp
@ -3,106 +3,106 @@
|
||||
|
||||
namespace Foo
|
||||
{
|
||||
//
|
||||
// Our sample UDT. A 'Pair'
|
||||
// will work as a range when the stored
|
||||
// elements are iterators.
|
||||
//
|
||||
template< class T >
|
||||
struct Pair
|
||||
{
|
||||
T first, last;
|
||||
};
|
||||
//
|
||||
// Our sample UDT. A 'Pair'
|
||||
// will work as a range when the stored
|
||||
// elements are iterators.
|
||||
//
|
||||
template< class T >
|
||||
struct Pair
|
||||
{
|
||||
T first, last;
|
||||
};
|
||||
|
||||
} // namespace 'Foo'
|
||||
|
||||
namespace boost
|
||||
{
|
||||
//
|
||||
// Specialize metafunctions. We must include the range.hpp header.
|
||||
// We must open the 'boost' namespace.
|
||||
//
|
||||
/*
|
||||
template< class T >
|
||||
struct range_value< Foo::Pair<T> >
|
||||
{
|
||||
typedef typename std::iterator_traits<T>::value_type type;
|
||||
};
|
||||
*/
|
||||
//
|
||||
// Specialize metafunctions. We must include the range.hpp header.
|
||||
// We must open the 'boost' namespace.
|
||||
//
|
||||
/*
|
||||
template< class T >
|
||||
struct range_value< Foo::Pair<T> >
|
||||
{
|
||||
typedef typename std::iterator_traits<T>::value_type type;
|
||||
};
|
||||
*/
|
||||
|
||||
template< class T >
|
||||
struct range_iterator< Foo::Pair<T> >
|
||||
{
|
||||
typedef T type;
|
||||
};
|
||||
template< class T >
|
||||
struct range_iterator< Foo::Pair<T> >
|
||||
{
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template< class T >
|
||||
struct range_const_iterator< Foo::Pair<T> >
|
||||
{
|
||||
//
|
||||
// Remark: this is defined similar to 'range_iterator'
|
||||
// because the 'Pair' type does not distinguish
|
||||
// between an iterator and a const_iterator.
|
||||
//
|
||||
typedef T type;
|
||||
};
|
||||
template< class T >
|
||||
struct range_const_iterator< Foo::Pair<T> >
|
||||
{
|
||||
//
|
||||
// Remark: this is defined similar to 'range_iterator'
|
||||
// because the 'Pair' type does not distinguish
|
||||
// between an iterator and a const_iterator.
|
||||
//
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
/*
|
||||
/*
|
||||
template< class T >
|
||||
struct range_difference< Foo::Pair<T> >
|
||||
{
|
||||
typedef typename std::iterator_traits<T>::difference_type type;
|
||||
};
|
||||
*/
|
||||
struct range_difference< Foo::Pair<T> >
|
||||
{
|
||||
typedef typename std::iterator_traits<T>::difference_type type;
|
||||
};
|
||||
*/
|
||||
|
||||
template< class T >
|
||||
template< class T >
|
||||
struct range_size< Foo::Pair<T> >
|
||||
{
|
||||
int static_assertion[ sizeof( std::size_t ) >=
|
||||
sizeof( typename range_difference< Foo::Pair<T> >::type ) ];
|
||||
typedef std::size_t type;
|
||||
};
|
||||
{
|
||||
int static_assertion[ sizeof( std::size_t ) >=
|
||||
sizeof( typename range_difference< Foo::Pair<T> >::type ) ];
|
||||
typedef std::size_t type;
|
||||
};
|
||||
|
||||
} // namespace 'boost'
|
||||
|
||||
namespace Foo
|
||||
{
|
||||
//
|
||||
// The required functions. These should be defined in
|
||||
// the same namespace as 'Pair', in this case
|
||||
// in namespace 'Foo'.
|
||||
//
|
||||
//
|
||||
// The required functions. These should be defined in
|
||||
// the same namespace as 'Pair', in this case
|
||||
// in namespace 'Foo'.
|
||||
//
|
||||
|
||||
template< class T >
|
||||
inline T boost_range_begin( Pair<T>& x )
|
||||
{
|
||||
return x.first;
|
||||
}
|
||||
template< class T >
|
||||
inline T boost_range_begin( Pair<T>& x )
|
||||
{
|
||||
return x.first;
|
||||
}
|
||||
|
||||
template< class T >
|
||||
inline T boost_range_begin( const Pair<T>& x )
|
||||
{
|
||||
return x.first;
|
||||
}
|
||||
inline T boost_range_begin( const Pair<T>& x )
|
||||
{
|
||||
return x.first;
|
||||
}
|
||||
|
||||
template< class T >
|
||||
template< class T >
|
||||
inline T boost_range_end( Pair<T>& x )
|
||||
{
|
||||
return x.last;
|
||||
}
|
||||
{
|
||||
return x.last;
|
||||
}
|
||||
|
||||
template< class T >
|
||||
template< class T >
|
||||
inline T boost_range_end( const Pair<T>& x )
|
||||
{
|
||||
return x.last;
|
||||
}
|
||||
{
|
||||
return x.last;
|
||||
}
|
||||
|
||||
template< class T >
|
||||
inline typename boost::range_size< Pair<T> >::type
|
||||
boost_range_size( const Pair<T>& x )
|
||||
{
|
||||
return std::distance(x.first,x.last);
|
||||
}
|
||||
template< class T >
|
||||
inline typename boost::range_size< Pair<T> >::type
|
||||
boost_range_size( const Pair<T>& x )
|
||||
{
|
||||
return std::distance(x.first,x.last);
|
||||
}
|
||||
|
||||
} // namespace 'Foo'
|
||||
|
||||
@ -110,32 +110,32 @@ namespace Foo
|
||||
|
||||
int main()
|
||||
{
|
||||
typedef std::vector<int>::iterator iter;
|
||||
std::vector<int> vec;
|
||||
vec.push_back( 42 );
|
||||
Foo::Pair<iter> pair = { vec.begin(), vec.end() };
|
||||
const Foo::Pair<iter>& cpair = pair;
|
||||
//
|
||||
// Notice that we call 'begin' etc with qualification.
|
||||
//
|
||||
iter i = boost::begin( pair );
|
||||
iter e = boost::end( pair );
|
||||
i = boost::begin( cpair );
|
||||
e = boost::end( cpair );
|
||||
boost::range_size< Foo::Pair<iter> >::type s = boost::size( pair );
|
||||
s = boost::size( cpair );
|
||||
boost::range_const_reverse_iterator< Foo::Pair<iter> >::type
|
||||
ri = boost::rbegin( cpair ),
|
||||
re = boost::rend( cpair );
|
||||
typedef std::vector<int>::iterator iter;
|
||||
std::vector<int> vec;
|
||||
vec.push_back( 42 );
|
||||
Foo::Pair<iter> pair = { vec.begin(), vec.end() };
|
||||
const Foo::Pair<iter>& cpair = pair;
|
||||
//
|
||||
// Notice that we call 'begin' etc with qualification.
|
||||
//
|
||||
iter i = boost::begin( pair );
|
||||
iter e = boost::end( pair );
|
||||
i = boost::begin( cpair );
|
||||
e = boost::end( cpair );
|
||||
boost::range_size< Foo::Pair<iter> >::type s = boost::size( pair );
|
||||
s = boost::size( cpair );
|
||||
boost::range_const_reverse_iterator< Foo::Pair<iter> >::type
|
||||
ri = boost::rbegin( cpair ),
|
||||
re = boost::rend( cpair );
|
||||
|
||||
//
|
||||
// Test metafunctions
|
||||
//
|
||||
//
|
||||
// Test metafunctions
|
||||
//
|
||||
|
||||
boost::range_value< Foo::Pair<iter> >::type
|
||||
v = *boost::begin(pair);
|
||||
boost::range_value< Foo::Pair<iter> >::type
|
||||
v = *boost::begin(pair);
|
||||
|
||||
boost::range_difference< Foo::Pair<iter> >::type
|
||||
d = boost::end(pair) - boost::begin(pair);
|
||||
boost::range_difference< Foo::Pair<iter> >::type
|
||||
d = boost::end(pair) - boost::begin(pair);
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ namespace range_detail
|
||||
|
||||
template< typename C >
|
||||
inline BOOST_DEDUCED_TYPENAME range_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
typename remove_const<C>::type >::type
|
||||
boost_range_begin( C& c )
|
||||
{
|
||||
return c.begin();
|
||||
@ -143,7 +143,7 @@ namespace range_detail
|
||||
|
||||
template< class T >
|
||||
inline BOOST_DEDUCED_TYPENAME range_iterator<
|
||||
typename remove_const<T>::type >::type begin( T& r )
|
||||
typename remove_const<T>::type >::type begin( T& r )
|
||||
{
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) && \
|
||||
!BOOST_WORKAROUND(__GNUC__, < 3) \
|
||||
|
@ -21,13 +21,13 @@
|
||||
|
||||
namespace boost
|
||||
{
|
||||
template< class T >
|
||||
struct range_difference
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME iterator_difference<
|
||||
BOOST_DEDUCED_TYPENAME range_const_iterator<T>::type >::type
|
||||
type;
|
||||
};
|
||||
template< class T >
|
||||
struct range_difference
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME iterator_difference<
|
||||
BOOST_DEDUCED_TYPENAME range_const_iterator<T>::type >::type
|
||||
type;
|
||||
};
|
||||
}
|
||||
|
||||
//#ifdef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
|
||||
|
@ -48,8 +48,8 @@ namespace range_detail
|
||||
}
|
||||
|
||||
template< typename C >
|
||||
inline BOOST_DEDUCED_TYPENAME range_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
inline BOOST_DEDUCED_TYPENAME range_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
boost_range_end( C& c )
|
||||
{
|
||||
return c.end();
|
||||
@ -142,7 +142,7 @@ namespace range_detail
|
||||
|
||||
template< class T >
|
||||
inline BOOST_DEDUCED_TYPENAME range_iterator<
|
||||
typename remove_const<T>::type >::type end( T& r )
|
||||
typename remove_const<T>::type >::type end( T& r )
|
||||
{
|
||||
#if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) && \
|
||||
!BOOST_WORKAROUND(__GNUC__, < 3) \
|
||||
|
@ -36,11 +36,11 @@ rbegin( C& c )
|
||||
|
||||
template< class C >
|
||||
inline BOOST_DEDUCED_TYPENAME range_reverse_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
typename remove_const<C>::type >::type
|
||||
rbegin( C& c )
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME range_reverse_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
typename remove_const<C>::type >::type
|
||||
iter_type;
|
||||
return iter_type( end( c ) );
|
||||
}
|
||||
|
@ -36,11 +36,11 @@ rend( C& c )
|
||||
|
||||
template< class C >
|
||||
inline BOOST_DEDUCED_TYPENAME range_reverse_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
typename remove_const<C>::type >::type
|
||||
rend( C& c )
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME range_reverse_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
typedef BOOST_DEDUCED_TYPENAME range_reverse_iterator<
|
||||
typename remove_const<C>::type >::type
|
||||
iter_type;
|
||||
return iter_type( begin( c ) );
|
||||
}
|
||||
|
@ -21,47 +21,47 @@
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace range_detail
|
||||
{
|
||||
template< class T >
|
||||
struct add_unsigned;
|
||||
namespace range_detail
|
||||
{
|
||||
template< class T >
|
||||
struct add_unsigned;
|
||||
|
||||
template<>
|
||||
struct add_unsigned<short>
|
||||
{
|
||||
typedef unsigned short type;
|
||||
};
|
||||
template<>
|
||||
struct add_unsigned<short>
|
||||
{
|
||||
typedef unsigned short type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct add_unsigned<int>
|
||||
{
|
||||
typedef unsigned int type;
|
||||
};
|
||||
template<>
|
||||
struct add_unsigned<int>
|
||||
{
|
||||
typedef unsigned int type;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct add_unsigned<long>
|
||||
{
|
||||
typedef unsigned long type;
|
||||
};
|
||||
template<>
|
||||
struct add_unsigned<long>
|
||||
{
|
||||
typedef unsigned long type;
|
||||
};
|
||||
|
||||
#ifdef BOOST_HAS_LONG_LONG
|
||||
|
||||
template<>
|
||||
struct add_unsigned<long long>
|
||||
{
|
||||
typedef unsigned long long type;
|
||||
};
|
||||
template<>
|
||||
struct add_unsigned<long long>
|
||||
{
|
||||
typedef unsigned long long type;
|
||||
};
|
||||
#endif
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
template< class T >
|
||||
struct range_size
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME range_detail::add_unsigned<
|
||||
BOOST_DEDUCED_TYPENAME range_difference<T>::type >::type
|
||||
type;
|
||||
};
|
||||
template< class T >
|
||||
struct range_size
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME range_detail::add_unsigned<
|
||||
BOOST_DEDUCED_TYPENAME range_difference<T>::type >::type
|
||||
type;
|
||||
};
|
||||
}
|
||||
*/
|
||||
|
||||
|
@ -26,13 +26,13 @@
|
||||
|
||||
namespace boost
|
||||
{
|
||||
template< class T >
|
||||
template< class T >
|
||||
struct range_value
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME iterator_value<
|
||||
BOOST_DEDUCED_TYPENAME range_iterator<T>::type >::type
|
||||
type;
|
||||
};
|
||||
{
|
||||
typedef BOOST_DEDUCED_TYPENAME iterator_value<
|
||||
BOOST_DEDUCED_TYPENAME range_iterator<T>::type >::type
|
||||
type;
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -27,72 +27,72 @@
|
||||
template< class Rng >
|
||||
typename boost::range_result_iterator<Rng>::type foo_algo( Rng& r )
|
||||
{
|
||||
//
|
||||
// This will only compile for Rng = UDT if the qualified calls
|
||||
// find boost_range_XXX via ADL.
|
||||
//
|
||||
return boost::size(r) == 0u ? boost::begin(r) : boost::end(r);
|
||||
//
|
||||
// This will only compile for Rng = UDT if the qualified calls
|
||||
// find boost_range_XXX via ADL.
|
||||
//
|
||||
return boost::size(r) == 0u ? boost::begin(r) : boost::end(r);
|
||||
}
|
||||
|
||||
namespace Foo
|
||||
{
|
||||
//
|
||||
// Our sample UDT
|
||||
//
|
||||
struct X
|
||||
{
|
||||
typedef std::vector<int> data_t;
|
||||
typedef data_t::iterator iterator;
|
||||
typedef data_t::const_iterator const_iterator;
|
||||
typedef data_t::size_type size_type;
|
||||
//
|
||||
// Our sample UDT
|
||||
//
|
||||
struct X
|
||||
{
|
||||
typedef std::vector<int> data_t;
|
||||
typedef data_t::iterator iterator;
|
||||
typedef data_t::const_iterator const_iterator;
|
||||
typedef data_t::size_type size_type;
|
||||
|
||||
data_t vec;
|
||||
data_t vec;
|
||||
|
||||
void push_back( int i )
|
||||
{ vec.push_back(i); }
|
||||
};
|
||||
void push_back( int i )
|
||||
{ vec.push_back(i); }
|
||||
};
|
||||
|
||||
//
|
||||
// The required functions. No type-traits need
|
||||
// to be defined because X defines the proper set of
|
||||
// nested types.
|
||||
//
|
||||
inline X::iterator boost_range_begin( X& x )
|
||||
{
|
||||
return x.vec.begin();
|
||||
}
|
||||
//
|
||||
// The required functions. No type-traits need
|
||||
// to be defined because X defines the proper set of
|
||||
// nested types.
|
||||
//
|
||||
inline X::iterator boost_range_begin( X& x )
|
||||
{
|
||||
return x.vec.begin();
|
||||
}
|
||||
|
||||
|
||||
inline X::const_iterator boost_range_begin( const X& x )
|
||||
{
|
||||
return x.vec.begin();
|
||||
}
|
||||
inline X::const_iterator boost_range_begin( const X& x )
|
||||
{
|
||||
return x.vec.begin();
|
||||
}
|
||||
|
||||
|
||||
inline X::iterator boost_range_end( X& x )
|
||||
{
|
||||
return x.vec.end();
|
||||
}
|
||||
{
|
||||
return x.vec.end();
|
||||
}
|
||||
|
||||
inline X::const_iterator boost_range_end( const X& x )
|
||||
{
|
||||
return x.vec.end();
|
||||
}
|
||||
inline X::const_iterator boost_range_end( const X& x )
|
||||
{
|
||||
return x.vec.end();
|
||||
}
|
||||
|
||||
inline X::size_type boost_range_size( const X& x )
|
||||
{
|
||||
return x.vec.size();
|
||||
}
|
||||
inline X::size_type boost_range_size( const X& x )
|
||||
{
|
||||
return x.vec.size();
|
||||
}
|
||||
}
|
||||
|
||||
void check_extension()
|
||||
{
|
||||
Foo::X x;
|
||||
x.push_back(3);
|
||||
const Foo::X x2;
|
||||
Foo::X x;
|
||||
x.push_back(3);
|
||||
const Foo::X x2;
|
||||
|
||||
foo_algo( x );
|
||||
foo_algo( x2 );
|
||||
foo_algo( x );
|
||||
foo_algo( x2 );
|
||||
}
|
||||
|
||||
using boost::unit_test::test_suite;
|
||||
|
Reference in New Issue
Block a user