blob: 7410bf906de572bbbec878bebc4aca082a595900 [file] [log] [blame]
Robert Sloanfe7cd212017-08-07 09:03:39 -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
22extern "C++" {
23
Pete Bentley17486112021-01-20 11:51:47 +000024#include <stdlib.h>
25
Robert Sloanfe7cd212017-08-07 09:03:39 -070026#include <algorithm>
27#include <type_traits>
28
Robert Sloan726e9d12018-09-11 11:45:04 -070029BSSL_NAMESPACE_BEGIN
Robert Sloanfe7cd212017-08-07 09:03:39 -070030
31template <typename T>
32class Span;
33
34namespace internal {
35template <typename T>
36class SpanBase {
Robert Sloan8f860b12017-08-28 07:37:06 -070037 // 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.
Robert Sloanfe7cd212017-08-07 09:03:39 -070039 static_assert(std::is_const<T>::value,
40 "Span<T> must be derived from SpanBase<const T>");
41
42 friend bool operator==(Span<T> lhs, Span<T> rhs) {
Robert Sloan8f860b12017-08-28 07:37:06 -070043 // 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.
Robert Sloanfe7cd212017-08-07 09:03:39 -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;
57 }
58
59 friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); }
60};
61} // namespace internal
62
Robert Sloan8f860b12017-08-28 07:37:06 -070063// 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.
Robert Sloanfe7cd212017-08-07 09:03:39 -070094template <typename T>
95class Span : private internal::SpanBase<const T> {
96 private:
Robert Sloanfe7cd212017-08-07 09:03:39 -070097 // Heuristically test whether C is a container type that can be converted into
98 // a Span by checking for data() and size() member functions.
Robert Sloan4562e9d2017-10-02 10:26:51 -070099 //
100 // TODO(davidben): Switch everything to std::enable_if_t when we remove
101 // support for MSVC 2015. Although we could write our own enable_if_t and MSVC
102 // 2015 has std::enable_if_t anyway, MSVC 2015's SFINAE implementation is
103 // problematic and does not work below unless we write the ::type at use.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700104 template <typename C>
Robert Sloan4562e9d2017-10-02 10:26:51 -0700105 using EnableIfContainer = std::enable_if<
Robert Sloanfe7cd212017-08-07 09:03:39 -0700106 std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
107 std::is_integral<decltype(std::declval<C>().size())>::value>;
108
Robert Sloanae1abf92017-10-05 12:50:08 -0700109 static const size_t npos = static_cast<size_t>(-1);
110
Robert Sloanfe7cd212017-08-07 09:03:39 -0700111 public:
112 constexpr Span() : Span(nullptr, 0) {}
113 constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {}
114
115 template <size_t N>
116 constexpr Span(T (&array)[N]) : Span(array, N) {}
117
Robert Sloan4562e9d2017-10-02 10:26:51 -0700118 template <
119 typename C, typename = typename EnableIfContainer<C>::type,
120 typename = typename std::enable_if<std::is_const<T>::value, C>::type>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700121 Span(const C &container) : data_(container.data()), size_(container.size()) {}
122
Robert Sloan4562e9d2017-10-02 10:26:51 -0700123 template <
124 typename C, typename = typename EnableIfContainer<C>::type,
125 typename = typename std::enable_if<!std::is_const<T>::value, C>::type>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700126 explicit Span(C &container)
127 : data_(container.data()), size_(container.size()) {}
128
129 T *data() const { return data_; }
130 size_t size() const { return size_; }
Robert Sloanae1abf92017-10-05 12:50:08 -0700131 bool empty() const { return size_ == 0; }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700132
133 T *begin() const { return data_; }
134 const T *cbegin() const { return data_; }
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800135 T *end() const { return data_ + size_; }
136 const T *cend() const { return end(); }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700137
Robert Sloan921ef2c2017-10-17 09:02:20 -0700138 T &front() const {
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700139 if (size_ == 0) {
140 abort();
141 }
Robert Sloan921ef2c2017-10-17 09:02:20 -0700142 return data_[0];
143 }
144 T &back() const {
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700145 if (size_ == 0) {
146 abort();
147 }
Robert Sloan921ef2c2017-10-17 09:02:20 -0700148 return data_[size_ - 1];
149 }
150
Robert Sloan5cbb5c82018-04-24 11:35:46 -0700151 T &operator[](size_t i) const {
152 if (i >= size_) {
153 abort();
154 }
155 return data_[i];
156 }
157 T &at(size_t i) const { return (*this)[i]; }
Robert Sloanfe7cd212017-08-07 09:03:39 -0700158
Robert Sloanae1abf92017-10-05 12:50:08 -0700159 Span subspan(size_t pos = 0, size_t len = npos) const {
160 if (pos > size_) {
161 abort(); // absl::Span throws an exception here.
162 }
163 return Span(data_ + pos, std::min(size_ - pos, len));
164 }
165
Robert Sloanfe7cd212017-08-07 09:03:39 -0700166 private:
167 T *data_;
168 size_t size_;
169};
170
171template <typename T>
Robert Sloanae1abf92017-10-05 12:50:08 -0700172const size_t Span<T>::npos;
173
174template <typename T>
Robert Sloanfe7cd212017-08-07 09:03:39 -0700175Span<T> MakeSpan(T *ptr, size_t size) {
176 return Span<T>(ptr, size);
177}
178
179template <typename C>
180auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) {
181 return MakeSpan(c.data(), c.size());
182}
183
184template <typename T>
185Span<const T> MakeConstSpan(T *ptr, size_t size) {
186 return Span<const T>(ptr, size);
187}
188
189template <typename C>
190auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) {
191 return MakeConstSpan(c.data(), c.size());
192}
193
Robert Sloan726e9d12018-09-11 11:45:04 -0700194BSSL_NAMESPACE_END
Robert Sloanfe7cd212017-08-07 09:03:39 -0700195
196} // extern C++
197
198#endif // !defined(BORINGSSL_NO_CXX)
199
Robert Sloan8f860b12017-08-28 07:37:06 -0700200#endif // OPENSSL_HEADER_SSL_SPAN_H