blob: c21f4442e94be5aa10e7cd7f15a8b64dfffb1903 [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
22#include <algorithm>
23#include <type_traits>
24
25#include "../crypto/internal.h"
26
27extern "C++" {
28
29namespace bssl {
30
31template <typename T>
32class Span;
33
34namespace internal {
35template <typename T>
36class SpanBase {
37 /* 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. */
39 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) {
Martin Kreichgauer2eee1312017-07-24 15:49:07 -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. */
47 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
63/* 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. */
94template <typename T>
95class Span : private internal::SpanBase<const T> {
96 private:
97 template <bool B, class V = void>
98 using enable_if_t = typename std::enable_if<B, V>::type;
99
100 // Heuristically test whether C is a container type that can be converted into
101 // a Span by checking for data() and size() member functions.
102 template <typename C>
103 using EnableIfContainer = enable_if_t<
104 std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
105 std::is_integral<decltype(std::declval<C>().size())>::value>;
106
107 public:
108 constexpr Span() : Span(nullptr, 0) {}
109 constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {}
110
111 template <size_t N>
112 constexpr Span(T (&array)[N]) : Span(array, N) {}
113
114 template <typename C, typename = EnableIfContainer<C>,
115 typename = enable_if_t<std::is_const<T>::value, C>>
116 Span(const C &container) : data_(container.data()), size_(container.size()) {}
117
118 template <typename C, typename = EnableIfContainer<C>,
119 typename = enable_if_t<!std::is_const<T>::value, C>>
120 explicit Span(C &container)
121 : data_(container.data()), size_(container.size()) {}
122
123 T *data() const { return data_; }
124 size_t size() const { return size_; }
125
126 T *begin() const { return data_; }
127 const T *cbegin() const { return data_; }
128 T *end() const { return data_ + size_; };
129 const T *cend() const { return end(); };
130
131 T &operator[](size_t i) const { return data_[i]; }
132 T &at(size_t i) const { return data_[i]; }
133
134 private:
135 T *data_;
136 size_t size_;
137};
138
139template <typename T>
140Span<T> MakeSpan(T *ptr, size_t size) {
141 return Span<T>(ptr, size);
142}
143
144template <typename C>
145auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) {
146 return MakeSpan(c.data(), c.size());
147}
148
149template <typename T>
150Span<const T> MakeConstSpan(T *ptr, size_t size) {
151 return Span<const T>(ptr, size);
152}
153
154template <typename C>
155auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) {
156 return MakeConstSpan(c.data(), c.size());
157}
158
159} // namespace bssl
160
161} // extern C++
162
163#endif // !defined(BORINGSSL_NO_CXX)
164
165#endif /* OPENSSL_HEADER_SSL_SPAN_H */