blob: 235af1d2b78d68b3e4e620c75975fc99ff3a1376 [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
42 OPENSSL_MSVC_PRAGMA(warning(push))
43 /* MSVC issues warning C4996 for calls to any unsafe methods in the stdlib.
44 * In this case, it complains about three-parameter std::equal, however
45 * the four-parameter variant is C++14. See
46 * https://msdn.microsoft.com/en-us/library/aa985974.aspx. */
47 OPENSSL_MSVC_PRAGMA(warning(disable : 4996))
48 friend bool operator==(Span<T> lhs, Span<T> rhs) {
49 return lhs.size() == rhs.size() &&
50 std::equal(lhs.begin(), lhs.end(), rhs.begin());
51 }
52 OPENSSL_MSVC_PRAGMA(warning(pop))
53
54 friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); }
55};
56} // namespace internal
57
58/* A Span<T> is a non-owning reference to a contiguous array of objects of type
59 * |T|. Conceptually, a Span is a simple a pointer to |T| and a count of
60 * elements accessible via that pointer. The elements referenced by the Span can
61 * be mutated if |T| is mutable.
62 *
63 * A Span can be constructed from container types implementing |data()| and
64 * |size()| methods. If |T| is constant, construction from a container type is
65 * implicit. This allows writing methods that accept data from some unspecified
66 * container type:
67 *
68 * // Foo views data referenced by v.
69 * void Foo(bssl::Span<const uint8_t> v) { ... }
70 *
71 * std::vector<uint8_t> vec;
72 * Foo(vec);
73 *
74 * For mutable Spans, conversion is explicit:
75 *
76 * // FooMutate mutates data referenced by v.
77 * void FooMutate(bssl::Span<uint8_t> v) { ... }
78 *
79 * FooMutate(bssl::Span<uint8_t>(vec));
80 *
81 * You can also use the |MakeSpan| and |MakeConstSpan| factory methods to
82 * construct Spans in order to deduce the type of the Span automatically.
83 *
84 * FooMutate(bssl::MakeSpan(vec));
85 *
86 * Note that Spans have value type sematics. They are cheap to construct and
87 * copy, and should be passed by value whenever a method would otherwise accept
88 * a reference or pointer to a container or array. */
89template <typename T>
90class Span : private internal::SpanBase<const T> {
91 private:
92 template <bool B, class V = void>
93 using enable_if_t = typename std::enable_if<B, V>::type;
94
95 // Heuristically test whether C is a container type that can be converted into
96 // a Span by checking for data() and size() member functions.
97 template <typename C>
98 using EnableIfContainer = enable_if_t<
99 std::is_convertible<decltype(std::declval<C>().data()), T *>::value &&
100 std::is_integral<decltype(std::declval<C>().size())>::value>;
101
102 public:
103 constexpr Span() : Span(nullptr, 0) {}
104 constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {}
105
106 template <size_t N>
107 constexpr Span(T (&array)[N]) : Span(array, N) {}
108
109 template <typename C, typename = EnableIfContainer<C>,
110 typename = enable_if_t<std::is_const<T>::value, C>>
111 Span(const C &container) : data_(container.data()), size_(container.size()) {}
112
113 template <typename C, typename = EnableIfContainer<C>,
114 typename = enable_if_t<!std::is_const<T>::value, C>>
115 explicit Span(C &container)
116 : data_(container.data()), size_(container.size()) {}
117
118 T *data() const { return data_; }
119 size_t size() const { return size_; }
120
121 T *begin() const { return data_; }
122 const T *cbegin() const { return data_; }
123 T *end() const { return data_ + size_; };
124 const T *cend() const { return end(); };
125
126 T &operator[](size_t i) const { return data_[i]; }
127 T &at(size_t i) const { return data_[i]; }
128
129 private:
130 T *data_;
131 size_t size_;
132};
133
134template <typename T>
135Span<T> MakeSpan(T *ptr, size_t size) {
136 return Span<T>(ptr, size);
137}
138
139template <typename C>
140auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) {
141 return MakeSpan(c.data(), c.size());
142}
143
144template <typename T>
145Span<const T> MakeConstSpan(T *ptr, size_t size) {
146 return Span<const T>(ptr, size);
147}
148
149template <typename C>
150auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) {
151 return MakeConstSpan(c.data(), c.size());
152}
153
154} // namespace bssl
155
156} // extern C++
157
158#endif // !defined(BORINGSSL_NO_CXX)
159
160#endif /* OPENSSL_HEADER_SSL_SPAN_H */