span.h 6.3 KB

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