The Gaudi Framework  v30r0 (c919700c)
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 
16 namespace details
17 {
19  template <class T>
20  std::remove_const_t<T>* nonConst( T* p )
21  {
22  return const_cast<std::remove_const_t<T>*>( p );
23  }
24 }
25 
27 {
28 protected:
37  GaudiHandleInfo( std::string myComponentType, std::string myParentName )
38  : m_componentType( std::move( myComponentType ) ), m_parentName( std::move( myParentName ) )
39  {
40  }
41 
42 public:
44  virtual ~GaudiHandleInfo() = default;
45  //
46  // Public member functions
47  //
48  const std::string& componentType() const { return m_componentType; }
49 
51  const std::string& propertyName() const { return m_propertyName; }
52 
54  void setPropertyName( std::string propName ) { m_propertyName = std::move( propName ); }
55 
57  const std::string& parentName() const { return m_parentName; }
58 
62  virtual std::string pythonPropertyClassName() const = 0;
63 
68  virtual std::string pythonRepr() const = 0;
69 
70 protected:
72  void setComponentType( const std::string& componentType ) { m_componentType = componentType; }
73 
75  void setParentName( const std::string& parent ) { m_parentName = parent; }
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 
94 {
95  //
96  // Ctors etc
97  //
98 protected:
110  GaudiHandleBase( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
111  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) )
112  {
113  setTypeAndName( std::move( myTypeAndName ) );
114  }
115 
116 public:
117  //
118  // Public member functions
119  //
121  std::string typeAndName() const { return m_typeAndName; }
122 
124  std::string type() const;
125 
127  std::string name() const;
128 
130  bool empty() const { return m_typeAndName.empty(); }
131 
133  void setTypeAndName( std::string myTypeAndName );
134 
136  void setName( const std::string& myName );
137 
141  std::string pythonPropertyClassName() const override;
142 
144  std::string messageName() const;
145 
149  std::string pythonRepr() const override;
150 
151 private:
152  //
153  // Data member
154  //
155  std::string m_typeAndName; // the full type and name: "type/name"
156 };
157 
166 template <class T>
168 {
169  //
170  // Constructors etc.
171  //
172 protected:
173  GaudiHandle( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
174  : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) )
175  {
176  }
177 
178 public:
183  : GaudiHandleBase( other )
184  {
185  m_pObject = other.get();
186  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
187  }
188 
190  GaudiHandle( const GaudiHandle& other ) : GaudiHandleBase( other )
191  {
192  m_pObject = other.m_pObject;
193  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
194  }
195 
199  operator=( const GaudiHandle<NCT>& other )
200  {
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 
212  {
214  // release any current tool
215  release().ignore();
216  m_pObject = other.m_pObject;
217  // update ref-counting
218  if ( m_pObject ) details::nonConst( m_pObject )->addRef();
219  return *this;
220  }
221 
224  { // not really const, because it updates m_pObject
226  if ( m_pObject && release().isFailure() ) {
227  sc = StatusCode::FAILURE;
228  }
229  if ( sc && retrieve( m_pObject ).isFailure() ) {
230  m_pObject = nullptr;
231  sc = StatusCode::FAILURE;
232  }
233  return sc;
234  }
235 
238  { // not really const, because it updates m_pObject
240  if ( m_pObject ) {
241  sc = release( m_pObject );
242  m_pObject = nullptr;
243  }
244  return sc;
245  }
246 
248  bool isValid() const
249  { // not really const, because it may update m_pObject
250  return m_pObject || retrieve().isSuccess();
251  }
252 
255  operator bool() const
256  { // not really const, because it may update m_pObject
257  return isValid();
258  }
259 
261  T* get() { return m_pObject; }
262 
264  typename std::add_const<T>::type* get() const { return m_pObject; }
265 
267  bool isSet() const { return get(); }
268 
270  {
271  assertObject();
272  return *m_pObject;
273  }
274 
276  {
277  assertObject();
278  return m_pObject;
279  }
280 
282  {
283  // not really const, because it may update m_pObject
284  assertObject();
285  return *m_pObject;
286  }
287 
289  {
290  // not really const, because it may update m_pObject
291  assertObject();
292  return m_pObject;
293  }
294 
296  std::string getDefaultType() { return System::typeinfoName( typeid( T ) ); }
297 
299  {
300  const auto defName = GaudiHandleBase::type();
301  return ( defName.empty() ? getDefaultType() : defName );
302  }
303 
304 protected:
306  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
307 
310  virtual StatusCode release( T* comp ) const
311  { // not really const, because it updates m_pObject
312  // const cast to support T being a const type
313  details::nonConst( comp )->release();
314  return StatusCode::SUCCESS;
315  }
316 
317 private:
320  {
321  const std::string& myType = getDefaultType();
322  GaudiHandleBase::setTypeAndName( myType + '/' + myType );
323  }
324 
326  void setDefaultType() { GaudiHandleBase::setTypeAndName( getDefaultType() ); }
327 
330  void assertObject() const
331  { // not really const, because it may update m_pObject
332  if ( UNLIKELY( !isValid() ) ) {
333  throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
334  componentType() + " retrieve", StatusCode::FAILURE );
335  }
336  }
337 
338 private:
339  //
340  // Data members
341  //
342  mutable T* m_pObject = nullptr;
343 };
344 
352 {
353 protected:
354  GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
355  : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) )
356  {
357  }
358 
359 public:
362 
365  bool setTypesAndNames( const std::vector<std::string>& myTypesAndNamesList );
366 
369  const std::vector<std::string> typesAndNames() const;
370 
372  const std::vector<std::string> types() const;
373 
375  const std::vector<std::string> names() const;
376 
379  const std::vector<std::string> getBaseInfos( std::string ( GaudiHandleBase::*pMemFunc )() const ) const;
380 
384  std::string pythonPropertyClassName() const override;
385 
389  std::string pythonRepr() const override;
390 
394  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
395 
397  virtual void clear() = 0;
398 
400  virtual bool empty() const = 0;
401 
404  virtual ConstBaseHandleArray getBaseArray() const = 0;
405 
408  virtual BaseHandleArray getBaseArray() = 0;
409 
411  virtual bool retrieved() const = 0;
412 };
413 
415 template <class T>
417 {
418 public:
419  //
420  // public nested types
421  //
423  typedef typename HandleVector::value_type value_type;
424  typedef typename HandleVector::size_type size_type;
426  typedef typename HandleVector::const_reference const_reference;
427  typedef typename HandleVector::iterator iterator;
428  typedef typename HandleVector::const_iterator const_iterator;
429  typedef typename HandleVector::reverse_iterator reverse_iterator;
430  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
431 
432 protected:
433  //
434  // Constructors
435  //
440  GaudiHandleArray( const std::vector<std::string>& myTypesAndNamesList, std::string myComponentType,
441  std::string myParentName )
442  : GaudiHandleArrayBase( std::move( myComponentType ), std::move( myParentName ) )
443  {
444  setTypesAndNames( myTypesAndNamesList );
445  }
446 
451  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
452  : GaudiHandleArrayBase( myComponentType, myParentName )
453  {
454  }
455 
456 public:
457  virtual ~GaudiHandleArray() = default;
458 
460  GaudiHandleArray& operator=( const std::vector<std::string>& myTypesAndNamesList )
461  {
462  setTypesAndNames( myTypesAndNamesList );
463  return *this;
464  }
465 
467  {
469  iterator it = begin(), itEnd = end();
470  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
471  return baseArray;
472  }
473 
475  {
477  const_iterator it = begin(), itEnd = end();
478  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
479  return baseArray;
480  }
481 
482  //
483  // Simulate (part of) an std::vector
484  //
485  iterator begin() { return m_handleArray.begin(); }
486 
487  iterator end() { return m_handleArray.end(); }
488 
489  const_iterator begin() const { return m_handleArray.begin(); }
490 
491  const_iterator end() const { return m_handleArray.end(); }
492 
493  const_iterator rbegin() const { return m_handleArray.rbegin(); }
494 
495  const_iterator rend() const { return m_handleArray.rend(); }
496 
497  size_type size() const { return m_handleArray.size(); }
498 
499  void clear() override { m_handleArray.clear(); }
500 
501  bool empty() const override { return m_handleArray.empty(); }
502 
503  T& operator[]( int index ) { return m_handleArray[index]; }
504 
505  const T& operator[]( int index ) const { return m_handleArray[index]; }
506 
509  {
510  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
511  return it != end() ? &*it : nullptr;
512  }
513 
515  const T* operator[]( const std::string& name ) const
516  {
517  auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
518  return it != end() ? &*it : nullptr;
519  }
520 
523  using GaudiHandleArrayBase::push_back; // avoid compiler warning
524  virtual bool push_back( const T& myHandle )
525  {
526  m_handleArray.push_back( myHandle );
527  return true;
528  }
529 
532  {
534  for ( auto& i : *this ) {
535  // stop at first failure
536  if ( i.retrieve().isFailure() ) {
537  sc = StatusCode::FAILURE;
538  break;
539  }
540  }
541  if ( sc ) {
542  m_retrieved = true;
543  }
544  return sc;
545  }
546 
549  {
551  for ( auto& i : *this ) {
552  // continue trying to release other tools even if we fail...
553  if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
554  }
555  return sc;
556  }
557 
559  virtual bool retrieved() const override { return m_retrieved; }
560 
561 private:
562  //
563  // Private data members
564  //
565  HandleVector m_handleArray;
566  bool m_retrieved{false};
567 };
568 
569 // Easy printing out of Handles and HandleArrays
570 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
571 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
572 
573 #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:426
#define UNLIKELY(x)
Definition: Kernel.h:128
HandleVector::value_type value_type
Definition: GaudiHandle.h:423
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:167
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:499
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:296
Define general base for Gaudi exception.
HandleVector::iterator iterator
Definition: GaudiHandle.h:427
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:466
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:336
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:474
std::vector< T > HandleVector
Definition: GaudiHandle.h:422
const_iterator rend() const
Definition: GaudiHandle.h:495
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:430
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:72
std::string getDefaultName()
Definition: GaudiHandle.h:298
STL namespace.
T & operator*()
Definition: GaudiHandle.h:269
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:173
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:57
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:326
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:330
std::string m_componentType
Definition: GaudiHandle.h:81
virtual bool retrieved() const override
has Array been retreived?
Definition: GaudiHandle.h:559
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
Definition: GaudiHandle.h:440
HandleVector::size_type size_type
Definition: GaudiHandle.h:424
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:460
HandleVector::reference reference
Definition: GaudiHandle.h:425
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:524
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:75
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:237
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:199
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:288
T push_back(T...args)
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:319
const T & operator[](int index) const
Definition: GaudiHandle.h:505
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:51
HandleVector m_handleArray
Definition: GaudiHandle.h:565
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:531
const_iterator begin() const
Definition: GaudiHandle.h:489
std::add_const< T >::type & operator*() const
Definition: GaudiHandle.h:281
iterator begin()
Definition: GaudiHandle.h:485
Definition: compose.h:3
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition: GaudiHandle.h:20
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:261
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:223
std::string m_typeAndName
Definition: GaudiHandle.h:155
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:361
T move(T...args)
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:501
T * operator->()
Definition: GaudiHandle.h:275
T find_if(T...args)
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:428
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:248
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:351
const_iterator rbegin() const
Definition: GaudiHandle.h:493
STL class.
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:37
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:54
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:267
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:429
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:360
virtual StatusCode release(T *comp) const
Release the component.
Definition: GaudiHandle.h:310
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:487
T & operator[](int index)
Definition: GaudiHandle.h:503
size_type size() const
Definition: GaudiHandle.h:497
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:515
const_iterator end() const
Definition: GaudiHandle.h:491
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:93
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:354
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:508
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:181
#define GAUDI_API
Definition: Kernel.h:110
STL class.
const std::string & componentType() const
Definition: GaudiHandle.h:48
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:110
std::string m_propertyName
Definition: GaudiHandle.h:82
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:190
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:130
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:416
std::string m_parentName
Definition: GaudiHandle.h:83
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:548
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:451