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
10#ifndef _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H
11#define _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H
12
13#include <__config>
14#include <__iterator/concepts.h> // indirectly_readable
15#include <__iterator/iterator_traits.h> // iter_reference_t
16#include <__memory/addressof.h>
17#include <__optional/nullopt_t.h>
18#include <__optional/optional.h>
19#include <__type_traits/is_object.h>
20#include <__utility/forward.h>
21
22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23# pragma GCC system_header
24#endif
25
26_LIBCPP_BEGIN_NAMESPACE_STD
27
28#if _LIBCPP_STD_VER >= 20
29
30namespace ranges {
31// __non_propagating_cache is a helper type that allows storing an optional value in it,
32// but which does not copy the source's value when it is copy constructed/assigned to,
33// and which resets the source's value when it is moved-from.
34//
35// This type is used as an implementation detail of some views that need to cache the
36// result of `begin()` in order to provide an amortized O(1) begin() method. Typically,
37// we don't want to propagate the value of the cache upon copy because the cached iterator
38// may refer to internal details of the source view.
39template <class _Tp>
40 requires is_object_v<_Tp>
41class __non_propagating_cache {
42 struct __from_tag {};
43 struct __forward_tag {};
44
45 // This helper class is needed to perform copy and move elision when
46 // constructing the contained type from an iterator.
47 struct __wrapper {
48 template <class... _Args>
49 _LIBCPP_HIDE_FROM_ABI constexpr explicit __wrapper(__forward_tag, _Args&&... __args)
50 : __t_(std::forward<_Args>(__args)...) {}
51 template <class _Fn>
52 _LIBCPP_HIDE_FROM_ABI constexpr explicit __wrapper(__from_tag, _Fn const& __f) : __t_(__f()) {}
53 _Tp __t_;
54 };
55
56 optional<__wrapper> __value_ = nullopt;
57
58public:
59 _LIBCPP_HIDE_FROM_ABI __non_propagating_cache() = default;
60
61 _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache(__non_propagating_cache const&) noexcept
62 : __value_(nullopt) {}
63
64 _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache(__non_propagating_cache&& __other) noexcept
65 : __value_(nullopt) {
66 __other.__value_.reset();
67 }
68
69 _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache& operator=(__non_propagating_cache const& __other) noexcept {
70 if (this != std::addressof(__other)) {
71 __value_.reset();
72 }
73 return *this;
74 }
75
76 _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache& operator=(__non_propagating_cache&& __other) noexcept {
77 __value_.reset();
78 __other.__value_.reset();
79 return *this;
80 }
81
82 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() { return __value_->__t_; }
83 _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const { return __value_->__t_; }
84
85 _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const { return __value_.has_value(); }
86
87 template <class _Fn>
88 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace_from(_Fn const& __f) {
89 return __value_.emplace(__from_tag{}, __f).__t_;
90 }
91
92 template <class... _Args>
93 _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace(_Args&&... __args) {
94 return __value_.emplace(__forward_tag{}, std::forward<_Args>(__args)...).__t_;
95 }
96};
97
98struct __empty_cache {};
99} // namespace ranges
100
101#endif // _LIBCPP_STD_VER >= 20
102
103_LIBCPP_END_NAMESPACE_STD
104
105#endif // _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H
106