ゴミ箱
span.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/beast
8 //
9 
10 #ifndef BOOST_BEAST_CORE_SPAN_HPP
11 #define BOOST_BEAST_CORE_SPAN_HPP
12 
13 #include <boost/beast/config.hpp>
15 #include <algorithm>
16 #include <iterator>
17 #include <string>
18 #include <type_traits>
19 
20 namespace boost {
21 namespace beast {
22 
31 template<class T>
32 class span
33 {
34  T* data_ = nullptr;
35  std::size_t size_ = 0;
36 
37 public:
39  using element_type = T;
40 
42  using value_type = typename std::remove_const<T>::type;
43 
45  using index_type = std::ptrdiff_t;
46 
48  using pointer = T*;
49 
51  using reference = T&;
52 
54  using iterator = pointer;
55 
57  using const_pointer = T const*;
58 
60  using const_reference = T const&;
61 
64 
66  span() = default;
67 
69  span(span const&) = default;
70 
72  span& operator=(span const&) = default;
73 
80  span(T* data, std::size_t size)
81  : data_(data), size_(size)
82  {
83  }
84 
89  template<class ContiguousContainer
90 #if ! BOOST_BEAST_DOXYGEN
91  , class = typename std::enable_if<
93  ContiguousContainer, T>::value>::type
94 #endif
95  >
96  explicit
97  span(ContiguousContainer&& container)
98  : data_(container.data())
99  , size_(container.size())
100  {
101  }
102 
103 #if ! BOOST_BEAST_DOXYGEN
104  template<class CharT, class Traits, class Allocator>
105  explicit
106  span(std::basic_string<CharT, Traits, Allocator>& s)
107  : data_(&s[0])
108  , size_(s.size())
109  {
110  }
111 
112  template<class CharT, class Traits, class Allocator>
113  explicit
114  span(std::basic_string<CharT, Traits, Allocator> const& s)
115  : data_(s.data())
116  , size_(s.size())
117  {
118  }
119 #endif
120 
125  template<class ContiguousContainer>
126 #if BOOST_BEAST_DOXYGEN
127  span&
128 #else
129  typename std::enable_if<detail::is_contiguous_container<
130  ContiguousContainer, T>::value,
131  span&>::type
132 #endif
133  operator=(ContiguousContainer&& container)
134  {
135  data_ = container.data();
136  size_ = container.size();
137  return *this;
138  }
139 
140 #if ! BOOST_BEAST_DOXYGEN
141  template<class CharT, class Traits, class Allocator>
142  span&
143  operator=(std::basic_string<
144  CharT, Traits, Allocator>& s)
145  {
146  data_ = &s[0];
147  size_ = s.size();
148  return *this;
149  }
150 
151  template<class CharT, class Traits, class Allocator>
152  span&
153  operator=(std::basic_string<
154  CharT, Traits, Allocator> const& s)
155  {
156  data_ = s.data();
157  size_ = s.size();
158  return *this;
159  }
160 #endif
161 
163  bool
164  empty() const
165  {
166  return size_ == 0;
167  }
168 
170  T*
171  data() const
172  {
173  return data_;
174  }
175 
177  std::size_t
178  size() const
179  {
180  return size_;
181  }
182 
185  begin() const
186  {
187  return data_;
188  }
189 
192  cbegin() const
193  {
194  return data_;
195  }
196 
199  end() const
200  {
201  return data_ + size_;
202  }
203 
206  cend() const
207  {
208  return data_ + size_;
209  }
210 };
211 
212 } // beast
213 } // boost
214 
215 #endif
span(std::basic_string< CharT, Traits, Allocator > &s)
Definition: span.hpp:106
T const & const_reference
The const reference used by the container.
Definition: span.hpp:60
span(T *data, std::size_t size)
Definition: span.hpp:80
Definition: async_result.hpp:20
T * data() const
Returns a pointer to the beginning of the span.
Definition: span.hpp:171
pointer iterator
The iterator used by the container.
Definition: span.hpp:54
const_pointer const_iterator
The const iterator used by the container.
Definition: span.hpp:63
Definition: span.hpp:32
T const * const_pointer
The const pointer used by the container.
Definition: span.hpp:57
T * pointer
A pointer to a span element.
Definition: span.hpp:48
typename std::remove_const< T >::type value_type
The type of value of each span element.
Definition: span.hpp:42
span(std::basic_string< CharT, Traits, Allocator > const &s)
Definition: span.hpp:114
span(ContiguousContainer &&container)
Definition: span.hpp:97
span & operator=(std::basic_string< CharT, Traits, Allocator > const &s)
Definition: span.hpp:153
span()=default
Constructor.
std::size_t size() const
Returns the number of elements in the span.
Definition: span.hpp:178
std::enable_if< detail::is_contiguous_container< ContiguousContainer, T >::value, span & >::type operator=(ContiguousContainer &&container)
Definition: span.hpp:133
const_iterator cend() const
Returns an iterator to one past the end of the span.
Definition: span.hpp:206
span & operator=(std::basic_string< CharT, Traits, Allocator > &s)
Definition: span.hpp:143
T element_type
The type of value, including cv qualifiers.
Definition: span.hpp:39
T & reference
A reference to a span element.
Definition: span.hpp:51
span & operator=(span const &)=default
Assignment.
std::ptrdiff_t index_type
The type of integer used to index the span.
Definition: span.hpp:45
const_iterator cbegin() const
Returns an iterator to the beginning of the span.
Definition: span.hpp:192
bool empty() const
Returns true if the span is empty.
Definition: span.hpp:164
const_iterator end() const
Returns an iterator to one past the end of the span.
Definition: span.hpp:199
const_iterator begin() const
Returns an iterator to the beginning of the span.
Definition: span.hpp:185
Definition: type_traits.hpp:163