]>
Commit | Line | Data |
---|---|---|
11fdf7f2 | 1 | /* Copyright 2016-2017 Joaquin M Lopez Munoz. |
b32b8144 FG |
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/iterator/iterator_facade.hpp> | |
17 | #include <type_traits> | |
18 | ||
19 | namespace boost{ | |
20 | ||
21 | namespace poly_collection{ | |
22 | ||
23 | namespace detail{ | |
24 | ||
25 | /* random-access iterator to Value elements laid out stride *chars* apart */ | |
26 | ||
27 | template<typename Value> | |
28 | class stride_iterator: | |
29 | public boost::iterator_facade< | |
30 | stride_iterator<Value>, | |
31 | Value, | |
32 | boost::random_access_traversal_tag | |
33 | > | |
34 | { | |
35 | public: | |
36 | stride_iterator()=default; | |
37 | stride_iterator(Value* p,std::size_t stride)noexcept:p{p},stride_{stride}{} | |
38 | stride_iterator(const stride_iterator&)=default; | |
39 | stride_iterator& operator=(const stride_iterator&)=default; | |
40 | ||
41 | template< | |
42 | typename NonConstValue, | |
43 | typename std::enable_if< | |
44 | std::is_same<Value,const NonConstValue>::value>::type* =nullptr | |
45 | > | |
46 | stride_iterator(const stride_iterator<NonConstValue>& x)noexcept: | |
47 | p{x.p},stride_{x.stride_}{} | |
48 | ||
49 | template< | |
50 | typename NonConstValue, | |
51 | typename std::enable_if< | |
52 | std::is_same<Value,const NonConstValue>::value>::type* =nullptr | |
53 | > | |
54 | stride_iterator& operator=(const stride_iterator<NonConstValue>& x)noexcept | |
55 | { | |
56 | p=x.p;stride_=x.stride_; | |
57 | return *this; | |
58 | } | |
59 | ||
60 | /* interoperability with [Derived]Value* */ | |
61 | ||
62 | stride_iterator& operator=(Value* p_)noexcept{p=p_;return *this;} | |
63 | operator Value*()const noexcept{return p;} | |
64 | ||
65 | template< | |
66 | typename DerivedValue, | |
67 | typename std::enable_if< | |
68 | std::is_base_of<Value,DerivedValue>::value&& | |
69 | (std::is_const<Value>::value||!std::is_const<DerivedValue>::value) | |
70 | >::type* =nullptr | |
71 | > | |
72 | explicit stride_iterator(DerivedValue* x)noexcept: | |
73 | p{x},stride_{sizeof(DerivedValue)}{} | |
74 | ||
75 | template< | |
76 | typename DerivedValue, | |
77 | typename std::enable_if< | |
78 | std::is_base_of<Value,DerivedValue>::value&& | |
79 | (!std::is_const<Value>::value||std::is_const<DerivedValue>::value) | |
80 | >::type* =nullptr | |
81 | > | |
82 | explicit operator DerivedValue*()const noexcept | |
83 | {return static_cast<DerivedValue*>(p);} | |
84 | ||
85 | std::size_t stride()const noexcept{return stride_;} | |
86 | ||
87 | private: | |
88 | template<typename> | |
89 | friend class stride_iterator; | |
90 | ||
91 | using char_pointer=typename std::conditional< | |
92 | std::is_const<Value>::value, | |
93 | const char*, | |
94 | char* | |
95 | >::type; | |
96 | ||
97 | static char_pointer char_ptr(Value* p)noexcept | |
98 | {return reinterpret_cast<char_pointer>(p);} | |
99 | static Value* value_ptr(char_pointer p)noexcept | |
100 | {return reinterpret_cast<Value*>(p);} | |
101 | ||
102 | friend class boost::iterator_core_access; | |
103 | ||
104 | Value& dereference()const noexcept{return *p;} | |
105 | bool equal(const stride_iterator& x)const noexcept{return p==x.p;} | |
106 | void increment()noexcept{p=value_ptr(char_ptr(p)+stride_);} | |
107 | void decrement()noexcept{p=value_ptr(char_ptr(p)-stride_);} | |
108 | template<typename Integral> | |
11fdf7f2 TL |
109 | void advance(Integral n)noexcept |
110 | {p=value_ptr(char_ptr(p)+n*(std::ptrdiff_t)stride_);} | |
b32b8144 | 111 | std::ptrdiff_t distance_to(const stride_iterator& x)const noexcept |
11fdf7f2 | 112 | {return (char_ptr(x.p)-char_ptr(p))/(std::ptrdiff_t)stride_;} |
b32b8144 FG |
113 | |
114 | Value* p; | |
115 | std::size_t stride_; | |
116 | }; | |
117 | ||
118 | } /* namespace poly_collection::detail */ | |
119 | ||
120 | } /* namespace poly_collection */ | |
121 | ||
122 | } /* namespace boost */ | |
123 | ||
124 | #endif |