VCCC  2024.05
VisualCamp Common C++ library
non_propagating_cache.hpp
Go to the documentation of this file.
1 //
2 // Created by yonggyulee on 1/24/24.
3 //
4 
5 #ifndef VCCC_RANGES_NON_PROPAGATING_CACHE_HPP
6 #define VCCC_RANGES_NON_PROPAGATING_CACHE_HPP
7 
8 #include <memory>
9 #include <type_traits>
10 
14 #include "vccc/optional.hpp"
15 
16 namespace vccc {
17 namespace ranges {
18 namespace detail {
19 
21 struct constructible_from_deref : std::false_type {};
22 template<typename T, typename U>
23 struct constructible_from_deref<T, U, true> : constructible_from<T, decltype(*std::declval<U>())> {};
24 
25 } // namespace detail
26 
29 
30 // https://eel.is/c++draft/range.adaptors#range.nonprop.cache
31 template<typename T>
32 class non_propagating_cache : private optional<T> {
33  using base = optional<T>;
34  public:
35  static_assert(std::is_object<T>::value, "Constraints not satisfied");
36 
37  using base::emplace;
38  using base::operator*;
39  using base::operator->;
40  using base::operator bool;
41  using base::has_value;
42  using base::value;
43  using base::value_or;
44  using base::and_then;
45  using base::transform;
46  using base::or_else;
47  using base::swap;
48  using base::reset;
49 
50  non_propagating_cache() = default;
51 
52  constexpr non_propagating_cache(const non_propagating_cache&) noexcept
53  : base() {}
54  constexpr non_propagating_cache(non_propagating_cache&& other) noexcept {
55  other.reset();
56  }
57 
58  constexpr non_propagating_cache& operator=(const non_propagating_cache& other) noexcept {
59  if (vccc::addressof(other) != this) {
60  reset();
61  }
62  return *this;
63  }
64 
65  constexpr non_propagating_cache& operator=(non_propagating_cache&& other) noexcept {
66  reset();
67  other.reset();
68  return *this;
69  }
70 
72  constexpr T& emplace_deref(const I& i) {
73  return emplace(*i);
74  }
75 };
76 
78 
79 } // namespace ranges
80 } // namespace vccc
81 
82 #endif // VCCC_RANGES_NON_PROPAGATING_CACHE_HPP
a wrapper that may or may not hold an object
Definition: optional.h:46
constexpr value_type value_or(U &&default_value) const &
Definition: optional.h:280
value_type & emplace()
Definition: optional.h:421
constexpr auto and_then(F &&f) &
Definition: optional.h:305
constexpr auto transform(F &&f) &
Definition: optional.h:340
constexpr bool has_value() const noexcept
Definition: optional.h:249
constexpr value_type & value() &
Definition: optional.h:255
void swap(optional &other) noexcept(std::is_nothrow_move_constructible< value_type >::value &&vccc::is_nothrow_swappable< value_type >::value)
Definition: optional.h:393
constexpr optional or_else(F &&f) const &
Definition: optional.h:379
Definition: non_propagating_cache.hpp:32
constexpr non_propagating_cache(non_propagating_cache &&other) noexcept
Definition: non_propagating_cache.hpp:54
constexpr non_propagating_cache & operator=(const non_propagating_cache &other) noexcept
Definition: non_propagating_cache.hpp:58
constexpr T & emplace_deref(const I &i)
Definition: non_propagating_cache.hpp:72
constexpr non_propagating_cache & operator=(non_propagating_cache &&other) noexcept
Definition: non_propagating_cache.hpp:65
constexpr non_propagating_cache(const non_propagating_cache &) noexcept
Definition: non_propagating_cache.hpp:52
std::enable_if_t< std::is_object< T >::value, T * > addressof(T &t) noexcept
Definition: addressof.hpp:33
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35