blob: 67a1a5c5e55c7c3bcc22b1d566c0e1ce5b33802a [file] [log] [blame]
Martin Kreichgauer17c30572017-07-18 12:42:18 -07001/* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#ifndef OPENSSL_HEADER_SSL_SPAN_H
16#define OPENSSL_HEADER_SSL_SPAN_H
17
18#include <openssl/base.h>
19
20#if !defined(BORINGSSL_NO_CXX)
21
David Benjaminaf2b1e82017-07-27 14:05:20 -040022extern "C++" {
23
David Benjamin061a7f52020-12-15 13:47:05 -050024#include <stdlib.h>
25
Martin Kreichgauer17c30572017-07-18 12:42:18 -070026#include <algorithm>
27#include <type_traits>
28
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070029BSSL_NAMESPACE_BEGIN
Martin Kreichgauer17c30572017-07-18 12:42:18 -070030
31template <typename T>
32class Span;
33
34namespace internal {
35template <typename T>
36class SpanBase {
David Benjamin4512b792017-08-18 19:21:50 -040037 // Put comparison operator implementations into a base class with const T, so
38 // they can be used with any type that implicitly converts into a Span.
Martin Kreichgauer17c30572017-07-18 12:42:18 -070039 static_assert(std::is_const<T>::value,
40 "Span<T> must be derived from SpanBase<const T>");
41
Martin Kreichgauer17c30572017-07-18 12:42:18 -070042 friend bool operator==(Span<T> lhs, Span<T> rhs) {
David Benjamin4512b792017-08-18 19:21:50 -040043 // MSVC issues warning C4996 because std::equal is unsafe. The pragma to
44 // suppress the warning mysteriously has no effect, hence this
45 // implementation. See
46 // https://msdn.microsoft.com/en-us/library/aa985974.aspx.
Martin Kreichgauer2eee1312017-07-24 15:49:07 -070047 if (lhs.size() != rhs.size()) {
48 return false;
49 }
50 for (T *l = lhs.begin(), *r = rhs.begin(); l != lhs.end() && r != rhs.end();
51 ++l, ++r) {
52 if (*l != *r) {
53 return false;
54 }
55 }
56 return true;
Martin Kreichgauer17c30572017-07-18 12:42:18 -070057 }
Martin Kreichgauer17c30572017-07-18 12:42:18 -070058
59 friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); }
60};
61} // namespace internal
62
David Benjamin4512b792017-08-18 19:21:50 -040063// A Span<T> is a non-owning reference to a contiguous array of objects of type
64// |T|. Conceptually, a Span is a simple a pointer to |T| and a count of
65// elements accessible via that pointer. The elements referenced by the Span can
66// be mutated if |T| is mutable.
67//
68// A Span can be constructed from container types implementing |data()| and
69// |size()| methods. If |T| is constant, construction from a container type is
70// implicit. This allows writing methods that accept data from some unspecified
71// container type:
72//
73// // Foo views data referenced by v.
74// void Foo(bssl::Span<const uint8_t> v) { ... }
75//
76// std::vector<uint8_t> vec;
77// Foo(vec);
78//
79// For mutable Spans, conversion is explicit:
80//
81// // FooMutate mutates data referenced by v.
82// void FooMutate(bssl::Span<uint8_t> v) { ... }
83//
84// FooMutate(bssl::Span<uint8_t>(vec));
85//
86// You can also use the |MakeSpan| and |MakeConstSpan| factory methods to
87// construct Spans in order to deduce the type of the Span automatically.
88//
89// FooMutate(bssl::MakeSpan(vec));
90//
91// Note that Spans have value type sematics. They are cheap to construct and
92// copy, and should be passed by value whenever a method would otherwise accept
93// a reference or pointer to a container or array.
Martin Kreichgauer17c30572017-07-18 12:42:18 -070094template <typename T>
95class Span : private internal::SpanBase<const T> {
96 private:
David Benjamin12fdd082017-10-05 13:28:54 -040097 static const size_t npos = static_cast<size_t>(-1);
David Benjaminb9493552017-09-27 19:02:51 -040098
David Benjamin21440762022-03-19 18:39:18 -040099 // Heuristically test whether C is a container type that can be converted into
100 // a Span by checking for data() and size() member functions.
101 //
David Benjamin493d5cb2022-04-18 17:20:27 -0400102 // TODO(davidben): Require C++17 support for std::is_convertible_v, etc.
David Benjamin21440762022-03-19 18:39:18 -0400103 template <typename C>
David Benjamin493d5cb2022-04-18 17:20:27 -0400104 using EnableIfContainer = std::enable_if_t<
David Benjamin21440762022-03-19 18:39:18 -0400105 std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
David Benjamin493d5cb2022-04-18 17:20:27 -0400106 std::is_integral<decltype(std::declval<C>().size())>::value>;
David Benjamin21440762022-03-19 18:39:18 -0400107
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700108 public:
109 constexpr Span() : Span(nullptr, 0) {}
110 constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {}
111
112 template <size_t N>
113 constexpr Span(T (&array)[N]) : Span(array, N) {}
114
David Benjamin493d5cb2022-04-18 17:20:27 -0400115 template <typename C, typename = EnableIfContainer<C>,
116 typename = std::enable_if_t<std::is_const<T>::value, C>>
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700117 Span(const C &container) : data_(container.data()), size_(container.size()) {}
118
David Benjamin493d5cb2022-04-18 17:20:27 -0400119 template <typename C, typename = EnableIfContainer<C>,
120 typename = std::enable_if_t<!std::is_const<T>::value, C>>
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700121 explicit Span(C &container)
122 : data_(container.data()), size_(container.size()) {}
123
124 T *data() const { return data_; }
125 size_t size() const { return size_; }
David Benjaminb9493552017-09-27 19:02:51 -0400126 bool empty() const { return size_ == 0; }
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700127
128 T *begin() const { return data_; }
129 const T *cbegin() const { return data_; }
David Benjamina6c689e2019-02-07 11:11:47 -0600130 T *end() const { return data_ + size_; }
131 const T *cend() const { return end(); }
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700132
David Benjaminc64d1232017-10-04 18:14:28 -0400133 T &front() const {
David Benjamin68478b72018-04-08 14:03:10 -0400134 if (size_ == 0) {
135 abort();
136 }
David Benjaminc64d1232017-10-04 18:14:28 -0400137 return data_[0];
138 }
139 T &back() const {
David Benjamin68478b72018-04-08 14:03:10 -0400140 if (size_ == 0) {
141 abort();
142 }
David Benjaminc64d1232017-10-04 18:14:28 -0400143 return data_[size_ - 1];
144 }
145
David Benjamin68478b72018-04-08 14:03:10 -0400146 T &operator[](size_t i) const {
147 if (i >= size_) {
148 abort();
149 }
150 return data_[i];
151 }
152 T &at(size_t i) const { return (*this)[i]; }
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700153
David Benjaminb9493552017-09-27 19:02:51 -0400154 Span subspan(size_t pos = 0, size_t len = npos) const {
155 if (pos > size_) {
David Benjamin006f20a2021-06-22 23:00:49 -0400156 // absl::Span throws an exception here. Note std::span and Chromium
157 // base::span additionally forbid pos + len being out of range, with a
158 // special case at npos/dynamic_extent, while absl::Span::subspan clips
159 // the span. For now, we align with absl::Span in case we switch to it in
160 // the future.
161 abort();
David Benjaminb9493552017-09-27 19:02:51 -0400162 }
163 return Span(data_ + pos, std::min(size_ - pos, len));
164 }
165
David Benjamin006f20a2021-06-22 23:00:49 -0400166 Span first(size_t len) {
167 if (len > size_) {
168 abort();
169 }
170 return Span(data_, len);
171 }
172
173 Span last(size_t len) {
174 if (len > size_) {
175 abort();
176 }
177 return Span(data_ + size_ - len, len);
178 }
179
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700180 private:
181 T *data_;
182 size_t size_;
183};
184
185template <typename T>
David Benjaminb9493552017-09-27 19:02:51 -0400186const size_t Span<T>::npos;
187
188template <typename T>
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700189Span<T> MakeSpan(T *ptr, size_t size) {
190 return Span<T>(ptr, size);
191}
192
193template <typename C>
194auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) {
195 return MakeSpan(c.data(), c.size());
196}
197
198template <typename T>
199Span<const T> MakeConstSpan(T *ptr, size_t size) {
200 return Span<const T>(ptr, size);
201}
202
203template <typename C>
204auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) {
205 return MakeConstSpan(c.data(), c.size());
206}
207
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700208BSSL_NAMESPACE_END
Martin Kreichgauer17c30572017-07-18 12:42:18 -0700209
210} // extern C++
211
212#endif // !defined(BORINGSSL_NO_CXX)
213
David Benjamin4512b792017-08-18 19:21:50 -0400214#endif // OPENSSL_HEADER_SSL_SPAN_H