master
  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
 10#ifndef _LIBCPP___RANGES_SINGLE_VIEW_H
 11#define _LIBCPP___RANGES_SINGLE_VIEW_H
 12
 13#include <__concepts/constructible.h>
 14#include <__config>
 15#include <__cstddef/ptrdiff_t.h>
 16#include <__cstddef/size_t.h>
 17#include <__ranges/movable_box.h>
 18#include <__ranges/range_adaptor.h>
 19#include <__ranges/view_interface.h>
 20#include <__type_traits/decay.h>
 21#include <__type_traits/is_object.h>
 22#include <__utility/forward.h>
 23#include <__utility/in_place.h>
 24#include <__utility/move.h>
 25
 26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 27#  pragma GCC system_header
 28#endif
 29
 30_LIBCPP_PUSH_MACROS
 31#include <__undef_macros>
 32
 33_LIBCPP_BEGIN_NAMESPACE_STD
 34
 35#if _LIBCPP_STD_VER >= 20
 36
 37namespace ranges {
 38#  if _LIBCPP_STD_VER >= 23
 39template <move_constructible _Tp>
 40#  else
 41template <copy_constructible _Tp>
 42#  endif
 43  requires is_object_v<_Tp>
 44class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS single_view : public view_interface<single_view<_Tp>> {
 45  _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Tp> __value_;
 46
 47public:
 48  _LIBCPP_HIDE_FROM_ABI single_view()
 49    requires default_initializable<_Tp>
 50  = default;
 51
 52  _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(const _Tp& __t)
 53#  if _LIBCPP_STD_VER >= 23
 54    requires copy_constructible<_Tp>
 55#  endif
 56      : __value_(in_place, __t) {
 57  }
 58
 59  _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(_Tp&& __t) : __value_(in_place, std::move(__t)) {}
 60
 61  template <class... _Args>
 62    requires constructible_from<_Tp, _Args...>
 63  _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(in_place_t, _Args&&... __args)
 64      : __value_{in_place, std::forward<_Args>(__args)...} {}
 65
 66  _LIBCPP_HIDE_FROM_ABI constexpr _Tp* begin() noexcept { return data(); }
 67
 68  _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* begin() const noexcept { return data(); }
 69
 70  _LIBCPP_HIDE_FROM_ABI constexpr _Tp* end() noexcept { return data() + 1; }
 71
 72  _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* end() const noexcept { return data() + 1; }
 73
 74  _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return false; }
 75
 76  _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 1; }
 77
 78  _LIBCPP_HIDE_FROM_ABI constexpr _Tp* data() noexcept { return __value_.operator->(); }
 79
 80  _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* data() const noexcept { return __value_.operator->(); }
 81};
 82
 83template <class _Tp>
 84single_view(_Tp) -> single_view<_Tp>;
 85
 86namespace views {
 87namespace __single_view {
 88
 89struct __fn : __range_adaptor_closure<__fn> {
 90  template <class _Range>
 91  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
 92      noexcept(noexcept(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))))
 93          -> decltype(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))) {
 94    return single_view<decay_t<_Range&&>>(std::forward<_Range>(__range));
 95  }
 96};
 97} // namespace __single_view
 98
 99inline namespace __cpo {
100inline constexpr auto single = __single_view::__fn{};
101} // namespace __cpo
102
103} // namespace views
104} // namespace ranges
105
106#endif // _LIBCPP_STD_VER >= 20
107
108_LIBCPP_END_NAMESPACE_STD
109
110_LIBCPP_POP_MACROS
111
112#endif // _LIBCPP___RANGES_SINGLE_VIEW_H