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> |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame^] | 25 | #include <cassert> |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 26 | #include <cstdlib> |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 27 | #include <type_traits> |
| 28 | |
| 29 | namespace bssl { |
| 30 | |
| 31 | template <typename T> |
| 32 | class Span; |
| 33 | |
| 34 | namespace internal { |
| 35 | template <typename T> |
| 36 | class SpanBase { |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 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. |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 39 | 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 Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 43 | // 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 Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 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; |
| 57 | } |
| 58 | |
| 59 | friend bool operator!=(Span<T> lhs, Span<T> rhs) { return !(lhs == rhs); } |
| 60 | }; |
| 61 | } // namespace internal |
| 62 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 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. |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 94 | template <typename T> |
| 95 | class Span : private internal::SpanBase<const T> { |
| 96 | private: |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 97 | // 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 Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 99 | // |
| 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 Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 104 | template <typename C> |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 105 | using EnableIfContainer = std::enable_if< |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 106 | std::is_convertible<decltype(std::declval<C>().data()), T *>::value && |
| 107 | std::is_integral<decltype(std::declval<C>().size())>::value>; |
| 108 | |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 109 | static const size_t npos = static_cast<size_t>(-1); |
| 110 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 111 | 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 Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 118 | template < |
| 119 | typename C, typename = typename EnableIfContainer<C>::type, |
| 120 | typename = typename std::enable_if<std::is_const<T>::value, C>::type> |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 121 | Span(const C &container) : data_(container.data()), size_(container.size()) {} |
| 122 | |
Robert Sloan | 4562e9d | 2017-10-02 10:26:51 -0700 | [diff] [blame] | 123 | template < |
| 124 | typename C, typename = typename EnableIfContainer<C>::type, |
| 125 | typename = typename std::enable_if<!std::is_const<T>::value, C>::type> |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 126 | 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 Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 131 | bool empty() const { return size_ == 0; } |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 132 | |
| 133 | T *begin() const { return data_; } |
| 134 | const T *cbegin() const { return data_; } |
| 135 | T *end() const { return data_ + size_; }; |
| 136 | const T *cend() const { return end(); }; |
| 137 | |
Robert Sloan | 921ef2c | 2017-10-17 09:02:20 -0700 | [diff] [blame^] | 138 | T &front() const { |
| 139 | assert(size_ != 0); |
| 140 | return data_[0]; |
| 141 | } |
| 142 | T &back() const { |
| 143 | assert(size_ != 0); |
| 144 | return data_[size_ - 1]; |
| 145 | } |
| 146 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 147 | T &operator[](size_t i) const { return data_[i]; } |
| 148 | T &at(size_t i) const { return data_[i]; } |
| 149 | |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 150 | Span subspan(size_t pos = 0, size_t len = npos) const { |
| 151 | if (pos > size_) { |
| 152 | abort(); // absl::Span throws an exception here. |
| 153 | } |
| 154 | return Span(data_ + pos, std::min(size_ - pos, len)); |
| 155 | } |
| 156 | |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 157 | private: |
| 158 | T *data_; |
| 159 | size_t size_; |
| 160 | }; |
| 161 | |
| 162 | template <typename T> |
Robert Sloan | ae1abf9 | 2017-10-05 12:50:08 -0700 | [diff] [blame] | 163 | const size_t Span<T>::npos; |
| 164 | |
| 165 | template <typename T> |
Robert Sloan | fe7cd21 | 2017-08-07 09:03:39 -0700 | [diff] [blame] | 166 | Span<T> MakeSpan(T *ptr, size_t size) { |
| 167 | return Span<T>(ptr, size); |
| 168 | } |
| 169 | |
| 170 | template <typename C> |
| 171 | auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) { |
| 172 | return MakeSpan(c.data(), c.size()); |
| 173 | } |
| 174 | |
| 175 | template <typename T> |
| 176 | Span<const T> MakeConstSpan(T *ptr, size_t size) { |
| 177 | return Span<const T>(ptr, size); |
| 178 | } |
| 179 | |
| 180 | template <typename C> |
| 181 | auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) { |
| 182 | return MakeConstSpan(c.data(), c.size()); |
| 183 | } |
| 184 | |
| 185 | } // namespace bssl |
| 186 | |
| 187 | } // extern C++ |
| 188 | |
| 189 | #endif // !defined(BORINGSSL_NO_CXX) |
| 190 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 191 | #endif // OPENSSL_HEADER_SSL_SPAN_H |