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 <vector>
11 #include <stdexcept>
12 #include <iostream>
13 
15 protected:
24  GaudiHandleInfo( const std::string& myComponentType, const std::string& myParentName )
25  : m_componentType(myComponentType), m_parentName(myParentName)
26  {}
27 public:
29  virtual ~GaudiHandleInfo() {}
30  //
31  // Public member functions
32  //
33  const std::string& componentType() const {
34  return m_componentType;
35  }
36 
38  const std::string& propertyName() const {
39  return m_propertyName;
40  }
41 
43  void setPropertyName( const std::string& propName ) {
44  m_propertyName = propName;
45  }
46 
48  const std::string& parentName() const {
49  return m_parentName;
50  }
51 
55  virtual const std::string pythonPropertyClassName() const = 0;
56 
61  virtual const std::string pythonRepr() const = 0;
62 
63 private:
64  //
65  // Data members
66  //
67  std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
68  std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
69  std::string m_parentName; // name of the parent having this handle as a member
70 };
71 
72 
81  //
82  // Ctors etc
83  //
84 protected:
96  GaudiHandleBase( const std::string& myTypeAndName, const std::string& myComponentType,
97  const std::string& myParentName )
98  : GaudiHandleInfo(myComponentType,myParentName)
99  {
100  setTypeAndName(myTypeAndName);
101  }
102 public:
103  //
104  // Public member functions
105  //
107  std::string typeAndName() const {
108  return m_typeAndName;
109  }
110 
112  std::string type() const;
113 
115  std::string name() const;
116 
118  bool empty() const {
119  return m_typeAndName.empty();
120  }
121 
123  void setTypeAndName( const std::string& myTypeAndName );
124 
126  void setName( const std::string& myName );
127 
131  const std::string pythonPropertyClassName() const;
132 
134  const std::string messageName() const;
135 
139  virtual const std::string pythonRepr() const;
140 
141 private:
142  //
143  // Data member
144  //
145  std::string m_typeAndName; // the full type and name: "type/name"
146 };
147 
148 
157 template< class T >
159  //
160  // Constructors etc.
161  //
162 protected:
163  GaudiHandle( const std::string& myTypeAndName, const std::string& myComponentType,
164  const std::string& myParentName )
165  : GaudiHandleBase(myTypeAndName, myComponentType, myParentName), m_pObject(0)
166  {}
167 
168 public:
170  GaudiHandle( const GaudiHandle& other )
171  : GaudiHandleBase( other ) {
172  m_pObject = other.m_pObject;
173  if ( m_pObject ) m_pObject->addRef();
174  }
175 
177  GaudiHandle& operator=( const GaudiHandle& other ) {
178  GaudiHandleBase::operator=( other );
179  // release any current tool
180  release().ignore();
181  m_pObject = other.m_pObject;
182  // update ref-counting
183  if ( m_pObject ) m_pObject->addRef();
184  return *this;
185  }
186 
188  StatusCode retrieve() const { // not really const, because it updates m_pObject
189  if ( m_pObject && release().isFailure() ) return StatusCode::FAILURE;
190  if ( retrieve( m_pObject ).isFailure() ) {
191  m_pObject = 0;
192  return StatusCode::FAILURE;
193  }
194  return StatusCode::SUCCESS;
195  }
196 
198  StatusCode release() const { // not really const, because it updates m_pObject
199  if ( m_pObject ) {
200  StatusCode sc = release( m_pObject );
201  m_pObject = 0;
202  return sc;
203  }
204  return StatusCode::SUCCESS;
205  }
206 
208  bool isValid() const { // not really const, because it may update m_pObject
209  return m_pObject || retrieve().isSuccess();
210  }
211 
214  operator bool() const { // not really const, because it may update m_pObject
215  return isValid();
216  }
217 
219  T* get() const {
220  return m_pObject;
221  }
222 
224  bool isSet() const {
225  return get();
226  }
227 
228  T& operator*() {
229  assertObject();
230  return *m_pObject;
231  }
232 
233  T* operator->() {
234  assertObject();
235  return m_pObject;
236  }
237 
238  T& operator*() const { // not really const, because it may update m_pObject
239  assertObject();
240  return *m_pObject;
241  }
242 
243  T* operator->() const { // not really const, because it may update m_pObject
244  assertObject();
245  return m_pObject;
246  }
247 
249  std::string getDefaultType() {
250  return System::typeinfoName( typeid(T) );
251  }
252 
253  std::string getDefaultName() {
254  std::string defName = GaudiHandleBase::type();
255  if ( defName.empty() ) defName = getDefaultType();
256  return defName;
257  }
258 
259 protected:
261  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
262 
265  virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
266  comp->release();
267  return StatusCode::SUCCESS;
268  }
269 
270 private:
273  const std::string& myType = getDefaultType();
274  GaudiHandleBase::setTypeAndName(myType+'/'+myType);
275  }
276 
278  void setDefaultType() {
279  GaudiHandleBase::setTypeAndName( getDefaultType() );
280  }
281 
284  void assertObject() const { // not really const, because it may update m_pObject
285  if ( !isValid() ) {
286  throw GaudiException("Failed to retrieve " + componentType() + ": " + typeAndName(),
287  componentType() + " retrieve", StatusCode::FAILURE);
288  }
289  }
290  //
291  // Data members
292  //
293  mutable T* m_pObject;
294 };
295 
296 
304 protected:
305  GaudiHandleArrayBase( const std::string& myComponentType, const std::string& myParentName )
306  : GaudiHandleInfo(myComponentType,myParentName)
307  {}
308 public:
309  typedef std::vector< GaudiHandleBase* > BaseHandleArray;
310  typedef std::vector< const GaudiHandleBase* > ConstBaseHandleArray;
311 
314  bool setTypesAndNames( const std::vector< std::string >& myTypesAndNamesList );
315 
318  const std::vector< std::string > typesAndNames() const;
319 
321  const std::vector< std::string > types() const;
322 
324  const std::vector< std::string > names() const;
325 
328  const std::vector< std::string > getBaseInfos( std::string (GaudiHandleBase::*pMemFunc)() const ) const;
329 
333  virtual const std::string pythonPropertyClassName() const;
334 
338  virtual const std::string pythonRepr() const;
339 
343  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
344 
346  virtual void clear() = 0;
347 
349  virtual bool empty() const = 0;
350 
353  virtual ConstBaseHandleArray getBaseArray() const = 0;
354 
357  virtual BaseHandleArray getBaseArray() = 0;
358 };
359 
360 
362 template <class T>
364 public:
365  //
366  // public nested types
367  //
368  typedef std::vector< T > HandleVector;
369  typedef typename HandleVector::value_type value_type;
370  typedef typename HandleVector::size_type size_type;
372  typedef typename HandleVector::const_reference const_reference;
373  typedef typename HandleVector::iterator iterator;
374  typedef typename HandleVector::const_iterator const_iterator;
375  typedef typename HandleVector::reverse_iterator reverse_iterator;
376  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
377 
378 protected:
379  //
380  // Constructors
381  //
386  GaudiHandleArray( const std::vector< std::string >& myTypesAndNamesList,
387  const std::string& myComponentType, const std::string& myParentName )
388  : GaudiHandleArrayBase(myComponentType,myParentName)
389  {
390  setTypesAndNames( myTypesAndNamesList );
391  }
392 
397  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
398  : GaudiHandleArrayBase(myComponentType,myParentName)
399  {}
400 
401 public:
402  virtual ~GaudiHandleArray() {};
403 
405  GaudiHandleArray& operator=( const std::vector< std::string >& myTypesAndNamesList ) {
406  setTypesAndNames( myTypesAndNamesList );
407  return *this;
408  }
409 
412  iterator it = begin(), itEnd = end();
413  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
414  return baseArray;
415  }
416 
419  const_iterator it = begin(), itEnd = end();
420  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
421  return baseArray;
422  }
423 
424  //
425  // Simulate (part of) an std::vector
426  //
427  iterator begin() {
428  return m_handleArray.begin();
429  }
430 
431  iterator end() {
432  return m_handleArray.end();
433  }
434 
435  const_iterator begin() const {
436  return m_handleArray.begin();
437  }
438 
439  const_iterator end() const {
440  return m_handleArray.end();
441  }
442 
443  const_iterator rbegin() const {
444  return m_handleArray.rbegin();
445  }
446 
447  const_iterator rend() const {
448  return m_handleArray.rend();
449  }
450 
451  size_type size() const {
452  return m_handleArray.size();
453  }
454 
455  virtual void clear() {
456  m_handleArray.clear();
457  }
458 
459  virtual bool empty() const {
460  return m_handleArray.empty();
461  }
462 
463  T& operator[]( int index ) {
464  return m_handleArray[index];
465  }
466 
467  const T& operator[]( int index ) const {
468  return m_handleArray[index];
469  }
470 
472  T* operator[]( const std::string& name ) {
473  iterator it = begin(), itEnd = end();
474  for ( ; it != itEnd; ++it ) {
475  if ( it->name() == name ) return &*it;
476  }
477  // not found
478  return 0;
479  }
480 
482  const T* operator[]( const std::string& name ) const {
483  const_iterator it = begin(), itEnd = end();
484  for ( ; it != itEnd; ++it ) {
485  if ( it->name() == name ) return &*it;
486  }
487  // not found
488  return 0;
489  }
490 
493  using GaudiHandleArrayBase::push_back; // avoid compiler warning
494  virtual bool push_back( const T& myHandle ) {
495  m_handleArray.push_back( myHandle );
496  return true;
497  }
498 
501  iterator it = begin(), itEnd = end();
502  for ( ; it != itEnd; ++it ) {
503  if ( it->retrieve().isFailure() ) {
504  // stop at first failure
505  return StatusCode::FAILURE;
506  }
507  }
508  return StatusCode::SUCCESS;
509  }
510 
514  iterator it = begin(), itEnd = end();
515  for ( ; it != itEnd; ++it ) {
516  if ( it->release().isFailure() ) {
517  // continue trying to release other tools
518  sc = StatusCode::FAILURE;
519  }
520  }
521  return sc;
522  }
523 
524 private:
525  //
526  // Private data members
527  //
528  HandleVector m_handleArray;
529 };
530 
531 // Easy printing out of Handles and HandleArrays
532 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
533 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
534 
535 #endif // ! GAUDIKERNEL_GAUDIHANDLE_H
virtual GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:417
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:177
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:372
HandleVector::value_type value_type
Definition: GaudiHandle.h:369
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:158
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:249
Define general base for Gaudi exception.
virtual const std::string pythonPropertyClassName() const =0
The python class name for the property in the genconf-generated configurables.
HandleVector::iterator iterator
Definition: GaudiHandle.h:373
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:310
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
const_iterator rend() const
Definition: GaudiHandle.h:447
virtual void clear()
Clear the list of handles.
Definition: GaudiHandle.h:455
virtual ~GaudiHandleInfo()
virtual destructor so that derived class destructor is called.
Definition: GaudiHandle.h:29
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:376
std::string getDefaultName()
Definition: GaudiHandle.h:253
T & operator*()
Definition: GaudiHandle.h:228
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:48
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:278
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:284
std::string m_componentType
Definition: GaudiHandle.h:67
HandleVector::size_type size_type
Definition: GaudiHandle.h:370
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:405
std::string names[100]
Definition: Node.cpp:19
HandleVector::reference reference
Definition: GaudiHandle.h:371
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:494
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:198
GaudiHandleArrayBase(const std::string &myComponentType, const std::string &myParentName)
Definition: GaudiHandle.h:305
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:272
const T & operator[](int index) const
Definition: GaudiHandle.h:467
T * operator->() const
Definition: GaudiHandle.h:243
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:38
HandleVector m_handleArray
Definition: GaudiHandle.h:528
string type
Definition: gaudirun.py:127
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:500
GaudiHandleBase(const std::string &myTypeAndName, const std::string &myComponentType, const std::string &myParentName)
Create a handle ('smart pointer') to a gaudi component.
Definition: GaudiHandle.h:96
std::vector< GaudiHandleBase * > BaseHandleArray
Definition: GaudiHandle.h:309
const_iterator begin() const
Definition: GaudiHandle.h:435
iterator begin()
Definition: GaudiHandle.h:427
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:30
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:188
std::string m_typeAndName
Definition: GaudiHandle.h:145
virtual GaudiHandleArrayBase::BaseHandleArray getBaseArray()
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:410
T & operator*() const
Definition: GaudiHandle.h:238
GaudiHandleInfo(const std::string &myComponentType, const std::string &myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:24
tuple end
Definition: IOTest.py:101
T * operator->()
Definition: GaudiHandle.h:233
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:374
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:208
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:303
const_iterator rbegin() const
Definition: GaudiHandle.h:443
virtual ~GaudiHandleArray()
Definition: GaudiHandle.h:402
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:224
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:375
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:265
void setTypeAndName(const std::string &myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
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)
virtual bool empty() const
Return whether the list of tools is empty.
Definition: GaudiHandle.h:459
iterator end()
Definition: GaudiHandle.h:431
GaudiHandle(const std::string &myTypeAndName, const std::string &myComponentType, const std::string &myParentName)
Definition: GaudiHandle.h:163
T & operator[](int index)
Definition: GaudiHandle.h:463
size_type size() const
Definition: GaudiHandle.h:451
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:482
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: GaudiHandle.h:386
const_iterator end() const
Definition: GaudiHandle.h:439
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:80
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:107
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:472
void setPropertyName(const std::string &propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:43
virtual const std::string pythonRepr() const =0
Python representation of handle, i.e.
#define GAUDI_API
Definition: Kernel.h:108
const std::string & componentType() const
Definition: GaudiHandle.h:33
std::vector< T > HandleVector
Definition: GaudiHandle.h:368
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
std::string m_propertyName
Definition: GaudiHandle.h:68
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:170
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:118
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:363
std::string m_parentName
Definition: GaudiHandle.h:69
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:512
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:397