1 // Copyright (c) 2009-2016 Vladimir Batov.
2 // Use, modification and distribution are subject to the Boost Software License,
3 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
5 #ifndef BOOST_CONVERT_DETAIL_RANGE_HPP
6 #define BOOST_CONVERT_DETAIL_RANGE_HPP
8 #include <boost/convert/detail/has_member.hpp>
9 #include <boost/convert/detail/char.hpp>
10 #include <boost/utility/enable_if.hpp>
11 #include <boost/range/iterator.hpp>
13 namespace boost { namespace cnv
17 template<typename T, bool is_class> struct is_range : mpl::false_ {};
19 template<typename T> struct is_range<T, /*is_class=*/true>
21 BOOST_DECLARE_HAS_MEMBER(has_begin, begin);
22 BOOST_DECLARE_HAS_MEMBER( has_end, end);
24 static bool const value = has_begin<T>::value && has_end<T>::value;
27 template<typename T> struct is_range : detail::is_range<typename remove_const<T>::type, boost::is_class<T>::value> {};
28 template<typename T, typename enable =void> struct range;
29 template<typename T, typename enable =void> struct iterator;
32 struct iterator<T, typename enable_if<is_range<T> >::type>
34 typedef typename boost::range_iterator<T>::type type;
35 typedef typename boost::range_iterator<T const>::type const_type;
36 typedef typename boost::iterator_value<type>::type value_type;
39 struct iterator<T*, void>
41 typedef typename remove_const<T>::type value_type;
43 typedef value_type const* const_type;
48 typedef typename cnv::iterator<T>::value_type value_type;
49 typedef typename cnv::iterator<T>::type iterator;
50 typedef typename cnv::iterator<T>::const_type const_iterator;
51 typedef const_iterator sentry_type;
53 iterator begin () { return begin_; }
54 const_iterator begin () const { return begin_; }
55 void operator++ () { ++begin_; }
56 // void operator-- () { --end_; }
60 range_base (iterator b, iterator e) : begin_(b), end_(e) {}
63 iterator mutable end_;
67 struct range<T, typename enable_if<is_range<T> >::type> : public range_base<T>
69 typedef range this_type;
70 typedef range_base<T> base_type;
71 typedef typename base_type::iterator iterator;
72 typedef typename base_type::const_iterator const_iterator;
73 typedef const_iterator sentry_type;
75 range (T& r) : base_type(r.begin(), r.end()) {}
77 iterator end () { return base_type::end_; }
78 const_iterator end () const { return base_type::end_; }
79 sentry_type sentry () const { return base_type::end_; }
80 bool empty () const { return base_type::begin_ == base_type::end_; }
84 struct range<T*, typename enable_if<cnv::is_char<T> >::type> : public range_base<T*>
86 typedef range this_type;
87 typedef range_base<T*> base_type;
88 typedef typename remove_const<T>::type value_type;
90 typedef value_type const* const_iterator;
94 friend bool operator!=(iterator it, sentry_type) { return !!*it; }
97 range (iterator b, iterator e =0) : base_type(b, e) {}
99 iterator end () { return base_type::end_ ? base_type::end_ : (base_type::end_ = base_type::begin_ + size()); }
100 const_iterator end () const { return base_type::end_ ? base_type::end_ : (base_type::end_ = base_type::begin_ + size()); }
101 sentry_type sentry () const { return sentry_type(); }
102 std::size_t size () const { return std::char_traits<value_type>::length(base_type::begin_); }
103 bool empty () const { return !*base_type::begin_; }
106 struct range<T* const, void> : public range<T*>
108 range (T* b, T* e =0) : range<T*>(b, e) {}
110 template <typename T, std::size_t N>
111 struct range<T [N], void> : public range<T*>
113 range (T* b, T* e =0) : range<T*>(b, e) {}
117 #endif // BOOST_CONVERT_DETAIL_RANGE_HPP