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 
44 {
50 };
51 
52 
54 {
59 };
60 
61 
63 {
66 };
67 
68 
70 {
75 };
76 
77 
78 template<typename _type>
80 {
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
100  {
101  switch(op)
102  {
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>
121 {
122 public:
124  using value_type = typename base::value_type;
125 
126  using base::get_reference;
127 
128  using base::init;
129 
130  static void
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>
153 {
154 public:
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>
169 {
170 public:
172  using value_type = typename base::value_type;
173 
174  using base::get_reference;
175 
176  using base::init;
177 
178  static void
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;
255 
256  static_assert(is_convertible<decltype(*std::declval<typename
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
268  : any(static_cast<any&&>(i))
269  {}
270 #else
273 #endif
274 
276 
278  operator=(const any_input_iterator&) = default;
280 #if YB_IMPL_MSCPP
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 
298 
300  return reference(t.access<void_ref>());
301  }
302 
303  pointer
304  operator->() const
305  {
306  return &**this;
307  }
308 
311  {
312  yassume(manager);
314  return *this;
315  }
316 
318  any
319  get() const
320  {
321  return static_cast<const any&>(*this);
322  }
323 
325  bool
327  {
328  if(manager)
329  {
331 
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 
348 
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 
366 
370 
371 
376 template<typename _type, typename _tDifference = ptrdiff_t,
377  typename _tPointer = _type*, typename _tReference = _type&>
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
395  : any_input_iterator(static_cast<any_input_iterator&&>(i))
396  {}
397 #else
399 #endif
400 
402 
404  operator=(const any_forward_iterator&) = default;
406 #if YB_IMPL_MSCPP
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 
421  {
423  return *this;
424  }
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 
437 
440 
441 
446 template<typename _type, typename _tDifference = ptrdiff_t,
447  typename _tPointer = _type*, typename _tReference = _type&>
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>
459  : any_input_iterator<_type, _tPointer, _tReference>(yforward(i))
460  {}
462 #if YB_IMPL_MSCPP
465  : any_forward_iterator(static_cast<any_forward_iterator&&>(i))
466  {}
467 #else
469 #endif
470 
472 
474  operator=(const any_bidirectional_iterator&) = default;
476 #if YB_IMPL_MSCPP
479  {
480  static_cast<any_forward_iterator&>(*this)
481  = static_cast<any_forward_iterator&&>(i);
482  return *this;
483  }
484 #else
486 #endif
487 
488 
491  {
493  return *this;
494  }
496 
498  operator--()
499  {
500  yassume(this->manager);
501  this->manager(this->storage, this->storage, any_ops::decrease);
502  return *this;
503  }
505 };
506 
508 
510 
511 YB_IterOp2(operator!=, bool, any_bidirectional_iterator, !(x == y))
512 
516 
519 
520 
521 #undef YB_IterOp1
522 #undef YB_IterOp2
523 #undef YB_IterOpPost
524 
526 
527 } // namespace ystdex;
528 
529 #endif
530 
动态泛型前向迭代器。
基于类型擦除的动态泛型对象。
Definition: any.h:584
any_bidirectional_iterator & operator=(const any_bidirectional_iterator &)=default
remove_cv_t< remove_reference_t< _type >> type
Definition: type_op.hpp:659
任意对象引用类型。
Definition: any.h:127
typename conditional< _bCond, _type, _type2 >::type conditional_t
Definition: type_op.hpp:277
bool equals(const any_input_iterator &i) const
void * access()
Definition: any.h:97
any_input_iterator & operator++()
动态泛型双向迭代器。
any_forward_iterator & operator=(const any_forward_iterator &)=default
#define YB_IterOpPost(_op, _it)
typename wrapped_traits< _type >::type value_type
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
typename wrap_handler< _type >::type base
static void manage(any_storage &d, const any_storage &s, any_ops::op_code op)
static void manage(any_storage &d, const any_storage &s, any_ops::op_code op)
any_input_iterator(_tIterator &&i)
构造:使用现有迭代器。
bool check_undereferenceable() const
reference operator*() const
typename base::value_type value_type
#define yassume
假定:环境语义。
Definition: cassert.h:58
any_forward_iterator & operator++()
const std::type_info & type() const
Definition: any.cpp:92
通用迭代器。
any_ops::any_storage storage
Definition: any.h:589
pointer operator->() const
any_forward_iterator(_tIterator &&i)
conditional_t< wrapped_traits< _type >::value, ref_handler< value_type >, value_handler< value_type >> type
#define YB_IterOp2(_n, _t, _it, _e)
动态泛型输入迭代器。
any_input_iterator & operator=(const any_input_iterator &)=default
动态泛型类型。
typename base::value_type value_type
std::uint32_t op_code
Definition: any.h:301
bool is_undereferenceable(const any_input_iterator< _type, _tDifference, _tPointer, _tReference > &i)
any_ops::any_manager manager
Definition: any.h:590
static void manage(any_storage &d, const any_storage &s, any_ops::op_code op)
any()
Definition: any.h:596
std::forward_iterator_tag iterator_category
#define YB_IterOp1(_n, _t, _it, _e)
any_bidirectional_iterator & operator++()
函数和可调用对象。