GaudiHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_GAUDIHANDLE_H
2 #define GAUDIKERNEL_GAUDIHANDLE_H
3 
4 //Includes
5 #include "GaudiKernel/IInterface.h"
6 #include "GaudiKernel/System.h"
7 #include "GaudiKernel/GaudiException.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 >
158 class GAUDI_API GaudiHandle: public GaudiHandleBase {
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:
272  void setDefaultTypeAndName() {
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;
371  typedef typename HandleVector::reference reference;
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 
500  StatusCode retrieve() {
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 
512  StatusCode release() {
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
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:158
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.
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:310
#define GAUDI_API
Definition: Kernel.h:108
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
virtual ConstBaseHandleArray getBaseArray() const =0
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
std::string names[100]
Definition: Node.cpp:19
std::vector< GaudiHandleBase * > BaseHandleArray
Definition: GaudiHandle.h:309
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
virtual void clear()=0
Clear the list of handles.
tuple end
Definition: IOTest.py:101
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:303
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in .
void setTypeAndName(const std::string &myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
virtual bool empty() const =0
Return whether the list of tools is empty.
std::string type() const
The concrete component class name: the part before the '/'.
Definition: GaudiHandle.cpp:13
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
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
virtual const std::string pythonRepr() const =0
Python representation of handle, i.e.
const std::string & componentType() const
Definition: GaudiHandle.h:33
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
string type
Definition: gaudirun.py:151
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:363