The Gaudi Framework  v30r3 (a5ef0a68)
GaudiHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_GAUDIHANDLE_H
2 #define GAUDIKERNEL_GAUDIHANDLE_H
3 
4 // Includes
7 #include "GaudiKernel/Property.h"
8 #include "GaudiKernel/System.h"
9 
10 #include <algorithm>
11 #include <iostream>
12 #include <stdexcept>
13 #include <string>
14 #include <type_traits>
15 #include <vector>
16 
17 namespace details
18 {
20  template <class T>
21  std::remove_const_t<T>* nonConst( T* p )
22  {
23  return const_cast<std::remove_const_t<T>*>( p );
24  }
25 }
26 
28 {
29 protected:
38  GaudiHandleInfo( std::string myComponentType, std::string myParentName )
39  : m_componentType( std::move( myComponentType ) ), m_parentName( std::move( myParentName ) )
40  {
41  }
42 
43 public:
45  virtual ~GaudiHandleInfo() = default;
46  //
47  // Public member functions
48  //
49  const std::string& componentType() const { return m_componentType; }
50 
52  const std::string& propertyName() const { return m_propertyName; }
53 
55  void setPropertyName( std::string propName ) { m_propertyName = std::move( propName ); }
56 
58  const std::string& parentName() const { return m_parentName; }
59 
63  virtual std::string pythonPropertyClassName() const = 0;
64 
69  virtual std::string pythonRepr() const = 0;
70 
71 protected:
73  void setComponentType( const std::string& componentType ) { m_componentType = componentType; }
74 
76  void setParentName( const std::string& parent ) { m_parentName = parent; }
77 
78 private:
79  //
80  // Data members
81  //
82  std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
83  std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
84  std::string m_parentName; // name of the parent having this handle as a member
85 };
86 
95 {
96  //
97  // Ctors etc
98  //
99 protected:
111  GaudiHandleBase( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
112  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) )
113  {
114  setTypeAndName( std::move( myTypeAndName ) );
115  }
116 
117 public:
118  //
119  // Public member functions
120  //
122  std::string typeAndName() const { return m_typeAndName; }
123 
125  std::string type() const;
126 
128  std::string name() const;
129 
131  bool empty() const { return m_typeAndName.empty(); }
132 
134  void setTypeAndName( std::string myTypeAndName );
135 
137  void setName( const std::string& myName );
138 
142  std::string pythonPropertyClassName() const override;
143 
145  std::string messageName() const;
146 
150  std::string pythonRepr() const override;
151 
153 
154 private:
155  //
156  // Data member
157  //
158  std::string m_typeAndName; // the full type and name: "type/name"
159 };
160 
169 template <class T>
171 {
172  //
173  // Constructors etc.
174  //
175 protected:
176  GaudiHandle( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
177  : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) )
178  {
179  }
180 
181 public:
186  : GaudiHandleBase( other )
187  {
188  m_pObject = other.get();
189  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
190  }
191 
193  GaudiHandle( const GaudiHandle& other ) : GaudiHandleBase( other )
194  {
195  m_pObject = other.m_pObject;
196  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
197  }
198 
202  operator=( const GaudiHandle<NCT>& other )
203  {
205  // release any current tool
206  release().ignore();
207  m_pObject = other.get();
208  // update ref-counting
209  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
210  return *this;
211  }
212 
215  {
217  // release any current tool
218  release().ignore();
219  m_pObject = other.m_pObject;
220  // update ref-counting
221  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
222  return *this;
223  }
224 
227  { // not really const, because it updates m_pObject
229  if ( m_pObject && release().isFailure() ) {
230  sc = StatusCode::FAILURE;
231  }
232  if ( sc && retrieve( m_pObject ).isFailure() ) {
233  m_pObject = nullptr;
234  sc = StatusCode::FAILURE;
235  }
236  return sc;
237  }
238 
241  { // not really const, because it updates m_pObject
243  if ( m_pObject ) {
244  sc = release( m_pObject );
245  m_pObject = nullptr;
246  }
247  return sc;
248  }
249 
251  bool isValid() const
252  { // not really const, because it may update m_pObject
253  return m_pObject || retrieve().isSuccess();
254  }
255 
258  operator bool() const
259  { // not really const, because it may update m_pObject
260  return isValid();
261  }
262 
264  T* get() { return m_pObject; }
265 
267  typename std::add_const<T>::type* get() const { return m_pObject; }
268 
270  bool isSet() const { return get(); }
271 
273  {
274  assertObject();
275  return *m_pObject;
276  }
277 
279  {
280  assertObject();
281  return m_pObject;
282  }
283 
285  {
286  // not really const, because it may update m_pObject
287  assertObject();
288  return *m_pObject;
289  }
290 
292  {
293  // not really const, because it may update m_pObject
294  assertObject();
295  return m_pObject;
296  }
297 
299  std::string getDefaultType() { return System::typeinfoName( typeid( T ) ); }
300 
302  {
303  const auto defName = GaudiHandleBase::type();
304  return ( defName.empty() ? getDefaultType() : defName );
305  }
306 
307 protected:
309  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
310 
313  virtual StatusCode release( T* comp ) const
314  { // not really const, because it updates m_pObject
315  // const cast to support T being a const type
316  details::nonConst( comp )->release();
317  return StatusCode::SUCCESS;
318  }
319 
320 private:
323  {
324  const std::string& myType = getDefaultType();
325  GaudiHandleBase::setTypeAndName( myType + '/' + myType );
326  }
327 
329  void setDefaultType() { GaudiHandleBase::setTypeAndName( getDefaultType() ); }
330 
333  void assertObject() const
334  { // not really const, because it may update m_pObject
335  if ( UNLIKELY( !isValid() ) ) {
336  throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
337  componentType() + " retrieve", StatusCode::FAILURE );
338  }
339  }
340 
341 private:
342  //
343  // Data members
344  //
345  mutable T* m_pObject = nullptr;
346 };
347 
355 {
356 protected:
357  GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
358  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) )
359  {
360  }
361 
362 public:
366 
369  bool setTypesAndNames( const std::vector<std::string>& myTypesAndNamesList );
370 
373  const std::vector<std::string> typesAndNames() const;
374 
376  const std::vector<std::string> types() const;
377 
379  const std::vector<std::string> names() const;
380 
383  const std::vector<std::string> getBaseInfos( std::string ( GaudiHandleBase::*pMemFunc )() const ) const;
384 
388  std::string pythonPropertyClassName() const override;
389 
393  std::string pythonRepr() const override;
394 
398  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
399 
401  virtual void clear() = 0;
402 
404  virtual bool empty() const = 0;
405 
408  virtual ConstBaseHandleArray getBaseArray() const = 0;
409 
412  virtual BaseHandleArray getBaseArray() = 0;
413 
415  virtual bool retrieved() const = 0;
416 };
417 
419 template <class T>
421 {
422 public:
423  //
424  // public nested types
425  //
427  typedef typename HandleVector::value_type value_type;
428  typedef typename HandleVector::size_type size_type;
429  typedef typename HandleVector::reference reference;
430  typedef typename HandleVector::const_reference const_reference;
431  typedef typename HandleVector::iterator iterator;
432  typedef typename HandleVector::const_iterator const_iterator;
433  typedef typename HandleVector::reverse_iterator reverse_iterator;
434  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
435 
436 protected:
437  //
438  // Constructors
439  //
444  GaudiHandleArray( const std::vector<std::string>& myTypesAndNamesList, std::string myComponentType,
445  std::string myParentName )
446  : GaudiHandleArrayBase( std::move( myComponentType ), std::move( myParentName ) )
447  {
448  setTypesAndNames( myTypesAndNamesList );
449  }
450 
455  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
456  : GaudiHandleArrayBase( myComponentType, myParentName )
457  {
458  }
459 
460 public:
461  virtual ~GaudiHandleArray() = default;
462 
464  GaudiHandleArray& operator=( const std::vector<std::string>& myTypesAndNamesList )
465  {
466  setTypesAndNames( myTypesAndNamesList );
467  return *this;
468  }
469 
471  {
473  iterator it = begin(), itEnd = end();
474  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
475  return baseArray;
476  }
477 
479  {
481  const_iterator it = begin(), itEnd = end();
482  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
483  return baseArray;
484  }
485 
486  //
487  // Simulate (part of) an std::vector
488  //
489  iterator begin() { return m_handleArray.begin(); }
490 
491  iterator end() { return m_handleArray.end(); }
492 
493  const_iterator begin() const { return m_handleArray.begin(); }
494 
495  const_iterator end() const { return m_handleArray.end(); }
496 
497  const_iterator rbegin() const { return m_handleArray.rbegin(); }
498 
499  const_iterator rend() const { return m_handleArray.rend(); }
500 
501  size_type size() const { return m_handleArray.size(); }
502 
503  void clear() override { m_handleArray.clear(); }
504 
505  bool empty() const override { return m_handleArray.empty(); }
506 
507  T& operator[]( int index ) { return m_handleArray[index]; }
508 
509  const T& operator[]( int index ) const { return m_handleArray[index]; }
510 
513  {
514  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
515  return it != end() ? &*it : nullptr;
516  }
517 
519  const T* operator[]( const std::string& name ) const
520  {
521  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
522  return it != end() ? &*it : nullptr;
523  }
524 
527  using GaudiHandleArrayBase::push_back; // avoid compiler warning
528  virtual bool push_back( const T& myHandle )
529  {
530  m_handleArray.push_back( myHandle );
531  return true;
532  }
533 
536  {
538  for ( auto& i : *this ) {
539  // stop at first failure
540  if ( i.retrieve().isFailure() ) {
541  sc = StatusCode::FAILURE;
542  break;
543  }
544  }
545  if ( sc ) {
546  m_retrieved = true;
547  }
548  return sc;
549  }
550 
553  {
555  for ( auto& i : *this ) {
556  // continue trying to release other tools even if we fail...
557  if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
558  }
559  return sc;
560  }
561 
563  virtual bool retrieved() const override { return m_retrieved; }
564 
565 private:
566  //
567  // Private data members
568  //
569  HandleVector m_handleArray;
570  bool m_retrieved{false};
571 };
572 
573 // Easy printing out of Handles and HandleArrays
574 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
575 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
576 
577 #endif // ! GAUDIKERNEL_GAUDIHANDLE_H
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:214
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:430
#define UNLIKELY(x)
Definition: Kernel.h:122
constexpr static const auto FAILURE
Definition: StatusCode.h:88
HandleVector::value_type value_type
Definition: GaudiHandle.h:427
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:170
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:503
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:299
Define general base for Gaudi exception.
HandleVector::iterator iterator
Definition: GaudiHandle.h:431
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:470
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:478
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:44
std::vector< T > HandleVector
Definition: GaudiHandle.h:426
const_iterator rend() const
Definition: GaudiHandle.h:499
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:434
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:73
std::string getDefaultName()
Definition: GaudiHandle.h:301
STL namespace.
T & operator*()
Definition: GaudiHandle.h:272
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:176
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:58
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:329
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:333
std::string m_componentType
Definition: GaudiHandle.h:82
virtual bool retrieved() const override
has Array been retreived?
Definition: GaudiHandle.h:563
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
Definition: GaudiHandle.h:444
HandleVector::size_type size_type
Definition: GaudiHandle.h:428
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:464
HandleVector::reference reference
Definition: GaudiHandle.h:429
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:528
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:76
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:240
void push_back(Container &c, const Value &v, std::true_type)
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:202
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
std::add_const< T >::type * operator->() const
Definition: GaudiHandle.h:291
T push_back(T...args)
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:322
const T & operator[](int index) const
Definition: GaudiHandle.h:509
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:52
HandleVector m_handleArray
Definition: GaudiHandle.h:569
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:535
const_iterator begin() const
Definition: GaudiHandle.h:493
std::add_const< T >::type & operator*() const
Definition: GaudiHandle.h:284
iterator begin()
Definition: GaudiHandle.h:489
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:21
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:264
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:226
std::string m_typeAndName
Definition: GaudiHandle.h:158
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:365
T move(T...args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:505
T * operator->()
Definition: GaudiHandle.h:278
T find_if(T...args)
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:432
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:354
const_iterator rbegin() const
Definition: GaudiHandle.h:497
STL class.
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:38
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:55
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:270
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:433
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:364
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:38
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:11
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
iterator end()
Definition: GaudiHandle.h:491
T & operator[](int index)
Definition: GaudiHandle.h:507
size_type size() const
Definition: GaudiHandle.h:501
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:519
const_iterator end() const
Definition: GaudiHandle.h:495
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:122
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:357
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:512
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:184
#define GAUDI_API
Definition: Kernel.h:104
STL class.
const std::string & componentType() const
Definition: GaudiHandle.h:49
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle (&#39;smart pointer&#39;) to a gaudi component.
Definition: GaudiHandle.h:111
std::string m_propertyName
Definition: GaudiHandle.h:83
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:193
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:131
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:420
std::string m_parentName
Definition: GaudiHandle.h:84
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:552
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:455