Horizon
single.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 
14 #ifndef RANGES_V3_VIEW_SINGLE_HPP
15 #define RANGES_V3_VIEW_SINGLE_HPP
16 
17 #include <type_traits>
18 #include <utility>
19 
20 #include <range/v3/range_fwd.hpp>
21 
27 #include <range/v3/utility/addressof.hpp>
30 #include <range/v3/utility/static_const.hpp>
31 #include <range/v3/view/facade.hpp>
32 
33 #include <range/v3/detail/prologue.hpp>
34 
35 namespace ranges
36 {
39  template<typename T>
40  struct single_view : view_interface<single_view<T>, (cardinality)1>
41  {
42  private:
43  CPP_assert(copy_constructible<T>);
44  static_assert(std::is_object<T>::value,
45  "The template parameter of single_view must be an object type");
46  semiregular_box_t<T> value_;
47  template<typename... Args>
48  constexpr single_view(in_place_t, std::true_type, Args &&... args)
49  : value_{static_cast<Args &&>(args)...}
50  {}
51  template<typename... Args>
52  constexpr single_view(in_place_t, std::false_type, Args &&... args)
53  : value_{in_place, static_cast<Args &&>(args)...}
54  {}
55 
56  public:
57  single_view() = default;
58  constexpr explicit single_view(T const & t)
59  : value_(t)
60  {}
61  constexpr explicit single_view(T && t)
62  : value_(std::move(t))
63  {}
64  template(class... Args)(
65  requires constructible_from<T, Args...>)
66  constexpr single_view(in_place_t, Args &&... args)
67  : single_view{in_place,
68  meta::bool_<(bool)semiregular<T>>{},
69  static_cast<Args &&>(args)...}
70  {}
71  constexpr T * begin() noexcept
72  {
73  return data();
74  }
75  constexpr T const * begin() const noexcept
76  {
77  return data();
78  }
79  constexpr T * end() noexcept
80  {
81  return data() + 1;
82  }
83  constexpr T const * end() const noexcept
84  {
85  return data() + 1;
86  }
87  static constexpr std::size_t size() noexcept
88  {
89  return 1u;
90  }
91  constexpr T * data() noexcept
92  {
93  return detail::addressof(static_cast<T &>(value_));
94  }
95  constexpr T const * data() const noexcept
96  {
97  return detail::addressof(static_cast<T const &>(value_));
98  }
99  };
100 
101 #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
102  template<class T>
103  explicit single_view(T &&) //
105 #endif
106 
107  namespace views
108  {
109  struct single_fn
110  {
111  template(typename Val)(
112  requires copy_constructible<Val>)
113  single_view<Val> operator()(Val value) const
114  {
115  return single_view<Val>{std::move(value)};
116  }
117  };
118 
122  } // namespace views
123 
124  namespace cpp20
125  {
126  namespace views
127  {
128  using ranges::views::single;
129  }
130  template(typename T)(
131  requires std::is_object<T>::value) //
132  using single_view = ranges::single_view<T>;
133  } // namespace cpp20
135 } // namespace ranges
136 
137 #include <range/v3/detail/epilogue.hpp>
138 #include <range/v3/detail/satisfy_boost_range.hpp>
139 RANGES_SATISFY_BOOST_RANGE(::ranges::single_view)
140 
141 #endif
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
std::integral_constant< bool, B > bool_
An integral constant wrapper for bool.
Definition: meta.hpp:168
std::integral_constant< std::size_t, N > size_t
An integral constant wrapper for std::size_t.
Definition: meta.hpp:163
Definition: in_place.hpp:27
Definition: single.hpp:41
Definition: interface.hpp:129
Definition: single.hpp:110