You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

compressed_tuple.h 13 kB

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