Go to the documentation of this file.
9 #ifndef EAGINE_VALID_IF_DECL_HPP
10 #define EAGINE_VALID_IF_DECL_HPP
12 #include "../assert.hpp"
13 #include "../tribool.hpp"
14 #include "../type_traits.hpp"
22 bool _is_valid{
false};
27 : _is_valid(is_valid) {}
37 constexpr do_log(X) noexcept {}
39 template <
typename Log,
typename T>
41 log <<
"Getting the value of an empty optional";
48 template <
typename T,
typename Policy,
typename DoLog =
typename Policy::do_log>
53 auto _base() noexcept ->
_base_t& {
57 using _base_t::_get_value;
60 using _base_t::_base_t;
73 _base() = std::move(v);
79 explicit operator bool() const noexcept {
85 template <
typename Func>
86 auto then(
const Func& func)
const -> std::enable_if_t<
87 !std::is_same_v<std::result_of_t<Func(T)>,
void>,
90 return {func(_get_value()),
true};
98 template <
typename Func>
105 auto operator/(
const T& fallback)
const noexcept ->
const T& {
125 return {_get_value() == v, !
is_valid()};
130 return {_get_value() != v, !
is_valid()};
135 return {_get_value() < v, !
is_valid()};
140 return {_get_value() > v, !
is_valid()};
145 return {_get_value() <= v, !
is_valid()};
150 return {_get_value() >= v, !
is_valid()};
156 template <
typename T,
typename P1,
typename P2>
157 static constexpr
auto
161 (v1.value_anyway() == v2.value_anyway()),
162 (!v1.is_valid() || !v2.is_valid())};
167 template <
typename T,
typename P1,
typename P2>
168 static constexpr
auto
172 (v1.value_anyway() != v2.value_anyway()),
173 (!v1.is_valid() || !v2.is_valid())};
178 template <
typename T,
typename P1,
typename P2>
179 static constexpr
auto
183 (v1.value_anyway() < v2.value_anyway()),
184 (!v1.is_valid() || !v2.is_valid())};
189 template <
typename T,
typename P1,
typename P2>
190 static constexpr
auto
194 (v1.value_anyway() > v2.value_anyway()),
195 (!v1.is_valid() || !v2.is_valid())};
200 template <
typename T,
typename P1,
typename P2>
201 static constexpr
auto
205 (v1.value_anyway() <= v2.value_anyway()),
206 (!v1.is_valid() || !v2.is_valid())};
211 template <
typename T,
typename P1,
typename P2>
212 static constexpr
auto
216 (v1.value_anyway() >= v2.value_anyway()),
217 (!v1.is_valid() || !v2.is_valid())};
222 template <
typename T,
typename P>
224 return EAGINE_CONSTEXPR_ASSERT(
bool(opt), opt.value_anyway());
229 template <
typename T,
typename P>
231 return EAGINE_CONSTEXPR_ASSERT(
bool(opt), opt.value_anyway());
236 template <
typename T,
typename P>
238 return EAGINE_CONSTEXPR_ASSERT(
bool(opt), std::move(opt.value_anyway()));
242 template <
typename T,
typename P,
typename F>
243 static constexpr
auto
245 -> std::enable_if_t<std::is_convertible_v<F, T>, T> {
247 return opt.value_anyway();
249 return T{std::forward<F>(fallback)};
253 template <
typename T,
typename P,
typename F>
257 return opt.value_anyway();
263 template <
typename T,
typename P,
typename F>
269 F(std::declval<F&&>())))
289 template <
typename T,
typename P,
typename F>
293 return {std::move(vi), std::move(f)};
299 template <
typename T>
304 #endif // EAGINE_VALID_IF_DECL_HPP
auto fallback() const noexcept -> const F &
Returns the stored fallback value.
Definition: decl.hpp:274
auto fallback() noexcept -> F &
Returns the stored fallback value.
Definition: decl.hpp:279
static constexpr auto operator>=(const valid_if< T, P1 > &v1, const valid_if< T, P2 > &v2) noexcept -> tribool
Greater-equal comparison of two conditionally valid values.
Definition: decl.hpp:213
static constexpr auto operator>(const valid_if< T, P1 > &v1, const valid_if< T, P2 > &v2) noexcept -> tribool
Greater-than comparison of two conditionally valid values.
Definition: decl.hpp:191
Common code is placed in this namespace.
Definition: eagine.hpp:21
auto operator/(const T &fallback) const noexcept -> const T &
Returns the stored value if valid, returns fallback otherwise.
Definition: decl.hpp:105
static constexpr auto operator<=(const valid_if< T, P1 > &v1, const valid_if< T, P2 > &v2) noexcept -> tribool
Less-equal comparison of two conditionally valid values.
Definition: decl.hpp:202
static auto either_or(valid_if< T, P > vi, F f) noexcept(noexcept(valid_if< T, P >(std::declval< valid_if< T, P > && >())) &&noexcept(F(std::declval< F && >()))) -> valid_if_or_fallback< T, P, F >
Constructor function for valid_if_or_fallback.
Definition: decl.hpp:290
constexpr auto operator<=(const T &v) const -> tribool
Less-equal comparison of the stored value with v.
Definition: decl.hpp:144
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
valid_if_or_fallback(valid_if< T, P > vi, F fallback) noexcept(noexcept(valid_if< T, P >(std::declval< valid_if< T, P > && >())) &&noexcept(F(std::declval< F && >())))
Constructor.
Definition: decl.hpp:267
Primary template for conditionally valid values.
Definition: decl.hpp:49
auto operator=(T &&v) -> auto &
Moves argument into this instance.
Definition: decl.hpp:72
auto then(const Func &func) const -> std::enable_if_t< !std::is_same_v< std::result_of_t< Func(T)>, void >, valid_if< std::result_of_t< Func(T)>, valid_flag_policy >>
Calls the specified function if the stored valus is valid.
Definition: decl.hpp:86
auto operator=(const T &v) -> auto &
Copies argument into this instance.
Definition: decl.hpp:66
constexpr auto operator<(const T &v) const -> tribool
Less-than comparison of the stored value with v.
Definition: decl.hpp:134
static constexpr auto extract_or(const valid_if< T, P > &opt, F &&fallback) noexcept -> std::enable_if_t< std::is_convertible_v< F, T >, T >
Overload of extract_or for conditionally valid values.
Definition: decl.hpp:244
constexpr auto is_valid(const T &val, P... p) const noexcept -> bool
Checks if val is valid according to this object's policy.
Definition: base.hpp:176
Tri-state boolean value.
Definition: tribool.hpp:61
Policy for optionally valid values, indicated by a boolean flag.
Definition: decl.hpp:21
static constexpr auto operator!=(const valid_if< T, P1 > &v1, const valid_if< T, P2 > &v2) noexcept -> tribool
Non-equality comparison of two conditionally valid values.
Definition: decl.hpp:169
auto operator()(const T &) const noexcept -> bool
Returns value validity depending on internally stored flag.
Definition: decl.hpp:31
constexpr auto operator!=(const T &v) const -> tribool
Non-equality comparison of the stored value with v.
Definition: decl.hpp:129
auto operator*() const noexcept -> const T &
Returns the stored value, throws if it is invalid.
Definition: decl.hpp:112
auto value_or(T &fallback, P... p) noexcept -> auto &
Returns the stored value if valid, otherwise returns fallback.
Definition: base.hpp:251
constexpr auto operator>(const T &v) const -> tribool
Greater-than comparison of the stored value with v.
Definition: decl.hpp:139
auto operator==(message_id l, static_message_id< ClassId, MethodId > r) noexcept
Equality comparison between message_id and static_message_id.
Definition: message_id.hpp:131
constexpr auto operator==(const T &v) const -> tribool
Equality comparison of the stored value with v.
Definition: decl.hpp:124
static constexpr auto operator<(const valid_if< T, P1 > &v1, const valid_if< T, P2 > &v2) noexcept -> tribool
Less-than comparison of two conditionally valid values.
Definition: decl.hpp:180
constexpr auto operator>=(const T &v) const -> tribool
Greater-equal comparison of the stored value with v.
Definition: decl.hpp:149
auto value(P... p) -> T &
Returns the stored value if it is valid otherwise throws.
Definition: base.hpp:236
Helper class storing both conditionally valid value and fallback.
Definition: decl.hpp:264
auto operator|(const Func &func) const
Calls the specified function if the stored valus is valid.
Definition: decl.hpp:99
Basic template for conditionally-valid values.
Definition: base.hpp:86
auto operator->() const noexcept -> const T *
Returns pointer to the stored value, throws if it is invalid.
Definition: decl.hpp:119