8template <
typename element_type>
9class contiguous_iterator;
20 const void *begin_, *end_;
21 std::vector<std::shared_ptr<life_validator>> validators_;
25 : alive_(true), begin_(
begin), end_(
end) {}
34 for (
const auto &validator : validators_) {
35 validator->valid_ =
false;
39 const void *
begin()
const {
return begin_; }
40 const void *
end()
const {
return end_; }
42 validators_.push_back(v);
45 const void *invalidate_from =
nullptr) {
46 bool end_changed = end_ != new_end;
47 for (
auto it = validators_.begin(); it != validators_.end();) {
49 if (validator->ptr_ < begin_ || validator->ptr_ > end_ ||
50 validator->ptr_ < new_begin || validator->ptr_ > new_end ||
51 (end_changed && validator->ptr_ == end_) ||
52 (end_changed && validator->ptr_ == new_end) ||
53 (invalidate_from !=
nullptr &&
54 validator->ptr_ >= invalidate_from)) {
55 validator->valid_ =
false;
56 it = validators_.erase(it);
64 bool alive()
const {
return alive_; }
67 return begin_ <=
ptr &&
ptr <= end_;
74 return static_cast<const T *
>(end_) -
static_cast<const T *
>(begin_);
78 return ptr -
static_cast<const T *
>(begin_);
90 std::shared_ptr<life_state> state_;
92 explicit life_observer(
const std::shared_ptr<life_state> &state)
105 const std::shared_ptr<life_validator> &
107 state_->push_validator(v);
110 const void *
begin()
const {
return state_->begin(); }
111 const void *
end()
const {
return state_->end(); }
112 template <
typename element_type>
120 if (!state_->alive()) {
123 if (!state_->in_range(
ptr)) {
125 func, state_->size<element_type>(), state_->index_of(
ptr));
129 template <
typename element_type>
131 const std::string &func,
136 if (!state_->alive()) {
140 if (!iter.validator_->valid_) {
143 if (!state_->in_range(iter.ptr_)) {
145 func, state_->size<element_type>(),
146 state_->index_of(iter.ptr_));
150 template <
typename element_type>
153 const std::string &func,
158 if (!state_->alive()) {
162 if (!iter.validator_->valid_) {
165 if (!state_->in_range_including_end(iter.ptr_)) {
167 func, state_->size<element_type>(),
168 state_->index_of(iter.ptr_));
172 template <
typename element_type>
175 const std::string &func,
180 if (!state_->alive()) {
184 if (!
begin.validator_->valid_) {
188 if (!
end.validator_->valid_) {
191 if (!state_->in_range(
begin.ptr_,
end.ptr_)) {
193 func, state_->size<element_type>(),
194 state_->index_of(
begin.ptr_), state_->index_of(
end.ptr_));
208 const void *begin, *end;
209 mutable std::shared_ptr<life_state> state_;
211 std::shared_ptr<life_state> init_state()
const {
213 state_ = std::shared_ptr<life_state>(
new life_state(begin, end));
219 explicit life(
const void *begin,
const void *end)
220 : begin(begin), end(end), state_(nullptr) {}
221 template <
typename T>
222 explicit life(T *begin) :
life(begin, begin + 1) {}
240 void update(
const void *begin,
const void *end,
241 const void *invalidate_from =
nullptr) {
243 if ((begin < state_->begin() && end <= state_->begin()) ||
244 (begin >= state_->end() && end > state_->end())) {
246 state_ = std::shared_ptr<life_state>(
new life_state(begin, end));
248 state_->update_range(begin, end, invalidate_from);
253 return this->state_ == obs.state_;
256 return this->state_ != obs.state_;
array, vector など各種コンテナのイテレータ
Definition iterator.h:26
ライフタイムの状態を観測するクラス
Definition life.h:89
void assert_range_iter(const contiguous_iterator< element_type > &begin, const contiguous_iterator< element_type > &end, const std::string &func, internal::skip_trace_tag={}) const
Definition life.h:173
element_type * assert_ptr(element_type *ptr, const std::string &func, internal::skip_trace_tag={}) const
Definition life.h:113
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
life_observer & operator=(const life_observer &)=default
life_observer(std::nullptr_t)
Definition life.h:100
life_observer(const life_observer &)=default
const std::shared_ptr< life_validator > & push_validator(const std::shared_ptr< life_validator > &v) const
Definition life.h:106
std::ptrdiff_t index_of(T *ptr) const
Definition life.h:77
void destroy()
Definition life.h:32
life_state(const life_state &)=delete
const void * end() const
Definition life.h:40
void push_validator(const std::shared_ptr< life_validator > &v)
Definition life.h:41
const void * begin() const
Definition life.h:39
life_state(const void *begin, const void *end)
Definition life.h:24
bool in_range(const void *begin, const void *end) const
Definition life.h:69
void update_range(const void *new_begin, const void *new_end, const void *invalidate_from=nullptr)
Definition life.h:44
life_state & operator=(const life_state &)=delete
life_state & operator=(life_state &&)=delete
bool in_range_including_end(const void *ptr) const
Definition life.h:66
std::size_t size() const
Definition life.h:73
life_state(life_state &&)=delete
bool in_range(const void *ptr) const
Definition life.h:65
bool alive() const
Definition life.h:64
オブジェクトのライフタイムを管理するクラス
Definition life.h:207
life & operator=(life &&)=delete
~life()
Definition life.h:227
void update(const void *begin, const void *end, const void *invalidate_from=nullptr)
Definition life.h:240
life(T *begin)
Definition life.h:222
life(const void *begin, const void *end)
Definition life.h:219
life(const life &)=delete
life_observer observer() const
Definition life.h:233
bool operator==(const life_observer &obs) const
Definition life.h:252
bool operator!=(const life_observer &obs) const
Definition life.h:255
life & operator=(const life &)=delete
ポインタ型wrap: element_type型のデータへのポインタと、 このポインタ自体の生存状態を管理するクラス
Definition wrap.h:318
void terminate_ub_access_nullptr(std::string func, Args &&...args)
Definition terminate.h:133
void terminate_ub_out_of_range(std::string func, Args &&...args)
Definition terminate.h:132
void terminate_ub_access_deleted(std::string func, Args &&...args)
Definition terminate.h:134
void terminate_ub_invalid_iter(std::string func, Args &&...args)
Definition terminate.h:136
wrap< element_type * > ptr
Definition wrap.h:487
bool valid_
Definition life.h:13
life_validator(const void *ptr, bool valid=true)
Definition life.h:15
const void * ptr_
Definition life.h:12
Definition terminate.h:27
#define y3c_assert_internal(cond)
Definition terminate.h:148