y3c-stl 0.3.3
Friendly C++ STL wrapper with automatic stacktrace
Loading...
Searching...
No Matches
iterator.h
Go to the documentation of this file.
1#pragma once
2#include "y3c/terminate.h"
3#include "y3c/life.h"
4#include "y3c/wrap.h"
5#include <memory>
6
7namespace y3c {
8namespace internal {
9template <typename element_type>
11
12template <typename element_type>
13element_type *unwrap(const contiguous_iterator<element_type> &wrapper) noexcept;
14
25template <typename element_type>
27 element_type *ptr_;
29 std::shared_ptr<internal::life_validator> validator_;
30 internal::life life_;
31
32 const std::string *type_name_;
33 const std::string &type_name() const {
34 // return internal::get_type_name<contiguous_iterator>();
35 return *type_name_;
36 }
37
38 element_type *assert_iter(const std::string &func,
39 internal::skip_trace_tag = {}) const {
40 return observer_.assert_iter(*this, func);
41 }
42 element_type *
43 assert_iter_including_end(const std::string &func,
44 internal::skip_trace_tag = {}) const {
45 return observer_.assert_iter_including_end(*this, func);
46 }
47 void update_iter(const std::string &func,
48 internal::skip_trace_tag = {}) const {
49 validator_->ptr_ = this->ptr_;
50 if (!validator_->valid_) {
51 // assert_iterではdeletedの場合deletedメッセージが優先されるが、
52 // ここではdeletedの場合にもinvalid iterのメッセージで止まる。
53 // あとコンストラクタで新しいvalidatorを作った後ここに来るというルートは若干無駄があるという問題もある
55 }
56 if (ptr_ > observer_.end()) {
58 }
59 if (ptr_ < observer_.begin()) {
61 }
62 }
63
64 public:
66 const std::string *type_name) noexcept
67 : ptr_(ptr), observer_(observer),
68 validator_(observer.push_validator(
69 std::make_shared<life_validator>(ptr /* always valid */))),
70 life_(this), type_name_(type_name) {}
72 const std::string *type_name, bool valid,
73 const std::string &func, internal::skip_trace_tag = {})
74 : ptr_(ptr), observer_(observer),
75 validator_(observer.push_validator(
76 std::make_shared<life_validator>(ptr, valid))),
77 life_(this), type_name_(type_name) {
78 update_iter(func);
79 }
80
81 template <typename T, typename std::enable_if<
82 std::is_same<const T, element_type>::value,
83 std::nullptr_t>::type = nullptr>
85 : ptr_(other.ptr_), observer_(other.observer_),
86 validator_(observer_.push_validator(
87 std::make_shared<life_validator>(*other.validator_))),
88 life_(this), type_name_(other.type_name_) {}
89
91 : ptr_(other.ptr_), observer_(other.observer_),
92 validator_(observer_.push_validator(
93 std::make_shared<life_validator>(*other.validator_))),
94 life_(this), type_name_(other.type_name_) {}
96 ptr_ = other.ptr_;
97 observer_ = other.observer_;
98 validator_ = observer_.push_validator(
99 std::make_shared<life_validator>(*other.validator_));
100 type_name_ = other.type_name_;
101 return *this;
102 }
104
105 template <typename T>
107 friend class life_observer;
108 const life_observer &get_observer_() const { return this->observer_; }
109 friend element_type *y3c::internal::unwrap<>(
110 const contiguous_iterator<element_type> &wrapper) noexcept;
111
112 using difference_type = std::ptrdiff_t;
113 using value_type = element_type;
114 using pointer = element_type *;
116 using iterator_category = std::random_access_iterator_tag;
117
118 template <typename = internal::skip_trace_tag>
120 std::string func = type_name() + "::operator*()";
121 return reference(assert_iter(func), observer_);
122 }
123 template <typename = internal::skip_trace_tag>
124 element_type *operator->() const {
125 std::string func = type_name() + "::operator->()";
126 return assert_iter(func);
127 }
128
129 template <typename = internal::skip_trace_tag>
131 ++this->ptr_;
132 update_iter(type_name() + "::operator++()");
133 return *this;
134 }
135 template <typename = internal::skip_trace_tag>
137 contiguous_iterator copy = *this;
138 ++this->ptr_;
139 update_iter(type_name() + "::operator++()");
140 return copy;
141 }
142 template <typename = internal::skip_trace_tag>
144 --this->ptr_;
145 update_iter(type_name() + "::operator--()");
146 return *this;
147 }
148 template <typename = internal::skip_trace_tag>
150 contiguous_iterator copy = *this;
151 --this->ptr_;
152 update_iter(type_name() + "::operator--()");
153 return copy;
154 }
155 template <typename = internal::skip_trace_tag>
156 contiguous_iterator &operator+=(std::ptrdiff_t n) {
157 this->ptr_ += n;
158 update_iter(type_name() + "::operator+=()");
159 return *this;
160 }
161 template <typename = internal::skip_trace_tag>
162 contiguous_iterator &operator-=(std::ptrdiff_t n) {
163 this->ptr_ -= n;
164 update_iter(type_name() + "::operator-=()");
165 return *this;
166 }
167 template <typename = internal::skip_trace_tag>
168 contiguous_iterator operator+(std::ptrdiff_t n) const {
169 return contiguous_iterator(this->ptr_ + n, this->observer_, type_name_,
170 this->validator_->valid_,
171 type_name() + "::operator+()");
172 }
173 template <typename = internal::skip_trace_tag>
174 contiguous_iterator operator-(std::ptrdiff_t n) const {
175 return contiguous_iterator(this->ptr_ - n, this->observer_, type_name_,
176 this->validator_->valid_,
177 type_name() + "::operator-()");
178 }
179
180 std::ptrdiff_t operator-(const contiguous_iterator &other) const noexcept {
181 return ptr_ - other.ptr_;
182 }
183 template <typename = internal::skip_trace_tag>
184 reference operator[](std::ptrdiff_t n) const {
185 static std::string func = type_name() + "::operator[]()";
186 return reference(contiguous_iterator(this->ptr_ + n, this->observer_,
187 type_name_,
188 this->validator_->valid_, func)
189 .assert_iter(func),
190 observer_);
191 }
192
193 operator wrap<contiguous_iterator &>() noexcept {
194 return wrap<contiguous_iterator &>(this, life_.observer());
195 }
196 operator wrap<const contiguous_iterator &>() const noexcept {
197 return wrap<const contiguous_iterator &>(this, life_.observer());
198 }
200 return wrap<contiguous_iterator *>(this, life_.observer());
201 }
203 return wrap<const contiguous_iterator *>(this, life_.observer());
204 }
205};
206
207template <typename element_type>
208element_type *
210 return wrapper.ptr_;
211}
212
213
214template <typename T>
216 const contiguous_iterator<T> &rhs) {
217 return unwrap(lhs) == unwrap(rhs);
218}
219template <typename T>
221 const contiguous_iterator<T> &rhs) {
222 return unwrap(lhs) != unwrap(rhs);
223}
224template <typename T>
226 const contiguous_iterator<T> &rhs) {
227 return unwrap(lhs) < unwrap(rhs);
228}
229template <typename T>
231 const contiguous_iterator<T> &rhs) {
232 return unwrap(lhs) > unwrap(rhs);
233}
234template <typename T>
236 const contiguous_iterator<T> &rhs) {
237 return unwrap(lhs) <= unwrap(rhs);
238}
239template <typename T>
241 const contiguous_iterator<T> &rhs) {
242 return unwrap(lhs) >= unwrap(rhs);
243}
244} // namespace internal
245
246template <typename element_type>
247element_type *
249 return internal::unwrap(wrapper);
250}
251
252} // namespace y3c
array, vector など各種コンテナのイテレータ
Definition iterator.h:26
contiguous_iterator(element_type *ptr, internal::life_observer observer, const std::string *type_name, bool valid, const std::string &func, internal::skip_trace_tag={})
Definition iterator.h:71
friend class contiguous_iterator
Definition iterator.h:106
contiguous_iterator operator-(std::ptrdiff_t n) const
Definition iterator.h:174
reference operator[](std::ptrdiff_t n) const
Definition iterator.h:184
contiguous_iterator(element_type *ptr, internal::life_observer observer, const std::string *type_name) noexcept
Definition iterator.h:65
contiguous_iterator(const contiguous_iterator &other)
Definition iterator.h:90
contiguous_iterator & operator++()
Definition iterator.h:130
contiguous_iterator operator+(std::ptrdiff_t n) const
Definition iterator.h:168
contiguous_iterator operator++(int)
Definition iterator.h:136
std::ptrdiff_t operator-(const contiguous_iterator &other) const noexcept
Definition iterator.h:180
contiguous_iterator & operator=(const contiguous_iterator &other)
Definition iterator.h:95
std::random_access_iterator_tag iterator_category
Definition iterator.h:116
contiguous_iterator & operator--()
Definition iterator.h:143
wrap< contiguous_iterator * > operator&()
Definition iterator.h:199
wrap_ref< element_type > reference
Definition iterator.h:115
contiguous_iterator & operator-=(std::ptrdiff_t n)
Definition iterator.h:162
element_type * operator->() const
Definition iterator.h:124
element_type value_type
Definition iterator.h:113
contiguous_iterator(const contiguous_iterator< T > &other)
Definition iterator.h:84
wrap< const contiguous_iterator * > operator&() const
Definition iterator.h:202
std::ptrdiff_t difference_type
Definition iterator.h:112
contiguous_iterator operator--(int)
Definition iterator.h:149
reference operator*() const
Definition iterator.h:119
const life_observer & get_observer_() const
Definition iterator.h:108
element_type * pointer
Definition iterator.h:114
contiguous_iterator & operator+=(std::ptrdiff_t n)
Definition iterator.h:156
ライフタイムの状態を観測するクラス
Definition life.h:89
element_type * assert_iter_including_end(const contiguous_iterator< element_type > &iter, const std::string &func, internal::skip_trace_tag={}) const
Definition life.h:152
const void * end() const
Definition life.h:111
element_type * assert_iter(const contiguous_iterator< element_type > &iter, const std::string &func, internal::skip_trace_tag={}) const
Definition life.h:130
const void * begin() const
Definition life.h:110
const std::shared_ptr< life_validator > & push_validator(const std::shared_ptr< life_validator > &v) const
Definition life.h:106
オブジェクトのライフタイムを管理するクラス
Definition life.h:207
life_observer observer() const
Definition life.h:233
ポインタ型wrap: element_type型のデータへのポインタと、 このポインタ自体の生存状態を管理するクラス
Definition wrap.h:318
参照型wrap: element_type型のデータへの参照を持つクラス
Definition wrap.h:168
値型wrap: base_type型のデータと、このデータの生存状態を管理するクラス
Definition wrap.h:26
element_type * unwrap(const contiguous_iterator< element_type > &wrapper) noexcept
Definition iterator.h:209
bool operator>(const contiguous_iterator< T > &lhs, const contiguous_iterator< T > &rhs)
Definition iterator.h:230
bool operator!=(const contiguous_iterator< T > &lhs, const contiguous_iterator< T > &rhs)
Definition iterator.h:220
void terminate_ub_iter_before_begin(std::string func, Args &&...args)
Definition terminate.h:138
void terminate_ub_iter_after_end(std::string func, Args &&...args)
Definition terminate.h:137
bool operator>=(const contiguous_iterator< T > &lhs, const contiguous_iterator< T > &rhs)
Definition iterator.h:240
bool operator<=(const contiguous_iterator< T > &lhs, const contiguous_iterator< T > &rhs)
Definition iterator.h:235
bool operator==(const contiguous_iterator< T > &lhs, const contiguous_iterator< T > &rhs)
Definition iterator.h:215
void terminate_ub_invalid_iter(std::string func, Args &&...args)
Definition terminate.h:136
bool operator<(const contiguous_iterator< T > &lhs, const contiguous_iterator< T > &rhs)
Definition iterator.h:225
Definition array.h:8
shared_ptr< T > make_shared(Args &&...args)
shared_ptrを構築する
Definition shared_ptr.h:298
std::array< T, N > & unwrap(array< T, N > &wrapper) noexcept
Definition array.h:372
Definition life.h:11
Definition terminate.h:27