owning_view.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_OWNING_VIEW_H
  10. #define _LIBCPP___RANGES_OWNING_VIEW_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/movable.h>
  13. #include <__config>
  14. #include <__ranges/access.h>
  15. #include <__ranges/concepts.h>
  16. #include <__ranges/data.h>
  17. #include <__ranges/empty.h>
  18. #include <__ranges/enable_borrowed_range.h>
  19. #include <__ranges/size.h>
  20. #include <__ranges/view_interface.h>
  21. #include <__type_traits/remove_cvref.h>
  22. #include <__utility/move.h>
  23. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  24. # pragma GCC system_header
  25. #endif
  26. _LIBCPP_PUSH_MACROS
  27. #include <__undef_macros>
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. #if _LIBCPP_STD_VER >= 20
  30. namespace ranges {
  31. template<range _Rp>
  32. requires movable<_Rp> && (!__is_std_initializer_list<remove_cvref_t<_Rp>>)
  33. class owning_view : public view_interface<owning_view<_Rp>> {
  34. _Rp __r_ = _Rp();
  35. public:
  36. _LIBCPP_HIDE_FROM_ABI owning_view() requires default_initializable<_Rp> = default;
  37. _LIBCPP_HIDE_FROM_ABI constexpr owning_view(_Rp&& __r) : __r_(std::move(__r)) {}
  38. _LIBCPP_HIDE_FROM_ABI owning_view(owning_view&&) = default;
  39. _LIBCPP_HIDE_FROM_ABI owning_view& operator=(owning_view&&) = default;
  40. _LIBCPP_HIDE_FROM_ABI constexpr _Rp& base() & noexcept { return __r_; }
  41. _LIBCPP_HIDE_FROM_ABI constexpr const _Rp& base() const& noexcept { return __r_; }
  42. _LIBCPP_HIDE_FROM_ABI constexpr _Rp&& base() && noexcept { return std::move(__r_); }
  43. _LIBCPP_HIDE_FROM_ABI constexpr const _Rp&& base() const&& noexcept { return std::move(__r_); }
  44. _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Rp> begin() { return ranges::begin(__r_); }
  45. _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Rp> end() { return ranges::end(__r_); }
  46. _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const requires range<const _Rp> { return ranges::begin(__r_); }
  47. _LIBCPP_HIDE_FROM_ABI constexpr auto end() const requires range<const _Rp> { return ranges::end(__r_); }
  48. _LIBCPP_HIDE_FROM_ABI constexpr bool empty() requires requires { ranges::empty(__r_); }
  49. { return ranges::empty(__r_); }
  50. _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const requires requires { ranges::empty(__r_); }
  51. { return ranges::empty(__r_); }
  52. _LIBCPP_HIDE_FROM_ABI constexpr auto size() requires sized_range<_Rp>
  53. { return ranges::size(__r_); }
  54. _LIBCPP_HIDE_FROM_ABI constexpr auto size() const requires sized_range<const _Rp>
  55. { return ranges::size(__r_); }
  56. _LIBCPP_HIDE_FROM_ABI constexpr auto data() requires contiguous_range<_Rp>
  57. { return ranges::data(__r_); }
  58. _LIBCPP_HIDE_FROM_ABI constexpr auto data() const requires contiguous_range<const _Rp>
  59. { return ranges::data(__r_); }
  60. };
  61. _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(owning_view);
  62. template<class _Tp>
  63. inline constexpr bool enable_borrowed_range<owning_view<_Tp>> = enable_borrowed_range<_Tp>;
  64. } // namespace ranges
  65. #endif // _LIBCPP_STD_VER >= 20
  66. _LIBCPP_END_NAMESPACE_STD
  67. _LIBCPP_POP_MACROS
  68. #endif // _LIBCPP___RANGES_OWNING_VIEW_H