1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H
10 #define _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H
11 
12 #include <__config>
13 #include <__iterator/concepts.h>        // indirectly_readable
14 #include <__iterator/iterator_traits.h> // iter_reference_t
15 #include <__memory/addressof.h>
16 #include <concepts>                     // constructible_from
17 #include <optional>
18 #include <type_traits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 // clang-format off
30 
31 #if !defined(_LIBCPP_HAS_NO_RANGES)
32 
33 namespace ranges {
34   // __non_propagating_cache is a helper type that allows storing an optional value in it,
35   // but which does not copy the source's value when it is copy constructed/assigned to,
36   // and which resets the source's value when it is moved-from.
37   //
38   // This type is used as an implementation detail of some views that need to cache the
39   // result of `begin()` in order to provide an amortized O(1) begin() method. Typically,
40   // we don't want to propagate the value of the cache upon copy because the cached iterator
41   // may refer to internal details of the source view.
42   template<class _Tp>
43     requires is_object_v<_Tp>
44   class _LIBCPP_TEMPLATE_VIS __non_propagating_cache {
45     optional<_Tp> __value_ = nullopt;
46 
47   public:
48     _LIBCPP_HIDE_FROM_ABI __non_propagating_cache() = default;
49 
50     _LIBCPP_HIDE_FROM_ABI
51     constexpr __non_propagating_cache(__non_propagating_cache const&) noexcept
52       : __value_(nullopt)
53     { }
54 
55     _LIBCPP_HIDE_FROM_ABI
56     constexpr __non_propagating_cache(__non_propagating_cache&& __other) noexcept
57       : __value_(nullopt)
58     {
59       __other.__value_.reset();
60     }
61 
62     _LIBCPP_HIDE_FROM_ABI
63     constexpr __non_propagating_cache& operator=(__non_propagating_cache const& __other) noexcept {
64       if (this != _VSTD::addressof(__other)) {
65         __value_.reset();
66       }
67       return *this;
68     }
69 
70     _LIBCPP_HIDE_FROM_ABI
71     constexpr __non_propagating_cache& operator=(__non_propagating_cache&& __other) noexcept {
72       __value_.reset();
73       __other.__value_.reset();
74       return *this;
75     }
76 
77     _LIBCPP_HIDE_FROM_ABI
78     constexpr _Tp& operator*() { return *__value_; }
79     _LIBCPP_HIDE_FROM_ABI
80     constexpr _Tp const& operator*() const { return *__value_; }
81 
82     _LIBCPP_HIDE_FROM_ABI
83     constexpr bool __has_value() const { return __value_.has_value(); }
84     _LIBCPP_HIDE_FROM_ABI
85     constexpr void __set(_Tp const& __value) { __value_.emplace(__value); }
86     _LIBCPP_HIDE_FROM_ABI
87     constexpr void __set(_Tp&& __value) { __value_.emplace(_VSTD::move(__value)); }
88   };
89 
90   struct __empty_cache { };
91 } // namespace ranges
92 
93 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
94 
95 _LIBCPP_END_NAMESPACE_STD
96 
97 _LIBCPP_POP_MACROS
98 
99 #endif // _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H
100