Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // Copyright 2018 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // Helper class to perform the Empty Base Optimization. |
| 16 | // Ts can contain classes and non-classes, empty or not. For the ones that |
| 17 | // are empty classes, we perform the optimization. If all types in Ts are empty |
| 18 | // classes, then CompressedTuple<Ts...> is itself an empty class. |
| 19 | // |
| 20 | // To access the members, use member get<N>() function. |
| 21 | // |
| 22 | // Eg: |
| 23 | // absl::container_internal::CompressedTuple<int, T1, T2, T3> value(7, t1, t2, |
| 24 | // t3); |
| 25 | // assert(value.get<0>() == 7); |
| 26 | // T1& t1 = value.get<1>(); |
| 27 | // const T2& t2 = value.get<2>(); |
| 28 | // ... |
| 29 | // |
| 30 | // https://en.cppreference.com/w/cpp/language/ebo |
| 31 | |
| 32 | #ifndef ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_ |
| 33 | #define ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_ |
| 34 | |
| 35 | #include <initializer_list> |
| 36 | #include <tuple> |
| 37 | #include <type_traits> |
| 38 | #include <utility> |
| 39 | |
| 40 | #include "absl/utility/utility.h" |
| 41 | |
| 42 | #if defined(_MSC_VER) && !defined(__NVCC__) |
| 43 | // We need to mark these classes with this declspec to ensure that |
| 44 | // CompressedTuple happens. |
| 45 | #define ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC __declspec(empty_bases) |
| 46 | #else |
| 47 | #define ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC |
| 48 | #endif |
| 49 | |
| 50 | namespace absl { |
| 51 | namespace container_internal { |
| 52 | |
| 53 | template <typename... Ts> |
| 54 | class CompressedTuple; |
| 55 | |
| 56 | namespace internal_compressed_tuple { |
| 57 | |
| 58 | template <typename D, size_t I> |
| 59 | struct Elem; |
| 60 | template <typename... B, size_t I> |
| 61 | struct Elem<CompressedTuple<B...>, I> |
| 62 | : std::tuple_element<I, std::tuple<B...>> {}; |
| 63 | template <typename D, size_t I> |
| 64 | using ElemT = typename Elem<D, I>::type; |
| 65 | |
| 66 | // Use the __is_final intrinsic if available. Where it's not available, classes |
| 67 | // declared with the 'final' specifier cannot be used as CompressedTuple |
| 68 | // elements. |
| 69 | // TODO(sbenza): Replace this with std::is_final in C++14. |
| 70 | template <typename T> |
| 71 | constexpr bool IsFinal() { |
| 72 | #if defined(__clang__) || defined(__GNUC__) |
| 73 | return __is_final(T); |
| 74 | #else |
| 75 | return false; |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | // We can't use EBCO on other CompressedTuples because that would mean that we |
| 80 | // derive from multiple Storage<> instantiations with the same I parameter, |
| 81 | // and potentially from multiple identical Storage<> instantiations. So anytime |
| 82 | // we use type inheritance rather than encapsulation, we mark |
| 83 | // CompressedTupleImpl, to make this easy to detect. |
| 84 | struct uses_inheritance {}; |
| 85 | |
| 86 | template <typename T> |
| 87 | constexpr bool ShouldUseBase() { |
| 88 | return std::is_class<T>::value && std::is_empty<T>::value && !IsFinal<T>() && |
| 89 | !std::is_base_of<uses_inheritance, T>::value; |
| 90 | } |
| 91 | |
| 92 | // The storage class provides two specializations: |
| 93 | // - For empty classes, it stores T as a base class. |
| 94 | // - For everything else, it stores T as a member. |
| 95 | template <typename T, size_t I, |
| 96 | #if defined(_MSC_VER) |
| 97 | bool UseBase = |
| 98 | ShouldUseBase<typename std::enable_if<true, T>::type>()> |
| 99 | #else |
| 100 | bool UseBase = ShouldUseBase<T>()> |
| 101 | #endif |
| 102 | struct Storage { |
| 103 | T value; |
| 104 | constexpr Storage() = default; |
| 105 | template <typename V> |
| 106 | explicit constexpr Storage(absl::in_place_t, V&& v) |
| 107 | : value(absl::forward<V>(v)) {} |
| 108 | constexpr const T& get() const& { return value; } |
| 109 | T& get() & { return value; } |
| 110 | constexpr const T&& get() const&& { return absl::move(*this).value; } |
| 111 | T&& get() && { return std::move(*this).value; } |
| 112 | }; |
| 113 | |
| 114 | template <typename T, size_t I> |
| 115 | struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC Storage<T, I, true> : T { |
| 116 | constexpr Storage() = default; |
| 117 | |
| 118 | template <typename V> |
| 119 | explicit constexpr Storage(absl::in_place_t, V&& v) |
| 120 | : T(absl::forward<V>(v)) {} |
| 121 | |
| 122 | constexpr const T& get() const& { return *this; } |
| 123 | T& get() & { return *this; } |
| 124 | constexpr const T&& get() const&& { return absl::move(*this); } |
| 125 | T&& get() && { return std::move(*this); } |
| 126 | }; |
| 127 | |
| 128 | template <typename D, typename I, bool ShouldAnyUseBase> |
| 129 | struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl; |
| 130 | |
| 131 | template <typename... Ts, size_t... I, bool ShouldAnyUseBase> |
| 132 | struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl< |
| 133 | CompressedTuple<Ts...>, absl::index_sequence<I...>, ShouldAnyUseBase> |
| 134 | // We use the dummy identity function through std::integral_constant to |
| 135 | // convince MSVC of accepting and expanding I in that context. Without it |
| 136 | // you would get: |
| 137 | // error C3548: 'I': parameter pack cannot be used in this context |
| 138 | : uses_inheritance, |
| 139 | Storage<Ts, std::integral_constant<size_t, I>::value>... { |
| 140 | constexpr CompressedTupleImpl() = default; |
| 141 | template <typename... Vs> |
| 142 | explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args) |
| 143 | : Storage<Ts, I>(absl::in_place, absl::forward<Vs>(args))... {} |
| 144 | friend CompressedTuple<Ts...>; |
| 145 | }; |
| 146 | |
| 147 | template <typename... Ts, size_t... I> |
| 148 | struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl< |
| 149 | CompressedTuple<Ts...>, absl::index_sequence<I...>, false> |
| 150 | // We use the dummy identity function as above... |
| 151 | : Storage<Ts, std::integral_constant<size_t, I>::value, false>... { |
| 152 | constexpr CompressedTupleImpl() = default; |
| 153 | template <typename... Vs> |
| 154 | explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args) |
| 155 | : Storage<Ts, I, false>(absl::in_place, absl::forward<Vs>(args))... {} |
| 156 | friend CompressedTuple<Ts...>; |
| 157 | }; |
| 158 | |
| 159 | std::false_type Or(std::initializer_list<std::false_type>); |
| 160 | std::true_type Or(std::initializer_list<bool>); |
| 161 | |
| 162 | // MSVC requires this to be done separately rather than within the declaration |
| 163 | // of CompressedTuple below. |
| 164 | template <typename... Ts> |
| 165 | constexpr bool ShouldAnyUseBase() { |
| 166 | return decltype( |
| 167 | Or({std::integral_constant<bool, ShouldUseBase<Ts>()>()...})){}; |
| 168 | } |
| 169 | |
| 170 | template <typename T, typename V> |
| 171 | using TupleMoveConstructible = typename std::conditional< |
| 172 | std::is_reference<T>::value, std::is_convertible<V, T>, |
| 173 | std::is_constructible<T, V&&>>::type; |
| 174 | |
| 175 | } // namespace internal_compressed_tuple |
| 176 | |
| 177 | // Helper class to perform the Empty Base Class Optimization. |
| 178 | // Ts can contain classes and non-classes, empty or not. For the ones that |
| 179 | // are empty classes, we perform the CompressedTuple. If all types in Ts are |
| 180 | // empty classes, then CompressedTuple<Ts...> is itself an empty class. (This |
| 181 | // does not apply when one or more of those empty classes is itself an empty |
| 182 | // CompressedTuple.) |
| 183 | // |
| 184 | // To access the members, use member .get<N>() function. |
| 185 | // |
| 186 | // Eg: |
| 187 | // absl::container_internal::CompressedTuple<int, T1, T2, T3> value(7, t1, t2, |
| 188 | // t3); |
| 189 | // assert(value.get<0>() == 7); |
| 190 | // T1& t1 = value.get<1>(); |
| 191 | // const T2& t2 = value.get<2>(); |
| 192 | // ... |
| 193 | // |
| 194 | // https://en.cppreference.com/w/cpp/language/ebo |
| 195 | template <typename... Ts> |
| 196 | class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple |
| 197 | : private internal_compressed_tuple::CompressedTupleImpl< |
| 198 | CompressedTuple<Ts...>, absl::index_sequence_for<Ts...>, |
| 199 | internal_compressed_tuple::ShouldAnyUseBase<Ts...>()> { |
| 200 | private: |
| 201 | template <int I> |
| 202 | using ElemT = internal_compressed_tuple::ElemT<CompressedTuple, I>; |
| 203 | |
| 204 | template <int I> |
| 205 | using StorageT = internal_compressed_tuple::Storage<ElemT<I>, I>; |
| 206 | |
| 207 | public: |
| 208 | // There seems to be a bug in MSVC dealing in which using '=default' here will |
| 209 | // cause the compiler to ignore the body of other constructors. The work- |
| 210 | // around is to explicitly implement the default constructor. |
| 211 | #if defined(_MSC_VER) |
| 212 | constexpr CompressedTuple() : CompressedTuple::CompressedTupleImpl() {} |
| 213 | #else |
| 214 | constexpr CompressedTuple() = default; |
| 215 | #endif |
| 216 | explicit constexpr CompressedTuple(const Ts&... base) |
| 217 | : CompressedTuple::CompressedTupleImpl(absl::in_place, base...) {} |
| 218 | |
| 219 | template <typename... Vs, |
| 220 | absl::enable_if_t< |
| 221 | absl::conjunction< |
| 222 | // Ensure we are not hiding default copy/move constructors. |
| 223 | absl::negation<std::is_same<void(CompressedTuple), |
| 224 | void(absl::decay_t<Vs>...)>>, |
| 225 | internal_compressed_tuple::TupleMoveConstructible< |
| 226 | Ts, Vs&&>...>::value, |
| 227 | bool> = true> |
| 228 | explicit constexpr CompressedTuple(Vs&&... base) |
| 229 | : CompressedTuple::CompressedTupleImpl(absl::in_place, |
| 230 | absl::forward<Vs>(base)...) {} |
| 231 | |
| 232 | template <int I> |
| 233 | ElemT<I>& get() & { |
| 234 | return internal_compressed_tuple::Storage<ElemT<I>, I>::get(); |
| 235 | } |
| 236 | |
| 237 | template <int I> |
| 238 | constexpr const ElemT<I>& get() const& { |
| 239 | return StorageT<I>::get(); |
| 240 | } |
| 241 | |
| 242 | template <int I> |
| 243 | ElemT<I>&& get() && { |
| 244 | return std::move(*this).StorageT<I>::get(); |
| 245 | } |
| 246 | |
| 247 | template <int I> |
| 248 | constexpr const ElemT<I>&& get() const&& { |
| 249 | return absl::move(*this).StorageT<I>::get(); |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | // Explicit specialization for a zero-element tuple |
| 254 | // (needed to avoid ambiguous overloads for the default constructor). |
| 255 | template <> |
| 256 | class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple<> {}; |
| 257 | |
| 258 | } // namespace container_internal |
| 259 | } // namespace absl |
| 260 | |
| 261 | #undef ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC |
| 262 | |
| 263 | #endif // ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_ |