YSTest: deref_op.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

deref_op.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
28 #ifndef YB_INC_ystdex_deref_op_hpp_
29 #define YB_INC_ystdex_deref_op_hpp_ 1
30
31 #include "type_traits.hpp" // for decay_t;
32
33 namespace ystdex
34 {
35
43 template<typename _tIter>
44 yconstfn bool
45 is_undereferenceable(const _tIter&) ynothrow
46 {
47 return {};
48 }
49 template<typename _type>
50 yconstfn bool
51 is_undereferenceable(_type* p) ynothrow
52 {
53 return !p;
54 }
56
57
59
60
62 template<typename _type>
63 yconstfn auto
64 nonnull_or(_type&& p) -> decay_t<decltype(yforward(p))>
65 {
66 return p ? yforward(p) : decay_t<decltype(yforward(p))>();
67 }
69 template<typename _tOther, typename _type>
70 yconstfn auto
71 nonnull_or(_type&& p, _tOther&& other)
72 -> yimpl(decltype(p ? yforward(p) : yforward(other)))
73 {
74 return p ? yforward(p) : yforward(other);
75 }
76 template<typename _tOther, typename _type, typename _tSentinal = nullptr_t>
77 yconstfn auto
78 nonnull_or(_type&& p, _tOther&& other, _tSentinal&& last) -> yimpl(
79 decltype(!bool(p == yforward(last)) ? yforward(p) : yforward(other)))
80 {
81 return !bool(p == yforward(last)) ? yforward(p) : yforward(other);
82 }
84
86
87 template<typename _type, typename _func>
88 yconstfn auto
89 call_nonnull_or(_func f, _type&& p) -> decay_t<decltype(f(yforward(p)))>
90 {
91 return p ? f(yforward(p)) : decay_t<decltype(f(yforward(p)))>();
92 }
93 template<typename _tOther, typename _type, typename _func>
94 yconstfn auto
95 call_nonnull_or(_func f, _type&& p, _tOther&& other)
96 -> yimpl(decltype(p ? f(yforward(p)) : yforward(other)))
97 {
98 return p ? f(yforward(p)) : yforward(other);
99 }
100 template<typename _tOther, typename _type, typename _func,
101 typename _tSentinal = nullptr_t>
102 yconstfn auto
103 call_nonnull_or(_func f, _type&& p, _tOther&& other, _tSentinal&& last)
104 -> yimpl(
105 decltype(!bool(p == yforward(last)) ? f(yforward(p)) : yforward(other)))
106 {
107 return !bool(p == yforward(last)) ? f(yforward(p)) : yforward(other);
108 }
110
111
113
114 template<typename _type>
115 yconstfn auto
116 value_or(_type&& p) -> decay_t<decltype(*yforward(p))>
117 {
118 return p ? *yforward(p) : decay_t<decltype(*yforward(p))>();
119 }
120 template<typename _tOther, typename _type>
121 yconstfn auto
122 value_or(_type&& p, _tOther&& other)
123 -> yimpl(decltype(p ? *yforward(p) : yforward(other)))
124 {
125 return p ? *yforward(p) : yforward(other);
126 }
127 template<typename _tOther, typename _type, typename _tSentinal = nullptr_t>
128 yconstfn auto
129 value_or(_type&& p, _tOther&& other, _tSentinal&& last) -> yimpl(
130 decltype(!bool(p == yforward(last)) ? *yforward(p) : yforward(other)))
131 {
132 return !bool(p == yforward(last)) ? *yforward(p) : yforward(other);
133 }
135
137
138 template<typename _type, typename _func>
139 yconstfn auto
140 call_value_or(_func f, _type&& p) -> decay_t<decltype(f(*yforward(p)))>
141 {
142 return p ? f(*yforward(p)) : decay_t<decltype(f(*yforward(p)))>();
143 }
144 template<typename _tOther, typename _type, typename _func>
145 yconstfn auto
146 call_value_or(_func f, _type&& p, _tOther&& other)
147 -> yimpl(decltype(p ? f(*yforward(p)) : yforward(other)))
148 {
149 return p ? f(*yforward(p)) : yforward(other);
150 }
151 template<typename _tOther, typename _type, typename _func,
152 typename _tSentinal = nullptr_t>
153 yconstfn auto
154 call_value_or(_func f, _type&& p, _tOther&& other, _tSentinal&& last) -> yimpl(
155 decltype(!bool(p == yforward(last)) ? f(*yforward(p)) : yforward(other)))
156 {
157 return !bool(p == yforward(last)) ? f(*yforward(p)) : yforward(other);
158 }
160
161
162 } // namespace ystdex;
163
164 #endif
165
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
CHRLib::f
f(d, s)
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:140
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1280
yimpl
#define yimpl(...)
实现标签。
Definition: ydef.h:262
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
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:89
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
type_traits.hpp
ISO C++ 类型特征扩展。
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::nonnull_or
auto nonnull_or(_type &&p) -> decay_t< decltype(std::forward< decltype(p)>(p))>
取非空引用或默认值。
Definition: deref_op.hpp:64
CHRLib::CharSet::other
Definition: Encoding.h:65
ystdex::value_or
auto value_or(_type &&p) -> decay_t< decltype(*std::forward< decltype(p)>(p))>
取非空值或默认值。
Definition: deref_op.hpp:116
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
CHRLib::p
const auto p(d)