YSTest: any_iterator.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

any_iterator.hpp
浏览该文件的文档.
1 /*
2 © 2012-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ystdex_any_iterator_hpp_
29 #define YB_INC_ystdex_any_iterator_hpp_ 1
30
31 #include "any.h" // for "any.h", any_ops, cond_t, wrap_handler, _t, decay_t,
32 // ptrdiff_t, any, exclude_self_t, any_ops::with_handler_t, is_convertible,
33 // indirect_t, decay_unwrap_t, remove_reference_t;
34 #include "iterator.hpp" // for is_undereferenceable, input_iteratable,
35 // std::unique_ptr, forward_iteratable, bidirectional_iteratable;
36
37 namespace ystdex
38 {
39
40 namespace any_ops
41 {
42
44
45 enum iterator_op : op_code
46 {
51 check_undereferenceable = end_base_op,
53 dereference,
55 increase,
56 end_iterator_op
57 };
58
59
60 enum input_iterator_op : op_code
61 {
63 equals = end_iterator_op,
64 end_input_iterator_op,
65 end_output_iterator_op = end_input_iterator_op,
66 end_forward_iterator_op = end_input_iterator_op
67 };
68
69
70 enum bidirectional_iteartor_op : op_code
71 {
73 decrease = end_forward_iterator_op,
74 end_bidirectional_iterator_op
75 };
76
77
78 enum random_access_iteartor_op : op_code
79 {
81 advance = end_forward_iterator_op,
82 distance,
83 less_compare,
84 end_random_access_iteartor_op
85 };
87
88
90
91 template<typename _type, typename _tReference>
92 class iterator_handler : public wrap_handler<_type>::type
93 {
94 public:
96
97 using base = _t<wrap_handler<_type>>;
98 using value_type = typename base::value_type;
99
100 using base::get_reference;
101
102 using base::init;
104
106 static void
107 manage(any_storage& d, any_storage& s, any_ops::op_code op)
108 {
109 switch(op)
110 {
111 case check_undereferenceable:
112 d.access<bool>() = is_undereferenceable(get_reference(s));
113 break;
114 case dereference:
115 {
116 using obj_t = cond_t<is_reference<_tReference>,
117 lref<decay_t<_tReference>>, _tReference>;
118 auto& p(d.access<any*>());
119
120 yassume(p);
121 *p = obj_t(*get_reference(s));
122 }
123 break;
124 case increase:
125 ++get_reference(d);
126 break;
127 default:
128 base::manage(d, s, op);
129 }
130 }
131 };
132
133
134 template<typename _type, typename _tReference>
135 class input_iterator_handler : public iterator_handler<_type, _tReference>
136 {
137 public:
139
140 using base = iterator_handler<_type, _tReference>;
141 using value_type = typename base::value_type;
142
143 using base::get_reference;
144
145 using base::init;
147
149 static void
150 manage(any_storage& d, any_storage& s, any_ops::op_code op)
151 {
152 switch(op)
153 {
154 case equals:
155 if(get_reference(*d.access<any_storage*>()) != get_reference(s))
156 d = static_cast< any_storage*>(nullptr);
157 break;
158 default:
159 base::manage(d, s, op);
160 }
161 }
162 };
163
164
165 template<typename _type, typename _tReference>
166 class forward_iterator_handler
167 : public input_iterator_handler<_type, _tReference>
168 {
169 public:
171
172 using base = input_iterator_handler<_type, _tReference>;
173 using value_type = typename base::value_type;
174
175 using base::get_reference;
176
177 using base::init;
178
179 using base::manage;
181 };
182
183
184 template<typename _type, typename _tReference>
185 class bidirectional_iterator_handler
186 : public forward_iterator_handler<_type, _tReference>
187 {
188 public:
190
191 using base = forward_iterator_handler<_type, _tReference>;
192 using value_type = typename base::value_type;
193
194 using base::get_reference;
195
196 using base::init;
198
200 static void
201 manage(any_storage& d, any_storage& s, any_ops::op_code op)
202 {
203 switch(op)
204 {
205 case decrease:
206 --get_reference(d);
207 break;
208 default:
209 base::manage(d, s, op);
210 }
211 }
212 };
214
215 } // namespace any_ops;
216
217
219
220
225 template<typename _type, typename _tDifference = ptrdiff_t,
226 typename _tPointer = _type*, typename _tReference = _type&>
227 class any_input_iterator : protected any,
228 public input_iteratable<any_input_iterator<_type, _tDifference, _tPointer,
229 _tReference>, _tReference>
230 {
231 protected:
233 using base = any;
234
235 public:
237
238 using iterator_category = std::input_iterator_tag;
239 using value_type = _type;
240 using difference_type = _tDifference;
242 using pointer = _tPointer;
243 using reference = _tReference;
244
246 any_input_iterator() = default;
251 template<typename _tIter,
252 yimpl(typename = exclude_self_t<any_input_iterator, _tIter>)>
253 any_input_iterator(_tIter&& i)
254 : any_input_iterator(any_ops::with_handler_t<
255 any_ops::input_iterator_handler<decay_t<_tIter>, reference>>(),
256 yforward(i))
257 {}
258
259 protected:
261 template<typename _tIter, typename _tHandler>
262 any_input_iterator(any_ops::with_handler_t<_tHandler> t, _tIter&& i)
263 : base(t, yforward(i))
264 {
265 static_assert(is_convertible<indirect_t<decay_unwrap_t<_tIter>>,
266 reference>(), "Wrong target iterator type found.");
267 }
268
269 public:
271 any_input_iterator(const any_input_iterator&) = default;
273 any_input_iterator(any_input_iterator&&) = default;
274
276
277 any_input_iterator&
278 operator=(const any_input_iterator&) = default;
279 any_input_iterator&
280 operator=(any_input_iterator&&) = default;
282
283 reference
284 operator*() const
285 {
286 // TODO: More graceful implementation for unused value?
287 any res;
288 any_ops::any_storage t;
289 const auto gd(t.pun<any*>(&res));
290
291 call(t, any_ops::dereference);
292 return reference(
293 *ystdex::unchecked_any_cast<remove_reference_t<reference>>(&res));
294 }
295
296 pointer
297 operator->() const
298 {
299 return &**this;
300 }
301
302 any_input_iterator&
303 operator++()
304 {
305 call(get_storage(), any_ops::increase);
306 return *this;
307 }
308
310 friend bool
311 operator==(const any_input_iterator& x, const any_input_iterator& y)
312 {
313 if(x.has_value() || y.has_value())
314 {
315 yassume(x.type() == y.type());
316
317 using any_ops::any_storage;
318 any_storage t;
319 const auto gd(t.pun<any_storage*>(&x.get_storage()));
320
321 return y.template
322 unchecked_access<any_storage*>(t, any_ops::equals);
323 }
324 return true;
325 }
326
328
329 any&
330 get() ynothrow
331 {
332 return *this;
333 }
334 const any&
335 get() const ynothrow
336 {
337 return *this;
338 }
340
345 friend bool
346 is_undereferenceable(const any_input_iterator& i)
347 {
348 return i.has_value() ? i.template unchecked_access<bool>(
349 any_ops::check_undereferenceable) : true;
350 }
351
353 using any::target;
354
356 using any::type;
357 };
358
359
360 using input_monomorphic_iterator
361 = any_input_iterator<void_ref, ptrdiff_t, void*, void_ref_any>;
363
364
369 template<typename _type, typename _tDifference = ptrdiff_t,
370 typename _tPointer = _type*, typename _tReference = _type&>
371 class any_forward_iterator : public any_input_iterator<_type, _tDifference,
372 _tPointer, _tReference>, public forward_iteratable<any_forward_iterator<
373 _type, _tDifference, _tPointer, _tReference>, _tReference>
374 {
375 protected:
377 using base
378 = any_input_iterator<_type, _tDifference, _tPointer, _tReference>;
379
380 public:
381 using iterator_category = std::forward_iterator_tag;
382 using pointer = _tPointer;
383 using reference = _tReference;
384
385 any_forward_iterator() = default;
390 template<typename _tIter,
391 yimpl(typename = exclude_self_t<any_forward_iterator, _tIter>)>
392 any_forward_iterator(_tIter&& i)
393 : any_forward_iterator(any_ops::with_handler_t<
394 any_ops::forward_iterator_handler<decay_t<_tIter>, reference>>(),
395 yforward(i))
396 {
397 static_assert(is_convertible<indirect_t<decay_unwrap_t<_tIter>>,
398 reference>(), "Wrong target iterator type found.");
399 }
400
401 protected:
403 template<typename _tIter, typename _tHandler>
404 any_forward_iterator(any_ops::with_handler_t<_tHandler> t, _tIter&& i)
405 : base(t, yforward(i))
406 {}
407
408 public:
409 any_forward_iterator(const any_forward_iterator&) = default;
410 any_forward_iterator(any_forward_iterator&&) = default;
411
413
414 any_forward_iterator&
415 operator=(const any_forward_iterator&) = default;
416 any_forward_iterator&
417 operator=(any_forward_iterator&&) = default;
419
420 any_forward_iterator&
421 operator++()
422 {
423 base::operator++();
424 return *this;
425 }
426
428 friend bool
429 operator==(const any_forward_iterator& x, const any_forward_iterator& y)
430 {
431 return static_cast<const base&>(x) == static_cast<const base&>(y);
432 }
433 };
434
435
436 using forward_monomorphic_iterator
437 = any_forward_iterator<void_ref, ptrdiff_t, void*, void_ref>;
438
439
444 template<typename _type, typename _tDifference = ptrdiff_t,
445 typename _tPointer = _type*, typename _tReference = _type&>
446 class any_bidirectional_iterator : public any_forward_iterator<_type,
447 _tDifference, _tPointer, _tReference>, public bidirectional_iteratable<
448 any_bidirectional_iterator<_type, _tDifference, _tPointer, _tReference>,
449 _tReference>
450 {
451 protected:
453 using base
454 = any_forward_iterator<_type, _tDifference, _tPointer, _tReference>;
455
456 public:
457 using iterator_category = std::bidirectional_iterator_tag;
458 using pointer = _tPointer;
459 using reference = _tReference;
460
461 any_bidirectional_iterator() = default;
466 template<typename _tIter,
467 yimpl(typename = exclude_self_t<any_bidirectional_iterator, _tIter>)>
468 any_bidirectional_iterator(_tIter&& i)
469 : any_forward_iterator<_type, _tPointer, _tReference>(yforward(i))
470 {}
471
472 protected:
474 template<typename _tIter, typename _tHandler>
475 any_bidirectional_iterator(any_ops::with_handler_t<_tHandler> t, _tIter&& i)
476 : base(t, yforward(i))
477 {}
478
479 public:
480 any_bidirectional_iterator(const any_bidirectional_iterator&) = default;
481 any_bidirectional_iterator(any_bidirectional_iterator&&) = default;
482
484
485 any_bidirectional_iterator&
486 operator=(const any_bidirectional_iterator&) = default;
487 any_bidirectional_iterator&
488 operator=(any_bidirectional_iterator&&) = default;
490
491 any_bidirectional_iterator&
492 operator++()
493 {
494 base::operator++();
495 return *this;
496 }
497
498 any_bidirectional_iterator&
499 operator--()
500 {
501 this->call(this->get_storage(), any_ops::decrease);
502 return *this;
503 }
504
506 friend bool
507 operator==(const any_bidirectional_iterator& x,
508 const any_bidirectional_iterator& y)
509 {
510 return static_cast<const base&>(x) == static_cast<const base&>(y);
511 }
512 };
513
514
515 using bidirectional_monomorphic_iterator
516 = any_bidirectional_iterator<void_ref, ptrdiff_t, void*, void_ref>;
518
519 } // namespace ystdex;
520
521 #endif
522
ystdex::any_forward_iterator::operator++
any_forward_iterator & operator++()
Definition: any_iterator.hpp:421
any.h
动态泛型类型。
ystdex::forward_monomorphic_iterator
any_forward_iterator< void_ref, ptrdiff_t, void *, void_ref > forward_monomorphic_iterator
Definition: any_iterator.hpp:437
ystdex::any_bidirectional_iterator::operator=
any_bidirectional_iterator & operator=(const any_bidirectional_iterator &)=default
ystdex::any_ops::with_handler_t
Definition: any.h:58
ystdex::any_ops::bidirectional_iterator_handler::manage
static void manage(any_storage &d, any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:201
ystdex::any_input_iterator::any_input_iterator
any_input_iterator(any_ops::with_handler_t< _tHandler > t, _tIter &&i)
Definition: any_iterator.hpp:262
ystdex::cond_t
conditional_t< _tCond::value, _tThen, _tElse > cond_t
条件判断。
Definition: type_traits.hpp:738
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator(_tIter &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:392
ystdex::any_ops::iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:98
ystdex::any_bidirectional_iterator
动态泛型双向迭代器。
Definition: any_iterator.hpp:446
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator()=default
ystdex::any_ops::end_bidirectional_iterator_op
Definition: any_iterator.hpp:74
ystdex::any_input_iterator::reference
_tReference reference
Definition: any_iterator.hpp:243
ystdex::any_bidirectional_iterator::operator==
friend bool operator==(const any_bidirectional_iterator &x, const any_bidirectional_iterator &y)
Definition: any_iterator.hpp:507
ystdex::any_input_iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: any_iterator.hpp:238
iterator.hpp
通用迭代器。
ystdex::any_ops::random_access_iteartor_op
random_access_iteartor_op
Definition: any_iterator.hpp:78
ystdex::any_input_iterator::pointer
_tPointer pointer
Definition: any_iterator.hpp:242
ystdex::details::ebases
Definition: operators.hpp:100
ystdex::bidirectional_monomorphic_iterator
any_bidirectional_iterator< void_ref, ptrdiff_t, void *, void_ref > bidirectional_monomorphic_iterator
Definition: any_iterator.hpp:516
ystdex::any_input_iterator::operator->
pointer operator->() const
Definition: any_iterator.hpp:297
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
ystdex::any_ops::op_code
std::uint32_t op_code
操作码。
Definition: any.h:218
ystdex::any_ops::check_undereferenceable
Definition: any_iterator.hpp:51
ystdex::any_ops::input_iterator_handler::manage
static void manage(any_storage &d, any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:150
ystdex::any_input_iterator::any_input_iterator
any_input_iterator()=default
ystdex::indirect_t
decltype(*std::declval< _type >()) indirect_t
Definition: type_traits.hpp:1037
ystdex::standard_layout_storage::pun
pun_ptr< _type > pun(_tParams &&...args)
Definition: type_pun.hpp:389
ystdex::any_ops::end_forward_iterator_op
Definition: any_iterator.hpp:66
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator(any_ops::with_handler_t< _tHandler > t, _tIter &&i)
Definition: any_iterator.hpp:404
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
ystdex::standard_layout_storage
任意标准布局类型对象提供空间的存储。
Definition: type_pun.hpp:307
ystdex::any_ops::equals
Definition: any_iterator.hpp:63
ystdex::any_input_iterator::any_input_iterator
any_input_iterator(_tIter &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:253
ystdex::any_ops::less_compare
Definition: any_iterator.hpp:83
ystdex::any_ops::iterator_handler
Definition: any_iterator.hpp:92
ystdex::any_input_iterator::operator==
friend bool operator==(const any_input_iterator &x, const any_input_iterator &y)
Definition: any_iterator.hpp:311
ystdex::any_ops::iterator_handler::base
_t< wrap_handler< _type >> base
Definition: any_iterator.hpp:97
ystdex::any_ops::end_input_iterator_op
Definition: any_iterator.hpp:64
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
ystdex::any::any
any()=default
ystdex::any_ops::end_output_iterator_op
Definition: any_iterator.hpp:65
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator(any_ops::with_handler_t< _tHandler > t, _tIter &&i)
Definition: any_iterator.hpp:475
ystdex::any::target
_type * target()
取目标指针。
Definition: any.h:1010
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
ystdex::any_ops::wrap_handler
根据类型选择引用或值处理器。
Definition: any.h:598
ystdex::any_ops::decrease
Definition: any_iterator.hpp:73
ystdex::any::type
const type_info & type() const
Definition: any.h:1024
ystdex::any_bidirectional_iterator::operator++
any_bidirectional_iterator & operator++()
Definition: any_iterator.hpp:492
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:845
ystdex::any_ops::distance
Definition: any_iterator.hpp:82
ystdex::decay_unwrap_t
_t< decay_unwrap< _type >> decay_unwrap_t
Definition: ref.hpp:214
ystdex::any_ops::increase
Definition: any_iterator.hpp:55
ystdex::standard_layout_storage::access
void * access()
Definition: type_pun.hpp:335
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: type_traits.hpp:340
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
ystdex
特性测试宏。
Definition: ydef.h:822
YSLib::i
return i
Definition: YCoreUtilities.h:176
ystdex::any_forward_iterator
动态泛型前向迭代器。
Definition: any_iterator.hpp:371
ystdex::any_ops::any_storage
standard_layout_storage< aligned_storage_t< sizeof(void *), std::alignment_of< void * >::value >> any_storage
Definition: any.h:247
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator()=default
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: type_traits.hpp:97
ystdex::details::any_base::call
any_ops::any_storage & call(any_ops::any_storage &, any_ops::op_code) const
Definition: any.cpp:67
ystdex::any_ops::forward_iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:173
ystdex::any_ops::forward_iterator_handler
Definition: any_iterator.hpp:166
ystdex::any_input_iterator::operator++
any_input_iterator & operator++()
Definition: any_iterator.hpp:303
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:748
ystdex::any_ops::bidirectional_iteartor_op
bidirectional_iteartor_op
Definition: any_iterator.hpp:70
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator(_tIter &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:468
ystdex::any_input_iterator::value_type
_type value_type
Definition: any_iterator.hpp:239
ystdex::any_input_iterator::difference_type
_tDifference difference_type
Definition: any_iterator.hpp:240
ystdex::any_ops::advance
Definition: any_iterator.hpp:81
ystdex::input_monomorphic_iterator
any_input_iterator< void_ref, ptrdiff_t, void *, void_ref_any > input_monomorphic_iterator
Definition: any_iterator.hpp:361
ystdex::any_forward_iterator::operator==
friend bool operator==(const any_forward_iterator &x, const any_forward_iterator &y)
Definition: any_iterator.hpp:429
ystdex::any_ops::end_base_op
Definition: any.h:241
ystdex::any_input_iterator::operator=
any_input_iterator & operator=(const any_input_iterator &)=default
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::any_ops::end_iterator_op
Definition: any_iterator.hpp:56
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:105
ystdex::any_ops::dereference
Definition: any_iterator.hpp:53
ystdex::any_ops::input_iterator_op
input_iterator_op
Definition: any_iterator.hpp:60
ystdex::any_ops::input_iterator_handler
Definition: any_iterator.hpp:135
ystdex::any_forward_iterator::operator=
any_forward_iterator & operator=(const any_forward_iterator &)=default
ystdex::any_ops::iterator_handler::manage
static void manage(any_storage &d, any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:107
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
ystdex::any_ops::bidirectional_iterator_handler
Definition: any_iterator.hpp:185
ystdex::any_input_iterator
动态泛型输入迭代器。
Definition: any_iterator.hpp:227
ystdex::any_ops::iterator_op
iterator_op
Definition: any_iterator.hpp:45
ystdex::any_input_iterator::operator*
reference operator*() const
Definition: any_iterator.hpp:284
ystdex::any_ops::end_random_access_iteartor_op
Definition: any_iterator.hpp:84
ystdex::any_bidirectional_iterator::operator--
any_bidirectional_iterator & operator--()
Definition: any_iterator.hpp:499
ystdex::any_ops::input_iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:141
ystdex::details::any_base::get_storage
any_ops::any_storage & get_storage() const
Definition: any.h:738
ystdex::any_input_iterator::is_undereferenceable
friend bool is_undereferenceable(const any_input_iterator &i)
Definition: any_iterator.hpp:346
CHRLib::p
const auto p(d)