compressed_tuple.h 9.9 KB

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