span.h 6.0 KB

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