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 { struct Null;}
29 
30  template<typename FromT, typename ToT> void moveFromTo(FromT& iFrom,
31  ToT& iTo) {
32  iTo = std::move(iFrom);
33  }
34 
35  template<typename FromT, typename ToT> void moveFromTo(std::unique_ptr<FromT>& iFrom, ToT& iTo) {
36  iTo = std::move(iFrom);
37  }
38  template<typename FromT, typename ToT> void moveFromTo(std::optional<FromT>& iFrom, ToT& iTo) {
39  iTo = std::move(iFrom.value());
40  }
41 
42  namespace produce {
43  struct Null {};
44  template <typename T> struct EndList {
45  static_assert((not std::is_pointer_v<T>), "use std::shared_ptr or std::unique_ptr to hold EventSetup data products, do not use bare pointers");
46  using tail_type = T;
47  using head_type = Null;
48  };
49  template <typename T> struct product_traits {
50  using type = T;
51  };
52  template< typename T> struct product_traits<T*> {
53  using type = EndList<T*>;
54  };
55  template< typename T> struct product_traits<std::unique_ptr<T>> {
57  };
58  template< typename T> struct product_traits<std::shared_ptr<T>> {
60  };
61  template< typename T> struct product_traits<std::optional<T>> {
63  };
64 
65  template<typename T> struct size {
66  using type = typename product_traits<T>::type;
68  };
69  template<> struct size<Null> {
70  constexpr static int value = 0;
71  };
72 
73  template<typename T> struct smart_pointer_traits {
74  using type = typename T::element_type;
75  static auto getPointer(T& iPtr)-> decltype(&*iPtr) { return &*iPtr;}
76  };
77 
78  template<typename T> struct smart_pointer_traits<std::unique_ptr<const T>> {
79  using type = T;
80  static auto getPointer(std::unique_ptr<const T>& iPtr)-> decltype(&*iPtr) { return &*iPtr;}
81  };
82 
83  template<typename T> struct smart_pointer_traits<std::shared_ptr<const T>> {
84  using type = T;
85  static auto getPointer(std::shared_ptr<const T>& iPtr)-> decltype(&*iPtr) { return &*iPtr;}
86  };
87 
88  template<typename T> struct smart_pointer_traits<std::optional<T>> {
89  using type = T;
90  static T* getPointer(std::optional<T>& iPtr) {
91  if(iPtr.has_value()) { return &*iPtr;}
92  return nullptr;
93  }
94  };
95 
96  template<typename T, typename FindT> struct find_index {
98  template<typename HeadT, typename TailT>
99  constexpr static int findIndexOf() {
100  if constexpr(not std::is_same_v<TailT,FindT>) {
102  return findIndexOf<typename container_type::head_type,
103  typename container_type::tail_type>()+1;
104  } else {
105  return 0;
106  }
107  }
108  constexpr static int value = findIndexOf<typename container_type::head_type, typename container_type::tail_type>();
109  };
110  namespace test {
111  template<typename T> const char* name(const T*);
112  }
113 
114  }
115 }
116 
117 #endif
typename product_traits< T >::type container_type
const char * name(const T *)
static auto getPointer(std::unique_ptr< const T > &iPtr) -> decltype(&*iPtr)
#define constexpr
static auto getPointer(T &iPtr) -> decltype(&*iPtr)
void moveFromTo(FromT &iFrom, ToT &iTo)
static auto getPointer(std::shared_ptr< const T > &iPtr) -> decltype(&*iPtr)
Definition: value.py:1
static constexpr int findIndexOf()
long double T
def move(src, dest)
Definition: eostools.py:511
typename product_traits< T >::type type