YSTest: ref.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

ref.hpp
浏览该文件的文档.
1 /*
2 © 2011-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_ref_hpp_
31 #define YB_INC_ystdex_ref_hpp_ 1
32
33 #include "addressof.hpp" // for "addressof.hpp", ystdex::constfn_addressof,
34 // false_, true_, exclude_self_t, cond_t, not_, is_object;
35 #include <functional> // for std::reference_wrapper;
36
44 #if YB_IMPL_MSCPP >= 1800
45 # ifndef __cpp_lib_transparent_operators
46 # define __cpp_lib_transparent_operators 201210
47 # endif
48 #endif
49 #if YB_IMPL_MSCPP >= 1900
50 # ifndef __cpp_lib_invoke
51 # define __cpp_lib_invoke 201411
52 # endif
53 #endif
54
55
56 namespace ystdex
57 {
58
60
61
73 template<typename _type>
74 class lref
75 {
76 public:
77 using type = _type;
78
79 private:
80 _type* ptr;
81
82 public:
83 yconstfn
84 lref(_type& t) ynothrow
85 : ptr(ystdex::constfn_addressof(t))
86 {}
88 lref(_type&&) = delete;
89 yconstfn
90 lref(std::reference_wrapper<_type> t) ynothrow
91 : lref(t.get())
92 {}
93
95 lref(const lref&) = default;
96
98 lref&
99 operator=(const lref&) = default;
100
101 yconstfn
102 operator _type&() const ynothrow
103 {
104 return *ptr;
105 }
106
107 yconstfn _type&
108 get() const ynothrow
109 {
110 return *ptr;
111 }
112
117 template<typename... _tParams>
118 yconstfn result_of_t<_type&(_tParams&&...)>
119 operator()(_tParams&&... args) const
120 {
121 return (*ptr)(yforward(args)...);
122 }
123 };
124
130 template<typename _type>
131 yconstfn lref<_type>
132 ref(_type& t)
133 {
134 return lref<_type>(t);
135 }
136 template<class _type>
137 void
138 ref(const _type&&) = delete;
139
140 template<typename _type>
141 yconstfn lref<const _type>
142 cref(const _type& t)
143 {
144 return lref<const _type>(t);
145 }
146 template<class _type>
147 void
148 cref(const _type&&) = delete;
150
151
152
160 template<typename _type>
161 struct is_reference_wrapper : false_
162 {};
163
164 template<typename _type>
165 struct is_reference_wrapper<std::reference_wrapper<_type>> : true_
166 {};
167
168 template<typename _type>
169 struct is_reference_wrapper<lref<_type>> : true_
170 {};
172
184 template<typename _type>
185 struct unwrap_reference
186 {
187 using type = _type;
188 };
189
190 template<typename _type>
191 using unwrap_reference_t = _t<unwrap_reference<_type>>;
192
193 template<typename _type>
194 struct unwrap_reference<std::reference_wrapper<_type>>
195 {
196 using type = _type&;
197 };
198
199 template<typename _type>
200 struct unwrap_reference<lref<_type>>
201 {
202 using type = _type&;
203 };
205
206
208
209 template<typename _type>
210 struct decay_unwrap : unwrap_reference<decay_t<_type>>
211 {};
212
213 template<typename _type>
214 using decay_unwrap_t = _t<decay_unwrap<_type>>;
216
217
218
223 template<typename _type, typename _tReference = lref<_type>>
224 class indirect_ref_adaptor
225 {
226 public:
227 using value_type = _type;
228 using reference = _tReference;
229
230 private:
231 reference ref;
232
233 public:
234 indirect_ref_adaptor(value_type& r)
235 : ref(r)
236 {}
237
238 auto
239 get() ynothrow -> decltype(ref.get().get())
240 {
241 return ref.get().get();
242 }
243 };
244
245
253 template<typename _type>
254 yconstfn _type&
255 unref(_type&& x) ynothrow
256 {
257 return x;
258 }
260 template<typename _type>
261 yconstfn _type&
262 unref(const lref<_type>& x) ynothrow
263 {
264 return x.get();
265 }
267
268
275 class void_ref
276 {
277 private:
278 const volatile void* ptr;
279
280 public:
281 template<typename _type>
282 yconstfn
283 void_ref(_type& obj)
284 : ptr(&obj)
285 {}
286
287 template<typename _type>
288 yconstfn YB_PURE
289 operator _type&() const
290 {
291 return *static_cast<_type*>(&*this);
292 }
293
294 YB_PURE void*
295 operator&() const volatile
296 {
297 return const_cast<void*>(ptr);
298 }
299 };
300
301
307 struct pseudo_output
308 {
310
311 template<typename... _tParams>
312 yconstfn
313 pseudo_output(_tParams&&...) ynothrow
314 {}
315
316 template<typename _tParam,
317 yimpl(exclude_self_t<pseudo_output, _tParam>)>
318 yconstfn const pseudo_output&
319 operator=(_tParam&&) const ynothrow
320 {
321 return *this;
322 }
323 template<typename... _tParams>
324 yconstfn const pseudo_output&
325 operator()(_tParams&&...) const ynothrow
326 {
327 return *this;
328 }
330 };
331
332
339 template<typename _type, typename _tRes = pseudo_output>
341 using nonvoid_result_t = cond_t<not_<is_void<_type>>, _type, pseudo_output>;
342
344 template<typename _type, typename _tRes = pseudo_output>
345 using object_result_t = cond_t<is_object<_type>, _type, pseudo_output>;
347
348 } // namespace ystdex;
349
350 #endif
351
ystdex::pseudo_output::operator()
const pseudo_output & operator()(_tParams &&...) const
Definition: ref.hpp:325
ystdex::unwrap_reference< lref< _type > >::type
_type & type
Definition: ref.hpp:202
ystdex::cpp2014::result_of_t
typename result_of< _type >::type result_of_t
Definition: type_traits.hpp:355
ystdex::indirect_ref_adaptor::indirect_ref_adaptor
indirect_ref_adaptor(value_type &r)
Definition: ref.hpp:234
ystdex::cond_t
conditional_t< _tCond::value, _tThen, _tElse > cond_t
条件判断。
Definition: type_traits.hpp:738
ystdex::true_
bool_< true > true_
Definition: type_traits.hpp:391
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::constfn_addressof
enable_if_t<!has_overloaded_addressof< _type >::value, _type * > constfn_addressof(_type &r)
尝试对非重载 operator& 提供 constexpr 的 std::addressof 替代。
Definition: addressof.hpp:78
ystdex::lref::lref
lref(std::reference_wrapper< _type > t)
Definition: ref.hpp:90
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
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
ystdex::false_
bool_< false > false_
Definition: type_traits.hpp:392
ystdex::void_ref::operator&
void * operator&() const volatile
Definition: ref.hpp:295
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
ystdex::nonvoid_result_t
cond_t< not_< is_void< _type >>, _type, pseudo_output > nonvoid_result_t
若类型不是空类型则取后备结果类型（默认为 pseudo_output ）。
Definition: ref.hpp:341
ystdex::pseudo_output::pseudo_output
pseudo_output(_tParams &&...)
Definition: ref.hpp:313
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
ystdex::lref::ptr
_type * ptr
Definition: ref.hpp:80
ystdex::unwrap_reference< decay_t< _type > >::type
decay_t< _type > type
Definition: ref.hpp:187
ystdex::unref
_type & unref(_type &&x)
解除引用包装。
Definition: ref.hpp:255
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:527
ystdex::is_reference_wrapper
判断模板参数指定的类型是否。
Definition: ref.hpp:161
ystdex::void_ref::void_ref
void_ref(_type &obj)
Definition: ref.hpp:283
ystdex::lref
左值引用包装。
Definition: ref.hpp:74
ystdex::indirect_ref_adaptor::reference
_tReference reference
Definition: ref.hpp:228
ystdex::indirect_ref_adaptor::value_type
_type value_type
Definition: ref.hpp:227
ystdex::decay_unwrap_t
_t< decay_unwrap< _type >> decay_unwrap_t
Definition: ref.hpp:214
ystdex::void_ref::ptr
const volatile void * ptr
Definition: ref.hpp:278
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::lref::operator()
result_of_t< _type &(_tParams &&...)> operator()(_tParams &&...args) const
Definition: ref.hpp:119
ystdex::lref::operator=
lref & operator=(const lref &)=default
ystdex::unwrap_reference< std::reference_wrapper< _type > >::type
_type & type
Definition: ref.hpp:196
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: type_traits.hpp:97
ystdex::indirect_ref_adaptor::ref
reference ref
Definition: ref.hpp:231
addressof.hpp
一元操作符 & 和取指针的相关接口。
ystdex::void_ref
任意对象引用类型。
Definition: ref.hpp:275
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:748
ystdex::pseudo_output
伪输出对象。
Definition: ref.hpp:307
std
Definition: memory.hpp:1183
ystdex::object_result_t
cond_t< is_object< _type >, _type, pseudo_output > object_result_t
若类型不是对象类型则取后备结果类型（默认为 pseudo_output ）。
Definition: ref.hpp:345
ystdex::lref<::FreeImageIO >::type
::FreeImageIO type
Definition: ref.hpp:77
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::pseudo_output::operator=
const pseudo_output & operator=(_tParam &&) const
Definition: ref.hpp:319
ystdex::unwrap_reference_t
_t< unwrap_reference< _type >> unwrap_reference_t
Definition: ref.hpp:191
ystdex::cref
lref< const _type > cref(const _type &t)
Definition: ref.hpp:142
ystdex::unwrap_reference
取引用包装的类型或未被包装的模板参数类型。
Definition: ref.hpp:185
ystdex::indirect_ref_adaptor
包装间接操作的引用适配器。
Definition: ref.hpp:224
ystdex::ref
lref< _type > ref(_type &t)
构造引用包装。
Definition: ref.hpp:132
ystdex::lref::lref
lref(_type &t)
Definition: ref.hpp:84
ystdex::decay_unwrap
Definition: ref.hpp:210