1 /* Copyright 2016-2019 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See http://www.boost.org/libs/poly_collection for library home page.
9 #ifndef BOOST_POLY_COLLECTION_DETAIL_STRIDE_ITERATOR_HPP
10 #define BOOST_POLY_COLLECTION_DETAIL_STRIDE_ITERATOR_HPP
16 #include <boost/detail/workaround.hpp>
17 #include <boost/iterator/iterator_facade.hpp>
18 #include <type_traits>
22 namespace poly_collection{
26 /* random-access iterator to Value elements laid out stride *chars* apart */
28 template<typename Value>
29 class stride_iterator:
30 public boost::iterator_facade<
31 stride_iterator<Value>,
33 boost::random_access_traversal_tag
37 stride_iterator()=default;
38 stride_iterator(Value* p,std::size_t stride)noexcept:p{p},stride_{stride}{}
39 stride_iterator(const stride_iterator&)=default;
40 stride_iterator& operator=(const stride_iterator&)=default;
43 typename NonConstValue,
44 typename std::enable_if<
45 std::is_same<Value,const NonConstValue>::value>::type* =nullptr
47 stride_iterator(const stride_iterator<NonConstValue>& x)noexcept:
48 p{x.p},stride_{x.stride_}{}
51 typename NonConstValue,
52 typename std::enable_if<
53 std::is_same<Value,const NonConstValue>::value>::type* =nullptr
55 stride_iterator& operator=(const stride_iterator<NonConstValue>& x)noexcept
57 p=x.p;stride_=x.stride_;
61 /* interoperability with [Derived]Value* */
63 stride_iterator& operator=(Value* p_)noexcept{p=p_;return *this;}
64 operator Value*()const noexcept{return p;}
67 typename DerivedValue,
68 typename std::enable_if<
69 std::is_base_of<Value,DerivedValue>::value&&
70 (std::is_const<Value>::value||!std::is_const<DerivedValue>::value)
73 explicit stride_iterator(DerivedValue* x)noexcept:
74 p{x},stride_{sizeof(DerivedValue)}{}
76 #if BOOST_WORKAROUND(BOOST_GCC_VERSION,>=40900)||\
77 BOOST_WORKAROUND(BOOST_CLANG,>=1)&&\
78 (__clang_major__>3 || __clang_major__==3 && __clang_minor__ >= 8)
79 /* https://github.com/boostorg/poly_collection/issues/15 */
81 #define BOOST_POLY_COLLECTION_NO_SANITIZE
83 /* UBSan seems not to be supported in some environments */
84 #if defined(BOOST_GCC_VERSION)
85 #pragma GCC diagnostic push
86 #pragma GCC diagnostic ignored "-Wattributes"
87 #elif defined(BOOST_CLANG)
88 #pragma clang diagnostic push
89 #pragma clang diagnostic ignored "-Wattributes"
94 typename DerivedValue,
95 typename std::enable_if<
96 std::is_base_of<Value,DerivedValue>::value&&
97 (!std::is_const<Value>::value||std::is_const<DerivedValue>::value)
100 #if defined(BOOST_POLY_COLLECTION_NO_SANITIZE)
101 __attribute__((no_sanitize("undefined")))
103 explicit operator DerivedValue*()const noexcept
104 {return static_cast<DerivedValue*>(p);}
106 #if defined(BOOST_POLY_COLLECTION_NO_SANITIZE)
107 #if defined(BOOST_GCC_VERSION)
108 #pragma GCC diagnostic pop
109 #elif defined(BOOST_CLANG)
110 #pragma clang diagnostic pop
113 #undef BOOST_POLY_COLLECTION_NO_SANITIZE
116 std::size_t stride()const noexcept{return stride_;}
120 friend class stride_iterator;
122 using char_pointer=typename std::conditional<
123 std::is_const<Value>::value,
128 static char_pointer char_ptr(Value* p)noexcept
129 {return reinterpret_cast<char_pointer>(p);}
130 static Value* value_ptr(char_pointer p)noexcept
131 {return reinterpret_cast<Value*>(p);}
133 friend class boost::iterator_core_access;
135 Value& dereference()const noexcept{return *p;}
136 bool equal(const stride_iterator& x)const noexcept{return p==x.p;}
137 void increment()noexcept{p=value_ptr(char_ptr(p)+stride_);}
138 void decrement()noexcept{p=value_ptr(char_ptr(p)-stride_);}
139 template<typename Integral>
140 void advance(Integral n)noexcept
141 {p=value_ptr(char_ptr(p)+n*(std::ptrdiff_t)stride_);}
142 std::ptrdiff_t distance_to(const stride_iterator& x)const noexcept
143 {return (char_ptr(x.p)-char_ptr(p))/(std::ptrdiff_t)stride_;}
149 } /* namespace poly_collection::detail */
151 } /* namespace poly_collection */
153 } /* namespace boost */