CMS 3D CMS Logo

produce_helpers.h
Go to the documentation of this file.
1 #ifndef Framework_produce_helpers_h
2 #define Framework_produce_helpers_h
3 // -*- C++ -*-
4 //
5 // Package: Framework
6 // Class : produce_helpers
7 //
16 //
17 // Author: Chris Jones
18 // Created: Fri Apr 15 10:25:20 EDT 2005
19 //
20 
21 // system include files
22 #include <memory>
23 #include <optional>
24 // user include files
25 
26 namespace edm::eventsetup {
27 
28  namespace produce {
29  struct Null;
30  }
31 
32  template <typename FromT, typename ToT>
33  void moveFromTo(FromT& iFrom, ToT& iTo) {
34  iTo = std::move(iFrom);
35  }
36 
37  template <typename FromT, typename ToT>
38  void moveFromTo(std::unique_ptr<FromT>& iFrom, ToT& iTo) {
39  iTo = std::move(iFrom);
40  }
41  template <typename FromT, typename ToT>
42  void moveFromTo(std::optional<FromT>& iFrom, ToT& iTo) {
43  iTo = std::move(iFrom.value());
44  }
45 
46  namespace produce {
47  struct Null {};
48  template <typename T>
49  struct EndList {
50  static_assert(
51  (not std::is_pointer_v<T>),
52  "use std::shared_ptr or std::unique_ptr to hold EventSetup data products, do not use bare pointers");
53  using tail_type = T;
54  using head_type = Null;
55  };
56  template <typename T>
57  struct product_traits {
58  using type = T;
59  };
60  template <typename T>
61  struct product_traits<T*> {
62  using type = EndList<T*>;
63  };
64  template <typename T>
67  };
68  template <typename T>
69  struct product_traits<std::shared_ptr<T>> {
71  };
72  template <typename T>
73  struct product_traits<std::optional<T>> {
75  };
76 
77  template <typename T>
78  struct size {
79  using type = typename product_traits<T>::type;
80  constexpr static int value = size<typename type::head_type>::value + 1;
81  };
82  template <>
83  struct size<Null> {
84  constexpr static int value = 0;
85  };
86 
87  template <typename T>
89  using type = typename T::element_type;
90  static auto getPointer(T const& iPtr) -> decltype(&*iPtr) { return &*iPtr; }
91  };
92 
93  template <typename T>
94  struct smart_pointer_traits<std::unique_ptr<const T>> {
95  using type = T;
96  static auto getPointer(std::unique_ptr<const T> const& iPtr) -> decltype(&*iPtr) { return &*iPtr; }
97  };
98 
99  template <typename T>
100  struct smart_pointer_traits<std::shared_ptr<const T>> {
101  using type = T;
102  static auto getPointer(std::shared_ptr<const T> const& iPtr) -> decltype(&*iPtr) { return &*iPtr; }
103  };
104 
105  template <typename T>
107  using type = T;
108  static T const* getPointer(std::optional<T> const& iPtr) {
109  if (iPtr.has_value()) {
110  return &*iPtr;
111  }
112  return nullptr;
113  }
114  };
115 
116  template <typename T, typename FindT>
117  struct find_index {
119  template <typename HeadT, typename TailT>
120  constexpr static int findIndexOf() {
121  if constexpr (not std::is_same_v<TailT, FindT>) {
123  return findIndexOf<typename container_type::head_type, typename container_type::tail_type>() + 1;
124  } else {
125  return 0;
126  }
127  }
128  constexpr static int value =
129  findIndexOf<typename container_type::head_type, typename container_type::tail_type>();
130  };
131  namespace test {
132  template <typename T>
133  const char* name(const T*);
134  }
135 
136  } // namespace produce
137 } // namespace edm::eventsetup
138 
139 #endif
typename product_traits< T >::type container_type
const char * name(const T *)
static T const * getPointer(std::optional< T > const &iPtr)
static auto getPointer(T const &iPtr) -> decltype(&*iPtr)
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
void moveFromTo(FromT &iFrom, ToT &iTo)
optional
Definition: Types.py:239
Definition: value.py:1
static auto getPointer(std::unique_ptr< const T > const &iPtr) -> decltype(&*iPtr)
static auto getPointer(std::shared_ptr< const T > const &iPtr) -> decltype(&*iPtr)
long double T
def move(src, dest)
Definition: eostools.py:511
typename product_traits< T >::type type