Go to the documentation of this file.
9 #ifndef EAGINE_EXTRACT_HPP
10 #define EAGINE_EXTRACT_HPP
15 #include <type_traits>
22 static constexpr
auto extract(T* ptr) noexcept -> T& {
23 return EAGINE_CONSTEXPR_ASSERT(
bool(ptr), *ptr);
29 extract_or(T* ptr, std::remove_const_t<T>& fallback) noexcept -> T& {
30 return bool(ptr) ? *ptr : fallback;
34 template <
typename T,
typename F>
36 -> std::enable_if_t<std::is_convertible_v<F, T>, T> {
37 return bool(ptr) ? *ptr : T{std::forward<F>(fallback)};
43 static constexpr
auto extract(std::shared_ptr<T>& ptr) noexcept -> T& {
44 return EAGINE_CONSTEXPR_ASSERT(
bool(ptr), *ptr);
50 static constexpr
auto extract(
const std::shared_ptr<T>& ptr) noexcept
52 return EAGINE_CONSTEXPR_ASSERT(
bool(ptr), *ptr);
58 extract_or(std::shared_ptr<T>& ptr, std::remove_const_t<T>& fallback) noexcept
60 return bool(ptr) ? *ptr : fallback;
64 template <
typename T,
typename F>
65 static constexpr
auto extract_or(std::shared_ptr<T>& ptr, F&& fallback)
66 -> std::enable_if_t<std::is_convertible_v<F, T>, T> {
67 return bool(ptr) ? *ptr : T{std::forward<F>(fallback)};
72 template <
typename T,
typename D>
73 static constexpr
auto extract(std::unique_ptr<T, D>& ptr) noexcept -> T& {
74 return EAGINE_CONSTEXPR_ASSERT(
bool(ptr), *ptr);
79 template <
typename T,
typename D>
80 static constexpr
auto extract(
const std::unique_ptr<T, D>& ptr) noexcept
82 return EAGINE_CONSTEXPR_ASSERT(
bool(ptr), *ptr);
86 template <
typename T,
typename D>
88 std::unique_ptr<T, D>& ptr,
89 std::remove_const_t<T>& fallback) noexcept -> T& {
90 return bool(ptr) ? *ptr : fallback;
94 template <
typename T,
typename D,
typename F>
95 static constexpr
auto extract_or(std::unique_ptr<T, D>& ptr, F&& fallback)
96 -> std::enable_if_t<std::is_convertible_v<F, T>, T> {
97 return bool(ptr) ? *ptr : T{std::forward<F>(fallback)};
102 template <
typename Outcome>
127 template <
typename Outcome>
136 constexpr
ok(Outcome&& outcome) noexcept(noexcept(std::declval<Outcome&&>()))
137 : _outcome{std::move(outcome)} {}
141 operator bool() noexcept(noexcept(
bool(std::declval<Outcome&>()))) {
142 return bool(_outcome);
146 explicit constexpr
operator bool() const
147 noexcept(noexcept(
bool(std::declval<const Outcome&>()))) {
148 return bool(_outcome);
154 -> decltype(
extract(_outcome)) {
160 constexpr
auto get() const noexcept(noexcept(
extract(_outcome)))
161 -> decltype(
extract(_outcome)) {
167 constexpr
operator decltype(
extract(std::declval<Outcome&>()))() noexcept(
174 constexpr
operator decltype(
extract(std::declval<const Outcome&>()))()
const
175 noexcept(noexcept(
extract(_outcome))) {
179 constexpr
auto nok() const noexcept
180 -> decltype(_traits::nok_info(_outcome)) {
184 constexpr
auto operator!() const noexcept
185 -> decltype(_traits::nok_info(_outcome)) {
192 template <
typename Outcome>
199 template <
typename Outcome>
202 decltype(std::declval<
const ok<Outcome>&>().get().begin())* =
nullptr) {
203 return x.
get().begin();
208 template <
typename Outcome>
211 decltype(std::declval<
const ok<Outcome>&>().get().end())* =
nullptr) {
212 return x.
get().end();
217 #endif // EAGINE_EXTRACT_HPP
constexpr ok(Outcome &&outcome) noexcept(noexcept(std::declval< Outcome && >()))
Construction from a function call outcome object.
Definition: extract.hpp:136
static constexpr auto extract_or(T *ptr, std::remove_const_t< T > &fallback) noexcept -> T &
Checks ptr and dereferences it if not null, otherwise returns fallback.
Definition: extract.hpp:29
Common code is placed in this namespace.
Definition: eagine.hpp:21
constexpr auto get() noexcept(noexcept(extract(_outcome))) -> decltype(extract(_outcome))
Extracts the stored outcome value.
Definition: extract.hpp:153
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
Class representing "none" / "nothing" values.
Definition: nothing.hpp:17
auto end(const ok< Outcome > &x, decltype(std::declval< const ok< Outcome > & >().get().end()) *=nullptr)
Overload of begin for instantiations of the ok template.
Definition: extract.hpp:209
auto begin(const ok< Outcome > &x, decltype(std::declval< const ok< Outcome > & >().get().begin()) *=nullptr)
Overload of begin for instantiations of the ok template.
Definition: extract.hpp:200
static constexpr auto nok_info(const Outcome &) noexcept -> nothing_t
Returns additional info type for values that are not-OK.
Definition: extract.hpp:105
Traits used for customization of class ok for the specified Outcome.
Definition: extract.hpp:103
Value typically wrapping function call result and success indicator.
Definition: extract.hpp:128
constexpr auto get() const noexcept(noexcept(extract(_outcome))) -> decltype(extract(_outcome))
Extracts the stored outcome value.
Definition: extract.hpp:160
auto extract(const ok< Outcome > &x) noexcept -> const auto &
Overload of extract for instantiations of the ok template.
Definition: extract.hpp:193