CMS 3D CMS Logo

WrapperDetail.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_WrapperDetail_h
2 #define DataFormats_Common_WrapperDetail_h
3 
4 /*----------------------------------------------------------------------
5 
6 WrapperDetail: Metafunction support for compile-time selection of code.
7 
8 ----------------------------------------------------------------------*/
9 
10 #include <memory>
11 #include <typeinfo>
12 #include <type_traits>
13 #include <vector>
14 
15 namespace edm {
16 
17  //Need to specialize the case of std::vector<edm::Ptr<T>>
18  template <typename T>
19  class Ptr;
20 
21  namespace detail {
22  using no_tag = std::false_type; // type indicating FALSE
23  using yes_tag = std::true_type; // type indicating TRUE
24 
25  // valueTypeInfo_() will return typeid(T::value_type) if T::value_type is declared and typeid(void) otherwise.
26  // Definitions for the following struct and function templates are not needed; we only require the declarations.
27  template <typename T>
28  static yes_tag has_value_type(typename T::value_type*);
29  template <typename T>
30  static no_tag has_value_type(...);
31 
32  template <typename T>
34  static constexpr bool value = std::is_same<decltype(has_value_type<T>(nullptr)), yes_tag>::value;
35  };
37  struct getValueType;
38  template <typename T>
39  struct getValueType<T, true> {
40  std::type_info const& operator()() { return typeid(typename T::value_type); }
41  };
42  template <typename T>
43  struct getValueType<T, false> {
44  std::type_info const& operator()() { return typeid(void); }
45  };
46 
47  // memberTypeInfo_() will return typeid(T::member_type) if T::member_type is declared and typeid(void) otherwise.
48  // Definitions for the following struct and function templates are not needed; we only require the declarations.
49  template <typename T>
50  static yes_tag has_member_type(typename T::member_type*);
51  template <typename T>
52  static no_tag has_member_type(...);
53 
54  template <typename T>
56  static constexpr bool value = std::is_same<decltype(has_member_type<T>(nullptr)), yes_tag>::value;
57  };
59  struct getMemberType;
60  template <typename T>
61  struct getMemberType<T, true> {
62  std::type_info const& operator()() { return typeid(typename T::member_type); }
63  };
64  template <typename T>
65  struct getMemberType<T, false> {
66  std::type_info const& operator()() { return typeid(void); }
67  };
68 
69  template <typename T>
70  struct has_typedef_member_type<std::vector<edm::Ptr<T> > > {
71  static constexpr bool value = true;
72  };
73 
74  template <typename T>
75  struct getMemberType<std::vector<edm::Ptr<T> >, true> {
76  std::type_info const& operator()() { return typeid(T); }
77  };
78 
79  template <typename T, typename Deleter>
80  struct has_typedef_member_type<std::vector<std::unique_ptr<T, Deleter> > > {
81  static constexpr bool value = true;
82  };
83 
84  template <typename T, typename Deleter>
85  struct getMemberType<std::vector<std::unique_ptr<T, Deleter> >, true> {
86  std::type_info const& operator()() { return typeid(T); }
87  };
88 
89  // bool isMergeable_() will return true if T::mergeProduct(T const&) is declared and false otherwise
90  // bool mergeProduct_(WrapperBase const*) will merge products if T::mergeProduct(T const&) is defined
91  // Definitions for the following struct and function templates are not needed; we only require the declarations.
92  template <typename T, bool (T::*)(T const&)>
94  template <typename T>
96  template <typename T>
97  static no_tag has_mergeProduct(...);
98 
99  template <typename T>
101  static constexpr bool value = std::is_same<decltype(has_mergeProduct<T>(nullptr)), yes_tag>::value;
102  };
103 
106  template <typename T>
108  bool operator()() { return true; }
109  };
110  template <typename T>
112  bool operator()() { return false; }
113  };
116  template <typename T>
117  struct doMergeProduct<T, true> {
118  bool operator()(T& thisProduct, T const& newProduct) { return thisProduct.mergeProduct(newProduct); }
119  };
120  template <typename T>
121  struct doMergeProduct<T, false> {
122  bool operator()(T& thisProduct, T const& newProduct) {
123  return true; // Should never be called
124  }
125  };
126 
127  // bool hasIsProductEqual_() will return true if T::isProductEqual(T const&) const is declared and false otherwise
128  // bool isProductEqual _(WrapperBase const*) will call T::isProductEqual(T const&) if it is defined
129  // Definitions for the following struct and function templates are not needed; we only require the declarations.
130  template <typename T, bool (T::*)(T const&) const>
132  template <typename T>
134  template <typename T>
135  static no_tag has_isProductEqual(...);
136 
137  template <typename T>
139  static constexpr bool value = std::is_same<decltype(has_isProductEqual<T>(nullptr)), yes_tag>::value;
140  };
141 
144  template <typename T>
146  bool operator()() { return true; }
147  };
148  template <typename T>
150  bool operator()() { return false; }
151  };
154  template <typename T>
156  bool operator()(T const& thisProduct, T const& newProduct) { return thisProduct.isProductEqual(newProduct); }
157  };
158  template <typename T>
160  bool operator()(T const& thisProduct, T const& newProduct) {
161  return true; // Should never be called
162  }
163  };
164 
165  // bool hasSwap_() will return true if T::swap(T&) is declared and false otherwise
166  // void swapProduct_() will call T::swap(T&) if it is defined otherwise it does nothing
167  // Definitions for the following struct and function templates are not needed; we only require the declarations.
168  template <typename T, void (T::*)(T&)>
170  template <typename T>
172  template <typename T>
173  static no_tag has_swap(...);
174 
175  template <typename T>
177  static constexpr bool value = std::is_same<decltype(has_swap<T>(nullptr)), yes_tag>::value;
178  };
179 
182  template <typename T>
184  bool operator()() { return true; }
185  };
186  template <typename T>
188  bool operator()() { return false; }
189  };
192  template <typename T>
193  struct doSwapProduct<T, true> {
194  void operator()(T& thisProduct, T& newProduct) { thisProduct.swap(newProduct); }
195  };
196  template <typename T>
197  struct doSwapProduct<T, false> {
198  void operator()(T&, T&) {
199  return; // Should never be called
200  }
201  };
202  } // namespace detail
203 } // namespace edm
204 #endif
edm::detail::getHasMergeFunction< T, false >::operator()
bool operator()()
Definition: WrapperDetail.h:112
funct::false
false
Definition: Factorize.h:34
edm::detail::doIsProductEqual< T, false >::operator()
bool operator()(T const &thisProduct, T const &newProduct)
Definition: WrapperDetail.h:160
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::detail::swap_function
Definition: WrapperDetail.h:169
edm::detail::doIsProductEqual
Definition: WrapperDetail.h:153
edm::detail::getValueType< T, true >::operator()
std::type_info const & operator()()
Definition: WrapperDetail.h:40
edm::detail::getHasSwapFunction< T, true >::operator()
bool operator()()
Definition: WrapperDetail.h:184
edm::detail::getMemberType< T, false >::operator()
std::type_info const & operator()()
Definition: WrapperDetail.h:66
edm::detail::getMemberType< std::vector< std::unique_ptr< T, Deleter > >, true >::operator()
std::type_info const & operator()()
Definition: WrapperDetail.h:86
edm::detail::no_tag
std::false_type no_tag
Definition: WrapperDetail.h:22
edm::detail::doMergeProduct< T, true >::operator()
bool operator()(T &thisProduct, T const &newProduct)
Definition: WrapperDetail.h:118
edm::detail::doSwapProduct< T, true >::operator()
void operator()(T &thisProduct, T &newProduct)
Definition: WrapperDetail.h:194
edm::detail::has_swap_function
Definition: WrapperDetail.h:176
edm::detail::has_typedef_member_type
Definition: WrapperDetail.h:55
edm::detail::getHasSwapFunction
Definition: WrapperDetail.h:181
edm::detail::has_mergeProduct_function
Definition: WrapperDetail.h:100
edm::detail::has_isProductEqual_function
Definition: WrapperDetail.h:138
edm::detail::has_typedef_value_type
Definition: WrapperDetail.h:33
edm::detail::getMemberType< T, true >::operator()
std::type_info const & operator()()
Definition: WrapperDetail.h:62
edm::detail::isProductEqual_function
Definition: WrapperDetail.h:131
funct::true
true
Definition: Factorize.h:173
edm::detail::has_member_type
static yes_tag has_member_type(typename T::member_type *)
edm::detail::getMemberType
Definition: WrapperDetail.h:59
edm::detail::has_isProductEqual
static yes_tag has_isProductEqual(isProductEqual_function< T, &T::isProductEqual > *dummy)
edm::detail::getHasIsProductEqual
Definition: WrapperDetail.h:143
edm::detail::doSwapProduct< T, false >::operator()
void operator()(T &, T &)
Definition: WrapperDetail.h:198
edm::detail::has_swap
static yes_tag has_swap(swap_function< T, &T::swap > *dummy)
value
Definition: value.py:1
edm::detail::doIsProductEqual< T, true >::operator()
bool operator()(T const &thisProduct, T const &newProduct)
Definition: WrapperDetail.h:156
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
edm::detail::mergeProduct_function
Definition: WrapperDetail.h:93
edm::detail::getHasIsProductEqual< T, true >::operator()
bool operator()()
Definition: WrapperDetail.h:146
edm::detail::getValueType< T, false >::operator()
std::type_info const & operator()()
Definition: WrapperDetail.h:44
edm::detail::doMergeProduct< T, false >::operator()
bool operator()(T &thisProduct, T const &newProduct)
Definition: WrapperDetail.h:122
edm::detail::has_mergeProduct
static yes_tag has_mergeProduct(mergeProduct_function< T, &T::mergeProduct > *dummy)
std
Definition: JetResolutionObject.h:76
edm::detail::yes_tag
std::true_type yes_tag
Definition: WrapperDetail.h:23
edm::detail::getHasMergeFunction
Definition: WrapperDetail.h:105
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
edm::detail::getHasSwapFunction< T, false >::operator()
bool operator()()
Definition: WrapperDetail.h:188
edm::detail::doSwapProduct
Definition: WrapperDetail.h:191
funct::void
TEMPL(T2) struct Divides void
Definition: Factorize.h:29
edm::detail::getValueType
Definition: WrapperDetail.h:37
edm::detail::getHasIsProductEqual< T, false >::operator()
bool operator()()
Definition: WrapperDetail.h:150
edm::detail::doMergeProduct
Definition: WrapperDetail.h:115
dummy
Definition: DummySelector.h:38
edm::detail::getMemberType< std::vector< edm::Ptr< T > >, true >::operator()
std::type_info const & operator()()
Definition: WrapperDetail.h:76
edm::detail::getHasMergeFunction< T, true >::operator()
bool operator()()
Definition: WrapperDetail.h:108
edm::detail::has_value_type
static yes_tag has_value_type(typename T::value_type *)