OGLplus  (0.59.0) a C++ wrapper for rendering APIs

optional_ref.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_OPTIONAL_REF_HPP
10 #define EAGINE_OPTIONAL_REF_HPP
11 
12 #include "assert.hpp"
13 #include "nothing.hpp"
14 #include "tribool.hpp"
15 #include <memory>
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
22 template <typename T>
24 public:
26  optional_reference_wrapper(T& ref) noexcept
27  : _ptr{std::addressof(ref)} {}
28 
31 
34 
36  auto operator=(optional_reference_wrapper&&) noexcept
37  -> optional_reference_wrapper& = default;
38 
40  auto operator=(const optional_reference_wrapper&)
41  -> optional_reference_wrapper& = default;
42 
43  ~optional_reference_wrapper() noexcept = default;
44 
47  constexpr optional_reference_wrapper(nothing_t) noexcept {}
48 
51  constexpr optional_reference_wrapper(std::nullptr_t) noexcept {}
52 
54  auto is_valid() const noexcept -> bool {
55  return _ptr != nullptr;
56  }
57 
60  explicit operator bool() const noexcept {
61  return is_valid();
62  }
63 
66  auto get() const noexcept -> T& {
67  EAGINE_ASSERT(is_valid());
68  return *_ptr;
69  }
70 
73  auto value() const noexcept -> const T& {
74  EAGINE_ASSERT(is_valid());
75  return *_ptr;
76  }
77 
80  template <typename U>
81  auto value_or(U&& fallback) const noexcept
82  -> std::enable_if_t<std::is_convertible_v<U, T>, T> {
83  if(is_valid()) {
84  return *_ptr;
85  }
86  return T(std::forward<U>(fallback));
87  }
88 
91  explicit operator T&() const noexcept {
92  return get();
93  }
94 
96  friend auto operator==(const optional_reference_wrapper& l, const T& r)
97  -> tribool {
98  if(l.is_valid()) {
99  return l.value() == r;
100  }
101  return indeterminate;
102  }
103 
105  friend auto operator!=(const optional_reference_wrapper& l, const T& r)
106  -> tribool {
107  if(l.is_valid()) {
108  return l.value() != r;
109  }
110  return indeterminate;
111  }
112 
113 private:
114  T* _ptr{nullptr};
115 };
116 //------------------------------------------------------------------------------
119 template <typename T>
120 static inline auto extract(optional_reference_wrapper<T> ref) noexcept -> T& {
121  return ref.get();
122 }
123 //------------------------------------------------------------------------------
126 template <typename T>
127 static inline auto
128 extract_or(optional_reference_wrapper<T> ref, T& fallback) noexcept -> T& {
129  if(ref) {
130  return ref.get();
131  }
132  return fallback;
133 }
134 //------------------------------------------------------------------------------
137 template <typename T, typename F>
138 static inline auto extract_or(optional_reference_wrapper<T> ref, F&& fallback)
139  -> std::enable_if_t<std::is_convertible_v<F, T>, T> {
140  if(ref) {
141  return ref.get();
142  }
143  return T{std::forward<F>(fallback)}; // NOLINT(hicpp-no-array-decay)
144 }
145 //------------------------------------------------------------------------------
146 } // namespace eagine
147 
148 #endif // EAGINE_OPTIONAL_REF_HPP
Common code is placed in this namespace.
Definition: eagine.hpp:21
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
static auto extract_or(optional_reference_wrapper< T > ref, T &fallback) noexcept -> T &
Overload of extract_or for optional_reference_wrapper.
Definition: optional_ref.hpp:128
friend auto operator!=(const optional_reference_wrapper &l, const T &r) -> tribool
Tri-state nonequality comparison of the referred instance with a value.
Definition: optional_ref.hpp:105
Optional reference to an instance of type T.
Definition: optional_ref.hpp:23
constexpr optional_reference_wrapper(std::nullptr_t) noexcept
Construction from nullptr.
Definition: optional_ref.hpp:51
constexpr static const indeterminate_t indeterminate
Constant representing unspecified tribool value.
Definition: tribool.hpp:24
optional_reference_wrapper(T &ref) noexcept
Construction from a reference to value of type T.
Definition: optional_ref.hpp:26
friend auto operator==(const optional_reference_wrapper &l, const T &r) -> tribool
Tri-state equality comparison of the referred instance with a value.
Definition: optional_ref.hpp:96
auto is_valid() const noexcept -> bool
Indicates if this stores a valid reference.
Definition: optional_ref.hpp:54
auto get() const noexcept -> T &
Returns the stored reference.
Definition: optional_ref.hpp:66
Tri-state boolean value.
Definition: tribool.hpp:61
auto value() const noexcept -> const T &
Returns the stored value.
Definition: optional_ref.hpp:73
auto value_or(U &&fallback) const noexcept -> std::enable_if_t< std::is_convertible_v< U, T >, T >
Returns the stored value if valid or fallback otherwise.
Definition: optional_ref.hpp:81

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).