| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <algorithm>
11 #include <vector>
12 #include <stdexcept>
13 #include <iostream>
14
15 class GAUDI_API GaudiHandleInfo {
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
93 class GAUDI_API GaudiHandleBase : public GaudiHandleInfo {
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 //
120 std::string typeAndName() const {
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
144 std::string pythonPropertyClassName() const;
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 >
171 class GAUDI_API GaudiHandle: public GaudiHandleBase {
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
200 ~GaudiHandle(){
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
266 std::string getDefaultType() {
267 return System::typeinfoName( typeid(T) );
268 }
269
270 std::string getDefaultName() {
271 std::string defName = GaudiHandleBase::type();
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:
289 void setDefaultTypeAndName() {
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
320 class GAUDI_API GaudiHandleArrayBase: public GaudiHandleInfo {
321 protected:
322 GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
323 : GaudiHandleInfo(std::move(myComponentType),std::move(myParentName))
324 {}
325 public:
326 typedef std::vector< GaudiHandleBase* > BaseHandleArray;
327 typedef std::vector< const GaudiHandleBase* > ConstBaseHandleArray;
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>
384 class GAUDI_API GaudiHandleArray: public GaudiHandleArrayBase {
385 public:
386 //
387 // public nested types
388 //
389 typedef std::vector< T > HandleVector;
390 typedef typename HandleVector::value_type value_type;
391 typedef typename HandleVector::size_type size_type;
392 typedef typename HandleVector::reference reference;
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
431 GaudiHandleArrayBase::BaseHandleArray getBaseArray() override {
432 GaudiHandleArrayBase::BaseHandleArray baseArray;
433 iterator it = begin(), itEnd = end();
434 for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
435 return baseArray;
436 }
437
438 GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override {
439 GaudiHandleArrayBase::ConstBaseHandleArray baseArray;
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
517 StatusCode retrieve() {
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
527 StatusCode release() {
528 StatusCode sc = StatusCode::SUCCESS;
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
System.h
GaudiHandle::operator=
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
Definition: GaudiHandle.h:190
GaudiHandle::~GaudiHandle
~GaudiHandle()
Definition: GaudiHandle.h:200
GaudiHandleArray::const_reference
HandleVector::const_reference const_reference
Definition: GaudiHandle.h:393
GaudiHandleArray::value_type
HandleVector::value_type value_type
Definition: GaudiHandle.h:390
GaudiHandle
Handle to be used in lieu of naked pointers to gaudis.
Definition: GaudiHandle.h:171
std::string::empty
T empty(T...args)
GaudiHandleArray::clear
void clear() override
Clear the list of handles.
Definition: GaudiHandle.h:476
GaudiHandle::getDefaultType
std::string getDefaultType()
Helper function to get default type string from the class type.
Definition: GaudiHandle.h:266
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
GaudiHandleArray::iterator
HandleVector::iterator iterator
Definition: GaudiHandle.h:394
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
GaudiHandle::m_pObject
T * m_pObject
Definition: GaudiHandle.h:310
GaudiHandleArray::getBaseArray
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:431
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
GaudiHandleArrayBase::ConstBaseHandleArray
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
Definition: GaudiHandle.h:327
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
GaudiHandleArray::rend
const_iterator rend() const
Definition: GaudiHandle.h:468
GaudiHandleArray::const_reverse_iterator
HandleVector::const_reverse_iterator const_reverse_iterator
Definition: GaudiHandle.h:397
GaudiHandleInfo::setComponentType
void setComponentType(const std::string &componentType)
The component type.
Definition: GaudiHandle.h:67
GaudiHandle::getDefaultName
std::string getDefaultName()
Definition: GaudiHandle.h:270
std
STL namespace.
GaudiHandle::operator*
T & operator*()
Definition: GaudiHandle.h:245
GaudiHandle::GaudiHandle
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:176
GaudiHandleInfo::parentName
const std::string & parentName() const
The name of the parent.
Definition: GaudiHandle.h:49
GaudiHandle::setDefaultType
void setDefaultType()
Helper function to set default type from the class type T.
Definition: GaudiHandle.h:295
GaudiHandle::assertObject
void assertObject() const
Load the pointer to the component.
Definition: GaudiHandle.h:301
GaudiHandleInfo::m_componentType
std::string m_componentType
Definition: GaudiHandle.h:80
GaudiHandleArray::GaudiHandleArray
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
Definition: GaudiHandle.h:407
GaudiHandleArray::size_type
HandleVector::size_type size_type
Definition: GaudiHandle.h:391
GaudiHandleArray::operator=
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Definition: GaudiHandle.h:426
GaudiHandleArray::empty
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:480
GaudiHandleArray::reference
HandleVector::reference reference
Definition: GaudiHandle.h:392
GaudiHandleArray::push_back
virtual bool push_back(const T &myHandle)
Definition: GaudiHandle.h:511
GaudiHandleInfo::setParentName
void setParentName(const std::string &parent)
The name of the parent.
Definition: GaudiHandle.h:72
GaudiHandle::release
StatusCode release() const
Release the component.
Definition: GaudiHandle.h:215
merge_files.sc
int sc
Definition: merge_files.py:167
GaudiHandleArray::retrieved
bool retrieved() const
has Array been retreived?
Definition: GaudiHandle.h:537
begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
std::string
STL class.
std::vector::push_back
T push_back(T...args)
GaudiHandle::setDefaultTypeAndName
void setDefaultTypeAndName()
Helper function to set default name and type.
Definition: GaudiHandle.h:289
GaudiHandleInfo::pythonRepr
virtual std::string pythonRepr() const =0
Python representation of handle, i.e.
GaudiHandleArray::operator[]
const T & operator[](int index) const
Definition: GaudiHandle.h:488
GaudiHandle::operator->
T * operator->() const
Definition: GaudiHandle.h:260
GaudiHandleInfo::propertyName
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition: GaudiHandle.h:39
GaudiHandleArray::m_handleArray
HandleVector m_handleArray
Definition: GaudiHandle.h:543
gaudirun.type
string type
Definition: gaudirun.py:151
StatusCode::FAILURE
Definition: StatusCode.h:29
GaudiHandleArray::retrieve
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:517
GaudiHandleArrayBase::BaseHandleArray
std::vector< GaudiHandleBase * > BaseHandleArray
Definition: GaudiHandle.h:326
GaudiHandleArray::begin
const_iterator begin() const
Definition: GaudiHandle.h:456
GaudiHandleArray::begin
iterator begin()
Definition: GaudiHandle.h:448
GaudiHandleArrayBase::setTypesAndNames
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in .
Definition: GaudiHandle.cpp:63
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GaudiException.h
GaudiHandleArray::getBaseArray
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
Definition: GaudiHandle.h:438
GaudiHandle::retrieve
StatusCode retrieve() const
Retrieve the component.
Definition: GaudiHandle.h:205
GaudiHandleBase::m_typeAndName
std::string m_typeAndName
Definition: GaudiHandle.h:158
end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
GaudiHandle::operator*
T & operator*() const
Definition: GaudiHandle.h:255
std::move
T move(T...args)
GaudiHandle::operator->
T * operator->()
Definition: GaudiHandle.h:250
std::find_if
T find_if(T...args)
GaudiHandleArray::const_iterator
HandleVector::const_iterator const_iterator
Definition: GaudiHandle.h:395
GaudiHandle::isValid
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
Definition: GaudiHandle.h:225
GaudiHandleArrayBase
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:320
GaudiHandleInfo::pythonPropertyClassName
virtual std::string pythonPropertyClassName() const =0
The python class name for the property in the genconf-generated configurables.
GaudiHandleArray::rbegin
const_iterator rbegin() const
Definition: GaudiHandle.h:464
std::vector
STL class.
GaudiHandleInfo::GaudiHandleInfo
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition: GaudiHandle.h:25
GaudiHandleInfo::setPropertyName
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:44
GaudiHandle::isSet
bool isSet() const
True if the wrapped pointer is not null.
Definition: GaudiHandle.h:241
GaudiHandleArray::reverse_iterator
HandleVector::reverse_iterator reverse_iterator
Definition: GaudiHandle.h:396
GaudiHandleArrayBase::push_back
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in .
GaudiHandle::release
virtual StatusCode release(T *comp) const
Release the component.
Definition: GaudiHandle.h:282
GaudiHandleBase::type
std::string type() const
The concrete component class name: the part before the '/'.
Definition: GaudiHandle.cpp:13
operator<<
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
Definition: GaudiHandle.cpp:108
GaudiHandleArray::end
iterator end()
Definition: GaudiHandle.h:452
GaudiHandleArray::operator[]
T & operator[](int index)
Definition: GaudiHandle.h:484
GaudiHandleArray::size
size_type size() const
Definition: GaudiHandle.h:472
GaudiHandleArray::operator[]
const T * operator[](const std::string &name) const
Get const pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:501
GaudiHandleArray::end
const_iterator end() const
Definition: GaudiHandle.h:460
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:93
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:120
GaudiHandleArrayBase::GaudiHandleArrayBase
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
Definition: GaudiHandle.h:322
GaudiHandleArray::operator[]
T * operator[](const std::string &name)
Get pointer (!) to ToolHandle by instance name.
Definition: GaudiHandle.h:493
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
IInterface.h
ana.i
list i
Definition: ana.py:128
std::ostream
STL class.
GaudiHandleInfo::componentType
const std::string & componentType() const
Definition: GaudiHandle.h:34
GaudiHandleArray::HandleVector
std::vector< T > HandleVector
Definition: GaudiHandle.h:389
Objects::reference
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
GaudiHandleBase::GaudiHandleBase
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle ('smart pointer') to a gaudi component.
Definition: GaudiHandle.h:109
GaudiHandleInfo::m_propertyName
std::string m_propertyName
Definition: GaudiHandle.h:81
GaudiHandle::GaudiHandle
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
Definition: GaudiHandle.h:183
GaudiHandleBase::empty
bool empty() const
Check if the handle has been set to empty string (i.e.
Definition: GaudiHandle.h:131
GaudiHandleArray
T is the concrete handle type, e.g.
Definition: GaudiHandle.h:384
GaudiHandleInfo::m_parentName
std::string m_parentName
Definition: GaudiHandle.h:82
GaudiHandleArray::release
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:527
GaudiHandleArray::GaudiHandleArray
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
Definition: GaudiHandle.h:418
GaudiHandleInfo
Definition: GaudiHandle.h:15