|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include <stdx/compiler.hpp> |
4 | | -#include <stdx/functional.hpp> |
| 4 | +#include <stdx/latched.hpp> |
5 | 5 | #include <stdx/type_traits.hpp> |
6 | 6 |
|
7 | | -#include <optional> |
8 | | -#include <type_traits> |
9 | | -#include <utility> |
10 | | - |
11 | 7 | namespace stdx { |
12 | 8 | inline namespace v1 { |
13 | | -template <typename F> struct cached { |
14 | | - using value_type = stdx::remove_cvref_t<std::invoke_result_t<F>>; |
15 | | - |
16 | | - constexpr explicit cached(F const &f) : lazy{f} {} |
17 | | - constexpr explicit cached(F &&f) : lazy{std::move(f)} {} |
18 | | - |
19 | | - constexpr auto has_value() const noexcept -> bool { |
20 | | - return opt.has_value(); |
21 | | - } |
22 | | - constexpr explicit operator bool() const noexcept { |
23 | | - return opt.has_value(); |
24 | | - } |
25 | | - |
26 | | - constexpr auto value() & LIFETIMEBOUND -> value_type & { |
27 | | - populate(); |
28 | | - return *opt; |
29 | | - } |
30 | | - constexpr auto value() const & LIFETIMEBOUND -> value_type const & { |
31 | | - populate(); |
32 | | - return *opt; |
33 | | - } |
34 | | - constexpr auto value() && LIFETIMEBOUND -> value_type && { |
35 | | - populate(); |
36 | | - return *std::move(opt); |
37 | | - } |
38 | | - constexpr auto value() const && LIFETIMEBOUND -> value_type const && { |
39 | | - populate(); |
40 | | - return *std::move(opt); |
41 | | - } |
42 | | - |
43 | | - constexpr auto operator->() const LIFETIMEBOUND->value_type const * { |
44 | | - populate(); |
45 | | - return opt.operator->(); |
46 | | - } |
47 | | - constexpr auto operator->() LIFETIMEBOUND->value_type * { |
48 | | - populate(); |
49 | | - return opt.operator->(); |
50 | | - } |
| 9 | +template <typename F> struct cached : latched<F> { |
| 10 | + using latched<F>::latched; |
51 | 11 |
|
52 | | - constexpr auto operator*() const & LIFETIMEBOUND->decltype(auto) { |
53 | | - return value(); |
54 | | - } |
55 | | - constexpr auto operator*() & LIFETIMEBOUND->decltype(auto) { |
56 | | - return value(); |
| 12 | + auto reset() { this->opt.reset(); } |
| 13 | + auto refresh() LIFETIMEBOUND -> typename latched<F>::value_type & { |
| 14 | + this->opt.reset(); |
| 15 | + this->populate(); |
| 16 | + return *this->opt; |
57 | 17 | } |
58 | | - constexpr auto operator*() const && LIFETIMEBOUND->decltype(auto) { |
59 | | - return std::move(*this).value(); |
60 | | - } |
61 | | - constexpr auto operator*() && LIFETIMEBOUND->decltype(auto) { |
62 | | - return std::move(*this).value(); |
63 | | - } |
64 | | - |
65 | | - auto reset() { opt.reset(); } |
66 | | - auto refresh() LIFETIMEBOUND -> value_type & { |
67 | | - opt.reset(); |
68 | | - populate(); |
69 | | - return *opt; |
70 | | - } |
71 | | - |
72 | | - private: |
73 | | - constexpr auto populate() const { |
74 | | - if (not opt.has_value()) { |
75 | | - opt.emplace(lazy); |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - with_result_of<F> lazy; |
80 | | - mutable std::optional<value_type> opt{}; |
81 | 18 | }; |
82 | 19 |
|
83 | | -template <typename C> |
84 | | -using cached_value_t = typename stdx::remove_cvref_t<C>::value_type; |
| 20 | +template <typename F> cached(F) -> cached<remove_cvref_t<F>>; |
| 21 | + |
| 22 | +template <typename C> using cached_value_t = latched_value_t<C>; |
85 | 23 | } // namespace v1 |
86 | 24 | } // namespace stdx |
0 commit comments