YSTest: pointer.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

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

- 文件列表
- 文件成员

pointer.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
30 #ifndef YB_INC_ystdex_pointer_hpp_
31 #define YB_INC_ystdex_pointer_hpp_ 1
32
33 #include "iterator_op.hpp" // for totally_ordered,
34 // iterator_operators_t, std::iterator_traits, yconstraint;
35 #include <functional> // for std::equal_to, std::less;
36
37 namespace ystdex
38 {
39
41 namespace details
42 {
43
44 template<typename _type>
45 using nptr_eq1 = bool_constant<_type() == _type()>;
46 template<typename _type>
47 using nptr_eq2 = bool_constant<_type(nullptr) == nullptr>;
48
49 } // namespace details;
50
52
53
58 template<typename _type>
59 class nptr : private totally_ordered<nptr<_type>>
60 {
62 static_assert(is_nothrow_copyable<_type>(), "Invalid type found.");
63 static_assert(is_destructible<_type>(), "Invalid type found.");
64 static_assert(detected_or_t<true_type, details::nptr_eq1, _type>(),
65 "Invalid type found.");
66 static_assert(detected_or_t<true_type, details::nptr_eq2, _type>(),
67 "Invalid type found.");
68
69 public:
70 using pointer = _type;
71
72 private:
73 pointer ptr{};
74
75 public:
76 nptr() = default;
78
79 yconstfn
80 nptr(std::nullptr_t) ynothrow
81 : nptr()
82 {}
83 nptr(pointer p) ynothrow
84 : ptr(p)
85 {}
87 nptr(const nptr&) = default;
88 nptr(nptr&& np) ynothrow
89 {
90 np.swap(*this);
91 }
92
93 nptr&
94 operator=(const nptr&) = default;
95 nptr&
96 operator=(nptr&& np) ynothrow
97 {
98 np.swap(*this);
99 return *this;
100 }
101
102 yconstfn bool
103 operator!() const ynothrow
104 {
105 return bool(*this);
106 }
107
109
110
112 yconstfn_relaxed auto
113 operator*() ynothrow -> decltype(*ptr)
114 {
115 return *ptr;
116 }
117 yconstfn auto
118 operator*() const ynothrow -> decltype(*ptr)
119 {
120 return *ptr;
121 }
123
124 yconstfn_relaxed pointer&
125 operator->() ynothrow
126 {
127 return ptr;
128 }
129 yconstfn const pointer&
130 operator->() const ynothrow
131 {
132 return ptr;
133 }
135
137 friend yconstfn bool
138 operator==(const nptr& x, const nptr& y) ynothrow
139 {
140 return std::equal_to<pointer>()(x.ptr, y.ptr);
141 }
142
144 friend yconstfn bool
145 operator<(const nptr& x, const nptr& y) ynothrow
146 {
147 return std::less<pointer>()(x.ptr, y.ptr);
148 }
149
151 explicit yconstfn
152 operator bool() const ynothrow
153 {
154 return bool(ptr);
155 }
156
158 yconstfn const pointer&
159 get() const ynothrow
160 {
161 return ptr;
162 }
163
164 yconstfn_relaxed pointer&
165 get_ref() ynothrow
166 {
167 return ptr;
168 }
169
171 void
172 swap(nptr& np) ynothrow
173 {
174 using std::swap;
175
176 swap(ptr, np.ptr);
177 }
178 };
179
184 template<typename _type>
185 inline void
186 swap(nptr<_type>& x, nptr<_type>& y) ynothrow
187 {
188 x.swap(y);
189 }
191
192
203 template<typename _type>
204 class pointer_iterator : public iterator_operators_t<pointer_iterator<_type>,
205 std::iterator_traits<_type*>>
206 {
207 public:
208 using iterator_type = _type*;
209 using iterator_category
210 = typename std::iterator_traits<iterator_type>::iterator_category;
211 using value_type = typename std::iterator_traits<iterator_type>::value_type;
212 using difference_type
213 = typename std::iterator_traits<iterator_type>::difference_type;
214 using pointer = typename std::iterator_traits<iterator_type>::pointer;
215 using reference = typename std::iterator_traits<iterator_type>::reference;
216
217 protected:
219 pointer raw;
220
221 public:
222 yconstfn
223 pointer_iterator(nullptr_t = {})
224 : raw()
225 {}
227 template<typename _tPointer>
228 yconstfn
229 pointer_iterator(_tPointer&& ptr)
230 : raw(yforward(ptr))
231 {}
232 inline
233 pointer_iterator(const pointer_iterator&) = default;
234
236
237 yconstfn_relaxed pointer_iterator&
238 operator+=(difference_type n) ynothrowv
239 {
240 yconstraint(raw);
241 raw += n;
242 return *this;
243 }
244
245 yconstfn_relaxed pointer_iterator&
246 operator-=(difference_type n) ynothrowv
247 {
248 yconstraint(raw);
249 raw -= n;
250 return *this;
251 }
252
254 yconstfn reference
255 operator*() const ynothrowv
256 {
257 return yconstraint(raw), *raw;
258 }
259
260 yconstfn_relaxed pointer_iterator&
261 operator++() ynothrowv
262 {
263 yconstraint(raw);
264 ++raw;
265 return *this;
266 }
267
268 yconstfn_relaxed pointer_iterator&
269 operator--() ynothrowv
270 {
271 --raw;
272 return *this;
273 }
274
276 friend yconstfn bool
277 operator==(const pointer_iterator& x, const pointer_iterator& y) ynothrow
278 {
279 return x.raw == y.raw;
280 }
281
283 friend yconstfn bool
284 operator<(const pointer_iterator& x, const pointer_iterator& y) ynothrow
285 {
286 return x.raw < y.raw;
287 }
288
289 yconstfn
290 operator pointer() const ynothrow
291 {
292 return raw;
293 }
295 };
296
297
306 template<typename _type>
307 struct pointer_classify
308 {
309 using type = _type;
310 };
311
312 template<typename _type>
313 struct pointer_classify<_type*>
314 {
315 using type = pointer_iterator<_type>;
316 };
318
319 } // namespace ystdex;
320
321 #endif
322
ystdex::pointer_iterator::operator<
friend bool operator<(const pointer_iterator &x, const pointer_iterator &y)
Definition: pointer.hpp:284
ystdex::nptr::operator=
nptr & operator=(const nptr &)=default
platform::p
while p p
Definition: YCLib/FileSystem.h:225
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
ystdex::pointer_iterator::difference_type
typename std::iterator_traits< iterator_type >::difference_type difference_type
Definition: pointer.hpp:213
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::nptr::swap
void swap(nptr< _type > &x, nptr< _type > &y)
Definition: pointer.hpp:186
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:82
ystdex::pointer_iterator::pointer_iterator
pointer_iterator(_tPointer &&ptr)
Definition: pointer.hpp:229
ystdex::nptr::pointer
_type pointer
Definition: pointer.hpp:70
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
ystdex::nptr::nptr
nptr(pointer p)
Definition: pointer.hpp:83
ystdex::nptr::nptr
nptr(nptr &&np)
Definition: pointer.hpp:88
ystdex::pointer_iterator::iterator_type
_type * iterator_type
Definition: pointer.hpp:208
ystdex::nptr::operator!
bool operator!() const
Definition: pointer.hpp:103
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
ystdex::bool_constant
integral_constant< bool, _b > bool_constant
bool 常量。
Definition: type_traits.hpp:382
ystdex::nptr::get_ref
pointer & get_ref()
Definition: pointer.hpp:165
ystdex::pointer_iterator::operator*
reference operator*() const
Definition: pointer.hpp:255
ystdex::pointer_iterator::reference
typename std::iterator_traits< iterator_type >::reference reference
Definition: pointer.hpp:215
ystdex::nptr::nptr
nptr()=default
ystdex::nptr::operator->
pointer & operator->()
Definition: pointer.hpp:125
ystdex::nptr::nptr
nptr(std::nullptr_t)
Definition: pointer.hpp:80
ystdex::pointer_iterator::operator+=
pointer_iterator & operator+=(difference_type n)
Definition: pointer.hpp:238
ystdex::pointer_iterator::pointer
typename std::iterator_traits< iterator_type >::pointer pointer
Definition: pointer.hpp:214
ystdex::pointer_iterator::pointer_iterator
pointer_iterator(nullptr_t={})
Definition: pointer.hpp:223
ystdex::pointer_iterator::operator-=
pointer_iterator & operator-=(difference_type n)
Definition: pointer.hpp:246
ystdex::nptr::operator->
const pointer & operator->() const
Definition: pointer.hpp:130
ystdex::pointer_iterator::value_type
typename std::iterator_traits< iterator_type >::value_type value_type
Definition: pointer.hpp:211
ystdex::pointer_iterator::operator==
friend bool operator==(const pointer_iterator &x, const pointer_iterator &y)
Definition: pointer.hpp:277
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:666
ystdex::detected_or_t
_t< detected_or< _tDefault, _gOp, _tParams... >> detected_or_t
Definition: type_traits.hpp:678
ystdex::pointer_iterator::raw
pointer raw
Definition: pointer.hpp:219
ystdex::pointer_iterator::operator++
pointer_iterator & operator++()
Definition: pointer.hpp:261
ystdex::nptr::operator*
auto operator*() const -> decltype(*ptr)
Definition: pointer.hpp:118
ystdex::iterator_operators_t
_t< details::iterator_operators< _tIter, _tTraits >> iterator_operators_t
按迭代器类别取可实现迭代器的重载操作符集合的实现。
Definition: iterator_op.hpp:191
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::pointer_classify< _tIter >::type
_tIter type
Definition: pointer.hpp:309
ystdex::details::nptr_eq1
bool_constant< _type()==_type()> nptr_eq1
Definition: pointer.hpp:45
ystdex::nullptr_t
空指针类。
Definition: ydef.h:835
ystdex::pointer_classify
指针包装为类类型迭代器。
Definition: pointer.hpp:307
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
iterator_op.hpp
迭代器操作。
ystdex::is_nothrow_copyable
Definition: type_traits.hpp:889
ystdex::pointer_iterator::operator--
pointer_iterator & operator--()
Definition: pointer.hpp:269
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
ystdex::nptr
可空指针包装：满足 NullablePointer 要求同时满足转移后为空。
Definition: pointer.hpp:59
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::nptr::operator=
nptr & operator=(nptr &&np)
Definition: pointer.hpp:96
ystdex::details::nptr_eq2
bool_constant< _type(nullptr)==nullptr > nptr_eq2
Definition: pointer.hpp:47
ystdex::nptr::swap
void swap(nptr &np)
Definition: pointer.hpp:172
ystdex::pointer_iterator
指针迭代器。
Definition: pointer.hpp:204
ystdex::nptr::operator==
friend bool operator==(const nptr &x, const nptr &y)
Definition: pointer.hpp:138
ystdex::dep_ops::totally_ordered
details::flat_ops< _type, _type2, _tOpt, less_than_comparable, equality_comparable > totally_ordered
Definition: operators.hpp:367
ystdex::pointer_iterator::iterator_category
typename std::iterator_traits< iterator_type >::iterator_category iterator_category
Definition: pointer.hpp:210
ystdex::nptr::operator*
auto operator*() -> decltype(*ptr)
Definition: pointer.hpp:113
ystdex::nptr::ptr
pointer ptr
Definition: pointer.hpp:73
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1187
ystdex::nptr::operator<
friend bool operator<(const nptr &x, const nptr &y)
Definition: pointer.hpp:145
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321