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 
16 protected:
25  GaudiHandleInfo( std::string myComponentType, std::string myParentName )
26  : m_componentType(std::move(myComponentType)), m_parentName(std::move(myParentName))
27  {}
28 public:
30  virtual ~GaudiHandleInfo() = default;
31  //
32  // Public member functions
33  //
34  const std::string& componentType() const {
35  return m_componentType;
36  }
37 
39  const std::string& propertyName() const {
40  return m_propertyName;
41  }
42 
44  void setPropertyName( std::string propName ) {
45  m_propertyName = std::move(propName);
46  }
47 
49  const std::string& parentName() const {
50  return m_parentName;
51  }
52 
56  virtual std::string pythonPropertyClassName() const = 0;
57 
62  virtual std::string pythonRepr() const = 0;
63 
64 protected:
65 
67  void setComponentType(const std::string& componentType) {
68  m_componentType = componentType;
69  }
70 
72  void setParentName(const std::string& parent) {
73  m_parentName = parent;
74  }
75 
76 private:
77  //
78  // Data members
79  //
80  std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
81  std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
82  std::string m_parentName; // name of the parent having this handle as a member
83 };
84 
85 
94  //
95  // Ctors etc
96  //
97 protected:
109  GaudiHandleBase( std::string myTypeAndName, std::string myComponentType,
110  std::string myParentName )
111  : GaudiHandleInfo(std::move(myComponentType),std::move(myParentName))
112  {
113  setTypeAndName(std::move(myTypeAndName));
114  }
115 public:
116  //
117  // Public member functions
118  //
121  return m_typeAndName;
122  }
123 
125  std::string type() const;
126 
128  std::string name() const;
129 
131  bool empty() const {
132  return m_typeAndName.empty();
133  }
134 
136  void setTypeAndName( std::string myTypeAndName );
137 
139  void setName( const std::string& myName );
140 
145 
147  std::string messageName() const;
148 
152  virtual std::string pythonRepr() const;
153 
154 private:
155  //
156  // Data member
157  //
158  std::string m_typeAndName; // the full type and name: "type/name"
159 };
160 
161 
170 template< class T >
172  //
173  // Constructors etc.
174  //
175 protected:
176  GaudiHandle( std::string myTypeAndName, std::string myComponentType,
177  std::string myParentName )
178  : GaudiHandleBase(std::move(myTypeAndName), std::move(myComponentType), std::move(myParentName))
179  {}
180 
181 public:
183  GaudiHandle( const GaudiHandle& other )
184  : GaudiHandleBase( other ) {
185  m_pObject = other.m_pObject;
186  if ( m_pObject ) m_pObject->addRef();
187  }
188 
190  GaudiHandle& operator=( const GaudiHandle& other ) {
191  GaudiHandleBase::operator=( other );
192  // release any current tool
193  release().ignore();
194  m_pObject = other.m_pObject;
195  // update ref-counting
196  if ( m_pObject ) m_pObject->addRef();
197  return *this;
198  }
199 
201  //release();
202  }
203 
205  StatusCode retrieve() const { // not really const, because it updates m_pObject
206  if ( m_pObject && release().isFailure() ) return StatusCode::FAILURE;
207  if ( retrieve( m_pObject ).isFailure() ) {
208  m_pObject = nullptr;
209  return StatusCode::FAILURE;
210  }
211  return StatusCode::SUCCESS;
212  }
213 
215  StatusCode release() const { // not really const, because it updates m_pObject
216  if ( m_pObject ) {
217  StatusCode sc = release( m_pObject );
218  m_pObject = nullptr;
219  return sc;
220  }
221  return StatusCode::SUCCESS;
222  }
223 
225  bool isValid() const { // not really const, because it may update m_pObject
226  return m_pObject || retrieve().isSuccess();
227  }
228 
231  operator bool() const { // not really const, because it may update m_pObject
232  return isValid();
233  }
234 
236  T* get() const {
237  return m_pObject;
238  }
239 
241  bool isSet() const {
242  return get();
243  }
244 
245  T& operator*() {
246  assertObject();
247  return *m_pObject;
248  }
249 
250  T* operator->() {
251  assertObject();
252  return m_pObject;
253  }
254 
255  T& operator*() const { // not really const, because it may update m_pObject
256  assertObject();
257  return *m_pObject;
258  }
259 
260  T* operator->() const { // not really const, because it may update m_pObject
261  assertObject();
262  return m_pObject;
263  }
264 
267  return System::typeinfoName( typeid(T) );
268  }
269 
272  if ( defName.empty() ) defName = getDefaultType();
273  return defName;
274  }
275 
276 protected:
278  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
279 
282  virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
283  comp->release();
284  return StatusCode::SUCCESS;
285  }
286 
287 private:
290  const std::string& myType = getDefaultType();
291  GaudiHandleBase::setTypeAndName(myType+'/'+myType);
292  }
293 
295  void setDefaultType() {
296  GaudiHandleBase::setTypeAndName( getDefaultType() );
297  }
298 
301  void assertObject() const { // not really const, because it may update m_pObject
302  if ( !isValid() ) {
303  throw GaudiException("Failed to retrieve " + componentType() + ": " + typeAndName(),
304  componentType() + " retrieve", StatusCode::FAILURE);
305  }
306  }
307  //
308  // Data members
309  //
310  mutable T* m_pObject = nullptr;
311 };
312 
313 
321 protected:
322  GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
323  : GaudiHandleInfo(std::move(myComponentType),std::move(myParentName))
324  {}
325 public:
328 
331  bool setTypesAndNames( const std::vector< std::string >& myTypesAndNamesList );
332 
335  const std::vector< std::string > typesAndNames() const;
336 
338  const std::vector< std::string > types() const;
339 
341  const std::vector< std::string > names() const;
342 
345  const std::vector< std::string > getBaseInfos( std::string (GaudiHandleBase::*pMemFunc)() const ) const;
346 
350  std::string pythonPropertyClassName() const override;
351 
355  std::string pythonRepr() const override;
356 
360  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
361 
363  virtual void clear() = 0;
364 
366  virtual bool empty() const = 0;
367 
370  virtual ConstBaseHandleArray getBaseArray() const = 0;
371 
374  virtual BaseHandleArray getBaseArray() = 0;
375 
377  virtual bool retrieved() const = 0;
378 
379 };
380 
381 
383 template <class T>
385 public:
386  //
387  // public nested types
388  //
390  typedef typename HandleVector::value_type value_type;
391  typedef typename HandleVector::size_type size_type;
393  typedef typename HandleVector::const_reference const_reference;
394  typedef typename HandleVector::iterator iterator;
395  typedef typename HandleVector::const_iterator const_iterator;
396  typedef typename HandleVector::reverse_iterator reverse_iterator;
397  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
398 
399 protected:
400  //
401  // Constructors
402  //
407  GaudiHandleArray( const std::vector< std::string >& myTypesAndNamesList,
408  std::string myComponentType, std::string myParentName )
409  : GaudiHandleArrayBase(std::move(myComponentType),std::move(myParentName))
410  {
411  setTypesAndNames( myTypesAndNamesList );
412  }
413 
418  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
419  : GaudiHandleArrayBase(myComponentType,myParentName)
420  {}
421 
422 public:
423  virtual ~GaudiHandleArray() = default;
424 
426  GaudiHandleArray& operator=( const std::vector< std::string >& myTypesAndNamesList ) {
427  setTypesAndNames( myTypesAndNamesList );
428  return *this;
429  }
430 
433  iterator it = begin(), itEnd = end();
434  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
435  return baseArray;
436  }
437 
440  const_iterator it = begin(), itEnd = end();
441  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
442  return baseArray;
443  }
444 
445  //
446  // Simulate (part of) an std::vector
447  //
448  iterator begin() {
449  return m_handleArray.begin();
450  }
451 
452  iterator end() {
453  return m_handleArray.end();
454  }
455 
456  const_iterator begin() const {
457  return m_handleArray.begin();
458  }
459 
460  const_iterator end() const {
461  return m_handleArray.end();
462  }
463 
464  const_iterator rbegin() const {
465  return m_handleArray.rbegin();
466  }
467 
468  const_iterator rend() const {
469  return m_handleArray.rend();
470  }
471 
472  size_type size() const {
473  return m_handleArray.size();
474  }
475 
476  void clear() override {
477  m_handleArray.clear();
478  }
479 
480  bool empty() const override {
481  return m_handleArray.empty();
482  }
483 
484  T& operator[]( int index ) {
485  return m_handleArray[index];
486  }
487 
488  const T& operator[]( int index ) const {
489  return m_handleArray[index];
490  }
491 
493  T* operator[]( const std::string& name ) {
494  auto it = std::find_if(begin(),end(),[&](const_reference r) {
495  return r.name() == name;
496  } );
497  return it != end() ? &*it : nullptr;
498  }
499 
501  const T* operator[]( const std::string& name ) const {
502  auto it = std::find_if(begin(),end(),[&](const_reference r) {
503  return r.name() == name;
504  } );
505  return it != end() ? &*it : nullptr;
506  }
507 
510  using GaudiHandleArrayBase::push_back; // avoid compiler warning
511  virtual bool push_back( const T& myHandle ) {
512  m_handleArray.push_back( myHandle );
513  return true;
514  }
515 
518  for (auto& i : *this) {
519  // stop at first failure
520  if ( i.retrieve().isFailure() ) return StatusCode::FAILURE;
521  }
522  m_retrieved = true;
523  return StatusCode::SUCCESS;
524  }
525 
529  for (auto& i : *this ) {
530  // continue trying to release other tools even if we fail...
531  if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
532  }
533  return sc;
534  }
535 
537  bool retrieved() const { return m_retrieved; }
538 
539 private:
540  //
541  // Private data members
542  //
543  HandleVector m_handleArray;
544  bool m_retrieved { false };
545 };
546 
547 // Easy printing out of Handles and HandleArrays
548 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
549 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
550 
551 #endif // ! GAUDIKERNEL_GAUDIHANDLE_H
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:190
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:393
HandleVector::value_type value_type
Definition: GaudiHandle.h:390
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:171
T empty(T...args)
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:476
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:266
Define general base for Gaudi exception.
HandleVector::iterator iterator
Definition: GaudiHandle.h:394
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:431
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:327
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
const_iterator rend() const
Definition: GaudiHandle.h:468
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:397
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:67
std::string getDefaultName()
Definition: GaudiHandle.h:270
STL namespace.
T & operator*()
Definition: GaudiHandle.h:245
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:49
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:295
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:301
std::string m_componentType
Definition: GaudiHandle.h:80
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
Definition: GaudiHandle.h:407
HandleVector::size_type size_type
Definition: GaudiHandle.h:391
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:426
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:480
HandleVector::reference reference
Definition: GaudiHandle.h:392
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:511
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:72
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:215
bool retrieved() const
has Array been retreived?
Definition: GaudiHandle.h:537
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
STL class.
T push_back(T...args)
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:289
virtual std::string pythonRepr() const =0
Python representation of handle, i.e.
const T & operator[](int index) const
Definition: GaudiHandle.h:488
T * operator->() const
Definition: GaudiHandle.h:260
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:39
HandleVector m_handleArray
Definition: GaudiHandle.h:543
string type
Definition: gaudirun.py:151
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:517
std::vector< GaudiHandleBase * > BaseHandleArray
Definition: GaudiHandle.h:326
const_iterator begin() const
Definition: GaudiHandle.h:456
iterator begin()
Definition: GaudiHandle.h:448
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in .
Definition: GaudiHandle.cpp:63
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:438
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:205
std::string m_typeAndName
Definition: GaudiHandle.h:158
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
T & operator*() const
Definition: GaudiHandle.h:255
T move(T...args)
T * operator->()
Definition: GaudiHandle.h:250
T find_if(T...args)
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:395
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:225
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:320
virtual std::string pythonPropertyClassName() const =0
The python class name for the property in the genconf-generated configurables.
const_iterator rbegin() const
Definition: GaudiHandle.h:464
STL class.
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:25
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:241
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:396
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in .
virtual StatusCode release(T *comp) const
Release the component.
Definition: GaudiHandle.h:282
std::string type() const
The concrete component class name: the part before the '/'.
Definition: GaudiHandle.cpp:13
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
iterator end()
Definition: GaudiHandle.h:452
T & operator[](int index)
Definition: GaudiHandle.h:484
size_type size() const
Definition: GaudiHandle.h:472
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:501
const_iterator end() const
Definition: GaudiHandle.h:460
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:120
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:322
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:493
#define GAUDI_API
Definition: Kernel.h:107
list i
Definition: ana.py:128
STL class.
const std::string & componentType() const
Definition: GaudiHandle.h:34
std::vector< T > HandleVector
Definition: GaudiHandle.h:389
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle ('smart pointer') to a gaudi component.
Definition: GaudiHandle.h:109
std::string m_propertyName
Definition: GaudiHandle.h:81
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:183
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:384
std::string m_parentName
Definition: GaudiHandle.h:82
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:527
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:418