YSTest: pointer.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

pointer.hpp
浏览该文件的文档.
1 /*
2 © 2012-2017 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
30 #ifndef YB_INC_ystdex_pointer_hpp_
31 #define YB_INC_ystdex_pointer_hpp_ 1
32
33 #include "iterator_op.hpp" // for bool_, true_, totally_ordered,
34 // nullptr_t, equality_comparable, add_ptr_t, add_ref_t, yconstraint,
35 // iterator_operators_t, std::iterator_traits;
36 #include <functional> // for std::equal_to, std::less;
37
38 namespace ystdex
39 {
40
42 namespace details
43 {
44
45 template<typename _type>
46 using nptr_eq1 = bool_<_type() == _type()>;
47 template<typename _type>
48 using nptr_eq2 = bool_<_type(nullptr) == nullptr>;
49
50 } // namespace details;
51
53
54
59 template<typename _type>
60 class nptr : private totally_ordered<nptr<_type>>
61 {
63 static_assert(is_nothrow_copyable<_type>(), "Invalid type found.");
64 static_assert(is_destructible<_type>(), "Invalid type found.");
65 static_assert(detected_or_t<true_, details::nptr_eq1, _type>(),
66 "Invalid type found.");
67 static_assert(detected_or_t<true_, details::nptr_eq2, _type>(),
68 "Invalid type found.");
69
70 public:
71 using pointer = _type;
72
73 private:
74 pointer ptr{};
75
76 public:
77 nptr() = default;
79
80 yconstfn
81 nptr(std::nullptr_t) ynothrow
82 : nptr()
83 {}
84 nptr(pointer p) ynothrow
85 : ptr(p)
86 {}
88 nptr(const nptr&) = default;
89 nptr(nptr&& np) ynothrow
90 {
91 np.swap(*this);
92 }
93
94 nptr&
95 operator=(const nptr&) = default;
96 nptr&
97 operator=(nptr&& np) ynothrow
98 {
99 np.swap(*this);
100 return *this;
101 }
102
103 yconstfn bool
104 operator!() const ynothrow
105 {
106 return bool(*this);
107 }
108
110
111
113 yconstfn_relaxed auto
114 operator*() ynothrow -> decltype(*ptr)
115 {
116 return *ptr;
117 }
118 yconstfn auto
119 operator*() const ynothrow -> decltype(*ptr)
120 {
121 return *ptr;
122 }
124
125 yconstfn_relaxed pointer&
126 operator->() ynothrow
127 {
128 return ptr;
129 }
130 yconstfn const pointer&
131 operator->() const ynothrow
132 {
133 return ptr;
134 }
136
138 friend yconstfn bool
139 operator==(const nptr& x, const nptr& y) ynothrow
140 {
141 return std::equal_to<pointer>()(x.ptr, y.ptr);
142 }
143
145 friend yconstfn bool
146 operator<(const nptr& x, const nptr& y) ynothrow
147 {
148 return std::less<pointer>()(x.ptr, y.ptr);
149 }
150
152 explicit yconstfn
153 operator bool() const ynothrow
154 {
155 return bool(ptr);
156 }
157
159 yconstfn const pointer&
160 get() const ynothrow
161 {
162 return ptr;
163 }
164
165 yconstfn_relaxed pointer&
166 get_ref() ynothrow
167 {
168 return ptr;
169 }
170
172 void
173 swap(nptr& np) ynothrow
174 {
175 using std::swap;
176
177 swap(ptr, np.ptr);
178 }
179 };
180
185 template<typename _type>
186 inline void
187 swap(nptr<_type>& x, nptr<_type>& y) ynothrow
188 {
189 x.swap(y);
190 }
192
193
195
196
200 template<typename _type>
201 class observer_ptr : private totally_ordered<observer_ptr<_type>>,
202 private equality_comparable<observer_ptr<_type>, nullptr_t>
203 {
204 public:
205 using element_type = _type;
206 using pointer = yimpl(add_ptr_t<_type>);
207 using reference = yimpl(add_ref_t<_type>);
208
209 private:
210 _type* ptr{};
211
212 public:
214
215 yconstfn
216 observer_ptr() ynothrow yimpl(= default);
217 yconstfn
218 observer_ptr(nullptr_t) ynothrow
219 : ptr()
220 {}
222 explicit yconstfn
223 observer_ptr(pointer p) ynothrow
224 : ptr(p)
225 {}
226 template<typename _tOther>
227 yconstfn
228 observer_ptr(observer_ptr<_tOther> other) ynothrow
229 : ptr(other.get())
230 {}
231
233 yconstfn reference
234 operator*() const ynothrowv
235 {
236 return yconstraint(get() != nullptr), *ptr;
237 }
238
239 yconstfn pointer
240 operator->() const ynothrow
241 {
242 return ptr;
243 }
244
246 friend yconstfn bool
247 operator==(observer_ptr p, nullptr_t) ynothrow
248 {
249 return !p.ptr;
250 }
251
252 explicit yconstfn
253 operator bool() const ynothrow
254 {
255 return ptr;
256 }
257
258 explicit yconstfn
259 operator pointer() const ynothrow
260 {
261 return ptr;
262 }
263
264 yconstfn pointer
265 get() const ynothrow
266 {
267 return ptr;
268 }
269
270 yconstfn_relaxed pointer
271 release() ynothrow
272 {
273 const auto res(ptr);
274
275 reset();
276 return res;
277 }
278
279 yconstfn_relaxed void
280 reset(pointer p = {}) ynothrow
281 {
282 ptr = p;
283 }
284
285 yconstfn_relaxed void
286 swap(observer_ptr& other) ynothrow
287 {
288 std::swap(ptr, other.ptr);
289 }
290 };
291
293
294
296 template<typename _type1, typename _type2>
297 yconstfn bool
298 operator==(observer_ptr<_type1> p1, observer_ptr<_type2> p2) ynothrowv
299 {
300 return p1.get() == p2.get();
301 }
302
303 template<typename _type1, typename _type2>
304 yconstfn bool
305 operator!=(observer_ptr<_type1> p1, observer_ptr<_type2> p2) ynothrowv
306 {
307 return !(p1 == p2);
308 }
309
310 template<typename _type1, typename _type2>
311 yconstfn bool
312 operator<(observer_ptr<_type1> p1, observer_ptr<_type2> p2) ynothrowv
313 {
314 return std::less<common_type_t<_type1, _type2>>(p1.get(), p2.get());
315 }
317
318 template<typename _type>
319 inline void
320 swap(observer_ptr<_type>& p1, observer_ptr<_type>& p2) ynothrow
321 {
322 p1.swap(p2);
323 }
324 template<typename _type>
325 inline observer_ptr<_type>
326 make_observer(_type* p) ynothrow
327 {
328 return observer_ptr<_type>(p);
329 }
331
332
333
335 template<typename _type>
336 using tidy_ptr = nptr<observer_ptr<_type>>;
337
338
349 template<typename _type>
350 class pointer_iterator : public iterator_operators_t<pointer_iterator<_type>,
351 std::iterator_traits<_type*>>
352 {
353 public:
354 using iterator_type = _type*;
355 using iterator_category
356 = typename std::iterator_traits<iterator_type>::iterator_category;
357 using value_type = typename std::iterator_traits<iterator_type>::value_type;
358 using difference_type
359 = typename std::iterator_traits<iterator_type>::difference_type;
360 using pointer = typename std::iterator_traits<iterator_type>::pointer;
361 using reference = typename std::iterator_traits<iterator_type>::reference;
362
363 protected:
365 pointer raw;
366
367 public:
368 yconstfn
369 pointer_iterator(nullptr_t = {})
370 : raw()
371 {}
373 template<typename _tPointer>
374 yconstfn
375 pointer_iterator(_tPointer&& ptr)
376 : raw(yforward(ptr))
377 {}
378 inline
379 pointer_iterator(const pointer_iterator&) = default;
380
382
383 yconstfn_relaxed pointer_iterator&
384 operator+=(difference_type n) ynothrowv
385 {
386 yconstraint(raw);
387 raw += n;
388 return *this;
389 }
390
391 yconstfn_relaxed pointer_iterator&
392 operator-=(difference_type n) ynothrowv
393 {
394 yconstraint(raw);
395 raw -= n;
396 return *this;
397 }
398
400 yconstfn reference
401 operator*() const ynothrowv
402 {
403 return yconstraint(raw), *raw;
404 }
405
406 yconstfn_relaxed pointer_iterator&
407 operator++() ynothrowv
408 {
409 yconstraint(raw);
410 ++raw;
411 return *this;
412 }
413
414 yconstfn_relaxed pointer_iterator&
415 operator--() ynothrowv
416 {
417 --raw;
418 return *this;
419 }
420
422 friend yconstfn bool
423 operator==(const pointer_iterator& x, const pointer_iterator& y) ynothrow
424 {
425 return x.raw == y.raw;
426 }
427
429 friend yconstfn bool
430 operator<(const pointer_iterator& x, const pointer_iterator& y) ynothrow
431 {
432 return x.raw < y.raw;
433 }
434
435 yconstfn
436 operator pointer() const ynothrow
437 {
438 return raw;
439 }
441 };
442
443
452 template<typename _type>
453 struct pointer_classify
454 {
455 using type = _type;
456 };
457
458 template<typename _type>
459 struct pointer_classify<_type*>
460 {
461 using type = pointer_iterator<_type>;
462 };
464
465 } // namespace ystdex;
466
467 #endif
468
ystdex::observer_ptr::release
pointer release()
Definition: pointer.hpp:271
ystdex::nptr::operator->
pointer & operator->()
Definition: pointer.hpp:126
ystdex::add_ref_t
_type & add_ref_t
Definition: type_traits.hpp:1024
ystdex::pointer_iterator::iterator_type
_type * iterator_type
Definition: pointer.hpp:354
ystdex::details::nptr_eq1
bool_< _type()==_type()> nptr_eq1
Definition: pointer.hpp:46
ystdex::observer_ptr::pointer
add_ptr_t< _type > pointer
Definition: pointer.hpp:206
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::observer_ptr::observer_ptr
observer_ptr(pointer p)
Definition: pointer.hpp:223
ystdex::observer_ptr::swap
void swap(observer_ptr &other)
Definition: pointer.hpp:286
ystdex::iterator_operators_t
_t< details::iterator_operators< _tIter, _tTraits >> iterator_operators_t
按迭代器类别取可实现迭代器的重载操作符集合的实现。
Definition: iterator_op.hpp:191
ystdex::pointer_iterator::iterator_category
typename std::iterator_traits< iterator_type >::iterator_category iterator_category
Definition: pointer.hpp:356
ystdex::dep_ops::totally_ordered
details::flat_ops< _type, _type2, _tOpt, less_than_comparable, equality_comparable > totally_ordered
Definition: operators.hpp:367
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: pointer.hpp:326
ystdex::swap
void swap(observer_ptr< _type > &p1, observer_ptr< _type > &p2)
Definition: pointer.hpp:320
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::details::nptr_eq2
bool_< _type(nullptr)==nullptr > nptr_eq2
Definition: pointer.hpp:48
ystdex::nptr::operator*
auto operator*() const -> decltype(*ptr)
Definition: pointer.hpp:119
ystdex::nptr::operator=
nptr & operator=(nptr &&np)
Definition: pointer.hpp:97
ystdex::pointer_iterator::pointer_iterator
pointer_iterator(_tPointer &&ptr)
Definition: pointer.hpp:375
ystdex::pointer_iterator::operator<
friend bool operator<(const pointer_iterator &x, const pointer_iterator &y)
Definition: pointer.hpp:430
ystdex::nptr::operator=
nptr & operator=(const nptr &)=default
ystdex::add_ptr_t
_type * add_ptr_t
Definition: type_traits.hpp:1021
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1280
ystdex::pointer_iterator::operator==
friend bool operator==(const pointer_iterator &x, const pointer_iterator &y)
Definition: pointer.hpp:423
ystdex::nptr::operator->
const pointer & operator->() const
Definition: pointer.hpp:131
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:692
ystdex::nptr::ptr
pointer ptr
Definition: pointer.hpp:74
ystdex::observer_ptr::observer_ptr
observer_ptr()=default
ystdex::pointer_iterator::pointer_iterator
pointer_iterator(nullptr_t={})
Definition: pointer.hpp:369
ystdex::nptr::swap
void swap(nptr &np)
Definition: pointer.hpp:173
ystdex::pointer_iterator::operator--
pointer_iterator & operator--()
Definition: pointer.hpp:415
ystdex::observer_ptr::operator*
reference operator*() const
Definition: pointer.hpp:234
ystdex::pointer_iterator::reference
typename std::iterator_traits< iterator_type >::reference reference
Definition: pointer.hpp:361
ystdex::operator!=
bool operator!=(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:908
ystdex::observer_ptr::reset
void reset(pointer p={})
Definition: pointer.hpp:280
ystdex::detected_or_t
_t< detected_or< _tDefault, _gOp, _tParams... >> detected_or_t
Definition: type_traits.hpp:719
ystdex::dep_ops::equality_comparable
_t< details::ops_seq< _type, _type2, _tOpt, cond_t< is_same< _type, _type2 >, index_sequence< 7 >, index_sequence< 6, 7, 8 >>>> equality_comparable
Definition: operators.hpp:243
ystdex::nptr::nptr
nptr()=default
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex::pointer_iterator::raw
pointer raw
Definition: pointer.hpp:365
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::pointer_iterator::pointer
typename std::iterator_traits< iterator_type >::pointer pointer
Definition: pointer.hpp:360
ystdex::nptr::nptr
nptr(pointer p)
Definition: pointer.hpp:84
ystdex::nptr::operator==
friend bool operator==(const nptr &x, const nptr &y)
Definition: pointer.hpp:139
ystdex::nptr::operator!
bool operator!() const
Definition: pointer.hpp:104
ystdex::operator==
bool operator==(nullptr_t lhs, const _type &rhs)
Definition: ydef.h:895
ystdex::pointer_iterator::operator+=
pointer_iterator & operator+=(difference_type n)
Definition: pointer.hpp:384
ystdex::observer_ptr::element_type
_type element_type
Definition: pointer.hpp:205
ystdex::nptr::operator<
friend bool operator<(const nptr &x, const nptr &y)
Definition: pointer.hpp:146
YSLib::Drawing::p1
The boundary is out of the const Graphics const Rect const Point & p1
Definition: YDraw.h:161
ystdex::nptr::nptr
nptr(std::nullptr_t)
Definition: pointer.hpp:81
ystdex::pointer_classify
指针包装为类类型迭代器。
Definition: pointer.hpp:453
ystdex::observer_ptr::observer_ptr
observer_ptr(observer_ptr< _tOther > other)
Definition: pointer.hpp:228
ystdex::nptr::operator*
auto operator*() -> decltype(*ptr)
Definition: pointer.hpp:114
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
ystdex::pointer_iterator::operator-=
pointer_iterator & operator-=(difference_type n)
Definition: pointer.hpp:392
CHRLib::CharSet::other
Definition: Encoding.h:65
ystdex::nullptr_t
空指针类。
Definition: ydef.h:861
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::nptr::get_ref
pointer & get_ref()
Definition: pointer.hpp:166
iterator_op.hpp
迭代器操作。
ystdex::is_nothrow_copyable
Definition: type_traits.hpp:929
ystdex::pointer_iterator::difference_type
typename std::iterator_traits< iterator_type >::difference_type difference_type
Definition: pointer.hpp:359
ystdex::observer_ptr::ptr
_type * ptr
Definition: pointer.hpp:210
ystdex::pointer_iterator
指针迭代器。
Definition: pointer.hpp:350
ystdex::nptr
可空指针包装：满足 NullablePointer 要求同时满足转移后为空。
Definition: pointer.hpp:60
ystdex::nptr::pointer
_type pointer
Definition: pointer.hpp:71
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::pointer_classify::type
_type type
Definition: pointer.hpp:455
ystdex::observer_ptr::reference
add_ref_t< _type > reference
Definition: pointer.hpp:207
ystdex::observer_ptr
观察者指针：无所有权的智能指针。
Definition: pointer.hpp:201
ystdex::observer_ptr::operator==
friend bool operator==(observer_ptr p, nullptr_t)
Definition: pointer.hpp:247
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:88
ystdex::pointer_iterator::operator*
reference operator*() const
Definition: pointer.hpp:401
ystdex::nptr::nptr
nptr(nptr &&np)
Definition: pointer.hpp:89
ystdex::observer_ptr::operator->
pointer operator->() const
Definition: pointer.hpp:240
ystdex::pointer_iterator::operator++
pointer_iterator & operator++()
Definition: pointer.hpp:407
ystdex::bool_
std::integral_constant< bool, _vInt > bool_
Definition: type_traits.hpp:375
ystdex::pointer_iterator::value_type
typename std::iterator_traits< iterator_type >::value_type value_type
Definition: pointer.hpp:357
ystdex::nptr::swap
void swap(nptr< _type > &x, nptr< _type > &y)
Definition: pointer.hpp:187
CHRLib::p
const auto p(d)
YSLib::Drawing::p2
The boundary is out of the const Graphics const Rect const Point const Point & p2
Definition: YDraw.h:161