YSTest: deref_op.hpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

deref_op.hpp
浏览该文件的文档.
1 /*
2 © 2011-2016, 2018-2019 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_deref_op_hpp_
29 #define YB_INC_ystdex_deref_op_hpp_ 1
30
31 #include "meta.hpp" // for internal "meta.hpp", decay_t;
32 #include "cassert.h" // for yverify;
33
34 namespace ystdex
35 {
36
44 template<typename _tIter>
45 yconstfn bool
46 is_undereferenceable(const _tIter&) ynothrow
47 {
48 return {};
49 }
50 template<typename _type>
51 yconstfn bool
52 is_undereferenceable(_type* p) ynothrow
53 {
54 return !p;
55 }
57
58
67 #define YB_VerifyIterator(_expr) yverify(!is_undereferenceable(_expr))
68
69
71
72
74 template<typename _type>
75 yconstfn auto
76 nonnull_or(_type&& p) -> decay_t<decltype(yforward(p))>
77 {
78 return p ? yforward(p) : decay_t<decltype(yforward(p))>();
79 }
81 template<typename _tOther, typename _type>
82 yconstfn auto
83 nonnull_or(_type&& p, _tOther&& other)
84 -> yimpl(decltype(p ? yforward(p) : yforward(other)))
85 {
86 return p ? yforward(p) : yforward(other);
87 }
88 template<typename _tOther, typename _type, typename _tSentinal = nullptr_t>
89 yconstfn auto
90 nonnull_or(_type&& p, _tOther&& other, _tSentinal&& last) -> yimpl(
91 decltype(!bool(p == yforward(last)) ? yforward(p) : yforward(other)))
92 {
93 return !bool(p == yforward(last)) ? yforward(p) : yforward(other);
94 }
96
98
99 template<typename _type, typename _func>
100 yconstfn auto
101 call_nonnull_or(_func f, _type&& p) -> decay_t<decltype(f(yforward(p)))>
102 {
103 return p ? f(yforward(p)) : decay_t<decltype(f(yforward(p)))>();
104 }
105 template<typename _tOther, typename _type, typename _func>
106 yconstfn auto
107 call_nonnull_or(_func f, _type&& p, _tOther&& other)
108 -> yimpl(decltype(p ? f(yforward(p)) : yforward(other)))
109 {
110 return p ? f(yforward(p)) : yforward(other);
111 }
112 template<typename _tOther, typename _type, typename _func,
113 typename _tSentinal = nullptr_t>
114 yconstfn auto
115 call_nonnull_or(_func f, _type&& p, _tOther&& other, _tSentinal&& last)
116 -> yimpl(
117 decltype(!bool(p == yforward(last)) ? f(yforward(p)) : yforward(other)))
118 {
119 return !bool(p == yforward(last)) ? f(yforward(p)) : yforward(other);
120 }
122
123
125
126 template<typename _type>
127 yconstfn auto
128 value_or(_type&& p) -> decay_t<decltype(*yforward(p))>
129 {
130 return p ? *yforward(p) : decay_t<decltype(*yforward(p))>();
131 }
132 template<typename _tOther, typename _type>
133 yconstfn auto
134 value_or(_type&& p, _tOther&& other)
135 -> yimpl(decltype(p ? *yforward(p) : yforward(other)))
136 {
137 return p ? *yforward(p) : yforward(other);
138 }
139 template<typename _tOther, typename _type, typename _tSentinal = nullptr_t>
140 yconstfn auto
141 value_or(_type&& p, _tOther&& other, _tSentinal&& last) -> yimpl(
142 decltype(!bool(p == yforward(last)) ? *yforward(p) : yforward(other)))
143 {
144 return !bool(p == yforward(last)) ? *yforward(p) : yforward(other);
145 }
147
149
150 template<typename _type, typename _func>
151 yconstfn auto
152 call_value_or(_func f, _type&& p) -> decay_t<decltype(f(*yforward(p)))>
153 {
154 return p ? f(*yforward(p)) : decay_t<decltype(f(*yforward(p)))>();
155 }
156 template<typename _tOther, typename _type, typename _func>
157 yconstfn auto
158 call_value_or(_func f, _type&& p, _tOther&& other)
159 -> yimpl(decltype(p ? f(*yforward(p)) : yforward(other)))
160 {
161 return p ? f(*yforward(p)) : yforward(other);
162 }
163 template<typename _tOther, typename _type, typename _func,
164 typename _tSentinal = nullptr_t>
165 yconstfn auto
166 call_value_or(_func f, _type&& p, _tOther&& other, _tSentinal&& last) -> yimpl(
167 decltype(!bool(p == yforward(last)) ? f(*yforward(p)) : yforward(other)))
168 {
169 return !bool(p == yforward(last)) ? f(*yforward(p)) : yforward(other);
170 }
172
173
174 } // namespace ystdex;
175
176 #endif
177
meta.hpp
通用元编程设施。
CHRLib::f
f(d, s)
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: meta.hpp:196
CHRLib::CharSet::other
@ other
Definition: Encoding.h:65
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
ystdex::call_nonnull_or
auto call_nonnull_or(_func f, _type &&p) -> decay_t< decltype(f(std::forward< decltype(p)>(p)))>
调用非引用或默认值。
Definition: deref_op.hpp:101
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:46
ystdex::nonnull_or
auto nonnull_or(_type &&p) -> decay_t< decltype(std::forward< decltype(p)>(p))>
取非空引用或默认值。
Definition: deref_op.hpp:76
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
ystdex::value_or
auto value_or(_type &&p) -> decay_t< decltype(*std::forward< decltype(p)>(p))>
取非空值或默认值。
Definition: deref_op.hpp:128
platform::p
return p p
Definition: YCLib/FileSystem.h:209
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:413
platform::bool
YB_ATTR_nodiscard std::FILE *fp bool
Definition: FileIO.h:1271
cassert.h
ISO C 断言/调试跟踪扩展。
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
ystdex::call_value_or
auto call_value_or(_func f, _type &&p) -> decay_t< decltype(f(*std::forward< decltype(p)>(p)))>
调用非空值或取默认值。
Definition: deref_op.hpp:152
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073