GaudiHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_GAUDIHANDLE_H
2 #define GAUDIKERNEL_GAUDIHANDLE_H
3 
4 //Includes
6 #include "GaudiKernel/System.h"
8 
9 #include <string>
10 #include <algorithm>
11 #include <vector>
12 #include <stdexcept>
13 #include <iostream>
14 #include <type_traits>
15 
17 protected:
26  GaudiHandleInfo( std::string myComponentType, std::string myParentName )
27  : m_componentType(std::move(myComponentType)), m_parentName(std::move(myParentName))
28  {}
29 public:
31  virtual ~GaudiHandleInfo() = default;
32  //
33  // Public member functions
34  //
35  const std::string& componentType() const {
36  return m_componentType;
37  }
38 
40  const std::string& propertyName() const {
41  return m_propertyName;
42  }
43 
45  void setPropertyName( std::string propName ) {
46  m_propertyName = std::move(propName);
47  }
48 
50  const std::string& parentName() const {
51  return m_parentName;
52  }
53 
57  virtual std::string pythonPropertyClassName() const = 0;
58 
63  virtual std::string pythonRepr() const = 0;
64 
65 protected:
66 
68  void setComponentType(const std::string& componentType) {
69  m_componentType = componentType;
70  }
71 
73  void setParentName(const std::string& parent) {
74  m_parentName = parent;
75  }
76 
77 private:
78  //
79  // Data members
80  //
81  std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
82  std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
83  std::string m_parentName; // name of the parent having this handle as a member
84 };
85 
86 
95  //
96  // Ctors etc
97  //
98 protected:
110  GaudiHandleBase( std::string myTypeAndName, std::string myComponentType,
111  std::string myParentName )
112  : GaudiHandleInfo(std::move(myComponentType),std::move(myParentName))
113  {
114  setTypeAndName(std::move(myTypeAndName));
115  }
116 public:
117  //
118  // Public member functions
119  //
122  return m_typeAndName;
123  }
124 
126  std::string type() const;
127 
129  std::string name() const;
130 
132  bool empty() const {
133  return m_typeAndName.empty();
134  }
135 
137  void setTypeAndName( std::string myTypeAndName );
138 
140  void setName( const std::string& myName );
141 
145  std::string pythonPropertyClassName() const override;
146 
148  std::string messageName() const;
149 
153  std::string pythonRepr() const override;
154 
155 private:
156  //
157  // Data member
158  //
159  std::string m_typeAndName; // the full type and name: "type/name"
160 };
161 
162 
171 template< class T >
173  //
174  // Constructors etc.
175  //
176 protected:
177  GaudiHandle( std::string myTypeAndName, std::string myComponentType,
178  std::string myParentName )
179  : GaudiHandleBase(std::move(myTypeAndName), std::move(myComponentType), std::move(myParentName))
180  {}
181 
182 public:
183 
185  template< typename CT = T,
186  typename NCT = typename std::remove_const<T>::type >
189  !std::is_same<CT,NCT>::value >::type * = nullptr )
190  : GaudiHandleBase( other ) {
191  m_pObject = other.get();
192  if ( m_pObject ) nonConst(m_pObject)->addRef();
193  }
194 
196  GaudiHandle( const GaudiHandle& other )
197  : GaudiHandleBase( other ) {
198  m_pObject = other.m_pObject;
199  if ( m_pObject ) nonConst(m_pObject)->addRef();
200  }
201 
203  template< typename CT = T,
204  typename NCT = typename std::remove_const<T>::type >
206  operator=( const GaudiHandle<NCT>& other ) {
208  // release any current tool
209  release().ignore();
210  m_pObject = other.get();
211  // update ref-counting
212  if ( m_pObject ) nonConst(m_pObject)->addRef();
213  return *this;
214  }
215 
217  GaudiHandle& operator=( const GaudiHandle& other ) {
219  // release any current tool
220  release().ignore();
221  m_pObject = other.m_pObject;
222  // update ref-counting
223  if ( m_pObject ) nonConst(m_pObject)->addRef();
224  return *this;
225  }
226 
228  StatusCode retrieve() const { // not really const, because it updates m_pObject
230  if ( m_pObject && release().isFailure() ) { sc = StatusCode::FAILURE; }
231  if ( sc && retrieve( m_pObject ).isFailure() )
232  {
233  m_pObject = nullptr;
234  sc = StatusCode::FAILURE;
235  }
236  return sc;
237  }
238 
240  StatusCode release() const { // not really const, because it updates m_pObject
242  if ( m_pObject )
243  {
244  sc = release( m_pObject );
245  m_pObject = nullptr;
246  }
247  return sc;
248  }
249 
251  bool isValid() const { // not really const, because it may update m_pObject
252  return m_pObject || retrieve().isSuccess();
253  }
254 
257  operator bool() const { // not really const, because it may update m_pObject
258  return isValid();
259  }
260 
262  T * get() { return m_pObject; }
263 
265  typename std::add_const<T>::type * get() const {
266  return m_pObject;
267  }
268 
270  bool isSet() const {
271  return get();
272  }
273 
274  T& operator*() {
275  assertObject();
276  return *m_pObject;
277  }
278 
279  T* operator->() {
280  assertObject();
281  return m_pObject;
282  }
283 
284  typename std::add_const<T>::type & operator*() const {
285  // not really const, because it may update m_pObject
286  assertObject();
287  return *m_pObject;
288  }
289 
290  typename std::add_const<T>::type * operator->() const {
291  // not really const, because it may update m_pObject
292  assertObject();
293  return m_pObject;
294  }
295 
298  return System::typeinfoName( typeid(T) );
299  }
300 
302  const auto defName = GaudiHandleBase::type();
303  return ( defName.empty() ? getDefaultType() : defName );
304  }
305 
306 protected:
307 
309  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
310 
313  virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
314  // const cast to support T being a const type
315  nonConst(comp)->release();
316  return StatusCode::SUCCESS;
317  }
318 
320  template< class CLASS >
321  typename std::remove_const<CLASS>::type * nonConst( CLASS* p ) const
322  {
323  return const_cast< typename std::remove_const<CLASS>::type * >( p );
324  }
325 
326  private:
327 
330  const std::string& myType = getDefaultType();
331  GaudiHandleBase::setTypeAndName(myType+'/'+myType);
332  }
333 
335  void setDefaultType() {
336  GaudiHandleBase::setTypeAndName( getDefaultType() );
337  }
338 
341  void assertObject() const { // not really const, because it may update m_pObject
342  if ( UNLIKELY(!isValid()) ) {
343  throw GaudiException("Failed to retrieve " + componentType() + ": " + typeAndName(),
344  componentType() + " retrieve", StatusCode::FAILURE);
345  }
346  }
347 
348  private:
349 
350  //
351  // Data members
352  //
353  mutable T* m_pObject = nullptr;
354 
355 };
356 
357 
365 protected:
366  GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
367  : GaudiHandleInfo(std::move(myComponentType),std::move(myParentName))
368  {}
369 public:
372 
375  bool setTypesAndNames( const std::vector< std::string >& myTypesAndNamesList );
376 
379  const std::vector< std::string > typesAndNames() const;
380 
382  const std::vector< std::string > types() const;
383 
385  const std::vector< std::string > names() const;
386 
389  const std::vector< std::string > getBaseInfos( std::string (GaudiHandleBase::*pMemFunc)() const ) const;
390 
394  std::string pythonPropertyClassName() const override;
395 
399  std::string pythonRepr() const override;
400 
404  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
405 
407  virtual void clear() = 0;
408 
410  virtual bool empty() const = 0;
411 
414  virtual ConstBaseHandleArray getBaseArray() const = 0;
415 
418  virtual BaseHandleArray getBaseArray() = 0;
419 
421  virtual bool retrieved() const = 0;
422 
423 };
424 
425 
427 template <class T>
429 public:
430  //
431  // public nested types
432  //
434  typedef typename HandleVector::value_type value_type;
435  typedef typename HandleVector::size_type size_type;
437  typedef typename HandleVector::const_reference const_reference;
438  typedef typename HandleVector::iterator iterator;
439  typedef typename HandleVector::const_iterator const_iterator;
440  typedef typename HandleVector::reverse_iterator reverse_iterator;
441  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
442 
443 protected:
444  //
445  // Constructors
446  //
451  GaudiHandleArray( const std::vector< std::string >& myTypesAndNamesList,
452  std::string myComponentType, std::string myParentName )
453  : GaudiHandleArrayBase(std::move(myComponentType),std::move(myParentName))
454  {
455  setTypesAndNames( myTypesAndNamesList );
456  }
457 
462  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
463  : GaudiHandleArrayBase(myComponentType,myParentName)
464  {}
465 
466 public:
467  virtual ~GaudiHandleArray() = default;
468 
470  GaudiHandleArray& operator=( const std::vector< std::string >& myTypesAndNamesList ) {
471  setTypesAndNames( myTypesAndNamesList );
472  return *this;
473  }
474 
477  iterator it = begin(), itEnd = end();
478  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
479  return baseArray;
480  }
481 
484  const_iterator it = begin(), itEnd = end();
485  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
486  return baseArray;
487  }
488 
489  //
490  // Simulate (part of) an std::vector
491  //
492  iterator begin() {
493  return m_handleArray.begin();
494  }
495 
496  iterator end() {
497  return m_handleArray.end();
498  }
499 
500  const_iterator begin() const {
501  return m_handleArray.begin();
502  }
503 
504  const_iterator end() const {
505  return m_handleArray.end();
506  }
507 
508  const_iterator rbegin() const {
509  return m_handleArray.rbegin();
510  }
511 
512  const_iterator rend() const {
513  return m_handleArray.rend();
514  }
515 
516  size_type size() const {
517  return m_handleArray.size();
518  }
519 
520  void clear() override {
521  m_handleArray.clear();
522  }
523 
524  bool empty() const override {
525  return m_handleArray.empty();
526  }
527 
528  T& operator[]( int index ) {
529  return m_handleArray[index];
530  }
531 
532  const T& operator[]( int index ) const {
533  return m_handleArray[index];
534  }
535 
537  T* operator[]( const std::string& name ) {
538  auto it = std::find_if(begin(),end(),[&](const_reference r) {
539  return r.name() == name;
540  } );
541  return it != end() ? &*it : nullptr;
542  }
543 
545  const T* operator[]( const std::string& name ) const {
546  auto it = std::find_if(begin(),end(),[&](const_reference r) {
547  return r.name() == name;
548  } );
549  return it != end() ? &*it : nullptr;
550  }
551 
554  using GaudiHandleArrayBase::push_back; // avoid compiler warning
555  virtual bool push_back( const T& myHandle ) {
556  m_handleArray.push_back( myHandle );
557  return true;
558  }
559 
562  {
564  for ( auto& i : *this )
565  {
566  // stop at first failure
567  if ( i.retrieve().isFailure() ) { sc = StatusCode::FAILURE; break; }
568  }
569  if ( sc ) { m_retrieved = true; }
570  return sc;
571  }
572 
576  for (auto& i : *this ) {
577  // continue trying to release other tools even if we fail...
578  if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
579  }
580  return sc;
581  }
582 
584  virtual bool retrieved() const override { return m_retrieved; }
585 
586 private:
587  //
588  // Private data members
589  //
590  HandleVector m_handleArray;
591  bool m_retrieved { false };
592 };
593 
594 // Easy printing out of Handles and HandleArrays
595 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
596 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
597 
598 #endif // ! GAUDIKERNEL_GAUDIHANDLE_H
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:217
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:437
HandleVector::value_type value_type
Definition: GaudiHandle.h:434
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:172
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:520
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:297
Define general base for Gaudi exception.
std::remove_const< CLASS >::type * nonConst(CLASS *p) const
Cast a pointer to a non const type.
Definition: GaudiHandle.h:321
HandleVector::iterator iterator
Definition: GaudiHandle.h:438
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:475
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:371
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:482
const_iterator rend() const
Definition: GaudiHandle.h:512
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:441
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:68
#define UNLIKELY(x)
Definition: Kernel.h:126
std::string getDefaultName()
Definition: GaudiHandle.h:301
STL namespace.
T & operator*()
Definition: GaudiHandle.h:274
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:177
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:50
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:335
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:341
std::string m_componentType
Definition: GaudiHandle.h:81
virtual bool retrieved() const override
has Array been retreived?
Definition: GaudiHandle.h:584
virtual std::string pythonPropertyClassName() const =0
The python class name for the property in the genconf-generated configurables.
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
Definition: GaudiHandle.h:451
HandleVector::size_type size_type
Definition: GaudiHandle.h:435
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:470
HandleVector::reference reference
Definition: GaudiHandle.h:436
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:555
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:73
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
void push_back(Container &c, const Value &v, std::true_type)
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:48
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
std::add_const< T >::type * operator->() const
Definition: GaudiHandle.h:290
T push_back(T...args)
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:329
const T & operator[](int index) const
Definition: GaudiHandle.h:532
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:40
HandleVector m_handleArray
Definition: GaudiHandle.h:590
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:561
std::vector< GaudiHandleBase * > BaseHandleArray
Definition: GaudiHandle.h:370
const_iterator begin() const
Definition: GaudiHandle.h:500
std::add_const< T >::type & operator*() const
Definition: GaudiHandle.h:284
iterator begin()
Definition: GaudiHandle.h:492
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
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:262
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:228
std::string m_typeAndName
Definition: GaudiHandle.h:159
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:50
T move(T...args)
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:524
T * operator->()
Definition: GaudiHandle.h:279
T find_if(T...args)
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:439
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:251
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:364
const_iterator rbegin() const
Definition: GaudiHandle.h:508
STL class.
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:26
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:45
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:270
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:440
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
virtual StatusCode release(T *comp) const
Release the component.
Definition: GaudiHandle.h:313
std::string type() const
The concrete component class name: the part before the &#39;/&#39;.
Definition: GaudiHandle.cpp:13
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
iterator end()
Definition: GaudiHandle.h:496
T & operator[](int index)
Definition: GaudiHandle.h:528
size_type size() const
Definition: GaudiHandle.h:516
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:545
const_iterator end() const
Definition: GaudiHandle.h:504
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:94
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:121
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:366
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:537
virtual std::string pythonRepr() const =0
Python representation of handle, i.e.
GaudiHandle(const GaudiHandle< NCT > &other, typename std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value >::type *=nullptr)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:187
std::enable_if< std::is_const< CT >::value &&!std::is_same< CT, NCT >::value, GaudiHandle & >::type operator=(const GaudiHandle< NCT > &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:206
#define GAUDI_API
Definition: Kernel.h:107
STL class.
const std::string & componentType() const
Definition: GaudiHandle.h:35
std::vector< T > HandleVector
Definition: GaudiHandle.h:433
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle (&#39;smart pointer&#39;) to a gaudi component.
Definition: GaudiHandle.h:110
std::string m_propertyName
Definition: GaudiHandle.h:82
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:196
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:132
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:428
std::string m_parentName
Definition: GaudiHandle.h:83
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:574
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:462