span.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2019 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 #ifndef ABSL_TYPES_INTERNAL_SPAN_H_
00017 #define ABSL_TYPES_INTERNAL_SPAN_H_
00018 
00019 #include <algorithm>
00020 #include <cstddef>
00021 #include <string>
00022 #include <type_traits>
00023 
00024 #include "absl/algorithm/algorithm.h"
00025 #include "absl/base/internal/throw_delegate.h"
00026 #include "absl/meta/type_traits.h"
00027 
00028 namespace absl {
00029 
00030 namespace span_internal {
00031 // A constexpr min function
00032 constexpr size_t Min(size_t a, size_t b) noexcept { return a < b ? a : b; }
00033 
00034 // Wrappers for access to container data pointers.
00035 template <typename C>
00036 constexpr auto GetDataImpl(C& c, char) noexcept  // NOLINT(runtime/references)
00037     -> decltype(c.data()) {
00038   return c.data();
00039 }
00040 
00041 // Before C++17, std::string::data returns a const char* in all cases.
00042 inline char* GetDataImpl(std::string& s,  // NOLINT(runtime/references)
00043                          int) noexcept {
00044   return &s[0];
00045 }
00046 
00047 template <typename C>
00048 constexpr auto GetData(C& c) noexcept  // NOLINT(runtime/references)
00049     -> decltype(GetDataImpl(c, 0)) {
00050   return GetDataImpl(c, 0);
00051 }
00052 
00053 // Detection idioms for size() and data().
00054 template <typename C>
00055 using HasSize =
00056     std::is_integral<absl::decay_t<decltype(std::declval<C&>().size())>>;
00057 
00058 // We want to enable conversion from vector<T*> to Span<const T* const> but
00059 // disable conversion from vector<Derived> to Span<Base>. Here we use
00060 // the fact that U** is convertible to Q* const* if and only if Q is the same
00061 // type or a more cv-qualified version of U.  We also decay the result type of
00062 // data() to avoid problems with classes which have a member function data()
00063 // which returns a reference.
00064 template <typename T, typename C>
00065 using HasData =
00066     std::is_convertible<absl::decay_t<decltype(GetData(std::declval<C&>()))>*,
00067                         T* const*>;
00068 
00069 // Extracts value type from a Container
00070 template <typename C>
00071 struct ElementType {
00072   using type = typename absl::remove_reference_t<C>::value_type;
00073 };
00074 
00075 template <typename T, size_t N>
00076 struct ElementType<T (&)[N]> {
00077   using type = T;
00078 };
00079 
00080 template <typename C>
00081 using ElementT = typename ElementType<C>::type;
00082 
00083 template <typename T>
00084 using EnableIfMutable =
00085     typename std::enable_if<!std::is_const<T>::value, int>::type;
00086 
00087 template <template <typename> class SpanT, typename T>
00088 bool EqualImpl(SpanT<T> a, SpanT<T> b) {
00089   static_assert(std::is_const<T>::value, "");
00090   return absl::equal(a.begin(), a.end(), b.begin(), b.end());
00091 }
00092 
00093 template <template <typename> class SpanT, typename T>
00094 bool LessThanImpl(SpanT<T> a, SpanT<T> b) {
00095   // We can't use value_type since that is remove_cv_t<T>, so we go the long way
00096   // around.
00097   static_assert(std::is_const<T>::value, "");
00098   return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
00099 }
00100 
00101 // The `IsConvertible` classes here are needed because of the
00102 // `std::is_convertible` bug in libcxx when compiled with GCC. This build
00103 // configuration is used by Android NDK toolchain. Reference link:
00104 // https://bugs.llvm.org/show_bug.cgi?id=27538.
00105 template <typename From, typename To>
00106 struct IsConvertibleHelper {
00107  private:
00108   static std::true_type testval(To);
00109   static std::false_type testval(...);
00110 
00111  public:
00112   using type = decltype(testval(std::declval<From>()));
00113 };
00114 
00115 template <typename From, typename To>
00116 struct IsConvertible : IsConvertibleHelper<From, To>::type {};
00117 
00118 // TODO(zhangxy): replace `IsConvertible` with `std::is_convertible` once the
00119 // older version of libcxx is not supported.
00120 template <typename From, typename To>
00121 using EnableIfConvertibleTo =
00122     typename std::enable_if<IsConvertible<From, To>::value>::type;
00123 }  // namespace span_internal
00124 }  // namespace absl
00125 
00126 #endif  // ABSL_TYPES_INTERNAL_SPAN_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15