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