YSTest: any_iterator.hpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
any_iterator.hpp
浏览该文件的文档.
1 /*
2 © 2012-2014 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"
32 #include "functional.hpp" // for ystdex::wrapped_traits;
33 #include "iterator.hpp" // for ystdex::is_undereferenceable;
34
35 namespace ystdex
36 {
37
38 namespace any_ops
39 {
40
42
43 enum iterator_op : op_code
44 {
46 check_undereferenceable = end_base_op,
47 dereference,
48 increase,
49 end_iterator_op
50 };
51
52
53 enum input_iterator_op : op_code
54 {
55 equals = end_iterator_op,
56 end_input_iterator_op,
57 end_output_iterator_op = end_input_iterator_op,
58 end_forward_iterator_op = end_input_iterator_op
59 };
60
61
62 enum bidirectional_iteartor_op : op_code
63 {
64 decrease = end_forward_iterator_op,
65 end_bidirectional_iterator_op
66 };
67
68
69 enum random_access_iteartor_op : op_code
70 {
71 advance = end_forward_iterator_op,
72 distance,
73 less_compare,
74 end_random_access_iteartor_op
75 };
76
77
78 template<typename _type>
79 struct wrap_handler
80 {
81 using value_type = typename wrapped_traits<_type>::type;
82 using type = conditional_t<wrapped_traits<_type>::value,
83 ref_handler<value_type>, value_handler<value_type>>;
84 };
85
86
87 template<typename _type>
88 class iterator_handler : public wrap_handler<_type>::type
89 {
90 public:
91 using base = typename wrap_handler<_type>::type;
92 using value_type = typename base::value_type;
93
94 using base::get_reference;
95
96 using base::init;
97
98 static void
99 manage(any_storage& d, const any_storage& s, any_ops::op_code op)
100 {
101 switch(op)
102 {
103 case check_undereferenceable:
104 d.access<bool>() = is_undereferenceable(get_reference(s));
105 break;
106 case dereference:
107 d = void_ref(*get_reference(s));
108 break;
109 case increase:
110 ++get_reference(d);
111 break;
112 default:
113 base::manage(d, s, op);
114 }
115 }
116 };
117
118
119 template<typename _type>
120 class input_iterator_handler : public iterator_handler<_type>
121 {
122 public:
123 using base = iterator_handler<_type>;
124 using value_type = typename base::value_type;
125
126 using base::get_reference;
127
128 using base::init;
129
130 static void
131 manage(any_storage& d, const any_storage& s, any_ops::op_code op)
132 {
133 switch(op)
134 {
135 case equals:
136 {
137 const auto p(d.access<any_storage*>());
138
139 d.access<bool>() = get_reference(*p) == get_reference(s);
140 }
141 break;
142 default:
143 base::manage(d, s, op);
144 }
145 }
146 };
148
149
151 template<typename _type>
152 class forward_iterator_handler : public input_iterator_handler<_type>
153 {
154 public:
155 using base = input_iterator_handler<_type>;
156 using value_type = typename base::value_type;
157
158 using base::get_reference;
159
160 using base::init;
161
162 using base::manage;
163 };
164
165
167 template<typename _type>
168 class bidirectional_iterator_handler : public forward_iterator_handler<_type>
169 {
170 public:
171 using base = forward_iterator_handler<_type>;
172 using value_type = typename base::value_type;
173
174 using base::get_reference;
175
176 using base::init;
177
178 static void
179 manage(any_storage& d, const any_storage& s, any_ops::op_code op)
180 {
181 switch(op)
182 {
183 case decrease:
184 --get_reference(d);
185 break;
186 default:
187 base::manage(d, s, op);
188 }
189 }
190 };
191
192 } // namespace any_ops;
193
194
196
197 #define YB_IterOp1(_n, _t, _it, _e) \
198 template<typename _type, typename _tDifference, typename _tPointer, \
199 typename _tReference> \
200 inline _t \
201 _n(const _it<_type, _tDifference, _tPointer, _tReference>& i) \
202 { \
203 return _e; \
204 }
205
206 #define YB_IterOp2(_n, _t, _it, _e) \
207 template<typename _type, typename _tDifference, typename _tPointer, \
208 typename _tReference> \
209 inline _t \
210 _n(const _it<_type, _tDifference, _tPointer, _tReference>& x, \
211 const _it<_type, _tDifference, _tPointer, _tReference>& y) \
212 { \
213 return _e; \
214 }
215
216 #define YB_IterOpPost(_op, _it) \
217 _it \
218 operator _op(int) \
219 { \
220 auto tmp = *this; \
221 \
222 _op *this; \
223 return tmp; \
224 }
225
226
228
229
234 template<typename _type, typename _tDifference = ptrdiff_t,
235 typename _tPointer = _type*, typename _tReference = _type&>
236 class any_input_iterator : public std::iterator<std::input_iterator_tag, _type,
237 _tDifference, _tPointer, _tReference>, protected any
238 {
239 public:
240 using pointer = _tPointer;
241 using reference = _tReference;
242
244 any_input_iterator() = default;
249 template<typename _tIterator>
250 any_input_iterator(_tIterator&& i)
251 : any()
252 {
253 using param_obj_type = typename remove_rcv<_tIterator>::type;
254 using handler = any_ops::input_iterator_handler<param_obj_type>;
255
256 static_assert(is_convertible<decltype(*std::declval<typename
257 wrapped_traits<param_obj_type>::type&>()), reference>::value,
258 "Wrong target iterator type found.");
259
260 manager = handler::manage;
261 handler::init(storage, yforward(i));
262 }
264 any_input_iterator(const any_input_iterator&) = default;
265 #if YB_IMPL_MSCPP
266 any_input_iterator(any_input_iterator&& i)
268 : any(static_cast<any&&>(i))
269 {}
270 #else
271 any_input_iterator(any_input_iterator&&) = default;
273 #endif
274
276
277 any_input_iterator&
278 operator=(const any_input_iterator&) = default;
279 any_input_iterator&
280 #if YB_IMPL_MSCPP
281 operator=(any_input_iterator&& i)
283 {
284 static_cast< any&>(*this) = static_cast< any&&>(i);
285 return *this;
286 }
287 #else
288 operator=(any_input_iterator&&) = default;
289 #endif
290
291
292 reference
293 operator*() const
294 {
295 yassume(manager);
296
297 any_ops::any_storage t;
298
299 manager(t, storage, any_ops::dereference);
300 return reference(t.access<void_ref>());
301 }
302
303 pointer
304 operator->() const
305 {
306 return &**this;
307 }
308
309 any_input_iterator&
310 operator++()
311 {
312 yassume(manager);
313 manager(storage, storage, any_ops::increase);
314 return *this;
315 }
316
318 any
319 get() const
320 {
321 return static_cast<const any&>(*this);
322 }
323
325 bool
326 check_undereferenceable() const
327 {
328 if(manager)
329 {
330 any_ops::any_storage t;
331
332 manager(t, storage, any_ops::check_undereferenceable);
333 return t.access<bool>();
334 }
335 return true;
336 }
337
339
340 bool
341 equals(const any_input_iterator& i) const
342 {
343 if(!*this && !i)
344 return true;
345 yassume(type() == i.type());
346
347 any_ops::any_storage t(&storage);
348
349 manager(t, i.storage, any_ops::equals);
350 return t.access<bool>();
351 }
352
353 using any::type;
355 };
356
358
359 YB_IterOp2(operator==, bool, any_input_iterator, x.equals(y))
360
361 YB_IterOp2(operator!=, bool, any_input_iterator, !(x == y))
362
363 YB_IterOp1(is_undereferenceable, bool, any_input_iterator,
364 i.check_undereferenceable())
366
367 using input_monomorphic_iterator
368 = any_input_iterator<void_ref, ptrdiff_t, void*, void_ref>;
370
371
376 template<typename _type, typename _tDifference = ptrdiff_t,
377 typename _tPointer = _type*, typename _tReference = _type&>
378 class any_forward_iterator
379 : public any_input_iterator<_type, _tDifference, _tPointer, _tReference>
380 {
381 public:
382 using iterator_category = std::forward_iterator_tag;
383 using pointer = _tPointer;
384 using reference = _tReference;
385
386 any_forward_iterator() = default;
387 template<typename _tIterator>
388 any_forward_iterator(_tIterator&& i)
389 : any_input_iterator<_type, _tPointer, _tReference>(yforward(i))
390 {}
391 any_forward_iterator(const any_forward_iterator&) = default;
392 #if YB_IMPL_MSCPP
393 any_forward_iterator(any_forward_iterator&& i)
395 : any_input_iterator(static_cast<any_input_iterator&&>(i))
396 {}
397 #else
398 any_forward_iterator(any_forward_iterator&&) = default;
399 #endif
400
402
403 any_forward_iterator&
404 operator=(const any_forward_iterator&) = default;
405 any_forward_iterator&
406 #if YB_IMPL_MSCPP
407 operator=(any_forward_iterator&& i)
409 {
410 static_cast< any_input_iterator&>(*this)
411 = static_cast< any_input_iterator&&>(i);
412 return *this;
413 }
414 #else
415 operator=(any_forward_iterator&&) = default;
416 #endif
417
418
419 any_forward_iterator&
420 operator++()
421 {
422 any_input_iterator<_type, _tPointer, _tReference>::operator++();
423 return *this;
424 }
425 YB_IterOpPost(++, any_forward_iterator)
426 };
427
429
430 YB_IterOp2(operator==, bool, any_forward_iterator, x.equals(y))
431
432 YB_IterOp2(operator!=, bool, any_forward_iterator, !(x == y))
433
434 YB_IterOp1(is_undereferenceable, bool, any_forward_iterator,
435 i.check_undereferenceable())
437
438 using forward_monomorphic_iterator
439 = any_forward_iterator<void_ref, ptrdiff_t, void*, void_ref>;
440
441
446 template<typename _type, typename _tDifference = ptrdiff_t,
447 typename _tPointer = _type*, typename _tReference = _type&>
448 class any_bidirectional_iterator
449 : public any_forward_iterator<_type, _tDifference, _tPointer, _tReference>
450 {
451 public:
452 using iterator_category = std::bidirectional_iterator_tag;
453 using pointer = _tPointer;
454 using reference = _tReference;
455
456 any_bidirectional_iterator() = default;
457 template<typename _tIterator>
458 any_bidirectional_iterator(_tIterator&& i)
459 : any_input_iterator<_type, _tPointer, _tReference>(yforward(i))
460 {}
461 any_bidirectional_iterator(const any_bidirectional_iterator&) = default;
462 #if YB_IMPL_MSCPP
463 any_bidirectional_iterator(any_bidirectional_iterator&& i)
465 : any_forward_iterator(static_cast<any_forward_iterator&&>(i))
466 {}
467 #else
468 any_bidirectional_iterator(any_bidirectional_iterator&&) = default;
469 #endif
470
472
473 any_bidirectional_iterator&
474 operator=(const any_bidirectional_iterator&) = default;
475 any_bidirectional_iterator&
476 #if YB_IMPL_MSCPP
477 operator=(any_bidirectional_iterator&& i)
479 {
480 static_cast< any_forward_iterator&>(*this)
481 = static_cast< any_forward_iterator&&>(i);
482 return *this;
483 }
484 #else
485 operator=(any_bidirectional_iterator&&) = default;
486 #endif
487
488
489 any_bidirectional_iterator&
490 operator++()
491 {
492 any_forward_iterator<_type, _tPointer, _tReference>::operator++();
493 return *this;
494 }
495 YB_IterOpPost(++, any_bidirectional_iterator)
496
497 any_bidirectional_iterator&
498 operator--()
499 {
500 yassume(this->manager);
501 this->manager(this->storage, this->storage, any_ops::decrease);
502 return *this;
503 }
504 YB_IterOpPost(--, any_bidirectional_iterator)
505 };
506
508
509 YB_IterOp2(operator==, bool, any_bidirectional_iterator, x.equals(y))
510
511 YB_IterOp2(operator!=, bool, any_bidirectional_iterator, !(x == y))
512
513 YB_IterOp1(is_undereferenceable, bool, any_bidirectional_iterator,
514 i.check_undereferenceable())
516
517 using bidirectional_monomorphic_iterator
518 = any_bidirectional_iterator<void_ref, ptrdiff_t, void*, void_ref>;
519
520
521 #undef YB_IterOp1
522 #undef YB_IterOp2
523 #undef YB_IterOpPost
524
526
527 } // namespace ystdex;
528
529 #endif
530
ystdex::any_forward_iterator
动态泛型前向迭代器。
Definition: any_iterator.hpp:378
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:584
ystdex::any_ops::equals
Definition: any_iterator.hpp:55
ystdex::any_bidirectional_iterator::operator=
any_bidirectional_iterator & operator=(const any_bidirectional_iterator &)=default
ystdex::remove_rcv::type
remove_cv_t< remove_reference_t< _type >> type
Definition: type_op.hpp:659
ystdex::any_ops::dereference
Definition: any_iterator.hpp:47
ystdex::any_input_iterator::any_input_iterator
any_input_iterator()=default
ystdex::void_ref
任意对象引用类型。
Definition: any.h:127
ystdex::conditional_t
typename conditional< _bCond, _type, _type2 >::type conditional_t
Definition: type_op.hpp:277
ystdex::any_input_iterator::equals
bool equals(const any_input_iterator &i) const
Definition: any_iterator.hpp:341
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator()=default
ystdex::pod_storage< non_aggregate_pod >
ystdex::pod_storage::access
void * access()
Definition: any.h:97
ystdex::any_input_iterator::operator++
any_input_iterator & operator++()
Definition: any_iterator.hpp:310
ystdex::any_bidirectional_iterator
动态泛型双向迭代器。
Definition: any_iterator.hpp:448
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator(_tIterator &&i)
Definition: any_iterator.hpp:458
ystdex::any_forward_iterator::operator=
any_forward_iterator & operator=(const any_forward_iterator &)=default
YB_IterOpPost
#define YB_IterOpPost(_op, _it)
Definition: any_iterator.hpp:216
ystdex::any_ops::end_forward_iterator_op
Definition: any_iterator.hpp:58
ystdex::any_ops::end_random_access_iteartor_op
Definition: any_iterator.hpp:74
ystdex::any_ops::input_iterator_handler
Definition: any_iterator.hpp:120
ystdex::any_ops::wrap_handler::value_type
typename wrapped_traits< _type >::type value_type
Definition: any_iterator.hpp:81
ystdex::any_input_iterator::reference
_tReference reference
Definition: any_iterator.hpp:241
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
ystdex::any_ops::bidirectional_iteartor_op
bidirectional_iteartor_op
Definition: any_iterator.hpp:62
ystdex::any_ops::forward_iterator_handler
Definition: any_iterator.hpp:152
ystdex::any_ops::iterator_handler::base
typename wrap_handler< _type >::type base
Definition: any_iterator.hpp:91
ystdex::any_ops::input_iterator_handler::manage
static void manage(any_storage &d, const any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:131
ystdex::any_ops::bidirectional_iterator_handler::manage
static void manage(any_storage &d, const any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:179
ystdex::any_input_iterator::any_input_iterator
any_input_iterator(_tIterator &&i)
构造：使用现有迭代器。
Definition: any_iterator.hpp:250
ystdex::any_input_iterator::check_undereferenceable
bool check_undereferenceable() const
Definition: any_iterator.hpp:326
ystdex::any_ops::input_iterator_op
input_iterator_op
Definition: any_iterator.hpp:53
ystdex::any_input_iterator::operator*
reference operator*() const
Definition: any_iterator.hpp:293
ystdex::any_ops::iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:92
ystdex::any_input_iterator::pointer
_tPointer pointer
Definition: any_iterator.hpp:240
ystdex::any_ops::iterator_op
iterator_op
Definition: any_iterator.hpp:43
yassume
#define yassume
假定：环境语义。
Definition: cassert.h:58
ystdex::any_ops::less_compare
Definition: any_iterator.hpp:73
ystdex::any_forward_iterator::operator++
any_forward_iterator & operator++()
Definition: any_iterator.hpp:420
ystdex::any::type
const std::type_info & type() const
Definition: any.cpp:92
ystdex::any_ops::end_output_iterator_op
Definition: any_iterator.hpp:57
ystdex::any_ops::iterator_handler
Definition: any_iterator.hpp:88
ystdex::any_ops::wrap_handler
Definition: any_iterator.hpp:79
ystdex::any_ops::end_bidirectional_iterator_op
Definition: any_iterator.hpp:65
iterator.hpp
通用迭代器。
ystdex::any_ops::random_access_iteartor_op
random_access_iteartor_op
Definition: any_iterator.hpp:69
ystdex::any::storage
any_ops::any_storage storage
Definition: any.h:589
ystdex::any_ops::advance
Definition: any_iterator.hpp:71
ystdex::any_input_iterator::operator->
pointer operator->() const
Definition: any_iterator.hpp:304
ystdex::any_ops::decrease
Definition: any_iterator.hpp:64
ystdex::any_forward_iterator::any_forward_iterator
any_forward_iterator(_tIterator &&i)
Definition: any_iterator.hpp:388
ystdex::any_ops::wrap_handler::type
conditional_t< wrapped_traits< _type >::value, ref_handler< value_type >, value_handler< value_type >> type
Definition: any_iterator.hpp:83
ystdex::any_ops::check_undereferenceable
Definition: any_iterator.hpp:46
YB_IterOp2
#define YB_IterOp2(_n, _t, _it, _e)
Definition: any_iterator.hpp:206
ystdex::any_input_iterator
动态泛型输入迭代器。
Definition: any_iterator.hpp:236
ystdex::any_input_iterator::operator=
any_input_iterator & operator=(const any_input_iterator &)=default
ystdex::wrapped_traits::type
_type type
Definition: functional.hpp:427
any.h
动态泛型类型。
ystdex::any_ops::input_iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:124
ystdex::any_ops::op_code
std::uint32_t op_code
Definition: any.h:301
ystdex::any_ops::forward_iterator_handler::value_type
typename base::value_type value_type
Definition: any_iterator.hpp:156
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
ystdex::any_ops::bidirectional_iterator_handler
Definition: any_iterator.hpp:168
ystdex::any_ops::increase
Definition: any_iterator.hpp:48
ystdex::is_undereferenceable
bool is_undereferenceable(const any_input_iterator< _type, _tDifference, _tPointer, _tReference > &i)
Definition: any_iterator.hpp:364
ystdex::any::manager
any_ops::any_manager manager
Definition: any.h:590
ystdex::any_ops::iterator_handler::manage
static void manage(any_storage &d, const any_storage &s, any_ops::op_code op)
Definition: any_iterator.hpp:99
ystdex::any_ops::end_base_op
Definition: any.h:315
ystdex::any_bidirectional_iterator::any_bidirectional_iterator
any_bidirectional_iterator()=default
ystdex::any::any
any()
Definition: any.h:596
ystdex::any_forward_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: any_iterator.hpp:382
YB_IterOp1
#define YB_IterOp1(_n, _t, _it, _e)
Definition: any_iterator.hpp:197
ystdex::any_ops::end_input_iterator_op
Definition: any_iterator.hpp:56
ystdex::any_bidirectional_iterator::operator++
any_bidirectional_iterator & operator++()
Definition: any_iterator.hpp:490
ystdex::any_ops::distance
Definition: any_iterator.hpp:72
functional.hpp
函数和可调用对象。
ystdex::any_ops::end_iterator_op
Definition: any_iterator.hpp:49