[PATCH] D19850: Make llvm::AlignedCharArrayUnion a variadic template.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon May 2 20:01:03 PDT 2016


ruiu created this revision.
ruiu added a reviewer: rsmith.
ruiu added a subscriber: llvm-commits.

Instead of a template that supports up to 10 arguments.

http://reviews.llvm.org/D19850

Files:
  include/llvm/Support/AlignOf.h

Index: include/llvm/Support/AlignOf.h
===================================================================
--- include/llvm/Support/AlignOf.h
+++ include/llvm/Support/AlignOf.h
@@ -216,44 +216,41 @@
 #endif // _MSC_VER
 
 namespace detail {
-template <typename T1,
-          typename T2 = char, typename T3 = char, typename T4 = char,
-          typename T5 = char, typename T6 = char, typename T7 = char,
-          typename T8 = char, typename T9 = char, typename T10 = char>
-class AlignerImpl {
-  T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
-
-  AlignerImpl() = delete;
+template <typename T, typename... Ts> struct AlignerImpl {
+  enum {
+    X = AlignOf<T>::Alignment,
+    Y = AlignerImpl<Ts...>::Alignment,
+    Alignment = (X > Y) ? X : Y
+  };
 };
 
-template <typename T1,
-          typename T2 = char, typename T3 = char, typename T4 = char,
-          typename T5 = char, typename T6 = char, typename T7 = char,
-          typename T8 = char, typename T9 = char, typename T10 = char>
-union SizerImpl {
-  char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
-       arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
-       arr9[sizeof(T9)], arr10[sizeof(T10)];
+template <typename T> struct AlignerImpl<T> {
+  enum { Alignment = AlignOf<T>::Alignment };
+};
+
+template <typename T, typename... Ts> struct SizerImpl {
+  enum {
+    X = sizeof(T),
+    Y = SizerImpl<Ts...>::SizeOf,
+    SizeOf = (X > Y) ? X : Y
+  };
+};
+
+template <typename T> struct SizerImpl<T> {
+  enum { SizeOf = sizeof(T) };
 };
 } // end namespace detail
 
 /// \brief This union template exposes a suitably aligned and sized character
 /// array member which can hold elements of any of up to ten types.
 ///
 /// These types may be arrays, structs, or any other types. The goal is to
 /// expose a char array buffer member which can be used as suitable storage for
-/// a placement new of any of these types. Support for more than ten types can
-/// be added at the cost of more boilerplate.
-template <typename T1,
-          typename T2 = char, typename T3 = char, typename T4 = char,
-          typename T5 = char, typename T6 = char, typename T7 = char,
-          typename T8 = char, typename T9 = char, typename T10 = char>
-struct AlignedCharArrayUnion : llvm::AlignedCharArray<
-    AlignOf<llvm::detail::AlignerImpl<T1, T2, T3, T4, T5,
-                                      T6, T7, T8, T9, T10> >::Alignment,
-    sizeof(::llvm::detail::SizerImpl<T1, T2, T3, T4, T5,
-                                     T6, T7, T8, T9, T10>)> {
-};
+/// a placement new of any of these types.
+template <typename... Ts>
+using AlignedCharArrayUnion =
+    llvm::AlignedCharArray<llvm::detail::AlignerImpl<Ts...>::Alignment,
+                           llvm::detail::SizerImpl<Ts...>::SizeOf>;
 } // end namespace llvm
 
 #endif // LLVM_SUPPORT_ALIGNOF_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19850.55938.patch
Type: text/x-patch
Size: 2922 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160503/f4a48733/attachment.bin>


More information about the llvm-commits mailing list