Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:41

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___RANGES_NON_PROPAGATING_CACHE_H
0011 #define _LIBCPP___CXX03___RANGES_NON_PROPAGATING_CACHE_H
0012 
0013 #include <__cxx03/__config>
0014 #include <__cxx03/__iterator/concepts.h>        // indirectly_readable
0015 #include <__cxx03/__iterator/iterator_traits.h> // iter_reference_t
0016 #include <__cxx03/__memory/addressof.h>
0017 #include <__cxx03/__utility/forward.h>
0018 #include <__cxx03/optional>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025 
0026 #if _LIBCPP_STD_VER >= 20
0027 
0028 namespace ranges {
0029 // __non_propagating_cache is a helper type that allows storing an optional value in it,
0030 // but which does not copy the source's value when it is copy constructed/assigned to,
0031 // and which resets the source's value when it is moved-from.
0032 //
0033 // This type is used as an implementation detail of some views that need to cache the
0034 // result of `begin()` in order to provide an amortized O(1) begin() method. Typically,
0035 // we don't want to propagate the value of the cache upon copy because the cached iterator
0036 // may refer to internal details of the source view.
0037 template <class _Tp>
0038   requires is_object_v<_Tp>
0039 class _LIBCPP_TEMPLATE_VIS __non_propagating_cache {
0040   struct __from_tag {};
0041   struct __forward_tag {};
0042 
0043   // This helper class is needed to perform copy and move elision when
0044   // constructing the contained type from an iterator.
0045   struct __wrapper {
0046     template <class... _Args>
0047     _LIBCPP_HIDE_FROM_ABI constexpr explicit __wrapper(__forward_tag, _Args&&... __args)
0048         : __t_(std::forward<_Args>(__args)...) {}
0049     template <class _Fn>
0050     _LIBCPP_HIDE_FROM_ABI constexpr explicit __wrapper(__from_tag, _Fn const& __f) : __t_(__f()) {}
0051     _Tp __t_;
0052   };
0053 
0054   optional<__wrapper> __value_ = nullopt;
0055 
0056 public:
0057   _LIBCPP_HIDE_FROM_ABI __non_propagating_cache() = default;
0058 
0059   _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache(__non_propagating_cache const&) noexcept
0060       : __value_(nullopt) {}
0061 
0062   _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache(__non_propagating_cache&& __other) noexcept
0063       : __value_(nullopt) {
0064     __other.__value_.reset();
0065   }
0066 
0067   _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache& operator=(__non_propagating_cache const& __other) noexcept {
0068     if (this != std::addressof(__other)) {
0069       __value_.reset();
0070     }
0071     return *this;
0072   }
0073 
0074   _LIBCPP_HIDE_FROM_ABI constexpr __non_propagating_cache& operator=(__non_propagating_cache&& __other) noexcept {
0075     __value_.reset();
0076     __other.__value_.reset();
0077     return *this;
0078   }
0079 
0080   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& operator*() { return __value_->__t_; }
0081   _LIBCPP_HIDE_FROM_ABI constexpr _Tp const& operator*() const { return __value_->__t_; }
0082 
0083   _LIBCPP_HIDE_FROM_ABI constexpr bool __has_value() const { return __value_.has_value(); }
0084 
0085   template <class _Fn>
0086   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace_from(_Fn const& __f) {
0087     return __value_.emplace(__from_tag{}, __f).__t_;
0088   }
0089 
0090   template <class... _Args>
0091   _LIBCPP_HIDE_FROM_ABI constexpr _Tp& __emplace(_Args&&... __args) {
0092     return __value_.emplace(__forward_tag{}, std::forward<_Args>(__args)...).__t_;
0093   }
0094 };
0095 
0096 struct __empty_cache {};
0097 } // namespace ranges
0098 
0099 #endif // _LIBCPP_STD_VER >= 20
0100 
0101 _LIBCPP_END_NAMESPACE_STD
0102 
0103 #endif // _LIBCPP___CXX03___RANGES_NON_PROPAGATING_CACHE_H