Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 1 | /* 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 | extern "C++" { |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <type_traits> |
| 26 | |
| 27 | namespace bssl { |
| 28 | |
| 29 | template <typename T> |
| 30 | class Span; |
| 31 | |
| 32 | namespace internal { |
| 33 | template <typename T> |
| 34 | class SpanBase { |
| 35 | /* Put comparison operator implementations into a base class with const T, so |
| 36 | * they can be used with any type that implicitly converts into a Span. */ |
| 37 | static_assert(std::is_const<T>::value, |
| 38 | "Span<T> must be derived from SpanBase<const T>"); |
| 39 | |
| 40 | friend bool operator==(Span<T> lhs, Span<T> rhs) { |
| 41 | /* MSVC issues warning C4996 because std::equal is unsafe. The pragma to |
| 42 | * suppress the warning mysteriously has no effect, hence this |
| 43 | * implementation. See |
| 44 | * https://msdn.microsoft.com/en-us/library/aa985974.aspx. */ |
| 45 | if (lhs.size() != rhs.size()) { |
| 46 | return false; |
| 47 | } |
| 48 | for (T *l = lhs.begin(), *r = rhs.begin(); l != lhs.end() && r != rhs.end(); |
| 49 | ++l, ++r) { |
| 50 | if (*l != *r) { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); } |
| 58 | }; |
| 59 | } // namespace internal |
| 60 | |
| 61 | /* A Span<T> is a non-owning reference to a contiguous array of objects of type |
| 62 | * |T|. Conceptually, a Span is a simple a pointer to |T| and a count of |
| 63 | * elements accessible via that pointer. The elements referenced by the Span can |
| 64 | * be mutated if |T| is mutable. |
| 65 | * |
| 66 | * A Span can be constructed from container types implementing |data()| and |
| 67 | * |size()| methods. If |T| is constant, construction from a container type is |
| 68 | * implicit. This allows writing methods that accept data from some unspecified |
| 69 | * container type: |
| 70 | * |
| 71 | * // Foo views data referenced by v. |
| 72 | * void Foo(bssl::Span<const uint8_t> v) { ... } |
| 73 | * |
| 74 | * std::vector<uint8_t> vec; |
| 75 | * Foo(vec); |
| 76 | * |
| 77 | * For mutable Spans, conversion is explicit: |
| 78 | * |
| 79 | * // FooMutate mutates data referenced by v. |
| 80 | * void FooMutate(bssl::Span<uint8_t> v) { ... } |
| 81 | * |
| 82 | * FooMutate(bssl::Span<uint8_t>(vec)); |
| 83 | * |
| 84 | * You can also use the |MakeSpan| and |MakeConstSpan| factory methods to |
| 85 | * construct Spans in order to deduce the type of the Span automatically. |
| 86 | * |
| 87 | * FooMutate(bssl::MakeSpan(vec)); |
| 88 | * |
| 89 | * Note that Spans have value type sematics. They are cheap to construct and |
| 90 | * copy, and should be passed by value whenever a method would otherwise accept |
| 91 | * a reference or pointer to a container or array. */ |
| 92 | template <typename T> |
| 93 | class Span : private internal::SpanBase<const T> { |
| 94 | private: |
| 95 | template <bool B, class V = void> |
| 96 | using enable_if_t = typename std::enable_if<B, V>::type; |
| 97 | |
| 98 | // Heuristically test whether C is a container type that can be converted into |
| 99 | // a Span by checking for data() and size() member functions. |
| 100 | template <typename C> |
| 101 | using EnableIfContainer = enable_if_t< |
| 102 | std::is_convertible<decltype(std::declval<C>().data()), T *>::value && |
| 103 | std::is_integral<decltype(std::declval<C>().size())>::value>; |
| 104 | |
| 105 | public: |
| 106 | constexpr Span() : Span(nullptr, 0) {} |
| 107 | constexpr Span(T *ptr, size_t len) : data_(ptr), size_(len) {} |
| 108 | |
| 109 | template <size_t N> |
| 110 | constexpr Span(T (&array)[N]) : Span(array, N) {} |
| 111 | |
| 112 | template <typename C, typename = EnableIfContainer<C>, |
| 113 | typename = enable_if_t<std::is_const<T>::value, C>> |
| 114 | Span(const C &container) : data_(container.data()), size_(container.size()) {} |
| 115 | |
| 116 | template <typename C, typename = EnableIfContainer<C>, |
| 117 | typename = enable_if_t<!std::is_const<T>::value, C>> |
| 118 | explicit Span(C &container) |
| 119 | : data_(container.data()), size_(container.size()) {} |
| 120 | |
| 121 | T *data() const { return data_; } |
| 122 | size_t size() const { return size_; } |
| 123 | |
| 124 | T *begin() const { return data_; } |
| 125 | const T *cbegin() const { return data_; } |
| 126 | T *end() const { return data_ + size_; }; |
| 127 | const T *cend() const { return end(); }; |
| 128 | |
| 129 | T &operator[](size_t i) const { return data_[i]; } |
| 130 | T &at(size_t i) const { return data_[i]; } |
| 131 | |
| 132 | private: |
| 133 | T *data_; |
| 134 | size_t size_; |
| 135 | }; |
| 136 | |
| 137 | template <typename T> |
| 138 | Span<T> MakeSpan(T *ptr, size_t size) { |
| 139 | return Span<T>(ptr, size); |
| 140 | } |
| 141 | |
| 142 | template <typename C> |
| 143 | auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) { |
| 144 | return MakeSpan(c.data(), c.size()); |
| 145 | } |
| 146 | |
| 147 | template <typename T> |
| 148 | Span<const T> MakeConstSpan(T *ptr, size_t size) { |
| 149 | return Span<const T>(ptr, size); |
| 150 | } |
| 151 | |
| 152 | template <typename C> |
| 153 | auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) { |
| 154 | return MakeConstSpan(c.data(), c.size()); |
| 155 | } |
| 156 | |
| 157 | } // namespace bssl |
| 158 | |
| 159 | } // extern C++ |
| 160 | |
| 161 | #endif // !defined(BORINGSSL_NO_CXX) |
| 162 | |
| 163 | #endif /* OPENSSL_HEADER_SSL_SPAN_H */ |