1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <iostream>
#include <stdexcept>
#include <iterator>
#include <type_traits>
namespace range_checked
{
template < typename BASE > struct bidirectional_iterator
{
using value_type = typename std::remove_reference< decltype( *std::declval<BASE>() ) >::type ;
using reference = decltype( *std::declval<BASE>() ) ;
// etc.
using iterator_category = std::bidirectional_iterator_tag ;
static_assert( std::is_convertible< typename std::iterator_traits<BASE>::iterator_category,
std::bidirectional_iterator_tag >::value,
"adaptee must be at least a bidirectional iterator" ) ;
bidirectional_iterator( BASE current, BASE begin, BASE end )
: current(current), begin(begin), end(end) {}
decltype(auto) operator* () { return *dereferencable_current() ; }
decltype(auto) operator-> () { return &**this ; }
bidirectional_iterator& operator++() { ++dereferencable_current() ; return *this ; }
bidirectional_iterator operator++(int) { auto temp(*this) ; ++*this ; return temp ; }
bidirectional_iterator& operator--() { --decrementable_current() ; return *this ; }
bidirectional_iterator operator--(int) { auto temp(*this) ; --*this ; return temp ; }
bool operator== ( const bidirectional_iterator& that ) const { return current == that.current ; }
bool operator!= ( const bidirectional_iterator& that ) const { return !(*this==that) ; }
private:
BASE current ;
BASE begin ;
BASE end ;
auto& dereferencable_current()
{
if( current == end ) throw std::out_of_range( "not dereferencable/incrementable" ) ;
return current ;
}
auto& decrementable_current()
{
if( current == begin ) throw std::out_of_range( "not decrementable" ) ;
return current ;
}
};
template < typename CNTR > auto begin( CNTR& cntr )
{
auto b = std::begin(cntr) ;
return bidirectional_iterator< decltype(b) > { b, b, std::end(cntr) } ;
}
template < typename CNTR > auto end( CNTR& cntr )
{
auto e = std::end(cntr) ;
return bidirectional_iterator< decltype(e) > { e, std::begin(cntr), e } ;
}
template < typename CNTR > auto cbegin( const CNTR& cntr )
{
auto b = std::begin(cntr) ;
return bidirectional_iterator< decltype(b) > { b, b, std::end(cntr) } ;
}
template < typename CNTR > auto cend( const CNTR& cntr )
{
auto e = std::end(cntr) ;
return bidirectional_iterator< decltype(e) > { e, std::begin(cntr), e } ;
}
template < typename CNTR > auto begin( const CNTR& cntr ) { return cbegin(cntr) ; }
template < typename CNTR > auto end( const CNTR& cntr ) { return cend(cntr) ; }
// likewise for rbegin, rend, crbegin, crend (use std::rbegin, std::rend)
}
int main()
{
int a[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
try { for( auto iter = range_checked::begin(a) ; ; ++iter ) std::cout << *iter << ' ' ; }
catch( const std::out_of_range& ) { std::cout << "reached end\n" ; }
try { for( auto iter = --range_checked::cend(a) ; ; --iter ) std::cout << *iter << ' ' ; }
catch( const std::out_of_range& ) { std::cout << "reached begin\n" ; }
}
|