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::optional<T>> {
79  using type = T;
80  static T* getPointer(std::optional<T>& iPtr) {
81  if(iPtr.has_value()) { return &*iPtr;}
82  return nullptr;
83  }
84  };
85 
86  template<typename T, typename FindT> struct find_index {
88  template<typename HeadT, typename TailT>
89  constexpr static int findIndexOf() {
90  if constexpr(not std::is_same_v<TailT,FindT>) {
92  return findIndexOf<typename container_type::head_type,
93  typename container_type::tail_type>()+1;
94  } else {
95  return 0;
96  }
97  }
98  constexpr static int value = findIndexOf<typename container_type::head_type, typename container_type::tail_type>();
99  };
100  namespace test {
101  template<typename T> const char* name(const T*);
102  }
103 
104  }
105 }
106 
107 #endif
typename product_traits< T >::type container_type
const char * name(const T *)
#define constexpr
static auto getPointer(T &iPtr) -> decltype(&*iPtr)
void moveFromTo(FromT &iFrom, ToT &iTo)
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