LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Dereference.h
Go to the documentation of this file.
1 
8 #ifndef LARDATA_UTILITIES_DEREFERENCE_H
9 #define LARDATA_UTILITIES_DEREFERENCE_H 1
10 
11 // C/C++ standard libraries
12 #include <type_traits>
13 
15 namespace lar {
17  namespace util {
19  namespace details {
20 
22  template <typename T>
23  struct is_type : public std::true_type {};
24 
26 
48  template <typename T, typename Enable = void>
49  struct has_dereference_class : public std::false_type {};
50 
51  template <typename T>
53  T,
54  typename std::enable_if<is_type<decltype(*(T()))>::value, void>::type>
55  : public std::true_type {};
57 
59 
94  template <typename T, bool CanDereference>
96  // using type = typename std::add_lvalue_reference<T>::type;
97  using type = T;
98  }; // dereferenced_type <>
99 
100  template <typename T>
101  struct dereferenced_type<T, true> {
102  using type = decltype(*T());
103  }; // dereferenced_type<T, true>
105 
107 
124  template <typename T, bool CanDereference>
126  using argument_type = T;
127  using reference_type = typename std::add_lvalue_reference<
129 
130  reference_type operator()(argument_type& ref) const { return ref; }
131  }; // dereference_class<T, bool>
132 
133  template <typename T>
134  struct dereference_class<T, true> {
135  using argument_type = T;
136  using reference_type =
137  typename std::add_lvalue_reference<typename dereferenced_type<T, true>::type>::type;
138 
139  reference_type operator()(argument_type& ref) const { return *ref; }
140  }; // dereference_class<T, true>
142 
144 
161  template <typename T, bool CanDereference>
163  using argument_type = T;
164  using pointer_type =
165  typename std::add_pointer<typename dereferenced_type<T, CanDereference>::type>::type;
166 
167  pointer_type operator()(argument_type& ref) const { return &ref; }
168  }; // make_pointer_class<T, bool>
169 
170  template <typename T>
171  struct make_pointer_class<T, true> {
172  using argument_type = T;
173  using pointer_type =
174  typename std::add_pointer<typename dereferenced_type<T, true>::type>::type;
175 
176  pointer_type operator()(argument_type& ref) const { return &*ref; }
177  }; // make_pointer_class<T, true>
179 
180  } // namespace details
181 
204  template <typename T>
206  : public details::dereferenced_type<T, details::has_dereference_class<T>::value> {};
207 
237  template <typename T>
238  inline
239  typename details::dereference_class<T,
242  {
244  }
245 
275  template <typename T>
276  inline
277  typename details::make_pointer_class<T,
280  {
282  }
283 
284  } // namespace util
285 
286 } // namespace lar
287 
288 #endif // LARDATA_UTILITIES_DEREFERENCE_H
Functor returning the dereferenced value of the argument.
Definition: Dereference.h:125
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
details::make_pointer_class< T, details::has_dereference_class< T >::value >::pointer_type make_pointer(T &v)
Returns a pointer to the value of argument, or the argument itself.
Definition: Dereference.h:279
reference_type operator()(argument_type &ref) const
Definition: Dereference.h:130
typename std::add_lvalue_reference< typename dereferenced_type< T, CanDereference >::type >::type reference_type
Definition: Dereference.h:128
STL namespace.
pointer_type operator()(argument_type &ref) const
Definition: Dereference.h:167
Functor returning the pointer to a value in the argument.
Definition: Dereference.h:162
details::dereference_class< T, details::has_dereference_class< T >::value >::reference_type dereference(T &v)
Returns the value pointed by the argument, or the argument itself.
Definition: Dereference.h:241
pointer_type operator()(argument_type &ref) const
Definition: Dereference.h:176
typename std::add_pointer< typename dereferenced_type< T, CanDereference >::type >::type pointer_type
Definition: Dereference.h:165
Class defining the dereferenced type of the specified type.
Definition: Dereference.h:205
LArSoft-specific namespace.
Class holding the type dereferenced from an object of type T.
Definition: Dereference.h:95
reference_type operator()(argument_type &ref) const
Definition: Dereference.h:139
Class compiling only if type T exists (then, it&#39;s std::true_type)
Definition: Dereference.h:23
Class defining whether the specified type can be dereferenced.
Definition: Dereference.h:49
typename std::add_lvalue_reference< typename dereferenced_type< T, true >::type >::type reference_type
Definition: Dereference.h:137
typename std::add_pointer< typename dereferenced_type< T, true >::type >::type pointer_type
Definition: Dereference.h:174