The Gaudi Framework  v30r3 (a5ef0a68)
Transformer.h
Go to the documentation of this file.
1 #ifndef TRANSFORMER_H
2 #define TRANSFORMER_H
3 
7 #include "GaudiKernel/apply.h"
8 #include <type_traits>
9 #include <utility>
10 
11 // Adapt an Algorithm (by default, GaudiAlgorithm) so that derived classes
12 // a) do not need to access the event store, and have to explicitly
13 // state their data dependencies
14 // b) are encouraged not to have state which depends on the events
15 // (eg. histograms, counters will have to be mutable)
16 
17 namespace Gaudi
18 {
19  namespace Functional
20  {
21 
22  template <typename Signature, typename Traits_ = Traits::useDefaults>
23  class Transformer;
24 
25  // general N -> 1 algorithms
26  template <typename Out, typename... In, typename Traits_>
27  class Transformer<Out( const In&... ), Traits_>
29  {
30  public:
31  using details::DataHandleMixin<std::tuple<Out>, std::tuple<In...>, Traits_>::DataHandleMixin;
32 
33  // derived classes can NOT implement execute
34  StatusCode execute() override final
35  {
36  try {
37  details::put( std::get<0>( this->m_outputs ),
39  return StatusCode::SUCCESS;
40  } catch ( GaudiException& e ) {
41  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
42  return e.code();
43  }
44  }
45 
46  // instead they MUST implement this operator
47  virtual Out operator()( const In&... ) const = 0;
48  };
49 
50  //
51  // general N -> M algorithms
52  //
53  template <typename Signature, typename Traits_ = Traits::useDefaults>
55 
56  template <typename... Out, typename... In, typename Traits_>
57  class MultiTransformer<std::tuple<Out...>( const In&... ), Traits_>
58  : public details::DataHandleMixin<std::tuple<Out...>, std::tuple<In...>, Traits_>
59  {
60  public:
61  using details::DataHandleMixin<std::tuple<Out...>, std::tuple<In...>, Traits_>::DataHandleMixin;
62 
63  // derived classes can NOT implement execute
64  StatusCode execute() override final
65  {
66  try {
68  [this]( auto&... ohandle ) {
69 
70 #if defined( __clang__ ) && ( __clang_major__ < 6 )
71 // clang-5 gives a spurious warning about not using the captured `ohandle`
72 #pragma clang diagnostic push
73 #pragma clang diagnostic ignored "-Wunused-lambda-capture"
74 #endif
75 
77  [&ohandle...]( auto&&... data ) {
78 #if __cplusplus < 201703L
79  (void)std::initializer_list<int>{
80  ( details::put( ohandle, std::forward<decltype( data )>( data ) ), 0 )...};
81 #else
82  ( details::put( ohandle, std::forward<decltype( data )>( data ) ), ... );
83 #endif
84  },
86 
87 #if defined( __clang__ ) && ( __clang_major__ < 6 )
88 #pragma clang diagnostic pop
89 #endif
90 
91  },
92  this->m_outputs );
93  return StatusCode::SUCCESS;
94  } catch ( GaudiException& e ) {
95  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
96  return e.code();
97  }
98  }
99 
100  // instead they MUST implement this operator
101  virtual std::tuple<Out...> operator()( const In&... ) const = 0;
102  };
103 
104  //
105  // general N -> M algorithms with filter functionality
106  //
107  template <typename Signature, typename Traits_ = Traits::useDefaults>
109 
110  template <typename... Out, typename... In, typename Traits_>
111  class MultiTransformerFilter<std::tuple<Out...>( const In&... ), Traits_>
112  : public details::DataHandleMixin<std::tuple<Out...>, std::tuple<In...>, Traits_>
113  {
114  public:
115  using details::DataHandleMixin<std::tuple<Out...>, std::tuple<In...>, Traits_>::DataHandleMixin;
116 
117  // derived classes can NOT implement execute
118  StatusCode execute() override final
119  {
120  try {
121  Gaudi::apply(
122  [&]( auto&... ohandle ) {
123  Gaudi::apply(
124  [&ohandle..., this]( bool passed, auto&&... data ) {
125  this->setFilterPassed( passed );
126 #if __cplusplus < 201703L
127  (void)std::initializer_list<int>{
128  ( details::put( ohandle, std::forward<decltype( data )>( data ) ), 0 )...};
129 #else
130  ( details::put( ohandle, std::forward<decltype( data )>( data ) ), ... );
131 #endif
132  },
134  },
135  this->m_outputs );
136  return StatusCode::SUCCESS;
137  } catch ( GaudiException& e ) {
138  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
139  return e.code();
140  }
141  }
142 
143  // instead they MUST implement this operator
144  virtual std::tuple<bool, Out...> operator()( const In&... ) const = 0;
145  };
146  }
147 }
148 
149 #endif
virtual const std::string & message() const
error message to be printed
Define general base for Gaudi exception.
STL namespace.
decltype(auto) constexpr apply(F &&f, Tuple &&t) noexcept(noexcept( detail::apply_impl(std::forward< F >(f), std::forward< Tuple >(t), std::make_index_sequence< std::tuple_size< std::remove_reference_t< Tuple >>::value >{})))
Definition: apply.h:31
virtual const StatusCode & code() const
StatusCode for Exception.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
static auto apply(const Algorithm &algo, Handles &handles)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
std::vector< InputHandle_t< In > > m_inputs
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
struct[[deprecated("use MergingTransformer instead")]] Traits_
T forward(T...args)
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
Out1 * put(DataObjectHandle< Out1 > &out_handle, Out2 &&out)