single_view.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_SINGLE_VIEW_H
  10. #define _LIBCPP___RANGES_SINGLE_VIEW_H
  11. #include <__config>
  12. #include <__ranges/copyable_box.h>
  13. #include <__ranges/view_interface.h>
  14. #include <__utility/forward.h>
  15. #include <__utility/in_place.h>
  16. #include <__utility/move.h>
  17. #include <concepts>
  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. template<copy_constructible _Tp>
  26. requires is_object_v<_Tp>
  27. class single_view : public view_interface<single_view<_Tp>> {
  28. __copyable_box<_Tp> __value_;
  29. public:
  30. _LIBCPP_HIDE_FROM_ABI
  31. single_view() requires default_initializable<_Tp> = default;
  32. _LIBCPP_HIDE_FROM_ABI
  33. constexpr explicit single_view(const _Tp& __t) : __value_(in_place, __t) {}
  34. _LIBCPP_HIDE_FROM_ABI
  35. constexpr explicit single_view(_Tp&& __t) : __value_(in_place, std::move(__t)) {}
  36. template<class... _Args>
  37. requires constructible_from<_Tp, _Args...>
  38. _LIBCPP_HIDE_FROM_ABI
  39. constexpr explicit single_view(in_place_t, _Args&&... __args)
  40. : __value_{in_place, std::forward<_Args>(__args)...} {}
  41. _LIBCPP_HIDE_FROM_ABI
  42. constexpr _Tp* begin() noexcept { return data(); }
  43. _LIBCPP_HIDE_FROM_ABI
  44. constexpr const _Tp* begin() const noexcept { return data(); }
  45. _LIBCPP_HIDE_FROM_ABI
  46. constexpr _Tp* end() noexcept { return data() + 1; }
  47. _LIBCPP_HIDE_FROM_ABI
  48. constexpr const _Tp* end() const noexcept { return data() + 1; }
  49. _LIBCPP_HIDE_FROM_ABI
  50. static constexpr size_t size() noexcept { return 1; }
  51. _LIBCPP_HIDE_FROM_ABI
  52. constexpr _Tp* data() noexcept { return __value_.operator->(); }
  53. _LIBCPP_HIDE_FROM_ABI
  54. constexpr const _Tp* data() const noexcept { return __value_.operator->(); }
  55. };
  56. template<class _Tp>
  57. single_view(_Tp) -> single_view<_Tp>;
  58. } // namespace ranges
  59. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  60. _LIBCPP_END_NAMESPACE_STD
  61. #endif // _LIBCPP___RANGES_SINGLE_VIEW_H