1 //
2 // Copyright 2019 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef ABSL_TYPES_INTERNAL_SPAN_H_
17 #define ABSL_TYPES_INTERNAL_SPAN_H_
18 
19 #include <algorithm>
20 #include <cstddef>
21 #include <string>
22 #include <type_traits>
23 
24 #include "absl/algorithm/algorithm.h"
25 #include "absl/base/internal/throw_delegate.h"
26 #include "absl/meta/type_traits.h"
27 
28 namespace absl {
29 ABSL_NAMESPACE_BEGIN
30 
31 namespace span_internal {
32 // A constexpr min function
Min(size_t a,size_t b)33 constexpr size_t Min(size_t a, size_t b) noexcept { return a < b ? a : b; }
34 
35 // Wrappers for access to container data pointers.
36 template <typename C>
37 constexpr auto GetDataImpl(C& c, char) noexcept  // NOLINT(runtime/references)
38     -> decltype(c.data()) {
39   return c.data();
40 }
41 
42 // Before C++17, std::string::data returns a const char* in all cases.
GetDataImpl(std::string & s,int)43 inline char* GetDataImpl(std::string& s,  // NOLINT(runtime/references)
44                          int) noexcept {
45   return &s[0];
46 }
47 
48 template <typename C>
49 constexpr auto GetData(C& c) noexcept  // NOLINT(runtime/references)
50     -> decltype(GetDataImpl(c, 0)) {
51   return GetDataImpl(c, 0);
52 }
53 
54 // Detection idioms for size() and data().
55 template <typename C>
56 using HasSize =
57     std::is_integral<absl::decay_t<decltype(std::declval<C&>().size())>>;
58 
59 // We want to enable conversion from vector<T*> to Span<const T* const> but
60 // disable conversion from vector<Derived> to Span<Base>. Here we use
61 // the fact that U** is convertible to Q* const* if and only if Q is the same
62 // type or a more cv-qualified version of U.  We also decay the result type of
63 // data() to avoid problems with classes which have a member function data()
64 // which returns a reference.
65 template <typename T, typename C>
66 using HasData =
67     std::is_convertible<absl::decay_t<decltype(GetData(std::declval<C&>()))>*,
68                         T* const*>;
69 
70 // Extracts value type from a Container
71 template <typename C>
72 struct ElementType {
73   using type = typename absl::remove_reference_t<C>::value_type;
74 };
75 
76 template <typename T, size_t N>
77 struct ElementType<T (&)[N]> {
78   using type = T;
79 };
80 
81 template <typename C>
82 using ElementT = typename ElementType<C>::type;
83 
84 template <typename T>
85 using EnableIfMutable =
86     typename std::enable_if<!std::is_const<T>::value, int>::type;
87 
88 template <template <typename> class SpanT, typename T>
89 bool EqualImpl(SpanT<T> a, SpanT<T> b) {
90   static_assert(std::is_const<T>::value, "");
91   return absl::equal(a.begin(), a.end(), b.begin(), b.end());
92 }
93 
94 template <template <typename> class SpanT, typename T>
95 bool LessThanImpl(SpanT<T> a, SpanT<T> b) {
96   // We can't use value_type since that is remove_cv_t<T>, so we go the long way
97   // around.
98   static_assert(std::is_const<T>::value, "");
99   return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
100 }
101 
102 // The `IsConvertible` classes here are needed because of the
103 // `std::is_convertible` bug in libcxx when compiled with GCC. This build
104 // configuration is used by Android NDK toolchain. Reference link:
105 // https://bugs.llvm.org/show_bug.cgi?id=27538.
106 template <typename From, typename To>
107 struct IsConvertibleHelper {
108  private:
109   static std::true_type testval(To);
110   static std::false_type testval(...);
111 
112  public:
113   using type = decltype(testval(std::declval<From>()));
114 };
115 
116 template <typename From, typename To>
117 struct IsConvertible : IsConvertibleHelper<From, To>::type {};
118 
119 // TODO(zhangxy): replace `IsConvertible` with `std::is_convertible` once the
120 // older version of libcxx is not supported.
121 template <typename From, typename To>
122 using EnableIfConvertibleTo =
123     typename std::enable_if<IsConvertible<From, To>::value>::type;
124 }  // namespace span_internal
125 ABSL_NAMESPACE_END
126 }  // namespace absl
127 
128 #endif  // ABSL_TYPES_INTERNAL_SPAN_H_
129