Nui
Loading...
Searching...
No Matches
lazy.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <functional>
5
6namespace Nui
7{
8 template <typename ValueT>
9 class Lazy
10 {
11 public:
12 Lazy() = default;
13 Lazy(Lazy const&) = default;
14 Lazy(Lazy&&) = default;
15 Lazy& operator=(Lazy const&) = default;
16 Lazy& operator=(Lazy&&) = default;
17
18 template <typename FuncT>
19 Lazy(FuncT&& func)
20 : value_{}
21 , obtainer_{std::forward<FuncT>(func)}
22 {}
23
24 explicit operator bool() const
25 {
26 return value_.has_value();
27 }
28
29 bool hasValue() const
30 {
31 return value_.has_value();
32 }
33
34 bool tryObtainValue() const
35 {
36 if (!value_)
37 value_ = obtainer_();
38 return hasValue();
39 }
40
41 std::optional<ValueT> const& value() const&
42 {
43 if (!value_)
44 value_ = obtainer_();
45 return value_;
46 }
47
48 std::optional<ValueT>&& value() &&
49 {
50 if (!value_)
51 value_ = obtainer_();
52 return std::move(value_);
53 }
54
55 std::optional<ValueT> value() const&&
56 {
57 if (!value_)
58 value_ = obtainer_();
59 return value_;
60 }
61
62 std::optional<ValueT>& value() &
63 {
64 if (!value_)
65 value_ = obtainer_();
66 return value_;
67 }
68
69 private:
70 std::optional<ValueT> value_{};
71 std::function<std::optional<ValueT>()> obtainer_{};
72 };
73}
Definition lazy.hpp:10
std::optional< ValueT > value() const &&
Definition lazy.hpp:55
Lazy(Lazy &&)=default
std::optional< ValueT > & value() &
Definition lazy.hpp:62
std::optional< ValueT > const & value() const &
Definition lazy.hpp:41
Lazy & operator=(Lazy &&)=default
std::optional< ValueT > && value() &&
Definition lazy.hpp:48
Lazy()=default
Lazy(FuncT &&func)
Definition lazy.hpp:19
Lazy(Lazy const &)=default
bool tryObtainValue() const
Definition lazy.hpp:34
Lazy & operator=(Lazy const &)=default
bool hasValue() const
Definition lazy.hpp:29
Definition file_dialog.hpp:6