The Gaudi Framework  v29r0 (ff2e7097)
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/System.h"
8 
9 #include <algorithm>
10 #include <iostream>
11 #include <stdexcept>
12 #include <string>
13 #include <type_traits>
14 #include <vector>
15 
17 {
18 protected:
27  GaudiHandleInfo( std::string myComponentType, std::string myParentName )
28  : m_componentType( std::move( myComponentType ) ), m_parentName( std::move( myParentName ) )
29  {
30  }
31 
32 public:
34  virtual ~GaudiHandleInfo() = default;
35  //
36  // Public member functions
37  //
38  const std::string& componentType() const { return m_componentType; }
39 
41  const std::string& propertyName() const { return m_propertyName; }
42 
44  void setPropertyName( std::string propName ) { m_propertyName = std::move( propName ); }
45 
47  const std::string& parentName() const { return m_parentName; }
48 
52  virtual std::string pythonPropertyClassName() const = 0;
53 
58  virtual std::string pythonRepr() const = 0;
59 
60 protected:
62  void setComponentType( const std::string& componentType ) { m_componentType = componentType; }
63 
65  void setParentName( const std::string& parent ) { m_parentName = parent; }
66 
67 private:
68  //
69  // Data members
70  //
71  std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
72  std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
73  std::string m_parentName; // name of the parent having this handle as a member
74 };
75 
84 {
85  //
86  // Ctors etc
87  //
88 protected:
100  GaudiHandleBase( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
101  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) )
102  {
103  setTypeAndName( std::move( myTypeAndName ) );
104  }
105 
106 public:
107  //
108  // Public member functions
109  //
111  std::string typeAndName() const { return m_typeAndName; }
112 
114  std::string type() const;
115 
117  std::string name() const;
118 
120  bool empty() const { return m_typeAndName.empty(); }
121 
123  void setTypeAndName( std::string myTypeAndName );
124 
126  void setName( const std::string& myName );
127 
131  std::string pythonPropertyClassName() const override;
132 
134  std::string messageName() const;
135 
139  std::string pythonRepr() const override;
140 
141 private:
142  //
143  // Data member
144  //
145  std::string m_typeAndName; // the full type and name: "type/name"
146 };
147 
156 template <class T>
158 {
159  //
160  // Constructors etc.
161  //
162 protected:
163  GaudiHandle( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
164  : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) )
165  {
166  }
167 
168 public:
173  : GaudiHandleBase( other )
174  {
175  m_pObject = other.get();
176  if ( m_pObject ) nonConst( m_pObject )->addRef();
177  }
178 
180  GaudiHandle( const GaudiHandle& other ) : GaudiHandleBase( other )
181  {
182  m_pObject = other.m_pObject;
183  if ( m_pObject ) nonConst( m_pObject )->addRef();
184  }
185 
189  operator=( const GaudiHandle<NCT>& other )
190  {
192  // release any current tool
193  release().ignore();
194  m_pObject = other.get();
195  // update ref-counting
196  if ( m_pObject ) nonConst( m_pObject )->addRef();
197  return *this;
198  }
199 
202  {
204  // release any current tool
205  release().ignore();
206  m_pObject = other.m_pObject;
207  // update ref-counting
208  if ( m_pObject ) nonConst( m_pObject )->addRef();
209  return *this;
210  }
211 
214  { // not really const, because it updates m_pObject
216  if ( m_pObject && release().isFailure() ) {
217  sc = StatusCode::FAILURE;
218  }
219  if ( sc && retrieve( m_pObject ).isFailure() ) {
220  m_pObject = nullptr;
221  sc = StatusCode::FAILURE;
222  }
223  return sc;
224  }
225 
228  { // not really const, because it updates m_pObject
230  if ( m_pObject ) {
231  sc = release( m_pObject );
232  m_pObject = nullptr;
233  }
234  return sc;
235  }
236 
238  bool isValid() const
239  { // not really const, because it may update m_pObject
240  return m_pObject || retrieve().isSuccess();
241  }
242 
245  operator bool() const
246  { // not really const, because it may update m_pObject
247  return isValid();
248  }
249 
251  T* get() { return m_pObject; }
252 
254  typename std::add_const<T>::type* get() const { return m_pObject; }
255 
257  bool isSet() const { return get(); }
258 
260  {
261  assertObject();
262  return *m_pObject;
263  }
264 
266  {
267  assertObject();
268  return m_pObject;
269  }
270 
272  {
273  // not really const, because it may update m_pObject
274  assertObject();
275  return *m_pObject;
276  }
277 
279  {
280  // not really const, because it may update m_pObject
281  assertObject();
282  return m_pObject;
283  }
284 
286  std::string getDefaultType() { return System::typeinfoName( typeid( T ) ); }
287 
289  {
290  const auto defName = GaudiHandleBase::type();
291  return ( defName.empty() ? getDefaultType() : defName );
292  }
293 
294 protected:
296  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
297 
300  virtual StatusCode release( T* comp ) const
301  { // not really const, because it updates m_pObject
302  // const cast to support T being a const type
303  nonConst( comp )->release();
304  return StatusCode::SUCCESS;
305  }
306 
308  template <class CLASS>
309  typename std::remove_const<CLASS>::type* nonConst( CLASS* p ) const
310  {
311  return const_cast<typename std::remove_const<CLASS>::type*>( p );
312  }
313 
314 private:
317  {
318  const std::string& myType = getDefaultType();
319  GaudiHandleBase::setTypeAndName( myType + '/' + myType );
320  }
321 
323  void setDefaultType() { GaudiHandleBase::setTypeAndName( getDefaultType() ); }
324 
327  void assertObject() const
328  { // not really const, because it may update m_pObject
329  if ( UNLIKELY( !isValid() ) ) {
330  throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
331  componentType() + " retrieve", StatusCode::FAILURE );
332  }
333  }
334 
335 private:
336  //
337  // Data members
338  //
339  mutable T* m_pObject = nullptr;
340 };
341 
349 {
350 protected:
351  GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
352  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) )
353  {
354  }
355 
356 public:
359 
362  bool setTypesAndNames( const std::vector<std::string>& myTypesAndNamesList );
363 
366  const std::vector<std::string> typesAndNames() const;
367 
369  const std::vector<std::string> types() const;
370 
372  const std::vector<std::string> names() const;
373 
376  const std::vector<std::string> getBaseInfos( std::string ( GaudiHandleBase::*pMemFunc )() const ) const;
377 
381  std::string pythonPropertyClassName() const override;
382 
386  std::string pythonRepr() const override;
387 
391  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
392 
394  virtual void clear() = 0;
395 
397  virtual bool empty() const = 0;
398 
401  virtual ConstBaseHandleArray getBaseArray() const = 0;
402 
405  virtual BaseHandleArray getBaseArray() = 0;
406 
408  virtual bool retrieved() const = 0;
409 };
410 
412 template <class T>
414 {
415 public:
416  //
417  // public nested types
418  //
420  typedef typename HandleVector::value_type value_type;
421  typedef typename HandleVector::size_type size_type;
423  typedef typename HandleVector::const_reference const_reference;
424  typedef typename HandleVector::iterator iterator;
425  typedef typename HandleVector::const_iterator const_iterator;
426  typedef typename HandleVector::reverse_iterator reverse_iterator;
427  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
428 
429 protected:
430  //
431  // Constructors
432  //
437  GaudiHandleArray( const std::vector<std::string>& myTypesAndNamesList, std::string myComponentType,
438  std::string myParentName )
439  : GaudiHandleArrayBase( std::move( myComponentType ), std::move( myParentName ) )
440  {
441  setTypesAndNames( myTypesAndNamesList );
442  }
443 
448  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
449  : GaudiHandleArrayBase( myComponentType, myParentName )
450  {
451  }
452 
453 public:
454  virtual ~GaudiHandleArray() = default;
455 
457  GaudiHandleArray& operator=( const std::vector<std::string>& myTypesAndNamesList )
458  {
459  setTypesAndNames( myTypesAndNamesList );
460  return *this;
461  }
462 
464  {
466  iterator it = begin(), itEnd = end();
467  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
468  return baseArray;
469  }
470 
472  {
474  const_iterator it = begin(), itEnd = end();
475  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
476  return baseArray;
477  }
478 
479  //
480  // Simulate (part of) an std::vector
481  //
482  iterator begin() { return m_handleArray.begin(); }
483 
484  iterator end() { return m_handleArray.end(); }
485 
486  const_iterator begin() const { return m_handleArray.begin(); }
487 
488  const_iterator end() const { return m_handleArray.end(); }
489 
490  const_iterator rbegin() const { return m_handleArray.rbegin(); }
491 
492  const_iterator rend() const { return m_handleArray.rend(); }
493 
494  size_type size() const { return m_handleArray.size(); }
495 
496  void clear() override { m_handleArray.clear(); }
497 
498  bool empty() const override { return m_handleArray.empty(); }
499 
500  T& operator[]( int index ) { return m_handleArray[index]; }
501 
502  const T& operator[]( int index ) const { return m_handleArray[index]; }
503 
506  {
507  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
508  return it != end() ? &*it : nullptr;
509  }
510 
512  const T* operator[]( const std::string& name ) const
513  {
514  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
515  return it != end() ? &*it : nullptr;
516  }
517 
520  using GaudiHandleArrayBase::push_back; // avoid compiler warning
521  virtual bool push_back( const T& myHandle )
522  {
523  m_handleArray.push_back( myHandle );
524  return true;
525  }
526 
529  {
531  for ( auto& i : *this ) {
532  // stop at first failure
533  if ( i.retrieve().isFailure() ) {
534  sc = StatusCode::FAILURE;
535  break;
536  }
537  }
538  if ( sc ) {
539  m_retrieved = true;
540  }
541  return sc;
542  }
543 
546  {
548  for ( auto& i : *this ) {
549  // continue trying to release other tools even if we fail...
550  if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
551  }
552  return sc;
553  }
554 
556  virtual bool retrieved() const override { return m_retrieved; }
557 
558 private:
559  //
560  // Private data members
561  //
562  HandleVector m_handleArray;
563  bool m_retrieved{false};
564 };
565 
566 // Easy printing out of Handles and HandleArrays
567 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
568 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
569 
570 #endif // ! GAUDIKERNEL_GAUDIHANDLE_H
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:201
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:423
#define UNLIKELY(x)
Definition: Kernel.h:128
HandleVector::value_type value_type
Definition: GaudiHandle.h:420
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:157
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:496
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:286
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:309
HandleVector::iterator iterator
Definition: GaudiHandle.h:424
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:463
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:329
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:471
std::vector< T > HandleVector
Definition: GaudiHandle.h:419
const_iterator rend() const
Definition: GaudiHandle.h:492
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:427
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:62
std::string getDefaultName()
Definition: GaudiHandle.h:288
STL namespace.
T & operator*()
Definition: GaudiHandle.h:259
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:163
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:47
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:323
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:327
std::string m_componentType
Definition: GaudiHandle.h:71
virtual bool retrieved() const override
has Array been retreived?
Definition: GaudiHandle.h:556
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:437
HandleVector::size_type size_type
Definition: GaudiHandle.h:421
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:457
HandleVector::reference reference
Definition: GaudiHandle.h:422
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:521
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:65
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:227
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:189
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
std::add_const< T >::type * operator->() const
Definition: GaudiHandle.h:278
T push_back(T...args)
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:316
const T & operator[](int index) const
Definition: GaudiHandle.h:502
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:41
HandleVector m_handleArray
Definition: GaudiHandle.h:562
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:528
const_iterator begin() const
Definition: GaudiHandle.h:486
std::add_const< T >::type & operator*() const
Definition: GaudiHandle.h:271
iterator begin()
Definition: GaudiHandle.h:482
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in <myTypesAndNamesList>.
Definition: GaudiHandle.cpp:58
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
T * get()
Return the wrapped pointer, not calling retrieve() if null.
Definition: GaudiHandle.h:251
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:213
std::string m_typeAndName
Definition: GaudiHandle.h:145
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:358
T move(T...args)
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:498
T * operator->()
Definition: GaudiHandle.h:265
T find_if(T...args)
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:425
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:238
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:348
const_iterator rbegin() const
Definition: GaudiHandle.h:490
STL class.
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:27
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:44
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:257
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:426
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:357
virtual StatusCode release(T *comp) const
Release the component.
Definition: GaudiHandle.h:300
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:484
T & operator[](int index)
Definition: GaudiHandle.h:500
size_type size() const
Definition: GaudiHandle.h:494
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:512
const_iterator end() const
Definition: GaudiHandle.h:488
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:83
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:111
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:351
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:505
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:171
#define GAUDI_API
Definition: Kernel.h:110
STL class.
const std::string & componentType() const
Definition: GaudiHandle.h:38
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle (&#39;smart pointer&#39;) to a gaudi component.
Definition: GaudiHandle.h:100
std::string m_propertyName
Definition: GaudiHandle.h:72
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:180
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:120
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:413
std::string m_parentName
Definition: GaudiHandle.h:73
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:545
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:448