YSTest: any_iterator.hpp 源文件

YSTest PreAlpha_b700_20160611 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, unwrap_reference_t, cond_t,
32 // is_reference_wrapper, ref_handler, _t. ptrdiff_t, any, exclude_self_t,
33 // any_ops::with_handler_t, decay_t, is_convertible, indirect_t;
34 #include "iterator.hpp" // for is_undereferenceable, input_iteratable,
35 // forward_iteratable, bidirectional_iteratable;
36
37 namespace ystdex
38 {
39
40 namespace any_ops
41 {
42
44
45 enum iterator_op : op_code
46 {
48 check_undereferenceable = end_base_op,
49 dereference,
50 increase,
51 end_iterator_op
52 };
53
54
55 enum input_iterator_op : op_code
56 {
57 equals = end_iterator_op,
58 end_input_iterator_op,
59 end_output_iterator_op = end_input_iterator_op,
60 end_forward_iterator_op = end_input_iterator_op
61 };
62
63
64 enum bidirectional_iteartor_op : op_code
65 {
66 decrease = end_forward_iterator_op,
67 end_bidirectional_iterator_op
68 };
69
70
71 enum random_access_iteartor_op : op_code
72 {
73 advance = end_forward_iterator_op,
74 distance,
75 less_compare,
76 end_random_access_iteartor_op
77 };
78
79
80 template<typename _type>
81 struct wrap_handler
82 {
83 using value_type = unwrap_reference_t<_type>;
84 using type = cond_t<is_reference_wrapper<_type>, ref_handler<value_type>,
85 value_handler<value_type>>;
86 };
87
88
89 template<typename _type>
90 class iterator_handler : public wrap_handler<_type>::type
91 {
92 public:
93 using base = _t<wrap_handler<_type>>;
94 using value_type = typename base::value_type;
95
96 using base::get_reference;
97
98 using base::init;
99
101 static void
102 manage(any_storage& d, any_storage& s, any_ops::op_code op)
103 {
104 switch(op)
105 {
106 case check_undereferenceable:
107 d.access<bool>() = is_undereferenceable(get_reference(s));
108 break;
109 case dereference:
110 d = void_ref(*get_reference(s));
111 break;
112 case increase:
113 ++get_reference(d);
114 break;
115 default:
116 base::manage(d, s, op);
117 }
118 }
119 };
120
121
122 template<typename _type>
123 class input_iterator_handler : public iterator_handler<_type>
124 {
125 public:
126 using base = iterator_handler<_type>;
127 using value_type = typename base::value_type;
128
129 using base::get_reference;
130
131 using base::init;
132
134 static void
135 manage(any_storage& d, any_storage& s, any_ops::op_code op)
136 {
137 switch(op)
138 {
139 case equals:
140 {
141 const auto p(d.access<any_storage*>());
142
143 d.access<bool>() = get_reference(*p) == get_reference(s);
144 }
145 break;
146 default:
147 base::manage(d, s, op);
148 }
149 }
150 };
152
153
155 template<typename _type>
156 class forward_iterator_handler : public input_iterator_handler<_type>
157 {
158 public:
159 using base = input_iterator_handler<_type>;
160 using value_type = typename base::value_type;
161
162 using base::get_reference;
163
164 using base::init;
165
166 using base::manage;
167 };
168
169
171 template<typename _type>
172 class bidirectional_iterator_handler : public forward_iterator_handler<_type>
173 {
174 public:
175 using base = forward_iterator_handler<_type>;
176 using value_type = typename base::value_type;
177
178 using base::get_reference;
179
180 using base::init;
181
183 static void
184 manage(any_storage& d, any_storage& s, any_ops::op_code op)
185 {
186 switch(op)
187 {
188 case decrease:
189 --get_reference(d);
190 break;
191 default:
192 base::manage(d, s, op);
193 }
194 }
195 };
196
197 } // namespace any_ops;
198
199
201
202
207 template<typename _type, typename _tDifference = ptrdiff_t,
208 typename _tPointer = _type*, typename _tReference = _type&>
209 class any_input_iterator : protected any,
210 public input_iteratable<any_input_iterator<_type, _tDifference, _tPointer,
211 _tReference>, _tReference>
212 {
213 protected:
215 using base = any;
216
217 public:
219
220 using iterator_category = std::input_iterator_tag;
221 using value_type = _type;
222 using difference_type = _tDifference;
224 using pointer = _tPointer;
225 using reference = _tReference;
226
228 any_input_iterator() = default;
233 template<typename _tIter,
234 yimpl(typename = exclude_self_t<any_input_iterator, _type>)>
235 any_input_iterator(_tIter&& i)
236 : any_input_iterator(any_ops::with_handler_t<
237 any_ops::input_iterator_handler<decay_t<_tIter>>>(), yforward(i))
238 {}
239
240 protected:
242 template<typename _tIter, typename _tHandler>
243 any_input_iterator(any_ops::with_handler_t<_tHandler> t, _tIter&& i)
244 : base(t, yforward(i))
245 {
246 static_assert(is_convertible<indirect_t<unwrap_reference_t<
247 decay_t<_tIter>>&>, reference>::value,
248 "Wrong target iterator type found.");
249 }
250
251 public:
253 any_input_iterator(const any_input_iterator&) = default;
255 any_input_iterator(any_input_iterator&&) = default;
256
258
259 any_input_iterator&
260 operator=(const any_input_iterator&) = default;
261 any_input_iterator&
262 operator=(any_input_iterator&&) = default;
264
265 reference
266 operator*() const
267 {
268 return reference(unchecked_access<void_ref>(any_ops::dereference));
269 }
270
271 pointer
272 operator->() const
273 {
274 return &**this;
275 }
276
277 any_input_iterator&
278 operator++()
279 {
280 call(get_storage(), any_ops::increase);
281 return *this;
282 }
283
285 friend bool
286 operator==(const any_input_iterator& x, const any_input_iterator& y)
287 {
288 if(x.empty() && y.empty())
289 return true;
290 yassume(x.type() == y.type());
291
292 any_ops::any_storage t(&x.get_storage());
293
294 return y.template unchecked_access<bool>(t, any_ops::equals);
295 }
296
298
299 any&
300 get() ynothrow
301 {
302 return *this;
303 }
304 const any&
305 get() const ynothrow
306 {
307 return *this;
308 }
310
315 friend bool
316 is_undereferenceable(const any_input_iterator& i)
317 {
318 return !i.empty() ? i.template unchecked_access<bool>(
319 any_ops::check_undereferenceable) : true;
320 }
321
323 using any::target;
324
326 using any::type;
327 };
328
329
330 using input_monomorphic_iterator
331 = any_input_iterator<void_ref, ptrdiff_t, void*, void_ref>;
333
334
339 template<typename _type, typename _tDifference = ptrdiff_t,
340 typename _tPointer = _type*, typename _tReference = _type&>
341 class any_forward_iterator : public any_input_iterator<_type, _tDifference,
342 _tPointer, _tReference>, public forward_iteratable<any_forward_iterator<
343 _type, _tDifference, _tPointer, _tReference>, _tReference>
344 {
345 protected:
347 using base
348 = any_input_iterator<_type, _tDifference, _tPointer, _tReference>;
349
350 public:
351 using iterator_category = std::forward_iterator_tag;
352 using pointer = _tPointer;
353 using reference = _tReference;
354
355 any_forward_iterator() = default;
360 template<typename _tIter,
361 yimpl(typename = exclude_self_t<any_forward_iterator, _type>)>
362 any_forward_iterator(_tIter&& i)
363 : any_forward_iterator(any_ops::with_handler_t<
364 any_ops::forward_iterator_handler<decay_t<_tIter>>>(), yforward(i))
365 {
366 static_assert(is_convertible<indirect_t<unwrap_reference_t<
367 decay_t<_tIter>>&>, reference>::value,
368 "Wrong target iterator type found.");
369 }
370
371 protected:
373 template<typename _tIter, typename _tHandler>
374 any_forward_iterator(any_ops::with_handler_t<_tHandler> t, _tIter&& i)
375 : base(t, yforward(i))
376 {}
377
378 public:
379 any_forward_iterator(const any_forward_iterator&) = default;
380 any_forward_iterator(any_forward_iterator&&) = default;
381
383
384 any_forward_iterator&
385 operator=(const any_forward_iterator&) = default;
386 any_forward_iterator&
387 operator=(any_forward_iterator&&) = default;
389
390 any_forward_iterator&
391 operator++()
392 {
393 base::operator++();
394 return *this;
395 }
396
398 friend bool
399 operator==(const any_forward_iterator& x, const any_forward_iterator& y)
400 {
401 return static_cast<const base&>(x) == static_cast<const base&>(y);
402 }
403 };
404
405
406 using forward_monomorphic_iterator
407 = any_forward_iterator<void_ref, ptrdiff_t, void*, void_ref>;
408
409
414 template<typename _type, typename _tDifference = ptrdiff_t,
415 typename _tPointer = _type*, typename _tReference = _type&>
416 class any_bidirectional_iterator : public any_forward_iterator<_type,
417 _tDifference, _tPointer, _tReference>, public bidirectional_iteratable<
418 any_bidirectional_iterator<_type, _tDifference, _tPointer, _tReference>,
419 _tReference>
420 {
421 protected:
423 using base
424 = any_forward_iterator<_type, _tDifference, _tPointer, _tReference>;
425
426 public:
427 using iterator_category = std::bidirectional_iterator_tag;
428 using pointer = _tPointer;
429 using reference = _tReference;
430
431 any_bidirectional_iterator() = default;
436 template<typename _tIter>
437 any_bidirectional_iterator(_tIter&& i)
438 : any_forward_iterator<_type, _tPointer, _tReference>(yforward(i))
439 {}
440
441 protected:
443 template<typename _tIter, typename _tHandler>
444 any_bidirectional_iterator(any_ops::with_handler_t<_tHandler> t, _tIter&& i)
445 : base(t, yforward(i))
446 {}
447
448 public:
449 any_bidirectional_iterator(const any_bidirectional_iterator&) = default;
450 any_bidirectional_iterator(any_bidirectional_iterator&&) = default;
451
453
454 any_bidirectional_iterator&
455 operator=(const any_bidirectional_iterator&) = default;
456 any_bidirectional_iterator&
457 operator=(any_bidirectional_iterator&&) = default;
459
460 any_bidirectional_iterator&
461 operator++()
462 {
463 base::operator++();
464 return *this;
465 }
466
467 any_bidirectional_iterator&
468 operator--()
469 {
470 this->call(this->get_storage(), any_ops::decrease);
471 return *this;
472 }
473
475 friend bool
476 operator==(const any_bidirectional_iterator& x,
477 const any_bidirectional_iterator& y)
478 {
479 return static_cast<const base&>(x) == static_cast<const base&>(y);
480 }
481 };
482
483
484 using bidirectional_monomorphic_iterator
485 = any_bidirectional_iterator<void_ref, ptrdiff_t, void*, void_ref>;
487
488 } // namespace ystdex;
489
490 #endif
491
yassume
#define yassume(_expr)
假定：环境语义。
Definition: cassert.h:99
platform::p
while p p
Definition: YCLib/FileSystem.h:225
ystdex::any_forward_iterator::operator++
any_forward_iterator & operator++()
Definition: any_iterator.hpp:391
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
ystdex::any_ops::decrease
Definition: any_iterator.hpp:66
ystdex::any_bidirectional_iterator::operator=
any_bidirectional_iterator & operator=(const any_bidirectional_iterator &)=default
ystdex::any_input_iterator::any_input_iterator
any_input_iterator(any_ops::with_handler_t< _tHandler > t, _tIter &&i)
Definition: any_iterator.hpp:243
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator(_tIter &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:362
ystdex::any_ops::bidirectional_iterator_handler
Definition: any_iterator.hpp:172
ystdex::any_bidirectional_iterator
动态泛型双向迭代器。
Definition: any_iterator.hpp:416
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator()=default
ystdex::any_ops::op_code
std::uint32_t op_code
Definition: any.h:244
ystdex::any_input_iterator::reference
_tReference reference
Definition: any_iterator.hpp:225
ystdex::any_bidirectional_iterator::operator==
friend bool operator==(const any_bidirectional_iterator &x, const any_bidirectional_iterator &y)
Definition: any_iterator.hpp:476
ystdex::forward_monomorphic_iterator
any_forward_iterator< void_ref, ptrdiff_t, void *, void_ref > forward_monomorphic_iterator
Definition: any_iterator.hpp:407
iterator.hpp
通用迭代器。
ystdex::any_input_iterator::iterator_category
std::input_iterator_tag iterator_category
Definition: any_iterator.hpp:220
ystdex::details::ebases
Definition: operators.hpp:100
ystdex::any_input_iterator::pointer
_tPointer pointer
Definition: any_iterator.hpp:224
ystdex::any_input_iterator::operator->
pointer operator->() const
Definition: any_iterator.hpp:272
ystdex::indirect_t
decltype(*std::declval< _type >()) indirect_t
Definition: type_traits.hpp:978
ystdex::any_ops::input_iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:127
ystdex::any_input_iterator::any_input_iterator
any_input_iterator()=default
ystdex::any_ops::end_bidirectional_iterator_op
Definition: any_iterator.hpp:67
ystdex::any_ops::iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:94
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator(any_ops::with_handler_t< _tHandler > t, _tIter &&i)
Definition: any_iterator.hpp:374
ystdex::any_input_iterator::any_input_iterator
any_input_iterator(_tIter &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:235
CHRLib::d
_tDst * d
Definition: CharacterProcessing.h:139
ystdex::any_input_iterator::operator==
friend bool operator==(const any_input_iterator &x, const any_input_iterator &y)
Definition: any_iterator.hpp:286
ystdex::standard_layout_storage::access
void * access()
Definition: type_pun.hpp:263
ystdex::any_ops::wrap_handler::value_type
unwrap_reference_t< _type > value_type
Definition: any_iterator.hpp:83
ystdex::any::any
any()=default
ystdex::any_ops::end_base_op
Definition: any.h:258
ystdex::any_ops::value_handler
动态泛型对象处理器。
Definition: any.h:295
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator(any_ops::with_handler_t< _tHandler > t, _tIter &&i)
Definition: any_iterator.hpp:444
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:708
ystdex::void_ref
任意对象引用类型。
Definition: ref.hpp:222
ystdex::any_ops::bidirectional_iteartor_op
bidirectional_iteartor_op
Definition: any_iterator.hpp:64
ystdex::any::target
_type * target()
取目标指针。
Definition: any.h:988
ystdex::any_ops::with_handler_t
Definition: any.h:89
ystdex::any_ops::random_access_iteartor_op
random_access_iteartor_op
Definition: any_iterator.hpp:71
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:256
ystdex::any::type
const type_info & type() const
Definition: any.h:1002
ystdex::any_bidirectional_iterator::operator++
any_bidirectional_iterator & operator++()
Definition: any_iterator.hpp:461
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:823
ystdex::any_input_iterator::is_undereferenceable
friend bool is_undereferenceable(const any_input_iterator &i)
Definition: any_iterator.hpp:316
ystdex::any_ops::end_forward_iterator_op
Definition: any_iterator.hpp:60
ystdex::any_ops::equals
Definition: any_iterator.hpp:57
ystdex::unwrap_reference_t
_t< unwrap_reference< _type >> unwrap_reference_t
Definition: ref.hpp:176
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: type_traits.hpp:96
ystdex::any_ops::advance
Definition: any_iterator.hpp:73
ystdex::any_ops::wrap_handler::type
cond_t< is_reference_wrapper< _type >, ref_handler< value_type >, value_handler< value_type >> type
Definition: any_iterator.hpp:85
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator(_tIter &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:437
ystdex::any_ops::check_undereferenceable
Definition: any_iterator.hpp:48
ystdex::any_ops::increase
Definition: any_iterator.hpp:50
ystdex::any_ops::end_random_access_iteartor_op
Definition: any_iterator.hpp:76
ystdex::any_forward_iterator
动态泛型前向迭代器。
Definition: any_iterator.hpp:341
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator()=default
ystdex::any_ops::iterator_handler::base
_t< wrap_handler< _type >> base
Definition: any_iterator.hpp:93
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: type_traits.hpp:347
ystdex::any_ops::less_compare
Definition: any_iterator.hpp:75
ystdex::details::any_base::call
any_ops::any_storage & call(any_ops::any_storage &, any_ops::op_code) const
Definition: any.cpp:80
ystdex::any_ops::forward_iterator_handler
Definition: any_iterator.hpp:156
ystdex::cond_t
conditional_t< _tCond::value, _tThen, _tElse > cond_t
条件判断。
Definition: type_traits.hpp:698
ystdex::any_ops::forward_iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:160
ystdex::any_ops::iterator_handler::manage
static void manage(any_storage &d, any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:102
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
ystdex::any_input_iterator::operator++
any_input_iterator & operator++()
Definition: any_iterator.hpp:278
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
YSLib::i
return i
Definition: YCoreUtilities.h:176
ystdex::input_monomorphic_iterator
any_input_iterator< void_ref, ptrdiff_t, void *, void_ref > input_monomorphic_iterator
Definition: any_iterator.hpp:331
ystdex::any_ops::wrap_handler
Definition: any_iterator.hpp:81
ystdex::any_input_iterator::value_type
_type value_type
Definition: any_iterator.hpp:221
ystdex::any_input_iterator::difference_type
_tDifference difference_type
Definition: any_iterator.hpp:222
ystdex::any_ops::end_iterator_op
Definition: any_iterator.hpp:51
ystdex::any_forward_iterator::operator==
friend bool operator==(const any_forward_iterator &x, const any_forward_iterator &y)
Definition: any_iterator.hpp:399
ystdex::any_ops::input_iterator_handler
Definition: any_iterator.hpp:123
ystdex::any_input_iterator::operator=
any_input_iterator & operator=(const any_input_iterator &)=default
ystdex::standard_layout_storage
任意标准布局类型存储。
Definition: type_pun.hpp:227
ystdex::any_ops::iterator_handler
Definition: any_iterator.hpp:90
ystdex::any_ops::input_iterator_op
input_iterator_op
Definition: any_iterator.hpp:55
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:135
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::bidirectional_monomorphic_iterator
any_bidirectional_iterator< void_ref, ptrdiff_t, void *, void_ref > bidirectional_monomorphic_iterator
Definition: any_iterator.hpp:485
ystdex::any_forward_iterator::operator=
any_forward_iterator & operator=(const any_forward_iterator &)=default
ystdex::any_ops::dereference
Definition: any_iterator.hpp:49
ystdex::any_ops::ref_handler
动态泛型引用处理器。
Definition: any.h:485
ystdex::any_input_iterator
动态泛型输入迭代器。
Definition: any_iterator.hpp:209
any.h
动态泛型类型。
ystdex::any_ops::iterator_op
iterator_op
Definition: any_iterator.hpp:45
ystdex::any_input_iterator::operator*
reference operator*() const
Definition: any_iterator.hpp:266
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
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:184
ystdex::any_ops::distance
Definition: any_iterator.hpp:74
ystdex::any_bidirectional_iterator::operator--
any_bidirectional_iterator & operator--()
Definition: any_iterator.hpp:468
ystdex::any_ops::end_input_iterator_op
Definition: any_iterator.hpp:58
ystdex::any_ops::end_output_iterator_op
Definition: any_iterator.hpp:59
ystdex::details::any_base::get_storage
any_ops::any_storage & get_storage() const
Definition: any.h:736