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
edm::eventsetup::produce::size
Definition: produce_helpers.h:78
edm::eventsetup::produce::product_traits::type
T type
Definition: produce_helpers.h:58
edm::eventsetup::produce::smart_pointer_traits
Definition: produce_helpers.h:88
edm::eventsetup::produce::smart_pointer_traits< std::unique_ptr< const T > >::getPointer
static auto getPointer(std::unique_ptr< const T > const &iPtr) -> decltype(&*iPtr)
Definition: produce_helpers.h:96
edm::eventsetup::produce::smart_pointer_traits< std::optional< T > >::getPointer
static T const * getPointer(std::optional< T > const &iPtr)
Definition: produce_helpers.h:108
edm::eventsetup::produce::EndList::tail_type
T tail_type
Definition: produce_helpers.h:53
Types.optional
optional
Definition: Types.py:199
edm::eventsetup::produce::product_traits
Definition: produce_helpers.h:57
reco::Null
Definition: TrackInfoEnum.h:16
test
Definition: SmallWORMDict.h:13
edm::eventsetup::produce::smart_pointer_traits::getPointer
static auto getPointer(T const &iPtr) -> decltype(&*iPtr)
Definition: produce_helpers.h:90
edm::eventsetup::produce::find_index::container_type
typename product_traits< T >::type container_type
Definition: produce_helpers.h:118
edm::eventsetup::produce::Null
Definition: produce_helpers.h:47
edm::eventsetup::produce::size::type
typename product_traits< T >::type type
Definition: produce_helpers.h:79
edm::eventsetup::produce::smart_pointer_traits< std::optional< T > >::type
T type
Definition: produce_helpers.h:107
edm::eventsetup::produce::smart_pointer_traits< std::unique_ptr< const T > >::type
T type
Definition: produce_helpers.h:95
edm::eventsetup::produce::smart_pointer_traits< std::shared_ptr< const T > >::getPointer
static auto getPointer(std::shared_ptr< const T > const &iPtr) -> decltype(&*iPtr)
Definition: produce_helpers.h:102
value
Definition: value.py:1
edm::eventsetup::produce::test::name
const char * name(const T *)
edm::eventsetup::produce::find_index
Definition: produce_helpers.h:117
cms::cuda::device::unique_ptr
std::unique_ptr< T, impl::DeviceDeleter > unique_ptr
Definition: device_unique_ptr.h:33
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::eventsetup::produce::smart_pointer_traits< std::shared_ptr< const T > >::type
T type
Definition: produce_helpers.h:101
T
long double T
Definition: Basic3DVectorLD.h:48
edm::eventsetup::produce::smart_pointer_traits::type
typename T::element_type type
Definition: produce_helpers.h:89
edm::eventsetup::produce::EndList
Definition: produce_helpers.h:49
edm::eventsetup
Definition: ES_DDDetector.cc:16
edm::eventsetup::moveFromTo
void moveFromTo(FromT &iFrom, ToT &iTo)
Definition: produce_helpers.h:33
edm::eventsetup::produce::find_index::findIndexOf
constexpr static int findIndexOf()
Definition: produce_helpers.h:120