Simple-Utility v2.3.1
Loading...
Searching...
No Matches
value_or.hpp
Go to the documentation of this file.
1// Copyright Dominic Koepke 2019 - 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef SL_UTILITY_NULLABLES_VALUE_OR_HPP
7#define SL_UTILITY_NULLABLES_VALUE_OR_HPP
8
9#pragma once
10
13
14namespace sl::nullables::detail
15{
16 template <input_nullable TInputNullable, std::convertible_to<value_t<TInputNullable>> TAlternative>
17 [[nodiscard]]
18 constexpr value_t<TInputNullable> value_or(TInputNullable&& inputNullable, TAlternative&& alternative)
19 {
20 if (inputNullable != null_v<TInputNullable>)
21 {
22 return value_t<TInputNullable>{unwrap(std::forward<TInputNullable>(inputNullable))};
23 }
24 return value_t<TInputNullable>{std::forward<TAlternative>(alternative)};
25 }
26
27 class value_or_caller_fn
28 {
29 public:
30 template <input_nullable TInputNullable, std::convertible_to<value_t<TInputNullable>> TAlternative>
31 [[nodiscard]]
32 constexpr value_t<TInputNullable> operator ()(
33 TInputNullable&& inputNullable,
34 TAlternative&& alternative
35 ) const
36 noexcept(noexcept(value_or(std::declval<TInputNullable>(), std::declval<TAlternative>())))
37 {
38 return value_or(
39 std::forward<TInputNullable>(inputNullable),
40 std::forward<TAlternative>(alternative)
41 );
42 }
43 };
44}
45
46namespace sl::nullables
47{
73 inline constexpr auto value_or = []<class Value>(
74 Value&& value
75 ) noexcept(std::is_nothrow_constructible_v<std::remove_cvref_t<Value>, Value>)
76 {
77 return Algorithm{functional::bind_back(detail::value_or_caller_fn{}, std::forward<Value>(value))};
78 };
79
81}
82
83#endif
The core algorithm helper, which executes the held operation when used as the right-hand-side of oper...
Definition: base.hpp:216
Checks whether the given template type is usable as value type for unique_handle types.
Definition: unique_handle.hpp:161
constexpr auto bind_back
Helper function, which generates a forwarding call wrapper for the given function and curries the par...
Definition: bind_back.hpp:100
constexpr auto value_or
Retrieves the value of a input_nullable if it's not equal to its ''null''-object. Returns the alterna...
Definition: value_or.hpp:73
constexpr detail::unwrap_fn unwrap
Retrieves the value of the given input_nullable.
Definition: base.hpp:160
Definition: adapter.hpp:19