Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/non_propagating_cache.h
35235 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H10#define _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H1112#include <__config>13#include <__iterator/concepts.h> // indirectly_readable14#include <__iterator/iterator_traits.h> // iter_reference_t15#include <__memory/addressof.h>16#include <__utility/forward.h>17#include <optional>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_BEGIN_NAMESPACE_STD2425#if _LIBCPP_STD_VER >= 202627namespace ranges {28// __non_propagating_cache is a helper type that allows storing an optional value in it,29// but which does not copy the source's value when it is copy constructed/assigned to,30// and which resets the source's value when it is moved-from.31//32// This type is used as an implementation detail of some views that need to cache the33// result of `begin()` in order to provide an amortized O(1) begin() method. Typically,34// we don't want to propagate the value of the cache upon copy because the cached iterator35// may refer to internal details of the source view.36template <class _Tp>37requires is_object_v<_Tp>38class _LIBCPP_TEMPLATE_VIS __non_propagating_cache {39struct __from_tag {};40struct __forward_tag {};4142// This helper class is needed to perform copy and move elision when43// constructing the contained type from an iterator.44struct __wrapper {45template <class... _Args>46_LIBCPP_HIDE_FROM_ABI constexpr explicit __wrapper(__forward_tag, _Args&&... __args)47: __t_(std::forward<_Args>(__args)...) {}48template <class _Fn>49_LIBCPP_HIDE_FROM_ABI constexpr explicit __wrapper(__from_tag, _Fn const& __f) : __t_(__f()) {}50_Tp __t_;51};5253optional<__wrapper> __value_ = nullopt;5455public:56_LIBCPP_HIDE_FROM_ABI __non_propagating_cache() = default;5758_LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache(__non_propagating_cache const&) noexcept59: __value_(nullopt) {}6061_LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache(__non_propagating_cache&& __other) noexcept62: __value_(nullopt) {63__other.__value_.reset();64}6566_LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache& operator=(__non_propagating_cache const& __other) noexcept {67if (this != std::addressof(__other)) {68__value_.reset();69}70return *this;71}7273_LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache& operator=(__non_propagating_cache&& __other) noexcept {74__value_.reset();75__other.__value_.reset();76return *this;77}7879_LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() { return __value_->__t_; }80_LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const { return __value_->__t_; }8182_LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const { return __value_.has_value(); }8384template <class _Fn>85_LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace_from(_Fn const& __f) {86return __value_.emplace(__from_tag{}, __f).__t_;87}8889template <class... _Args>90_LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace(_Args&&... __args) {91return __value_.emplace(__forward_tag{}, std::forward<_Args>(__args)...).__t_;92}93};9495struct __empty_cache {};96} // namespace ranges9798#endif // _LIBCPP_STD_VER >= 2099100_LIBCPP_END_NAMESPACE_STD101102#endif // _LIBCPP___RANGES_NON_PROPAGATING_CACHE_H103104105