All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
209  operator bool() const { // not really const, because it may update m_pObject
210  return getObject();
211  }
212 
213  T& operator*() {
214  assertObject();
215  return *m_pObject;
216  }
217 
218  T* operator->() {
219  assertObject();
220  return m_pObject;
221  }
222 
223  T& operator*() const { // not really const, because it may update m_pObject
224  assertObject();
225  return *m_pObject;
226  }
227 
228  T* operator->() const { // not really const, because it may update m_pObject
229  assertObject();
230  return m_pObject;
231  }
232 
234  std::string getDefaultType() {
235  return System::typeinfoName( typeid(T) );
236  }
237 
238  std::string getDefaultName() {
239  std::string defName = GaudiHandleBase::type();
240  if ( defName.empty() ) defName = getDefaultType();
241  return defName;
242  }
243 
244 protected:
246  virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
247 
250  virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
251  comp->release();
252  return StatusCode::SUCCESS;
253  }
254 
255 private:
258  const std::string& myType = getDefaultType();
259  GaudiHandleBase::setTypeAndName(myType+'/'+myType);
260  }
261 
263  void setDefaultType() {
264  GaudiHandleBase::setTypeAndName( getDefaultType() );
265  }
266 
268  bool getObject() const { // not really const, because it may update m_pObject
269  return m_pObject || retrieve().isSuccess();
270  }
271 
274  void assertObject() const { // not really const, because it may update m_pObject
275  if ( !getObject() ) {
276  throw GaudiException("Failed to retrieve " + componentType() + ": " + typeAndName(),
277  componentType() + " retrieve", StatusCode::FAILURE);
278  }
279  }
280  //
281  // Data members
282  //
283  mutable T* m_pObject;
284 };
285 
286 
294 protected:
295  GaudiHandleArrayBase( const std::string& myComponentType, const std::string& myParentName )
296  : GaudiHandleInfo(myComponentType,myParentName)
297  {}
298 public:
299  typedef std::vector< GaudiHandleBase* > BaseHandleArray;
300  typedef std::vector< const GaudiHandleBase* > ConstBaseHandleArray;
301 
304  bool setTypesAndNames( const std::vector< std::string >& myTypesAndNamesList );
305 
308  const std::vector< std::string > typesAndNames() const;
309 
311  const std::vector< std::string > types() const;
312 
314  const std::vector< std::string > names() const;
315 
318  const std::vector< std::string > getBaseInfos( std::string (GaudiHandleBase::*pMemFunc)() const ) const;
319 
323  virtual const std::string pythonPropertyClassName() const;
324 
328  virtual const std::string pythonRepr() const;
329 
333  virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
334 
336  virtual void clear() = 0;
337 
339  virtual bool empty() const = 0;
340 
343  virtual ConstBaseHandleArray getBaseArray() const = 0;
344 
347  virtual BaseHandleArray getBaseArray() = 0;
348 };
349 
350 
352 template <class T>
354 public:
355  //
356  // public nested types
357  //
358  typedef std::vector< T > HandleVector;
359  typedef typename HandleVector::value_type value_type;
360  typedef typename HandleVector::size_type size_type;
362  typedef typename HandleVector::const_reference const_reference;
363  typedef typename HandleVector::iterator iterator;
364  typedef typename HandleVector::const_iterator const_iterator;
365  typedef typename HandleVector::reverse_iterator reverse_iterator;
366  typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
367 
368 protected:
369  //
370  // Constructors
371  //
376  GaudiHandleArray( const std::vector< std::string >& myTypesAndNamesList,
377  const std::string& myComponentType, const std::string& myParentName )
378  : GaudiHandleArrayBase(myComponentType,myParentName)
379  {
380  setTypesAndNames( myTypesAndNamesList );
381  }
382 
387  GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
388  : GaudiHandleArrayBase(myComponentType,myParentName)
389  {}
390 
391 public:
392  virtual ~GaudiHandleArray() {};
393 
395  GaudiHandleArray& operator=( const std::vector< std::string >& myTypesAndNamesList ) {
396  setTypesAndNames( myTypesAndNamesList );
397  return *this;
398  }
399 
402  iterator it = begin(), itEnd = end();
403  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
404  return baseArray;
405  }
406 
409  const_iterator it = begin(), itEnd = end();
410  for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
411  return baseArray;
412  }
413 
414  //
415  // Simulate (part of) an std::vector
416  //
418  return m_handleArray.begin();
419  }
420 
422  return m_handleArray.end();
423  }
424 
426  return m_handleArray.begin();
427  }
428 
429  const_iterator end() const {
430  return m_handleArray.end();
431  }
432 
434  return m_handleArray.rbegin();
435  }
436 
438  return m_handleArray.rend();
439  }
440 
441  size_type size() const {
442  return m_handleArray.size();
443  }
444 
445  virtual void clear() {
446  m_handleArray.clear();
447  }
448 
449  virtual bool empty() const {
450  return m_handleArray.empty();
451  }
452 
453  T& operator[]( int index ) {
454  return m_handleArray[index];
455  }
456 
457  const T& operator[]( int index ) const {
458  return m_handleArray[index];
459  }
460 
462  T* operator[]( const std::string& name ) {
463  iterator it = begin(), itEnd = end();
464  for ( ; it != itEnd; ++it ) {
465  if ( it->name() == name ) return &*it;
466  }
467  // not found
468  return 0;
469  }
470 
472  const T* operator[]( const std::string& name ) const {
473  const_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 
483  using GaudiHandleArrayBase::push_back; // avoid compiler warning
484  virtual bool push_back( const T& myHandle ) {
485  m_handleArray.push_back( myHandle );
486  return true;
487  }
488 
491  iterator it = begin(), itEnd = end();
492  for ( ; it != itEnd; ++it ) {
493  if ( it->retrieve().isFailure() ) {
494  // stop at first failure
495  return StatusCode::FAILURE;
496  }
497  }
498  return StatusCode::SUCCESS;
499  }
500 
504  iterator it = begin(), itEnd = end();
505  for ( ; it != itEnd; ++it ) {
506  if ( it->release().isFailure() ) {
507  // continue trying to release other tools
508  sc = StatusCode::FAILURE;
509  }
510  }
511  return sc;
512  }
513 
514 private:
515  //
516  // Private data members
517  //
519 };
520 
521 // Easy printing out of Handles and HandleArrays
522 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
523 std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
524 
525 #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:407
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:177
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:362
HandleVector::value_type value_type
Definition: GaudiHandle.h:359
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:234
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:363
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:300
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
const_iterator rend() const
Definition: GaudiHandle.h:437
virtual void clear()
Clear the list of handles.
Definition: GaudiHandle.h:445
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:366
bool getObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:268
std::string getDefaultName()
Definition: GaudiHandle.h:238
T & operator*()
Definition: GaudiHandle.h:213
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:263
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:274
std::string m_componentType
Definition: GaudiHandle.h:67
HandleVector::size_type size_type
Definition: GaudiHandle.h:360
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:395
std::string names[100]
Definition: Node.cpp:19
HandleVector::reference reference
Definition: GaudiHandle.h:361
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:484
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:198
GaudiHandleArrayBase(const std::string &myComponentType, const std::string &myParentName)
Definition: GaudiHandle.h:295
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:257
const T & operator[](int index) const
Definition: GaudiHandle.h:457
T * operator->() const
Definition: GaudiHandle.h:228
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:38
HandleVector m_handleArray
Definition: GaudiHandle.h:518
string type
Definition: gaudirun.py:126
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:490
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:299
const_iterator begin() const
Definition: GaudiHandle.h:425
iterator begin()
Definition: GaudiHandle.h:417
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:400
T & operator*() const
Definition: GaudiHandle.h:223
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:218
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:364
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:293
const_iterator rbegin() const
Definition: GaudiHandle.h:433
virtual ~GaudiHandleArray()
Definition: GaudiHandle.h:392
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:365
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:250
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:449
iterator end()
Definition: GaudiHandle.h:421
GaudiHandle(const std::string &myTypeAndName, const std::string &myComponentType, const std::string &myParentName)
Definition: GaudiHandle.h:163
T & operator[](int index)
Definition: GaudiHandle.h:453
size_type size() const
Definition: GaudiHandle.h:441
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:472
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, const std::string &myComponentType, const std::string &myParentName)
Generic constructor.
Definition: GaudiHandle.h:376
const_iterator end() const
Definition: GaudiHandle.h:429
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:462
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:358
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:353
std::string m_parentName
Definition: GaudiHandle.h:69
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:502
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:387