YSTest: ref.hpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

ref.hpp
浏览该文件的文档.
1 /*
2 © 2011-2016, 2018-2020 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", std::declval, exclude_self_t,
34 // ystdex::addressof, false_, true_;
35 #include "invoke.hpp" // for "invoke.hpp", invoke_result_t, ystdex::invoke,
36 // std::reference_wrapper;
37
38 namespace ystdex
39 {
40
42
43
59 template<typename _type>
60 class lref
61 {
62 public:
63 using type = _type;
64
65 private:
66 _type* ptr;
67
69
70 yconstfn static _type*
71 fun(_type&) ynothrow;
72 yconstfn static _type*
73 fun(_type&&) = delete;
74
75 public:
76 template<typename _type2, yimpl(typename = decltype(
77 fun(std::declval<_type2>())), typename = exclude_self_t<lref, _type2>)>
78 lref(_type2&& u) ynoexcept(yimpl(noexcept(fun(std::declval<_type2>()))))
79 : ptr(ystdex::addressof(static_cast<_type&>(std::forward<_type2>(u))))
80 {}
82 lref(const lref&) = default;
84
86 lref&
87 operator=(const lref&) = default;
88
89 yconstfn
90 operator _type&() const ynothrow
91 {
92 return *ptr;
93 }
94
95 yconstfn _type&
96 get() const ynothrow
97 {
98 return *ptr;
99 }
100
105 template<typename... _tParams>
106 yconstfn invoke_result_t<_type&, _tParams...>
107 operator()(_tParams&&... args) const
108 {
109 return ystdex::invoke(*ptr, yforward(args)...);
110 }
111 };
112
118 template<typename _type>
119 yconstfn lref<_type>
120 ref(_type& t)
121 {
122 return lref<_type>(t);
123 }
124 template<class _type>
125 void
126 ref(const _type&&) = delete;
127
128 template<typename _type>
129 yconstfn lref<const _type>
130 cref(const _type& t)
131 {
132 return lref<const _type>(t);
133 }
134 template<class _type>
135 void
136 cref(const _type&&) = delete;
138
139
140
148 template<typename _type>
149 struct is_reference_wrapper : false_
150 {};
151
152 template<typename _type>
153 struct is_reference_wrapper<std::reference_wrapper<_type>> : true_
154 {};
155
156 template<typename _type>
157 struct is_reference_wrapper<lref<_type>> : true_
158 {};
160
173 template<typename _type>
174 struct unwrap_reference
175 {
176 using type = _type;
177 };
178
179 template<typename _type>
180 using unwrap_reference_t = _t<unwrap_reference<_type>>;
181
182 template<typename _type>
183 struct unwrap_reference<std::reference_wrapper<_type>>
184 {
185 using type = _type&;
186 };
187
188 template<typename _type>
189 struct unwrap_reference<lref<_type>>
190 {
191 using type = _type&;
192 };
194
195
197
198 template<typename _type>
199 struct unwrap_ref_decay : unwrap_reference<decay_t<_type>>
200 {};
201
202 template<typename _type>
203 using unwrap_ref_decay_t = _t<unwrap_ref_decay<_type>>;
205
206
207
212 template<typename _type, typename _tReference = lref<_type>>
213 class indirect_ref_adaptor
214 {
215 public:
216 using value_type = _type;
217 using reference = _tReference;
218
219 private:
220 reference ref;
221
222 public:
223 indirect_ref_adaptor(value_type& r)
224 : ref(r)
225 {}
226
227 auto
228 get() ynothrow -> decltype(ref.get().get())
229 {
230 return ref.get().get();
231 }
232 };
233
234
241 template<typename _type>
242 yconstfn _type&
243 unref(_type&& x) ynothrow
244 {
245 return x;
246 }
248 template<typename _type>
249 yconstfn _type&
250 unref(std::reference_wrapper<_type> x) ynothrow
251 {
252 return x.get();
253 }
255 template<typename _type>
256 yconstfn _type&
257 unref(lref<_type> x) ynothrow
258 {
259 return x.get();
260 }
262
263
270 class void_ref
271 {
272 private:
273 const volatile void* ptr;
274
275 public:
276 template<typename _type>
277 yconstfn
278 void_ref(_type& obj)
279 : ptr(&obj)
280 {}
281
282 template<typename _type>
283 YB_PURE yconstfn
284 operator _type&() const
285 {
286 return *static_cast<_type*>(&*this);
287 }
288
289 YB_PURE void*
290 operator&() const volatile
291 {
292 return const_cast<void*>(ptr);
293 }
294 };
295
296 } // namespace ystdex;
297
298 #endif
299
invoke.hpp
可调用对象和调用包装接口。
ystdex::lref::operator()
invoke_result_t< _type &, _tParams... > operator()(_tParams &&... args) const
Definition: ref.hpp:107
ystdex::unwrap_reference_t
_t< unwrap_reference< _type > > unwrap_reference_t
Definition: ref.hpp:180
x
const EncapsulationBase & x
Definition: NPLA1Forms.cpp:1536
ystdex::unwrap_ref_decay
Definition: ref.hpp:200
ystdex::lref
左值引用包装。
Definition: ref.hpp:61
ystdex::invoke
invoke_result_t< _fCallable, _tParams... > invoke(_fCallable &&f, _tParams &&... args)
调用可调用对象。
Definition: invoke.hpp:538
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
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::lref::ptr
_type * ptr
Definition: ref.hpp:66
ystdex::unwrap_reference< std::reference_wrapper< _type > >::type
_type & type
Definition: ref.hpp:185
ystdex::lref::fun
static _type * fun(_type &)
ystdex::void_ref::ptr
const volatile void * ptr
Definition: ref.hpp:273
ystdex::cref
lref< const _type > cref(const _type &t)
Definition: ref.hpp:130
ystdex::void_ref::operator&
void * operator&() const volatile
Definition: ref.hpp:290
ystdex::lref::lref
lref(_type2 &&u)
Definition: ref.hpp:78
ystdex::lref::operator=
lref & operator=(const lref &)=default
CHRLib::args
YB_ATTR_nodiscard _tParams && args
Definition: CharacterProcessing.h:649
ystdex::details::_type2
_type2
Definition: operators.hpp:241
ystdex::unwrap_reference
取引用包装的类型或未被包装的模板参数类型。
Definition: ref.hpp:175
ystdex::void_ref
任意对象引用类型。
Definition: ref.hpp:271
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
ystdex::indirect_ref_adaptor::value_type
_type value_type
Definition: ref.hpp:216
std
Definition: optional.h:982
ystdex::unwrap_reference< lref< _type > >::type
_type & type
Definition: ref.hpp:191
ystdex::lref<::FreeImageIO >::type
::FreeImageIO type
Definition: ref.hpp:63
ystdex::is_reference_wrapper
判断模板参数指定的类型是否为引用包装的实例。
Definition: ref.hpp:150
ystdex::cpp2017::invoke_result_t
_t< invoke_result< _fCallable, _tParams... > > invoke_result_t
Definition: invoke.hpp:393
ystdex::lref::ref
lref< _type > ref(_type &t)
构造引用包装。
Definition: ref.hpp:120
ystdex::true_
bool_< true > true_
Definition: integral_constant.hpp:109
ystdex::unref
_type & unref(_type &&x)
解除引用包装。
Definition: ref.hpp:243
ystdex::exclude_self_t
enable_if_t<!is_same_param< _tSelected, _tParam >::value, _type > exclude_self_t
排除选择类型的特定参数的重载。
Definition: meta.hpp:865
ystdex::lref::get
_type & get() const
Definition: ref.hpp:96
ystdex::void_ref::void_ref
void_ref(_type &obj)
Definition: ref.hpp:278
ystdex::indirect_ref_adaptor::reference
_tReference reference
Definition: ref.hpp:217
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
ystdex::unwrap_ref_decay_t
_t< unwrap_ref_decay< _type > > unwrap_ref_decay_t
Definition: ref.hpp:203
ystdex::addressof
enable_if_t< has_no_overloaded_addressof< _type >::value, _type * > addressof(_type &r)
尝试对非重载 operator& 提供 constexpr 的 std::addressof 替代。
Definition: addressof.hpp:131
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: meta.hpp:45
ystdex::lref::fun
static _type * fun(_type &&)=delete
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
addressof.hpp
一元操作符 & 和取指针的相关接口。
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
ystdex::indirect_ref_adaptor::indirect_ref_adaptor
indirect_ref_adaptor(value_type &r)
Definition: ref.hpp:223
ystdex::indirect_ref_adaptor
包装间接操作的引用适配器。
Definition: ref.hpp:214
ystdex::false_
bool_< false > false_
Definition: integral_constant.hpp:110
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::indirect_ref_adaptor::ref
reference ref
Definition: ref.hpp:220
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:1151
ystdex::indirect_ref_adaptor::get
auto get() -> decltype(ref.get().get())
Definition: ref.hpp:228
ystdex::ref
void ref(const _type &&)=delete
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
ystdex::unwrap_reference< decay_t< _type > >::type
decay_t< _type > type
Definition: ref.hpp:176