compressed_tuple.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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(std::forward<V>(v)) {}
  80. constexpr const T& get() const& { return value; }
  81. constexpr T& get() & { return value; }
  82. constexpr const T&& get() const&& { return std::move(*this).value; }
  83. constexpr 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) : T(std::forward<V>(v)) {}
  90. constexpr const T& get() const& { return *this; }
  91. constexpr T& get() & { return *this; }
  92. constexpr const T&& get() const&& { return std::move(*this); }
  93. constexpr T&& get() && { return std::move(*this); }
  94. };
  95. template <typename D, typename I, bool ShouldAnyUseBase>
  96. struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl;
  97. template <typename... Ts, size_t... I, bool ShouldAnyUseBase>
  98. struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl<
  99. CompressedTuple<Ts...>, absl::index_sequence<I...>, ShouldAnyUseBase>
  100. // We use the dummy identity function through std::integral_constant to
  101. // convince MSVC of accepting and expanding I in that context. Without it
  102. // you would get:
  103. // error C3548: 'I': parameter pack cannot be used in this context
  104. : uses_inheritance,
  105. Storage<Ts, std::integral_constant<size_t, I>::value>... {
  106. constexpr CompressedTupleImpl() = default;
  107. template <typename... Vs>
  108. explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args)
  109. : Storage<Ts, I>(absl::in_place, std::forward<Vs>(args))... {}
  110. friend CompressedTuple<Ts...>;
  111. };
  112. template <typename... Ts, size_t... I>
  113. struct ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTupleImpl<
  114. CompressedTuple<Ts...>, absl::index_sequence<I...>, false>
  115. // We use the dummy identity function as above...
  116. : Storage<Ts, std::integral_constant<size_t, I>::value, false>... {
  117. constexpr CompressedTupleImpl() = default;
  118. template <typename... Vs>
  119. explicit constexpr CompressedTupleImpl(absl::in_place_t, Vs&&... args)
  120. : Storage<Ts, I, false>(absl::in_place, std::forward<Vs>(args))... {}
  121. friend CompressedTuple<Ts...>;
  122. };
  123. std::false_type Or(std::initializer_list<std::false_type>);
  124. std::true_type Or(std::initializer_list<bool>);
  125. // MSVC requires this to be done separately rather than within the declaration
  126. // of CompressedTuple below.
  127. template <typename... Ts>
  128. constexpr bool ShouldAnyUseBase() {
  129. return decltype(
  130. Or({std::integral_constant<bool, ShouldUseBase<Ts>()>()...})){};
  131. }
  132. template <typename T, typename V>
  133. using TupleElementMoveConstructible =
  134. typename std::conditional<std::is_reference<T>::value,
  135. std::is_convertible<V, T>,
  136. std::is_constructible<T, V&&>>::type;
  137. template <bool SizeMatches, class T, class... Vs>
  138. struct TupleMoveConstructible : std::false_type {};
  139. template <class... Ts, class... Vs>
  140. struct TupleMoveConstructible<true, CompressedTuple<Ts...>, Vs...>
  141. : std::integral_constant<
  142. bool, absl::conjunction<
  143. TupleElementMoveConstructible<Ts, Vs&&>...>::value> {};
  144. template <typename T>
  145. struct compressed_tuple_size;
  146. template <typename... Es>
  147. struct compressed_tuple_size<CompressedTuple<Es...>>
  148. : public std::integral_constant<std::size_t, sizeof...(Es)> {};
  149. template <class T, class... Vs>
  150. struct TupleItemsMoveConstructible
  151. : std::integral_constant<
  152. bool, TupleMoveConstructible<compressed_tuple_size<T>::value ==
  153. sizeof...(Vs),
  154. T, Vs...>::value> {};
  155. } // namespace internal_compressed_tuple
  156. // Helper class to perform the Empty Base Class Optimization.
  157. // Ts can contain classes and non-classes, empty or not. For the ones that
  158. // are empty classes, we perform the CompressedTuple. If all types in Ts are
  159. // empty classes, then CompressedTuple<Ts...> is itself an empty class. (This
  160. // does not apply when one or more of those empty classes is itself an empty
  161. // CompressedTuple.)
  162. //
  163. // To access the members, use member .get<N>() function.
  164. //
  165. // Eg:
  166. // absl::container_internal::CompressedTuple<int, T1, T2, T3> value(7, t1, t2,
  167. // t3);
  168. // assert(value.get<0>() == 7);
  169. // T1& t1 = value.get<1>();
  170. // const T2& t2 = value.get<2>();
  171. // ...
  172. //
  173. // https://en.cppreference.com/w/cpp/language/ebo
  174. template <typename... Ts>
  175. class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple
  176. : private internal_compressed_tuple::CompressedTupleImpl<
  177. CompressedTuple<Ts...>, absl::index_sequence_for<Ts...>,
  178. internal_compressed_tuple::ShouldAnyUseBase<Ts...>()> {
  179. private:
  180. template <int I>
  181. using ElemT = internal_compressed_tuple::ElemT<CompressedTuple, I>;
  182. template <int I>
  183. using StorageT = internal_compressed_tuple::Storage<ElemT<I>, I>;
  184. public:
  185. // There seems to be a bug in MSVC dealing in which using '=default' here will
  186. // cause the compiler to ignore the body of other constructors. The work-
  187. // around is to explicitly implement the default constructor.
  188. #if defined(_MSC_VER)
  189. constexpr CompressedTuple() : CompressedTuple::CompressedTupleImpl() {}
  190. #else
  191. constexpr CompressedTuple() = default;
  192. #endif
  193. explicit constexpr CompressedTuple(const Ts&... base)
  194. : CompressedTuple::CompressedTupleImpl(absl::in_place, base...) {}
  195. template <typename First, typename... Vs,
  196. absl::enable_if_t<
  197. absl::conjunction<
  198. // Ensure we are not hiding default copy/move constructors.
  199. absl::negation<std::is_same<void(CompressedTuple),
  200. void(absl::decay_t<First>)>>,
  201. internal_compressed_tuple::TupleItemsMoveConstructible<
  202. CompressedTuple<Ts...>, First, Vs...>>::value,
  203. bool> = true>
  204. explicit constexpr CompressedTuple(First&& first, Vs&&... base)
  205. : CompressedTuple::CompressedTupleImpl(absl::in_place,
  206. std::forward<First>(first),
  207. std::forward<Vs>(base)...) {}
  208. template <int I>
  209. constexpr ElemT<I>& get() & {
  210. return StorageT<I>::get();
  211. }
  212. template <int I>
  213. constexpr const ElemT<I>& get() const& {
  214. return StorageT<I>::get();
  215. }
  216. template <int I>
  217. constexpr ElemT<I>&& get() && {
  218. return std::move(*this).StorageT<I>::get();
  219. }
  220. template <int I>
  221. constexpr const ElemT<I>&& get() const&& {
  222. return std::move(*this).StorageT<I>::get();
  223. }
  224. };
  225. // Explicit specialization for a zero-element tuple
  226. // (needed to avoid ambiguous overloads for the default constructor).
  227. template <>
  228. class ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC CompressedTuple<> {};
  229. } // namespace container_internal
  230. ABSL_NAMESPACE_END
  231. } // namespace absl
  232. #undef ABSL_INTERNAL_COMPRESSED_TUPLE_DECLSPEC
  233. #endif // ABSL_CONTAINER_INTERNAL_COMPRESSED_TUPLE_H_