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

decl.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_VALID_IF_DECL_HPP
10 #define EAGINE_VALID_IF_DECL_HPP
11 
12 #include "../assert.hpp"
13 #include "../tribool.hpp"
14 #include "../type_traits.hpp"
15 #include "base.hpp"
16 
17 namespace eagine {
18 //------------------------------------------------------------------------------
22  bool _is_valid{false};
23 
24  constexpr valid_flag_policy() noexcept = default;
25 
26  constexpr valid_flag_policy(bool is_valid) noexcept
27  : _is_valid(is_valid) {}
28 
30  template <typename T>
31  auto operator()(const T&) const noexcept -> bool {
32  return _is_valid;
33  }
34 
35  struct do_log {
36  template <typename X>
37  constexpr do_log(X) noexcept {}
38 
39  template <typename Log, typename T>
40  void operator()(Log& log, const T&) const {
41  log << "Getting the value of an empty optional";
42  }
43  };
44 };
45 //------------------------------------------------------------------------------
48 template <typename T, typename Policy, typename DoLog = typename Policy::do_log>
49 class valid_if : public basic_valid_if<T, Policy, DoLog> {
50 private:
52 
53  auto _base() noexcept -> _base_t& {
54  return *this;
55  }
56 
57  using _base_t::_get_value;
58 
59 public:
60  using _base_t::_base_t;
61  using _base_t::is_valid;
62  using _base_t::value;
63  using _base_t::value_or;
64 
66  auto operator=(const T& v) -> auto& {
67  _base() = v;
68  return *this;
69  }
70 
72  auto operator=(T&& v) -> auto& {
73  _base() = std::move(v);
74  return *this;
75  }
76 
79  explicit operator bool() const noexcept {
80  return is_valid();
81  }
82 
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>,
88  valid_if<std::result_of_t<Func(T)>, valid_flag_policy>> {
89  if(is_valid()) {
90  return {func(_get_value()), true};
91  }
92  return {};
93  }
94 
98  template <typename Func>
99  auto operator|(const Func& func) const {
100  return then(func);
101  }
102 
105  auto operator/(const T& fallback) const noexcept -> const T& {
106  return value_or(fallback);
107  }
108 
112  auto operator*() const noexcept -> const T& {
113  return value();
114  }
115 
119  auto operator->() const noexcept -> const T* {
120  return &value();
121  }
122 
124  constexpr auto operator==(const T& v) const -> tribool {
125  return {_get_value() == v, !is_valid()};
126  }
127 
129  constexpr auto operator!=(const T& v) const -> tribool {
130  return {_get_value() != v, !is_valid()};
131  }
132 
134  constexpr auto operator<(const T& v) const -> tribool {
135  return {_get_value() < v, !is_valid()};
136  }
137 
139  constexpr auto operator>(const T& v) const -> tribool {
140  return {_get_value() > v, !is_valid()};
141  }
142 
144  constexpr auto operator<=(const T& v) const -> tribool {
145  return {_get_value() <= v, !is_valid()};
146  }
147 
149  constexpr auto operator>=(const T& v) const -> tribool {
150  return {_get_value() >= v, !is_valid()};
151  }
152 };
153 //------------------------------------------------------------------------------
156 template <typename T, typename P1, typename P2>
157 static constexpr auto
158 operator==(const valid_if<T, P1>& v1, const valid_if<T, P2>& v2) noexcept
159  -> tribool {
160  return {
161  (v1.value_anyway() == v2.value_anyway()),
162  (!v1.is_valid() || !v2.is_valid())};
163 }
164 
167 template <typename T, typename P1, typename P2>
168 static constexpr auto
169 operator!=(const valid_if<T, P1>& v1, const valid_if<T, P2>& v2) noexcept
170  -> tribool {
171  return {
172  (v1.value_anyway() != v2.value_anyway()),
173  (!v1.is_valid() || !v2.is_valid())};
174 }
175 
178 template <typename T, typename P1, typename P2>
179 static constexpr auto
180 operator<(const valid_if<T, P1>& v1, const valid_if<T, P2>& v2) noexcept
181  -> tribool {
182  return {
183  (v1.value_anyway() < v2.value_anyway()),
184  (!v1.is_valid() || !v2.is_valid())};
185 }
186 
189 template <typename T, typename P1, typename P2>
190 static constexpr auto
191 operator>(const valid_if<T, P1>& v1, const valid_if<T, P2>& v2) noexcept
192  -> tribool {
193  return {
194  (v1.value_anyway() > v2.value_anyway()),
195  (!v1.is_valid() || !v2.is_valid())};
196 }
197 
200 template <typename T, typename P1, typename P2>
201 static constexpr auto
202 operator<=(const valid_if<T, P1>& v1, const valid_if<T, P2>& v2) noexcept
203  -> tribool {
204  return {
205  (v1.value_anyway() <= v2.value_anyway()),
206  (!v1.is_valid() || !v2.is_valid())};
207 }
208 
211 template <typename T, typename P1, typename P2>
212 static constexpr auto
213 operator>=(const valid_if<T, P1>& v1, const valid_if<T, P2>& v2) noexcept
214  -> tribool {
215  return {
216  (v1.value_anyway() >= v2.value_anyway()),
217  (!v1.is_valid() || !v2.is_valid())};
218 }
219 //------------------------------------------------------------------------------
222 template <typename T, typename P>
223 static constexpr auto extract(const valid_if<T, P>& opt) noexcept -> const T& {
224  return EAGINE_CONSTEXPR_ASSERT(bool(opt), opt.value_anyway());
225 }
226 
229 template <typename T, typename P>
230 static constexpr auto extract(valid_if<T, P>& opt) noexcept -> T& {
231  return EAGINE_CONSTEXPR_ASSERT(bool(opt), opt.value_anyway());
232 }
233 
236 template <typename T, typename P>
237 static constexpr auto extract(valid_if<T, P>&& opt) noexcept -> T&& {
238  return EAGINE_CONSTEXPR_ASSERT(bool(opt), std::move(opt.value_anyway()));
239 }
240 //------------------------------------------------------------------------------
242 template <typename T, typename P, typename F>
243 static constexpr auto
244 extract_or(const valid_if<T, P>& opt, F&& fallback) noexcept
245  -> std::enable_if_t<std::is_convertible_v<F, T>, T> {
246  if(bool(opt)) {
247  return opt.value_anyway();
248  }
249  return T{std::forward<F>(fallback)};
250 }
251 //------------------------------------------------------------------------------
253 template <typename T, typename P, typename F>
254 static constexpr auto extract_or(valid_if<T, P>& opt, T& fallback) noexcept
255  -> T& {
256  if(bool(opt)) {
257  return opt.value_anyway();
258  }
259  return fallback;
260 }
261 //------------------------------------------------------------------------------
263 template <typename T, typename P, typename F>
264 class valid_if_or_fallback : public valid_if<T, P> {
265 public:
268  noexcept(valid_if<T, P>(std::declval<valid_if<T, P>&&>())) && noexcept(
269  F(std::declval<F&&>())))
270  : valid_if<T, P>{std::move(vi)}
271  , _fallback{std::move(fallback)} {}
272 
274  auto fallback() const noexcept -> const F& {
275  return _fallback;
276  }
277 
279  auto fallback() noexcept -> F& {
280  return _fallback;
281  }
282 
283 private:
284  F _fallback{};
285 };
286 //------------------------------------------------------------------------------
289 template <typename T, typename P, typename F>
290 static inline auto either_or(valid_if<T, P> vi, F f) noexcept(
291  noexcept(valid_if<T, P>(std::declval<valid_if<T, P>&&>())) && noexcept(
292  F(std::declval<F&&>()))) -> valid_if_or_fallback<T, P, F> {
293  return {std::move(vi), std::move(f)};
294 }
295 //------------------------------------------------------------------------------
299 template <typename T>
301 //------------------------------------------------------------------------------
302 } // namespace eagine
303 
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

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