// // Copyright 2019 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. // #ifndef ABSL_TYPES_INTERNAL_SPAN_H_ #define ABSL_TYPES_INTERNAL_SPAN_H_ #include <algorithm> #include <cstddef> #include <string> #include <type_traits> #include "absl/algorithm/algorithm.h" #include "absl/base/internal/throw_delegate.h" #include "absl/meta/type_traits.h" namespace absl { ABSL_NAMESPACE_BEGIN template <typename T> class Span; namespace span_internal { // Wrappers for access to container data pointers. template <typename C> constexpr auto GetDataImpl(C& c, char) noexcept // NOLINT(runtime/references) -> decltype(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 { … } template <typename C> constexpr auto GetData(C& c) noexcept // NOLINT(runtime/references) -> decltype(GetDataImpl(c, 0)) { … } // Detection idioms for size() and data(). HasSize; // We want to enable conversion from vector<T*> to Span<const T* const> but // disable conversion from vector<Derived> to Span<Base>. 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. HasData; // Extracts value type from a Container template <typename C> struct ElementType { … }; ElementType<T (&)[N]>; ElementT; EnableIfMutable; template <template <typename> class SpanT, typename T> bool EqualImpl(SpanT<T> a, SpanT<T> b) { … } template <template <typename> class SpanT, typename T> bool LessThanImpl(SpanT<T> a, SpanT<T> b) { … } EnableIfConvertibleTo; // IsView is true for types where the return type of .data() is the same for // mutable and const instances. This isn't foolproof, but it's only used to // enable a compiler warning. template <typename T, typename = void, typename = void> struct IsView { … }; IsView<T, absl::void_t<decltype(span_internal::GetData(std::declval<const T &>()))>, absl::void_t<decltype(span_internal::GetData(std::declval<T &>()))>>; // These enablers result in 'int' so they can be used as typenames or defaults // in template parameters lists. EnableIfIsView; EnableIfNotIsView; } // namespace span_internal ABSL_NAMESPACE_END } // namespace absl #endif // ABSL_TYPES_INTERNAL_SPAN_H_