The Gaudi Framework  v32r1 (f65d50dc)
MergingTransformer.h
Go to the documentation of this file.
1 #ifndef MERGING_TRANSFORMER_H
2 #define MERGING_TRANSFORMER_H
3 
4 #include <functional>
5 #include <string>
6 #include <vector>
7 
8 #include "Gaudi/Algorithm.h"
11 
12 namespace Gaudi::Functional {
13 
14  using details::vector_of_const_;
15 
16  namespace details {
17 
18  template <typename Signature, typename Traits_, bool isLegacy>
20 
22  template <typename Out, typename In, typename Traits_>
23  struct MergingTransformer<Out( const vector_of_const_<In>& ), Traits_, true>
24  : details::DataHandleMixin<std::tuple<Out>, void, Traits_> {
25  private:
27 
28  public:
29  using KeyValue = typename base_class::KeyValue;
30  using KeyValues = typename base_class::KeyValues;
31 
32  MergingTransformer( const std::string& name, ISvcLocator* locator, const KeyValues& inputs,
33  const KeyValue& output )
34  : base_class( name, locator, output )
35  , m_inputLocations{this, inputs.first, inputs.second,
37  this->m_inputs = details::make_vector_of_handles<decltype( this->m_inputs )>(
38  this, m_inputLocations );
39  if ( std::is_pointer_v<In> ) { // handle constructor does not (yet) allow to set
40  // optional flag... so do it
41  // explicitly here...
42  std::for_each( this->m_inputs.begin(), this->m_inputs.end(),
43  []( auto& h ) { h.setOptional( true ); } );
44  }
45  },
47 
48  // accessor to input Locations
49  const std::string& inputLocation( unsigned int n ) const { return m_inputLocations.value()[n]; }
50  unsigned int inputLocationSize() const { return m_inputLocations.value().size(); }
51 
52  // derived classes can NOT implement execute
53  StatusCode execute() override final {
55  ins.reserve( m_inputs.size() );
56  std::transform( m_inputs.begin(), m_inputs.end(), std::back_inserter( ins ),
58  try {
59  details::put( std::get<0>( this->m_outputs ), std::as_const( *this )( std::as_const( ins ) ) );
60  return StatusCode::SUCCESS;
61  } catch ( GaudiException& e ) {
62  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
63  return e.code();
64  }
65  }
66 
67  virtual Out operator()( const vector_of_const_<In>& inputs ) const = 0;
68 
69  private:
70  // if In is a pointer, it signals optional (as opposed to mandatory) input
71  template <typename T>
73  std::vector<InputHandle_t<In>> m_inputs; // and make the handles properties instead...
74  Gaudi::Property<std::vector<std::string>> m_inputLocations; // TODO/FIXME: remove this duplication...
75  // TODO/FIXME: replace vector of string property + call-back with a
76  // vector<handle> property ... as soon as declareProperty can deal with that.
77  };
78 
79  template <typename Out, typename In, typename Traits_>
80  struct MergingTransformer<Out( const vector_of_const_<In>& ), Traits_, false>
81  : details::DataHandleMixin<std::tuple<Out>, void, Traits_> {
82  private:
84 
85  public:
86  using KeyValue = typename base_class::KeyValue;
87  using KeyValues = typename base_class::KeyValues;
88 
89  MergingTransformer( const std::string& name, ISvcLocator* locator, const KeyValues& inputs,
90  const KeyValue& output )
91  : base_class( name, locator, output )
92  , m_inputLocations{this, inputs.first, inputs.second,
94  this->m_inputs = details::make_vector_of_handles<decltype( this->m_inputs )>(
95  this, m_inputLocations );
96  if ( std::is_pointer_v<In> ) { // handle constructor does not (yet) allow to set
97  // optional flag... so do it
98  // explicitly here...
99  std::for_each( this->m_inputs.begin(), this->m_inputs.end(),
100  []( auto& h ) { h.setOptional( true ); } );
101  }
102  },
104 
105  // accessor to input Locations
106  const std::string& inputLocation( unsigned int n ) const { return m_inputLocations.value()[n]; }
107  unsigned int inputLocationSize() const { return m_inputLocations.value().size(); }
108 
109  // derived classes can NOT implement execute
110  StatusCode execute( const EventContext& ) const override final {
112  ins.reserve( m_inputs.size() );
113  std::transform( m_inputs.begin(), m_inputs.end(), std::back_inserter( ins ),
115  try {
116  details::put( std::get<0>( this->m_outputs ), ( *this )( std::as_const( ins ) ) );
117  return StatusCode::SUCCESS;
118  } catch ( GaudiException& e ) {
119  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
120  return e.code();
121  }
122  }
123 
124  virtual Out operator()( const vector_of_const_<In>& inputs ) const = 0;
125 
126  private:
127  // if In is a pointer, it signals optional (as opposed to mandatory) input
128  template <typename T>
130  std::vector<InputHandle_t<In>> m_inputs; // and make the handles properties instead...
131  Gaudi::Property<std::vector<std::string>> m_inputLocations; // TODO/FIXME: remove this duplication...
132  // TODO/FIXME: replace vector of string property + call-back with a
133  // vector<handle> property ... as soon as declareProperty can deal with that.
134  };
135 
136  } // namespace details
137 
138  template <typename Signature, typename Traits_ = Traits::useDefaults>
140 
141  // Many of the same -> N
142  template <typename Signature, typename Traits_ = Traits::BaseClass_t<Gaudi::Algorithm>>
144 
145  template <typename... Outs, typename In, typename Traits_>
146  struct MergingMultiTransformer<std::tuple<Outs...>( vector_of_const_<In> const& ), Traits_>
147  : details::DataHandleMixin<std::tuple<Outs...>, void, Traits_> {
148 
149  private:
150  using base_class = details::DataHandleMixin<std::tuple<Outs...>, void, Traits_>;
151 
152  public:
153  using KeyValue = typename base_class::KeyValue;
154  using KeyValues = typename base_class::KeyValues;
155  using OutKeys = std::array<KeyValue, sizeof...( Outs )>;
156 
157  MergingMultiTransformer( std::string const& name, ISvcLocator* locator, KeyValues const& inputs,
158  OutKeys const& outputs );
159 
160  // accessor to input Locations
161  std::string const& inputLocation( unsigned int n ) const { return m_inputLocations.value()[n]; }
162  unsigned int inputLocationSize() const { return m_inputLocations.value().size(); }
163 
164  // derived classes can NOT implement execute
165  StatusCode execute( EventContext const& ) const override final {
167  ins.reserve( m_inputs.size() );
168  std::transform( m_inputs.begin(), m_inputs.end(), std::back_inserter( ins ),
170  try {
171  std::apply(
172  [&]( auto&... outhandle ) {
173  std::apply(
174  [&outhandle...]( auto&&... data ) {
175  ( details::put( outhandle, std::forward<decltype( data )>( data ) ), ... );
176  },
177  std::as_const( *this )( std::as_const( ins ) ) );
178  },
179  this->m_outputs );
180  } catch ( GaudiException& e ) {
181  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
182  return e.code();
183  }
184  return StatusCode::SUCCESS;
185  }
186 
187  virtual std::tuple<Outs...> operator()( const vector_of_const_<In>& inputs ) const = 0;
188 
189  private:
190  // if In is a pointer, it signals optional (as opposed to mandatory) input
191  template <typename T>
193  std::vector<InputHandle_t<In>> m_inputs; // and make the handles properties instead...
194  Gaudi::Property<std::vector<std::string>> m_inputLocations; // TODO/FIXME: remove this duplication...
195  // TODO/FIXME: replace vector of string property + call-back with a
196  // vector<handle> property ... as soon as declareProperty can deal with that.
197  };
198 
199  template <typename... Outs, typename In, typename Traits_>
201  std::string const& name, ISvcLocator* pSvcLocator, KeyValues const& inputs, OutKeys const& outputs )
202  : base_class( name, pSvcLocator, outputs )
203  , m_inputLocations{
204  this, inputs.first, inputs.second,
206  this->m_inputs = details::make_vector_of_handles<decltype( this->m_inputs )>( this, m_inputLocations );
207  if ( std::is_pointer_v<In> ) { // handle constructor does not (yet) allow to set
208  // optional flag... so do it
209  // explicitly here...
210  std::for_each( this->m_inputs.begin(), this->m_inputs.end(), []( auto& h ) { h.setOptional( true ); } );
211  }
212  },
214 
215 } // namespace Gaudi::Functional
216 
217 #endif
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
Implementation of property with value of concrete type.
Definition: Property.h:352
Gaudi::cpp17::detected_or_t< DataObjectReadHandle< T >, detail2::InputHandle_t, Tr, T > InputHandle_t
Gaudi::tagged_bool< class ImmediatelyInvokeHandler_tag > ImmediatelyInvokeHandler
Definition: Property.h:141
virtual const std::string & message() const
error message to be printed
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
STL namespace.
details::InputHandle_t< Traits_, typename std::remove_pointer< T >::type > InputHandle_t
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
MergingTransformer(const std::string &name, ISvcLocator *locator, const KeyValues &inputs, const KeyValue &output)
virtual const StatusCode & code() const
StatusCode for Exception.
STL class.
T back_inserter(T... args)
Out1 * put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
STL class.
MergingTransformer(const std::string &name, ISvcLocator *locator, const KeyValues &inputs, const KeyValue &output)
T transform(T... args)
T for_each(T... args)
Handles make_vector_of_handles(IDataHandleHolder *owner, const std::vector< std::string > &init)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192