| 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) |
| 5 | * |
| 6 | * See http://www.boost.org/libs/poly_collection for library home page. |
| 7 | */ |
| 8 | |
| 9 | #ifndef BOOST_POLY_COLLECTION_DETAIL_STRIDE_ITERATOR_HPP |
| 10 | #define BOOST_POLY_COLLECTION_DETAIL_STRIDE_ITERATOR_HPP |
| 11 | |
| 12 | #if defined(_MSC_VER) |
| 13 | #pragma once |
| 14 | #endif |
| 15 | |
| 16 | #include <boost/detail/workaround.hpp> |
| 17 | #include <boost/iterator/iterator_facade.hpp> |
| 18 | #include <type_traits> |
| 19 | |
| 20 | namespace boost{ |
| 21 | |
| 22 | namespace poly_collection{ |
| 23 | |
| 24 | namespace detail{ |
| 25 | |
| 26 | /* random-access iterator to Value elements laid out stride *chars* apart */ |
| 27 | |
| 28 | template<typename Value> |
| 29 | class stride_iterator: |
| 30 | public boost::iterator_facade< |
| 31 | stride_iterator<Value>, |
| 32 | Value, |
| 33 | boost::random_access_traversal_tag |
| 34 | > |
| 35 | { |
| 36 | public: |
| 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; |
| 41 | |
| 42 | template< |
| 43 | typename NonConstValue, |
| 44 | typename std::enable_if< |
| 45 | std::is_same<Value,const NonConstValue>::value>::type* =nullptr |
| 46 | > |
| 47 | stride_iterator(const stride_iterator<NonConstValue>& x)noexcept: |
| 48 | p{x.p},stride_{x.stride_}{} |
| 49 | |
| 50 | template< |
| 51 | typename NonConstValue, |
| 52 | typename std::enable_if< |
| 53 | std::is_same<Value,const NonConstValue>::value>::type* =nullptr |
| 54 | > |
| 55 | stride_iterator& operator=(const stride_iterator<NonConstValue>& x)noexcept |
| 56 | { |
| 57 | p=x.p;stride_=x.stride_; |
| 58 | return *this; |
| 59 | } |
| 60 | |
| 61 | /* interoperability with [Derived]Value* */ |
| 62 | |
| 63 | stride_iterator& operator=(Value* p_)noexcept{p=p_;return *this;} |
| 64 | operator Value*()const noexcept{return p;} |
| 65 | |
| 66 | template< |
| 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) |
| 71 | >::type* =nullptr |
| 72 | > |
| 73 | explicit stride_iterator(DerivedValue* x)noexcept: |
| 74 | p{x},stride_{sizeof(DerivedValue)}{} |
| 75 | |
| 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 */ |
| 80 | |
| 81 | #define BOOST_POLY_COLLECTION_NO_SANITIZE |
| 82 | |
| 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" |
| 90 | #endif |
| 91 | #endif |
| 92 | |
| 93 | template< |
| 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) |
| 98 | >::type* =nullptr |
| 99 | > |
| 100 | #if defined(BOOST_POLY_COLLECTION_NO_SANITIZE) |
| 101 | __attribute__((no_sanitize("undefined" ))) |
| 102 | #endif |
| 103 | explicit operator DerivedValue*()const noexcept |
| 104 | {return static_cast<DerivedValue*>(p);} |
| 105 | |
| 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 |
| 111 | #endif |
| 112 | |
| 113 | #undef BOOST_POLY_COLLECTION_NO_SANITIZE |
| 114 | #endif |
| 115 | |
| 116 | std::size_t stride()const noexcept{return stride_;} |
| 117 | |
| 118 | private: |
| 119 | template<typename> |
| 120 | friend class stride_iterator; |
| 121 | |
| 122 | using char_pointer=typename std::conditional< |
| 123 | std::is_const<Value>::value, |
| 124 | const char*, |
| 125 | char* |
| 126 | >::type; |
| 127 | |
| 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);} |
| 132 | |
| 133 | friend class boost::iterator_core_access; |
| 134 | |
| 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(p: char_ptr(p)+stride_);} |
| 138 | void decrement()noexcept{p=value_ptr(p: char_ptr(p)-stride_);} |
| 139 | template<typename Integral> |
| 140 | void advance(Integral n)noexcept |
| 141 | {p=value_ptr(p: char_ptr(p)+n*(std::ptrdiff_t)stride_);} |
| 142 | std::ptrdiff_t distance_to(const stride_iterator& x)const noexcept |
| 143 | {return (char_ptr(p: x.p)-char_ptr(p))/(std::ptrdiff_t)stride_;} |
| 144 | |
| 145 | Value* p; |
| 146 | std::size_t stride_; |
| 147 | }; |
| 148 | |
| 149 | } /* namespace poly_collection::detail */ |
| 150 | |
| 151 | } /* namespace poly_collection */ |
| 152 | |
| 153 | } /* namespace boost */ |
| 154 | |
| 155 | #endif |
| 156 | |