The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
GaudiHandle.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11#pragma once
12
13#include <Gaudi/Property.h>
16#include <GaudiKernel/System.h>
17#include <algorithm>
18#include <iostream>
19#include <string>
20#include <type_traits>
21#include <vector>
22
23namespace details {
25 template <class T>
26 std::remove_const_t<T>* nonConst( T* p ) {
27 return const_cast<std::remove_const_t<T>*>( p );
28 }
29} // namespace details
30
32protected:
41 GaudiHandleInfo( std::string myComponentType, std::string myParentName )
42 : m_componentType( std::move( myComponentType ) ), m_parentName( std::move( myParentName ) ) {}
43
44public:
46 // Don't use =default here. Otherwise, in c++20 mode, clang will
47 // instantiate the handle virtual functions early, breaking the case
48 // where handles are used with a forward-declared class.
49 virtual ~GaudiHandleInfo() {}
50 //
51 // Public member functions
52 //
53 const std::string& componentType() const { return m_componentType; }
54
56 const std::string& propertyName() const { return m_propertyName; }
57
59 void setPropertyName( std::string propName ) { m_propertyName = std::move( propName ); }
60
62 const std::string& parentName() const { return m_parentName; }
63
67 virtual std::string pythonPropertyClassName() const = 0;
68
73 virtual std::string pythonRepr() const = 0;
74
75 // Easy printing out of Handles and HandleArrays
76 // It prints <propertyName> = <HandleType>( <HandleType(s)AndName(s)> )
77 friend std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle );
78
79protected:
81 void setComponentType( std::string componentType ) { m_componentType = std::move( componentType ); }
82
84 void setParentName( std::string parent ) { m_parentName = std::move( parent ); }
85
86private:
87 //
88 // Data members
89 //
90 std::string m_componentType; // e.g.: "PublicTool","PrivateTool","Service"
91 std::string m_propertyName; // name as used in declareProperty(name,gaudiHandle)
92 std::string m_parentName; // name of the parent having this handle as a member
93};
94
103 //
104 // Ctors etc
105 //
106protected:
118 GaudiHandleBase( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
119 : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) ) {
120 setTypeAndName( std::move( myTypeAndName ) );
121 }
122
123public:
124 //
125 // Public member functions
126 //
128 const std::string& typeAndName() const { return m_typeAndName; }
129
131 std::string type() const;
132
134 std::string name() const;
135
137 bool empty() const { return m_typeAndName.empty(); }
138
140 void setTypeAndName( std::string myTypeAndName );
141
143 void setName( std::string_view myName );
144
148 std::string pythonPropertyClassName() const override;
149
151 std::string messageName() const;
152
156 std::string pythonRepr() const override;
157
159
160private:
161 //
162 // Data member
163 //
164 std::string m_typeAndName; // the full type and name: "type/name"
165};
166
175template <class T>
177 //
178 // Constructors etc.
179 //
180protected:
181 GaudiHandle( std::string myTypeAndName, std::string myComponentType, std::string myParentName )
182 : GaudiHandleBase( std::move( myTypeAndName ), std::move( myComponentType ), std::move( myParentName ) ) {}
183
184public:
186 template <typename CT = T, typename NCT = std::remove_const_t<T>>
188 requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
189 : GaudiHandleBase( other ), m_pObject( other.get() ) {
190 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
191 }
192
194 GaudiHandle( const GaudiHandle& other ) : GaudiHandleBase( other ), m_pObject( other.m_pObject.load() ) {
195 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
196 }
197
199 template <typename CT = T, typename NCT = std::remove_const_t<T>>
200 requires( std::is_const_v<CT> && !std::is_same_v<CT, NCT> )
202 GaudiHandleBase::operator=( other );
203 // release any current tool
204 release().ignore();
205 m_pObject = other.get();
206 // update ref-counting
207 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
208 return *this;
209 }
210
213 GaudiHandleBase::operator=( other );
214 // release any current tool
215 release().ignore();
216 m_pObject = other.m_pObject.load();
217 // update ref-counting
218 if ( m_pObject ) ::details::nonConst( m_pObject.load() )->addRef();
219 return *this;
220 }
221
224 // not really const, because it updates m_pObject
225 // Do the lookup into a temporary pointer.
226 T* p = nullptr;
227 if ( retrieve( p ).isFailure() ) { return StatusCode::FAILURE; }
228
229 // If m_pObject is null, then copy p to m_pObject.
230 // Otherwise, release p.
231 T* old = nullptr;
232 if ( m_pObject.compare_exchange_strong( old, p ) ) { return StatusCode::SUCCESS; }
233 return release( p );
234 }
235
238 // not really const, because it updates m_pObject
240 if ( m_pObject ) {
241 sc = release( m_pObject );
242 m_pObject = nullptr;
243 }
244 return sc;
245 }
246
248 bool isValid() const {
249 // not really const, because it may update m_pObject
250 return m_pObject || retrieve().isSuccess();
251 }
252
255 operator bool() const {
256 // not really const, because it may update m_pObject
257 return isValid();
258 }
259
261 T* get() { return m_pObject; }
262
264 std::add_const_t<T>* get() const { return m_pObject; }
265
267 bool isSet() const { return get(); }
268
270 assertObject();
271 return *m_pObject;
272 }
273
275 assertObject();
276 return m_pObject;
277 }
278
279 std::add_const_t<T>& operator*() const {
280 // not really const, because it may update m_pObject
281 assertObject();
282 return *m_pObject;
283 }
284
285 std::add_const_t<T>* operator->() const {
286 // not really const, because it may update m_pObject
287 assertObject();
288 return m_pObject;
289 }
290
292 std::string getDefaultType() { return System::typeinfoName( typeid( T ) ); }
293
294 std::string getDefaultName() {
295 const auto defName = GaudiHandleBase::type();
296 return ( defName.empty() ? getDefaultType() : defName );
297 }
298
299protected:
301 virtual StatusCode retrieve( T*& ) const = 0; // not really const, because it updates m_pObject
302
305 virtual StatusCode release( T* comp ) const { // not really const, because it updates m_pObject
306 // const cast to support T being a const type
307 ::details::nonConst( comp )->release();
308 return StatusCode::SUCCESS;
309 }
310
311private:
314 void assertObject() const { // not really const, because it may update m_pObject
315 if ( !isValid() ) {
316 throw GaudiException( "Failed to retrieve " + componentType() + ": " + typeAndName(),
317 componentType() + " retrieve", StatusCode::FAILURE );
318 }
319 }
320
321private:
322 //
323 // Data members
324 //
325 mutable std::atomic<T*> m_pObject = nullptr;
326};
327
333
335protected:
336 GaudiHandleArrayBase( std::string myComponentType, std::string myParentName )
337 : GaudiHandleInfo( std::move( myComponentType ), std::move( myParentName ) ) {}
338
339public:
341 typedef std::vector<GaudiHandleBase*> BaseHandleArray;
342 typedef std::vector<const GaudiHandleBase*> ConstBaseHandleArray;
343
346 bool setTypesAndNames( const std::vector<std::string>& myTypesAndNamesList );
347
350 const std::vector<std::string> typesAndNames() const;
351
353 const std::vector<std::string> types() const;
354
356 const std::vector<std::string> names() const;
357
360 const std::vector<std::string> getBaseInfos( auto ( GaudiHandleBase::*pMemFunc )() const ) const;
361
365 std::string pythonPropertyClassName() const override;
366
370 std::string pythonRepr() const override;
371
375 virtual bool push_back( const std::string& myHandleTypeAndName ) = 0;
376
378 virtual void clear() = 0;
379
381 virtual bool empty() const = 0;
382
386
390
392 virtual StatusCode retrieve() = 0;
393
395 virtual StatusCode release() = 0;
396
398 virtual bool retrieved() const = 0;
399};
400
402template <class T>
404public:
405 //
406 // public nested types
407 //
408 typedef std::vector<T> HandleVector;
409 typedef typename HandleVector::value_type value_type;
410 typedef typename HandleVector::size_type size_type;
411 typedef typename HandleVector::reference reference;
412 typedef typename HandleVector::const_reference const_reference;
413 typedef typename HandleVector::iterator iterator;
414 typedef typename HandleVector::const_iterator const_iterator;
415 typedef typename HandleVector::reverse_iterator reverse_iterator;
416 typedef typename HandleVector::const_reverse_iterator const_reverse_iterator;
417
418protected:
419 //
420 // Constructors
421 //
426 GaudiHandleArray( const std::vector<std::string>& myTypesAndNamesList, std::string myComponentType,
427 std::string myParentName )
428 : GaudiHandleArrayBase( std::move( myComponentType ), std::move( myParentName ) ) {
429 setTypesAndNames( myTypesAndNamesList );
430 }
431
436 GaudiHandleArray( const std::string& myComponentType, const std::string& myParentName )
437 : GaudiHandleArrayBase( myComponentType, myParentName ) {}
438
439public:
441 GaudiHandleArray& operator=( const std::vector<std::string>& myTypesAndNamesList ) {
442 setTypesAndNames( myTypesAndNamesList );
443 return *this;
444 }
445
448 for ( auto& h : m_handleArray ) baseArray.push_back( &h );
449 return baseArray;
450 }
451
454 for ( auto& h : m_handleArray ) baseArray.push_back( &h );
455 return baseArray;
456 }
457
458 //
459 // Simulate (part of) an std::vector
460 //
461 iterator begin() { return m_handleArray.begin(); }
462
463 iterator end() { return m_handleArray.end(); }
464
465 const_iterator begin() const { return m_handleArray.begin(); }
466
467 const_iterator end() const { return m_handleArray.end(); }
468
469 const_iterator rbegin() const { return m_handleArray.rbegin(); }
470
471 const_iterator rend() const { return m_handleArray.rend(); }
472
473 size_type size() const { return m_handleArray.size(); }
474
475 void clear() override { m_handleArray.clear(); }
476
477 bool empty() const override { return m_handleArray.empty(); }
478
479 T& operator[]( int index ) { return m_handleArray[index]; }
480
481 const T& operator[]( int index ) const { return m_handleArray[index]; }
482
484 T* operator[]( std::string_view name ) {
485 auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
486 return it != end() ? &*it : nullptr;
487 }
488
490 const T* operator[]( std::string_view name ) const {
491 auto it = std::find_if( begin(), end(), [&]( const_reference r ) { return r.name() == name; } );
492 return it != end() ? &*it : nullptr;
493 }
494
497 using GaudiHandleArrayBase::push_back; // avoid compiler warning
498 virtual bool push_back( const T& myHandle ) {
499 m_handleArray.push_back( myHandle );
500 return true;
501 }
502
504 StatusCode retrieve() override {
506 for ( auto& i : *this ) {
507 // stop at first failure
508 if ( i.retrieve().isFailure() ) {
510 break;
511 }
512 }
513 if ( sc ) { m_retrieved = true; }
514 return sc;
515 }
516
518 StatusCode release() override {
520 for ( auto& i : *this ) {
521 // continue trying to release other tools even if we fail...
522 if ( i.release().isFailure() ) sc = StatusCode::FAILURE;
523 }
524 return sc;
525 }
526
528 virtual bool retrieved() const override { return m_retrieved; }
529
530private:
531 //
532 // Private data members
533 //
535 bool m_retrieved{ false };
536};
std::ostream & operator<<(std::ostream &s, AlgsExecutionStates::State x)
Streaming of State values.
#define GAUDI_API
Definition Kernel.h:49
Define general base for Gaudi exception.
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
GaudiHandleArrayBase(std::string myComponentType, std::string myParentName)
std::vector< const GaudiHandleBase * > ConstBaseHandleArray
virtual void clear()=0
Clear the list of handles.
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in <myTypesAndNamesList>.
std::vector< GaudiHandleBase * > BaseHandleArray
std::string pythonRepr() const override
Python representation of array of handles, i.e.
const std::vector< std::string > typesAndNames() const
Return a vector with "type/name" strings of all handles in the array.
const std::vector< std::string > types() const
Return a vector with "type" strings of all handles in the array.
const std::vector< std::string > getBaseInfos(auto(GaudiHandleBase::*pMemFunc)() const) const
Helper function to get a vector of strings filled with the return value of each tool of a member func...
virtual ConstBaseHandleArray getBaseArray() const =0
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
virtual BaseHandleArray getBaseArray()=0
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
virtual StatusCode retrieve()=0
Retrieve all tools.
virtual bool empty() const =0
Return whether the list of tools is empty.
const std::vector< std::string > names() const
Return a vector with "type/name" strings of all handles in the array.
virtual StatusCode release()=0
Release all tools.
GaudiHandleArrayProperty PropertyType
virtual bool retrieved() const =0
To be able to tell if Array was ever retreived.
std::string pythonPropertyClassName() const override
Name of the componentType with "HandleArray" appended.
virtual bool push_back(const T &myHandle)
HandleVector::iterator iterator
T & operator[](int index)
const_iterator end() const
const T * operator[](std::string_view name) const
Get const pointer (!) to ToolHandle by instance name.
void clear() override
Clear the list of handles.
std::vector< T > HandleVector
HandleVector::reverse_iterator reverse_iterator
HandleVector::size_type size_type
size_type size() const
const T & operator[](int index) const
HandleVector m_handleArray
StatusCode release() override
Release all tools.
StatusCode retrieve() override
Retrieve all tools.
const_iterator rend() const
HandleVector::value_type value_type
T * operator[](std::string_view name)
Get pointer (!) to ToolHandle by instance name.
GaudiHandleArrayBase::BaseHandleArray getBaseArray() override
Get a read-write vector of GaudiHandleBase* pointing to the real handles.
iterator begin()
bool empty() const override
Return whether the list of tools is empty.
HandleVector::const_reference const_reference
GaudiHandleArray(const std::string &myComponentType, const std::string &myParentName)
Constructor creating an empty array.
HandleVector::const_reverse_iterator const_reverse_iterator
virtual bool retrieved() const override
has Array been retreived?
const_iterator begin() const
GaudiHandleArrayBase::ConstBaseHandleArray getBaseArray() const override
Get a read-only vector of const GaudiHandleBase* pointing to the real handles.
const_iterator rbegin() const
HandleVector::const_iterator const_iterator
GaudiHandleArray(const std::vector< std::string > &myTypesAndNamesList, std::string myComponentType, std::string myParentName)
Generic constructor.
HandleVector::reference reference
GaudiHandleArray & operator=(const std::vector< std::string > &myTypesAndNamesList)
Set the array of GaudiHandles from typeAndNames given in vector of strings.
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
GaudiHandleBase(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
Create a handle ('smart pointer') to a gaudi component.
GaudiHandleProperty PropertyType
const std::string & typeAndName() const
The full type and name: "type/name".
bool empty() const
Check if the handle has been set to empty string (i.e.
std::string m_typeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
std::string type() const
The concrete component class name: the part before the '/'.
StatusCode release() const
Release the component.
T * get()
Return the wrapped pointer, not calling retrieve() if null.
std::add_const_t< T > & operator*() const
bool isValid() const
Check if the handle is valid (try to retrive the object is not done yet).
T * operator->()
GaudiHandle & operator=(const GaudiHandle< NCT > &other)
Assignment operator for correct ref-counting.
GaudiHandle(const GaudiHandle &other)
Copy constructor needed for correct ref-counting.
StatusCode retrieve() const
Retrieve the component.
GaudiHandle & operator=(const GaudiHandle &other)
Assignment operator for correct ref-counting.
std::add_const_t< T > * get() const
Return the wrapped pointer, not calling retrieve() if null.
void assertObject() const
Load the pointer to the component.
virtual StatusCode release(T *comp) const
Release the component.
GaudiHandle(std::string myTypeAndName, std::string myComponentType, std::string myParentName)
std::string getDefaultName()
virtual StatusCode retrieve(T *&) const =0
Retrieve the component.
std::string getDefaultType()
Helper function to get default type string from the class type.
bool isSet() const
True if the wrapped pointer is not null.
std::add_const_t< T > * operator->() const
GaudiHandle(const GaudiHandle< NCT > &other)
Copy constructor needed for correct ref-counting.
std::atomic< T * > m_pObject
T & operator*()
std::string m_componentType
Definition GaudiHandle.h:90
const std::string & componentType() const
Definition GaudiHandle.h:53
std::string m_propertyName
Definition GaudiHandle.h:91
GaudiHandleInfo(std::string myComponentType, std::string myParentName)
Some basic information and helper functions shared between various handles/arrays.
Definition GaudiHandle.h:41
const std::string & propertyName() const
name as used in declareProperty(name,gaudiHandle)
Definition GaudiHandle.h:56
void setParentName(std::string parent)
The name of the parent.
Definition GaudiHandle.h:84
virtual std::string pythonPropertyClassName() const =0
The python class name for the property in the genconf-generated configurables.
std::string m_parentName
Definition GaudiHandle.h:92
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition GaudiHandle.h:59
const std::string & parentName() const
The name of the parent.
Definition GaudiHandle.h:62
virtual ~GaudiHandleInfo()
virtual destructor so that derived class destructor is called.
Definition GaudiHandle.h:49
void setComponentType(std::string componentType)
The component type.
Definition GaudiHandle.h:81
virtual std::string pythonRepr() const =0
Python representation of handle, i.e.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition System.cpp:260
std::remove_const_t< T > * nonConst(T *p)
Cast a pointer to a non const type.
Definition GaudiHandle.h:26
STL namespace.