The Gaudi Framework  v33r1 (b1225454)
GaudiHandle.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 GAUDIKERNEL_GAUDIHANDLE_H
12 #define GAUDIKERNEL_GAUDIHANDLE_H
13 
14 // Includes
16 #include "GaudiKernel/IInterface.h"
17 #include "GaudiKernel/Property.h"
18 #include "GaudiKernel/System.h"
19 
20 #include <algorithm>
21 #include <iostream>
22 #include <stdexcept>
23 #include <string>
24 #include <type_traits>
25 #include <vector>
26 
27 namespace details {
29  template <class T>
30  std::remove_const_t<T>* nonConst( T* p ) {
31  return const_cast<std::remove_const_t<T>*>( p );
32  }
33 } // namespace details
34 
36 protected:
45  GaudiHandleInfo( std::string myComponentType, std::string myParentName )
46  : m_componentType( std::move( myComponentType ) ), m_parentName( std::move( myParentName ) ) {}
47 
48 public:
50  virtual ~GaudiHandleInfo() = default;
51  //
52  // Public member functions
53  //
54  const std::string& componentType() const { return m_componentType; }
55 
57  const std::string& propertyName() const { return m_propertyName; }
58 
60  void setPropertyName( std::string propName ) { m_propertyName = std::move( propName ); }
61 
63  const std::string& parentName() const { return m_parentName; }
64 
68  virtual std::string pythonPropertyClassName() const = 0;
69 
74  virtual std::string pythonRepr() const = 0;
75 
76 protected:
78  void setComponentType( std::string componentType ) { m_componentType = std::move( componentType ); }
79 
81  void setParentName( std::string parent ) { m_parentName = std::move( parent ); }
82 
83 private:
84  //
85  // Data members
86  //
87  std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
88  std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
89  std::string m_parentName; // name of the parent having this handle as a member
90 };
91 
100  //
101  // Ctors etc
102  //
103 protected:
115  GaudiHandleBase( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
116  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) ) {
117  setTypeAndName( std::move( myTypeAndName ) );
118  }
119 
120 public:
121  //
122  // Public member functions
123  //
125  std::string typeAndName() const { return m_typeAndName; }
126 
128  std::string type() const;
129 
131  std::string name() const;
132 
134  bool empty() const { return m_typeAndName.empty(); }
135 
137  void setTypeAndName( std::string myTypeAndName );
138 
140  void setName( std::string_view myName );
141 
145  std::string pythonPropertyClassName() const override;
146 
148  std::string messageName() const;
149 
153  std::string pythonRepr() const override;
154 
156 
157 private:
158  //
159  // Data member
160  //
161  std::string m_typeAndName; // the full type and name: "type/name"
162 };
163 
172 template <class T>
174  //
175  // Constructors etc.
176  //
177 protected:
178  GaudiHandle( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
179  : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) ) {}
180 
181 public:
183  template <typename CT = T, typename NCT = std::remove_const_t<T>>
185  std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>>* = nullptr )
186  : GaudiHandleBase( other ) {
187  m_pObject = other.get();
188  if ( m_pObject ) ::details::nonConst( m_pObject )->addRef();
189  }
190 
192  GaudiHandle( const GaudiHandle& other ) : GaudiHandleBase( other ) {
193  m_pObject = other.m_pObject;
194  if ( m_pObject ) ::details::nonConst( m_pObject )->addRef();
195  }
196 
198  template <typename CT = T, typename NCT = std::remove_const_t<T>>
199  std::enable_if_t<std::is_const_v<CT> && !std::is_same_v<CT, NCT>, GaudiHandle&>
200  operator=( const GaudiHandle<NCT>& other ) {
201  GaudiHandleBase::operator=( other );
202  // release any current tool
203  release().ignore();
204  m_pObject = other.get();
205  // update ref-counting
206  if ( m_pObject ) ::details::nonConst( m_pObject )->addRef();
207  return *this;
208  }
209 
211  GaudiHandle& operator=( const GaudiHandle& other ) {
212  GaudiHandleBase::operator=( other );
213  // release any current tool
214  release().ignore();
215  m_pObject = other.m_pObject;
216  // update ref-counting
217  if ( m_pObject ) ::details::nonConst( m_pObject )->addRef();
218  return *this;
219  }
220 
223  // not really const, because it updates m_pObject
225  if ( m_pObject && release().isFailure() ) { sc = StatusCode::FAILURE; }
226  if ( sc && retrieve( m_pObject ).isFailure() ) {
227  m_pObject = nullptr;
228  sc = StatusCode::FAILURE;
229  }
230  return sc;
231  }
232 
234  StatusCode release() const {
235  // not really const, because it updates m_pObject
237  if ( m_pObject ) {
238  sc = release( m_pObject );
239  m_pObject = nullptr;
240  }
241  return sc;
242  }
243 
245  bool isValid() const {
246  // not really const, because it may update m_pObject
247  return m_pObject || retrieve().isSuccess();
248  }
249 
252  operator bool() const {
253  // not really const, because it may update m_pObject
254  return isValid();
255  }
256 
258  T* get() { return m_pObject; }
259 
261  std::add_const_t<T>* get() const { return m_pObject; }
262 
264  bool isSet() const { return get(); }
265 
266  T& operator*() {
267  assertObject();
268  return *m_pObject;
269  }
270 
271  T* operator->() {
272  assertObject();
273  return m_pObject;
274  }
275 
276  std::add_const_t<T>& operator*() const {
277  // not really const, because it may update m_pObject
278  assertObject();
279  return *m_pObject;
280  }
281 
282  std::add_const_t<T>* operator->() const {
283  // not really const, because it may update m_pObject
284  assertObject();
285  return m_pObject;
286  }
287 
289  std::string getDefaultType() { return System::typeinfoName( typeid( T ) ); }
290 
292  const auto defName = GaudiHandleBase::type();
293  return ( defName.empty() ? getDefaultType() : defName );
294  }
295 
296 protected:
298  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
299 
302  virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
303  // const cast to support T being a const type
304  ::details::nonConst( comp )->release();
305  return StatusCode::SUCCESS;
306  }
307 
308 private:
311  const std::string& myType = getDefaultType();
312  GaudiHandleBase::setTypeAndName( myType + '/' + myType );
313  }
314 
316  void setDefaultType() { GaudiHandleBase::setTypeAndName( getDefaultType() ); }
317 
320  void assertObject() const { // not really const, because it may update m_pObject
321  if ( UNLIKELY( !isValid() ) ) {
322  throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
323  componentType() + " retrieve", StatusCode::FAILURE );
324  }
325  }
326 
327 private:
328  //
329  // Data members
330  //
331  mutable T* m_pObject = nullptr;
332 };
333 
341 protected:
342  GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
343  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) ) {}
344 
345 public:
349 
352  bool setTypesAndNames( const std::vector<std::string>& myTypesAndNamesList );
353 
356  const std::vector<std::string> typesAndNames() const;
357 
359  const std::vector<std::string> types() const;
360 
362  const std::vector<std::string> names() const;
363 
366  const std::vector<std::string> getBaseInfos( std::string ( GaudiHandleBase::*pMemFunc )() const ) const;
367 
371  std::string pythonPropertyClassName() const override;
372 
376  std::string pythonRepr() const override;
377 
381  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
382 
384  virtual void clear() = 0;
385 
387  virtual bool empty() const = 0;
388 
391  virtual ConstBaseHandleArray getBaseArray() const = 0;
392 
395  virtual BaseHandleArray getBaseArray() = 0;
396 
398  virtual bool retrieved() const = 0;
399 };
400 
402 template <class T>
404 public:
405  //
406  // public nested types
407  //
409  typedef typename HandleVector::value_type value_type;
410  typedef typename HandleVector::size_type size_type;
411  typedef typename HandleVector::reference reference;
412  typedef typename HandleVector::const_reference const_reference;
413  typedef typename HandleVector::iterator iterator;
414  typedef typename HandleVector::const_iterator const_iterator;
415  typedef typename HandleVector::reverse_iterator reverse_iterator;
416  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
417 
418 protected:
419  //
420  // Constructors
421  //
426  GaudiHandleArray( const std::vector<std::string>& myTypesAndNamesList, std::string myComponentType,
427  std::string myParentName )
428  : GaudiHandleArrayBase( std::move( myComponentType ), std::move( myParentName ) ) {
429  setTypesAndNames( myTypesAndNamesList );
430  }
431 
436  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
437  : GaudiHandleArrayBase( myComponentType, myParentName ) {}
438 
439 public:
441  GaudiHandleArray& operator=( const std::vector<std::string>& myTypesAndNamesList ) {
442  setTypesAndNames( myTypesAndNamesList );
443  return *this;
444  }
445 
448  for ( auto& h : m_handleArray ) baseArray.push_back( &h );
449  return baseArray;
450  }
451 
454  for ( auto& h : m_handleArray ) baseArray.push_back( &h );
455  return baseArray;
456  }
457 
458  //
459  // Simulate (part of) an std::vector
460  //
461  iterator begin() { return m_handleArray.begin(); }
462 
463  iterator end() { return m_handleArray.end(); }
464 
465  const_iterator begin() const { return m_handleArray.begin(); }
466 
467  const_iterator end() const { return m_handleArray.end(); }
468 
469  const_iterator rbegin() const { return m_handleArray.rbegin(); }
470 
471  const_iterator rend() const { return m_handleArray.rend(); }
472 
473  size_type size() const { return m_handleArray.size(); }
474 
475  void clear() override { m_handleArray.clear(); }
476 
477  bool empty() const override { return m_handleArray.empty(); }
478 
479  T& operator[]( int index ) { return m_handleArray[index]; }
480 
481  const T& operator[]( int index ) const { return m_handleArray[index]; }
482 
484  T* operator[]( std::string_view name ) {
485  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
486  return it != end() ? &*it : nullptr;
487  }
488 
490  const T* operator[]( std::string_view name ) const {
491  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
492  return it != end() ? &*it : nullptr;
493  }
494 
497  using GaudiHandleArrayBase::push_back; // avoid compiler warning
498  virtual bool push_back( const T& myHandle ) {
499  m_handleArray.push_back( myHandle );
500  return true;
501  }
502 
506  for ( auto& i : *this ) {
507  // stop at first failure
508  if ( i.retrieve().isFailure() ) {
509  sc = StatusCode::FAILURE;
510  break;
511  }
512  }
513  if ( sc ) { m_retrieved = true; }
514  return sc;
515  }
516 
520  for ( auto& i : *this ) {
521  // continue trying to release other tools even if we fail...
522  if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
523  }
524  return sc;
525  }
526 
528  virtual bool retrieved() const override { return m_retrieved; }
529 
530 private:
531  //
532  // Private data members
533  //
535  bool m_retrieved{false};
536 };
537 
538 // Easy printing out of Handles and HandleArrays
539 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
540 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
541 
542 #endif // ! GAUDIKERNEL_GAUDIHANDLE_H
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:211
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:412
#define UNLIKELY(x)
Definition: Kernel.h:106
HandleVector::value_type value_type
Definition: GaudiHandle.h:409
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:173
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:234
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:475
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:289
Define general base for Gaudi exception.
std::add_const_t< T > * get() const
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:261
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:134
HandleVector::iterator iterator
Definition: GaudiHandle.h:413
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:446
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:19
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:57
GaudiHandle(const GaudiHandle< NCT > &other, std::enable_if_t< std::is_const_v< CT > &&!std::is_same_v< CT, NCT >> *=nullptr)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:184
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
std::vector< T > HandleVector
Definition: GaudiHandle.h:408
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:125
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:416
std::string getDefaultName()
Definition: GaudiHandle.h:291
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
size_type size() const
Definition: GaudiHandle.h:473
STL namespace.
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:222
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:178
void setComponentType(std::string componentType)
The component type.
Definition: GaudiHandle.h:78
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:316
std::string m_componentType
Definition: GaudiHandle.h:87
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
Definition: GaudiHandle.h:426
HandleVector::size_type size_type
Definition: GaudiHandle.h:410
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:441
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:477
HandleVector::reference reference
Definition: GaudiHandle.h:411
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:498
std::enable_if_t< std::is_const_v< CT > &&!std::is_same_v< CT, NCT >, GaudiHandle & > operator=(const GaudiHandle< NCT > &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:200
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
const T & operator[](int index) const
Definition: GaudiHandle.h:481
auto operator *(const std::chrono::duration< Rep1, Period > &lhs, const std::chrono::duration< Rep2, Period > &rhs)
Multiplication of two std::chrono::duration objects with same Period.
Definition: Counters.h:40
std::add_const_t< T > * operator->() const
Definition: GaudiHandle.h:282
STL class.
void push_back(Container &c, const Value &v, std::true_type)
T push_back(T... args)
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:310
virtual std::string pythonRepr() const =0
Python representation of handle, i.e.
HandleVector m_handleArray
Definition: GaudiHandle.h:534
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:504
const_iterator rend() const
Definition: GaudiHandle.h:471
iterator begin()
Definition: GaudiHandle.h:461
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:30
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in <myTypesAndNamesList>.
Definition: GaudiHandle.cpp:63
const_iterator end() const
Definition: GaudiHandle.h:467
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:63
virtual bool retrieved() const override
has Array been retreived?
Definition: GaudiHandle.h:528
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:258
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:452
std::string m_typeAndName
Definition: GaudiHandle.h:161
def end
Definition: IOTest.py:123
const T * operator[](std::string_view name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:490
const_iterator rbegin() const
Definition: GaudiHandle.h:469
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:348
T move(T... args)
T * operator->()
Definition: GaudiHandle.h:271
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:320
const std::string & componentType() const
Definition: GaudiHandle.h:54
T find_if(T... args)
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:414
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:340
virtual StatusCode release(T *comp) const
Release the component.
Definition: GaudiHandle.h:302
virtual std::string pythonPropertyClassName() const =0
The python class name for the property in the genconf-generated configurables.
STL class.
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:264
std::string type() const
The concrete component class name: the part before the '/'.
Definition: GaudiHandle.cpp:21
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:45
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:60
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:415
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
std::vector< GaudiHandleBase * > BaseHandleArray
Definition: GaudiHandle.h:347
void setParentName(std::string parent)
The name of the parent.
Definition: GaudiHandle.h:81
constexpr static const auto FAILURE
Definition: StatusCode.h:101
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
const Gaudi::Algorithm & parent
iterator end()
Definition: GaudiHandle.h:463
T & operator[](int index)
Definition: GaudiHandle.h:479
const_iterator begin() const
Definition: GaudiHandle.h:465
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:99
AttribStringParser::Iterator begin(const AttribStringParser &parser)
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:342
#define GAUDI_API
Definition: Kernel.h:81
T * operator[](std::string_view name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:484
STL class.
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle ('smart pointer') to a gaudi component.
Definition: GaudiHandle.h:115
std::string m_propertyName
Definition: GaudiHandle.h:88
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:192
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:245
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:403
std::string m_parentName
Definition: GaudiHandle.h:89
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:518
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:436