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 ABSL_NAMESPACE_BEGIN 52 namespace container_internal { 53 54 template <typename... Ts> 55 class CompressedTuple; 56 57 namespace internal_compressed_tuple { 58 59 template <typename D, size_t I> 60 struct Elem; 61 template <typename... B, size_t I> 62 struct Elem<CompressedTuple<B...>, I> 63 : std::tuple_element<I, std::tuple<B...>> {}; 64 template <typename D, size_t I> 65 using ElemT = typename Elem<D, I>::type; 66 67 // We can't use EBCO on other CompressedTuples because that would mean that we 68 // derive from multiple Storage<> instantiations with the same I parameter, 69 // and potentially from multiple identical Storage<> instantiations. So anytime 70 // we use type inheritance rather than encapsulation, we mark 71 // CompressedTupleImpl, to make this easy to detect. 72 struct uses_inheritance {}; 73 74 template <typename T> 75 constexpr bool ShouldUseBase() { 76 return std::is_class<T>::value && std::is_empty<T>::value && 77 !std::is_final<T>::value && 78 !std::is_base_of<uses_inheritance, T>::value; 79 } 80 81 // The storage class provides two specializations: 82 // - For empty classes, it stores T as a base class. 83 // - For everything else, it stores T as a member. 84 template <typename T, size_t I, bool UseBase = ShouldUseBase<T>()> 85 struct Storage { 86 T value; 87 constexpr Storage() = default; 88 template <typename V> 89 explicit constexpr Storage(absl::in_place_t, V&& v) 90 : value(absl::forward<V>(v)) {} 91 constexpr const T& get() const& { return value; } 92 T& get() & { return value; } 93 constexpr const T&& get() const&& { return absl::move(*this).value; } 94 T&& get() && { return std::move(*this).value; } 95 }; 96 97 template <typename T, size_t I> 98 struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC Storage<T, I, true> : T { 99 constexpr Storage() = default; 100 101 template <typename V> 102 explicit constexpr Storage(absl::in_place_t, V&& v) 103 : T(absl::forward<V>(v)) {} 104 105 constexpr const T& get() const& { return *this; } 106 T& get() & { return *this; } 107 constexpr const T&& get() const&& { return absl::move(*this); } 108 T&& get() && { return std::move(*this); } 109 }; 110 111 template <typename D, typename I, bool ShouldAnyUseBase> 112 struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl; 113 114 template <typename... Ts, size_t... I, bool ShouldAnyUseBase> 115 struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl< 116 CompressedTuple<Ts...>, absl::index_sequence<I...>, ShouldAnyUseBase> 117 // We use the dummy identity function through std::integral_constant to 118 // convince MSVC of accepting and expanding I in that context. Without it 119 // you would get: 120 // error C3548: 'I': parameter pack cannot be used in this context 121 : uses_inheritance, 122 Storage<Ts, std::integral_constant<size_t, I>::value>... { 123 constexpr CompressedTupleImpl() = default; 124 template <typename... Vs> 125 explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args) 126 : Storage<Ts, I>(absl::in_place, absl::forward<Vs>(args))... {} 127 friend CompressedTuple<Ts...>; 128 }; 129 130 template <typename... Ts, size_t... I> 131 struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl< 132 CompressedTuple<Ts...>, absl::index_sequence<I...>, false> 133 // We use the dummy identity function as above... 134 : Storage<Ts, std::integral_constant<size_t, I>::value, false>... { 135 constexpr CompressedTupleImpl() = default; 136 template <typename... Vs> 137 explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args) 138 : Storage<Ts, I, false>(absl::in_place, absl::forward<Vs>(args))... {} 139 friend CompressedTuple<Ts...>; 140 }; 141 142 std::false_type Or(std::initializer_list<std::false_type>); 143 std::true_type Or(std::initializer_list<bool>); 144 145 // MSVC requires this to be done separately rather than within the declaration 146 // of CompressedTuple below. 147 template <typename... Ts> 148 constexpr bool ShouldAnyUseBase() { 149 return decltype( 150 Or({std::integral_constant<bool, ShouldUseBase<Ts>()>()...})){}; 151 } 152 153 template <typename T, typename V> 154 using TupleElementMoveConstructible = 155 typename std::conditional<std::is_reference<T>::value, 156 std::is_convertible<V, T>, 157 std::is_constructible<T, V&&>>::type; 158 159 template <bool SizeMatches, class T, class... Vs> 160 struct TupleMoveConstructible : std::false_type {}; 161 162 template <class... Ts, class... Vs> 163 struct TupleMoveConstructible<true, CompressedTuple<Ts...>, Vs...> 164 : std::integral_constant< 165 bool, absl::conjunction< 166 TupleElementMoveConstructible<Ts, Vs&&>...>::value> {}; 167 168 template <typename T> 169 struct compressed_tuple_size; 170 171 template <typename... Es> 172 struct compressed_tuple_size<CompressedTuple<Es...>> 173 : public std::integral_constant<std::size_t, sizeof...(Es)> {}; 174 175 template <class T, class... Vs> 176 struct TupleItemsMoveConstructible 177 : std::integral_constant< 178 bool, TupleMoveConstructible<compressed_tuple_size<T>::value == 179 sizeof...(Vs), 180 T, Vs...>::value> {}; 181 182 } // namespace internal_compressed_tuple 183 184 // Helper class to perform the Empty Base Class Optimization. 185 // Ts can contain classes and non-classes, empty or not. For the ones that 186 // are empty classes, we perform the CompressedTuple. If all types in Ts are 187 // empty classes, then CompressedTuple<Ts...> is itself an empty class. (This 188 // does not apply when one or more of those empty classes is itself an empty 189 // CompressedTuple.) 190 // 191 // To access the members, use member .get<N>() function. 192 // 193 // Eg: 194 // absl::container_internal::CompressedTuple<int, T1, T2, T3> value(7, t1, t2, 195 // t3); 196 // assert(value.get<0>() == 7); 197 // T1& t1 = value.get<1>(); 198 // const T2& t2 = value.get<2>(); 199 // ... 200 // 201 // https://en.cppreference.com/w/cpp/language/ebo 202 template <typename... Ts> 203 class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple 204 : private internal_compressed_tuple::CompressedTupleImpl< 205 CompressedTuple<Ts...>, absl::index_sequence_for<Ts...>, 206 internal_compressed_tuple::ShouldAnyUseBase<Ts...>()> { 207 private: 208 template <int I> 209 using ElemT = internal_compressed_tuple::ElemT<CompressedTuple, I>; 210 211 template <int I> 212 using StorageT = internal_compressed_tuple::Storage<ElemT<I>, I>; 213 214 public: 215 // There seems to be a bug in MSVC dealing in which using '=default' here will 216 // cause the compiler to ignore the body of other constructors. The work- 217 // around is to explicitly implement the default constructor. 218 #if defined(_MSC_VER) 219 constexpr CompressedTuple() : CompressedTuple::CompressedTupleImpl() {} 220 #else 221 constexpr CompressedTuple() = default; 222 #endif 223 explicit constexpr CompressedTuple(const Ts&... base) 224 : CompressedTuple::CompressedTupleImpl(absl::in_place, base...) {} 225 226 template <typename First, typename... Vs, 227 absl::enable_if_t< 228 absl::conjunction< 229 // Ensure we are not hiding default copy/move constructors. 230 absl::negation<std::is_same<void(CompressedTuple), 231 void(absl::decay_t<First>)>>, 232 internal_compressed_tuple::TupleItemsMoveConstructible< 233 CompressedTuple<Ts...>, First, Vs...>>::value, 234 bool> = true> 235 explicit constexpr CompressedTuple(First&& first, Vs&&... base) 236 : CompressedTuple::CompressedTupleImpl(absl::in_place, 237 absl::forward<First>(first), 238 absl::forward<Vs>(base)...) {} 239 240 template <int I> 241 ElemT<I>& get() & { 242 return StorageT<I>::get(); 243 } 244 245 template <int I> 246 constexpr const ElemT<I>& get() const& { 247 return StorageT<I>::get(); 248 } 249 250 template <int I> 251 ElemT<I>&& get() && { 252 return std::move(*this).StorageT<I>::get(); 253 } 254 255 template <int I> 256 constexpr const ElemT<I>&& get() const&& { 257 return absl::move(*this).StorageT<I>::get(); 258 } 259 }; 260 261 // Explicit specialization for a zero-element tuple 262 // (needed to avoid ambiguous overloads for the default constructor). 263 template <> 264 class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple<> {}; 265 266 } // namespace container_internal 267 ABSL_NAMESPACE_END 268 } // namespace absl 269 270 #undef ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC 271 272 #endif // ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_ 273