compressed_tuple.h 10 KB

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