// // Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // ----------------------------------------------------------------------------- // span.h // ----------------------------------------------------------------------------- // // This header file defines a `Span` type for holding a view of an existing // array of data. The `Span` object, much like the `absl::string_view` object, // does not own such data itself. A span provides a lightweight way to pass // around view of such data. // // Additionally, this header file defines `MakeSpan()` and `MakeConstSpan()` // factory functions, for clearly creating spans of type `Span` or read-only // `Span` when such types may be difficult to identify due to issues // with implicit conversion. // // The C++ standards committee currently has a proposal for a `std::span` type, // (http://wg21.link/p0122), which is not yet part of the standard (though may // become part of C++20). As of August 2017, the differences between // `absl::Span` and this proposal are: // * `absl::Span` uses `size_t` for `size_type` // * `absl::Span` has no `operator()` // * `absl::Span` has no constructors for `std::unique_ptr` or // `std::shared_ptr` // * `absl::Span` has the factory functions `MakeSpan()` and // `MakeConstSpan()` // * `absl::Span` has `front()` and `back()` methods // * bounds-checked access to `absl::Span` is accomplished with `at()` // * `absl::Span` has compiler-provided move and copy constructors and // assignment. This is due to them being specified as `constexpr`, but that // implies const in C++11. // * `absl::Span` has no `element_type` or `index_type` typedefs // * A read-only `absl::Span` can be implicitly constructed from an // initializer list. // * `absl::Span` has no `bytes()`, `size_bytes()`, `as_bytes()`, or // `as_mutable_bytes()` methods // * `absl::Span` has no static extent template parameter, nor constructors // which exist only because of the static extent parameter. // * `absl::Span` has an explicit mutable-reference constructor // // For more information, see the class comments below. #ifndef ABSL_TYPES_SPAN_H_ #define ABSL_TYPES_SPAN_H_ #include #include #include #include #include #include #include #include #include "absl/algorithm/algorithm.h" #include "absl/base/internal/throw_delegate.h" #include "absl/base/macros.h" #include "absl/base/optimization.h" #include "absl/base/port.h" #include "absl/meta/type_traits.h" namespace absl { namespace span_internal { // A constexpr min function constexpr size_t Min(size_t a, size_t b) noexcept { return a < b ? a : b; } // Wrappers for access to container data pointers. template constexpr auto GetDataImpl(C& c, char) noexcept // NOLINT(runtime/references) -> decltype(c.data()) { return c.data(); } // Before C++17, std::string::data returns a const char* in all cases. inline char* GetDataImpl(std::string& s, // NOLINT(runtime/references) int) noexcept { return &s[0]; } template constexpr auto GetData(C& c) noexcept // NOLINT(runtime/references) -> decltype(GetDataImpl(c, 0)) { return GetDataImpl(c, 0); } // Detection idioms for size() and data(). template using HasSize = std::is_integral().size())>>; // We want to enable conversion from vector to Span but // disable conversion from vector to Span. Here we use // the fact that U** is convertible to Q* const* if and only if Q is the same // type or a more cv-qualified version of U. We also decay the result type of // data() to avoid problems with classes which have a member function data() // which returns a reference. template using HasData = std::is_convertible()))>*, T* const*>; // Extracts value type from a Container template struct ElementType { using type = typename absl::remove_reference_t::value_type; }; template struct ElementType { using type = T; }; template using ElementT = typename ElementType::type; template using EnableIfMutable = typename std::enable_if::value, int>::type; template