CMS 3D CMS Logo

PortableCollectionCommon.h
Go to the documentation of this file.
1 #ifndef DataFormats_Portable_interface_PortableCollectionCommon_h
2 #define DataFormats_Portable_interface_PortableCollectionCommon_h
3 
4 #include <cstddef>
5 #include <type_traits>
6 #include <array>
7 
8 namespace portablecollection {
9 
10  // Note: if there are other uses for this, it could be moved to a central place
11  template <std::size_t Start, std::size_t End, std::size_t Inc = 1, typename F>
13  if constexpr (Start < End) {
14  f(std::integral_constant<std::size_t, Start>());
15  constexpr_for<Start + Inc, End, Inc>(std::forward<F>(f));
16  }
17  }
18 
19  template <std::size_t Idx, typename T>
20  struct CollectionLeaf {
21  CollectionLeaf() = default;
23  template <std::size_t N>
24  CollectionLeaf(std::byte* buffer, std::array<int32_t, N> const& sizes)
25  : layout_(buffer, sizes[Idx]), view_(layout_) {
26  static_assert(N >= Idx);
27  }
28  using Layout = T;
29  using View = typename Layout::View;
30  using ConstView = typename Layout::ConstView;
33  // Make sure types are not void.
34  static_assert(not std::is_same<T, void>::value);
35  };
36 
37  template <std::size_t Idx, typename T, typename... Args>
38  struct CollectionImpl : public CollectionLeaf<Idx, T>, public CollectionImpl<Idx + 1, Args...> {
39  CollectionImpl() = default;
41 
42  template <std::size_t N>
43  CollectionImpl(std::byte* buffer, std::array<int32_t, N> const& sizes)
44  : CollectionLeaf<Idx, T>(buffer, sizes),
45  CollectionImpl<Idx + 1, Args...>(CollectionLeaf<Idx, T>::layout_.metadata().nextByte(), sizes) {}
46  };
47 
48  template <std::size_t Idx, typename T>
49  struct CollectionImpl<Idx, T> : public CollectionLeaf<Idx, T> {
50  CollectionImpl() = default;
52 
53  template <std::size_t N>
54  CollectionImpl(std::byte* buffer, std::array<int32_t, N> const& sizes) : CollectionLeaf<Idx, T>(buffer, sizes) {
55  static_assert(N == Idx + 1);
56  }
57  };
58 
59  template <typename... Args>
60  struct Collections : public CollectionImpl<0, Args...> {};
61 
62  // return the type at the Idx position in Args...
63  template <std::size_t Idx, typename... Args>
64  using TypeResolver = typename std::tuple_element<Idx, std::tuple<Args...>>::type;
65 
66  // count how many times the type T occurs in Args...
67  template <typename T, typename... Args>
68  inline constexpr std::size_t typeCount = ((std::is_same<T, Args>::value ? 1 : 0) + ... + 0);
69 
70  // count the non-void elements of Args...
71  template <typename... Args>
72  inline constexpr std::size_t membersCount = sizeof...(Args);
73 
74  // if the type T occurs in Tuple, TupleTypeIndex has a static member value with the corresponding index;
75  // otherwise there is no such data member.
76  template <typename T, typename Tuple>
77  struct TupleTypeIndex {};
78 
79  template <typename T, typename... Args>
80  struct TupleTypeIndex<T, std::tuple<T, Args...>> {
81  static_assert(typeCount<T, Args...> == 0, "the requested type appears more than once among the arguments");
82  static constexpr std::size_t value = 0;
83  };
84 
85  template <typename T, typename U, typename... Args>
86  struct TupleTypeIndex<T, std::tuple<U, Args...>> {
87  static_assert(not std::is_same_v<T, U>);
88  static_assert(typeCount<T, Args...> == 1, "the requested type does not appear among the arguments");
89  static constexpr std::size_t value = 1 + TupleTypeIndex<T, std::tuple<Args...>>::value;
90  };
91 
92  // if the type T occurs in Args..., TypeIndex has a static member value with the corresponding index;
93  // otherwise there is no such data member.
94  template <typename T, typename... Args>
95  using TypeIndex = TupleTypeIndex<T, std::tuple<Args...>>;
96 
97  // return the index where the type T occurs in Args...
98  template <typename T, typename... Args>
99  inline constexpr std::size_t typeIndex = TypeIndex<T, Args...>::value;
100 
101 } // namespace portablecollection
102 
103 #endif // DataFormats_Portable_interface_PortableCollectionCommon_h
typename std::tuple_element< Idx, std::tuple< Args... > >::type TypeResolver
CollectionImpl(std::byte *buffer, std::array< int32_t, N > const &sizes)
uint32_t Idx
Definition: config.h:14
constexpr std::size_t typeCount
CollectionImpl(std::byte *buffer, int32_t elements)
constexpr std::size_t membersCount
double f[11][100]
Definition: value.py:1
CollectionLeaf(std::byte *buffer, std::array< int32_t, N > const &sizes)
CollectionLeaf(std::byte *buffer, int32_t elements)
#define N
Definition: blowfish.cc:9
constexpr void constexpr_for(F &&f)
constexpr std::size_t typeIndex
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
CollectionImpl(std::byte *buffer, int32_t elements)
long double T
CollectionImpl(std::byte *buffer, std::array< int32_t, N > const &sizes)