non_propagating_cache.h 3.7 KB

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