/* Copyright (c) 2017, Google Inc. * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef OPENSSL_HEADER_SSL_SPAN_H #define OPENSSL_HEADER_SSL_SPAN_H #include <openssl/base.h> #if !defined(BORINGSSL_NO_CXX) extern "C++" { #include <stdlib.h> #include <algorithm> #include <type_traits> #if __cplusplus >= 201703L #include <string_view> #endif #if defined(__has_include) #if __has_include(<version>) #include <version> #endif #endif #if defined(__cpp_lib_ranges) && __cpp_lib_ranges >= 201911L #include <ranges> BSSL_NAMESPACE_BEGIN template <typename T> class Span; BSSL_NAMESPACE_END // Mark `Span` as satisfying the `view` and `borrowed_range` concepts. This // should be done before the definition of `Span`, so that any inlined calls to // range functionality use the correct specializations. enable_view; enable_borrowed_range; #endif BSSL_NAMESPACE_BEGIN template <typename T> class Span; namespace internal { template <typename T> class SpanBase { … }; // Heuristically test whether C is a container type that can be converted into // a Span<T> by checking for data() and size() member functions. // // TODO(davidben): Require C++17 support for std::is_convertible_v, etc. EnableIfContainer; } // namespace internal // A Span<T> is a non-owning reference to a contiguous array of objects of type // |T|. Conceptually, a Span is a simple a pointer to |T| and a count of // elements accessible via that pointer. The elements referenced by the Span can // be mutated if |T| is mutable. // // A Span can be constructed from container types implementing |data()| and // |size()| methods. If |T| is constant, construction from a container type is // implicit. This allows writing methods that accept data from some unspecified // container type: // // // Foo views data referenced by v. // void Foo(bssl::Span<const uint8_t> v) { ... } // // std::vector<uint8_t> vec; // Foo(vec); // // For mutable Spans, conversion is explicit: // // // FooMutate mutates data referenced by v. // void FooMutate(bssl::Span<uint8_t> v) { ... } // // FooMutate(bssl::Span<uint8_t>(vec)); // // You can also use the |MakeSpan| and |MakeConstSpan| factory methods to // construct Spans in order to deduce the type of the Span automatically. // // FooMutate(bssl::MakeSpan(vec)); // // Note that Spans have value type sematics. They are cheap to construct and // copy, and should be passed by value whenever a method would otherwise accept // a reference or pointer to a container or array. template <typename T> class Span : private internal::SpanBase<const T> { … }; template <typename T> const size_t Span<T>::npos; #if __cplusplus >= 201703L template <typename T> Span(T *, size_t) -> Span<T>; template <typename T, size_t size> Span(T (&array)[size]) -> Span<T>; template < typename C, typename T = std::remove_pointer_t<decltype(std::declval<C>().data())>, typename = internal::EnableIfContainer<C, T>> Span(C &) -> Span<T>; #endif // C++17 callers can instead rely on CTAD and the deduction guides defined // above. template <typename T> constexpr Span<T> MakeSpan(T *ptr, size_t size) { … } template <typename C> constexpr auto MakeSpan(C &c) -> decltype(MakeSpan(c.data(), c.size())) { … } template <typename T> constexpr Span<const T> MakeConstSpan(T *ptr, size_t size) { … } template <typename C> constexpr auto MakeConstSpan(const C &c) -> decltype(MakeConstSpan(c.data(), c.size())) { … } template <typename T, size_t size> constexpr Span<const T> MakeConstSpan(T (&array)[size]) { … } #if __cplusplus >= 201703L inline Span<const uint8_t> StringAsBytes(std::string_view s) { … } inline std::string_view BytesAsStringView(bssl::Span<const uint8_t> b) { … } #endif BSSL_NAMESPACE_END } // extern C++ #endif // !defined(BORINGSSL_NO_CXX) #endif // OPENSSL_HEADER_SSL_SPAN_H