| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

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

- File List
- File Members

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
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
14 class GAUDI_API GaudiHandleInfo {
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
80 class GAUDI_API GaudiHandleBase : public GaudiHandleInfo {
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
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:
257 void setDefaultTypeAndName() {
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
293 class GAUDI_API GaudiHandleArrayBase: public GaudiHandleInfo {
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>
353 class GAUDI_API GaudiHandleArray: public GaudiHandleArrayBase {
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;
361 typedef typename HandleVector::reference reference;
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
400 virtual GaudiHandleArrayBase::BaseHandleArray getBaseArray() {
401 GaudiHandleArrayBase::BaseHandleArray baseArray;
402 iterator it = begin(), itEnd = end();
403 for ( ; it != itEnd; ++it ) baseArray.push_back( &*it );
404 return baseArray;
405 }
406
407 virtual GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const {
408 GaudiHandleArrayBase::ConstBaseHandleArray baseArray;
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 //
417 iterator begin() {
418 return m_handleArray.begin();
419 }
420
421 iterator end() {
422 return m_handleArray.end();
423 }
424
425 const_iterator begin() const {
426 return m_handleArray.begin();
427 }
428
429 const_iterator end() const {
430 return m_handleArray.end();
431 }
432
433 const_iterator rbegin() const {
434 return m_handleArray.rbegin();
435 }
436
437 const_iterator rend() const {
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
490 StatusCode retrieve() {
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
502 StatusCode release() {
503 StatusCode sc = StatusCode::SUCCESS;
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 //
518 HandleVector m_handleArray;
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