The Gaudi Framework  v33r1 (b1225454)
MergingTransformer.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef MERGING_TRANSFORMER_H
12 #define MERGING_TRANSFORMER_H
13 
14 #include <functional>
15 #include <string>
16 #include <vector>
17 
18 #include "Gaudi/Algorithm.h"
22 
23 namespace Gaudi::Functional {
24 
25  using details::vector_of_const_;
26 
27  namespace details {
28 
29  template <typename Signature, typename Traits_, bool isLegacy>
31 
33  template <typename Out, typename In, typename Traits_>
34  struct MergingTransformer<Out( const vector_of_const_<In>& ), Traits_, true>
35  : DataHandleMixin<std::tuple<Out>, std::tuple<>, Traits_> {
36  private:
37  using base_class = DataHandleMixin<std::tuple<Out>, std::tuple<>, Traits_>;
38 
39  public:
40  using KeyValue = typename base_class::KeyValue;
41  using KeyValues = typename base_class::KeyValues;
42 
44  : base_class( std::move( name ), locator, output )
45  , m_inputLocations{this, inputs.first, inputs.second,
47  this->m_inputs =
48  make_vector_of_handles<decltype( this->m_inputs )>( this, m_inputLocations );
49  if ( std::is_pointer_v<In> ) { // handle constructor does not (yet) allow to set
50  // optional flag... so do it
51  // explicitly here...
52  std::for_each( this->m_inputs.begin(), this->m_inputs.end(),
53  []( auto& h ) { h.setOptional( true ); } );
54  }
55  },
57 
58  // accessor to input Locations
59  const std::string& inputLocation( unsigned int n ) const { return m_inputLocations.value()[n]; }
60  unsigned int inputLocationSize() const { return m_inputLocations.value().size(); }
61 
62  // derived classes can NOT implement execute
63  StatusCode execute() override final {
64  vector_of_const_<In> ins;
65  ins.reserve( m_inputs.size() );
66  std::transform( m_inputs.begin(), m_inputs.end(), std::back_inserter( ins ), details2::get_from_handle<In>{} );
67  try {
68  put( std::get<0>( this->m_outputs ), std::as_const( *this )( std::as_const( ins ) ) );
70  } catch ( GaudiException& e ) {
71  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
72  return e.code();
73  }
74  }
75 
76  virtual Out operator()( const vector_of_const_<In>& inputs ) const = 0;
77 
78  private:
79  // if In is a pointer, it signals optional (as opposed to mandatory) input
80  template <typename T>
82  std::vector<InputHandle_t<In>> m_inputs; // and make the handles properties instead...
83  Gaudi::Property<std::vector<std::string>> m_inputLocations; // TODO/FIXME: remove this duplication...
84  // TODO/FIXME: replace vector of string property + call-back with a
85  // vector<handle> property ... as soon as declareProperty can deal with that.
86  };
87 
88  template <typename Out, typename In, typename Traits_>
89  struct MergingTransformer<Out( const vector_of_const_<In>& ), Traits_, false>
90  : DataHandleMixin<std::tuple<Out>, std::tuple<>, Traits_> {
91  private:
92  using base_class = DataHandleMixin<std::tuple<Out>, std::tuple<>, Traits_>;
93 
94  public:
95  using KeyValue = typename base_class::KeyValue;
96  using KeyValues = typename base_class::KeyValues;
97 
99  : base_class( std::move( name ), locator, output )
100  , m_inputLocations{this, inputs.first, inputs.second,
102  this->m_inputs =
103  make_vector_of_handles<decltype( this->m_inputs )>( this, m_inputLocations );
104  if ( std::is_pointer_v<In> ) { // handle constructor does not (yet) allow to set
105  // optional flag... so do it
106  // explicitly here...
107  std::for_each( this->m_inputs.begin(), this->m_inputs.end(),
108  []( auto& h ) { h.setOptional( true ); } );
109  }
110  },
112 
113  // accessor to input Locations
114  const std::string& inputLocation( unsigned int n ) const { return m_inputLocations.value()[n]; }
115  unsigned int inputLocationSize() const { return m_inputLocations.value().size(); }
116 
117  // derived classes can NOT implement execute
118  StatusCode execute( const EventContext& ) const override final {
119  vector_of_const_<In> ins;
120  ins.reserve( m_inputs.size() );
121  std::transform( m_inputs.begin(), m_inputs.end(), std::back_inserter( ins ), details2::get_from_handle<In>{} );
122  try {
123  put( std::get<0>( this->m_outputs ), ( *this )( std::as_const( ins ) ) );
124  return FilterDecision::PASSED;
125  } catch ( GaudiException& e ) {
126  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
127  return e.code();
128  }
129  }
130 
131  virtual Out operator()( const vector_of_const_<In>& inputs ) const = 0;
132 
133  private:
134  // if In is a pointer, it signals optional (as opposed to mandatory) input
135  template <typename T>
137  std::vector<InputHandle_t<In>> m_inputs; // and make the handles properties instead...
138  Gaudi::Property<std::vector<std::string>> m_inputLocations; // TODO/FIXME: remove this duplication...
139  // TODO/FIXME: replace vector of string property + call-back with a
140  // vector<handle> property ... as soon as declareProperty can deal with that.
141  };
142 
143  } // namespace details
144 
145  template <typename Signature, typename Traits_ = Traits::useDefaults>
147 
148  // Many of the same -> N
149  template <typename Signature, typename Traits_ = Traits::BaseClass_t<Gaudi::Algorithm>>
151 
152  template <typename... Outs, typename In, typename Traits_>
153  struct MergingMultiTransformer<std::tuple<Outs...>( vector_of_const_<In> const& ), Traits_>
154  : details::DataHandleMixin<std::tuple<Outs...>, std::tuple<>, Traits_> {
155 
156  private:
157  using base_class = details::DataHandleMixin<std::tuple<Outs...>, std::tuple<>, Traits_>;
158 
159  public:
160  using KeyValue = typename base_class::KeyValue;
161  using KeyValues = typename base_class::KeyValues;
162  using OutKeys = std::array<KeyValue, sizeof...( Outs )>;
163 
164  MergingMultiTransformer( std::string const& name, ISvcLocator* locator, KeyValues const& inputs,
165  OutKeys const& outputs );
166 
167  // accessor to input Locations
168  std::string const& inputLocation( unsigned int n ) const { return m_inputLocations.value()[n]; }
169  unsigned int inputLocationSize() const { return m_inputLocations.value().size(); }
170 
171  // derived classes can NOT implement execute
172  StatusCode execute( EventContext const& ) const override final {
173  vector_of_const_<In> ins;
174  ins.reserve( m_inputs.size() );
175  std::transform( m_inputs.begin(), m_inputs.end(), std::back_inserter( ins ),
176  details::details2::get_from_handle<In>{} );
177  try {
178  std::apply(
179  [&]( auto&... outhandle ) {
181  std::apply(
182  [&outhandle...]( auto&&... data ) {
183  ( details::put( outhandle, std::forward<decltype( data )>( data ) ), ... );
184  },
185  std::as_const( *this )( std::as_const( ins ) ) );
187  },
188  this->m_outputs );
189  return FilterDecision::PASSED;
190  } catch ( GaudiException& e ) {
191  ( e.code() ? this->warning() : this->error() ) << e.message() << endmsg;
192  return e.code();
193  }
194  }
195 
196  virtual std::tuple<Outs...> operator()( const vector_of_const_<In>& inputs ) const = 0;
197 
198  private:
199  // if In is a pointer, it signals optional (as opposed to mandatory) input
200  template <typename T>
202  std::vector<InputHandle_t<In>> m_inputs; // and make the handles properties instead...
203  Gaudi::Property<std::vector<std::string>> m_inputLocations; // TODO/FIXME: remove this duplication...
204  // TODO/FIXME: replace vector of string property + call-back with a
205  // vector<handle> property ... as soon as declareProperty can deal with that.
206  };
207 
208  template <typename... Outs, typename In, typename Traits_>
209  MergingMultiTransformer<std::tuple<Outs...>( const vector_of_const_<In>& ), Traits_>::MergingMultiTransformer(
210  std::string const& name, ISvcLocator* pSvcLocator, KeyValues const& inputs, OutKeys const& outputs )
211  : base_class( name, pSvcLocator, outputs )
212  , m_inputLocations{
213  this, inputs.first, inputs.second,
215  this->m_inputs = details::make_vector_of_handles<decltype( this->m_inputs )>( this, m_inputLocations );
216  if ( std::is_pointer_v<In> ) { // handle constructor does not (yet) allow to set
217  // optional flag... so do it
218  // explicitly here...
219  std::for_each( this->m_inputs.begin(), this->m_inputs.end(), []( auto& h ) { h.setOptional( true ); } );
220  }
221  },
223 
224 } // namespace Gaudi::Functional
225 
226 #endif
Out1 * put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
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:35
Implementation of property with value of concrete type.
Definition: Property.h:370
MergingTransformer(std::string name, ISvcLocator *locator, const KeyValues &inputs, const KeyValue &output)
#define GF_SUPPRESS_SPURIOUS_CLANG_WARNING_BEGIN
Gaudi::tagged_bool< class ImmediatelyInvokeHandler_tag > ImmediatelyInvokeHandler
Definition: Property.h:159
virtual const std::string & message() const
error message to be printed
MergingTransformer(std::string name, ISvcLocator *locator, const KeyValues &inputs, const KeyValue &output)
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:34
STL class.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
virtual const StatusCode & code() const
StatusCode for Exception.
STL class.
T back_inserter(T... args)
STL class.
Handles make_vector_of_handles(IDataHandleHolder *owner, const std::vector< std::string > &init)
T transform(T... args)
#define GF_SUPPRESS_SPURIOUS_CLANG_WARNING_END
T for_each(T... args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202