The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
GaudiHandle.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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\***********************************************************************************/
12#include <iostream>
13#include <string>
14
15//
16// GaudiHandleBase implementation
17//
18
19void GaudiHandleBase::setTypeAndName( std::string myTypeAndName ) { m_typeAndName = std::move( myTypeAndName ); }
20
21std::string GaudiHandleBase::type() const {
22 std::string::size_type slash = m_typeAndName.find( '/' );
23 if ( slash != std::string::npos ) {
24 // return only part before /
25 return m_typeAndName.substr( 0, slash );
26 } else {
27 // return full string
28 return m_typeAndName;
29 }
30}
31
32std::string GaudiHandleBase::name() const {
33 auto slash = m_typeAndName.find( '/' );
34 if ( slash == std::string::npos ) {
35 // only type is given, or string is empty.
36 // return default name (=type or empty, in this case full string)
37 return m_typeAndName;
38 } else if ( slash != m_typeAndName.length() - 1 ) {
39 // an explicit name is given, so return it
40 return m_typeAndName.substr( slash + 1 );
41 } else {
42 // ends with /, i.e. explicit empty name.
43 // Should probably never happen.
44 return "";
45 }
46}
47
48void GaudiHandleBase::setName( std::string_view myName ) { m_typeAndName = type().append( "/" ).append( myName ); }
49
50std::string GaudiHandleBase::pythonPropertyClassName() const { return componentType() + "Handle"; }
51
52std::string GaudiHandleBase::messageName() const {
53 std::string propName = propertyName();
54 if ( propName.empty() ) { propName = pythonPropertyClassName() + "('" + m_typeAndName + "')"; }
55 return parentName() + "." + propName;
56}
57
58std::string GaudiHandleBase::pythonRepr() const { return pythonPropertyClassName() + "('" + m_typeAndName + "')"; }
59
60//
61// GaudiHandleArrayBase implementation
62//
63bool GaudiHandleArrayBase::setTypesAndNames( const std::vector<std::string>& myTypesAndNamesList ) {
64 clear();
65 for ( const auto& it : myTypesAndNamesList ) {
66 if ( !push_back( it ) ) return false;
67 }
68 return true;
69}
70
71const std::vector<std::string> GaudiHandleArrayBase::getBaseInfos( auto ( GaudiHandleBase::*pMemFunc )() const ) const {
72 std::vector<std::string> theList;
73 for ( const auto& it : getBaseArray() ) theList.push_back( ( it->*pMemFunc )() );
74 return theList;
75}
76
77const std::vector<std::string> GaudiHandleArrayBase::typesAndNames() const {
79}
80
81const std::vector<std::string> GaudiHandleArrayBase::types() const { return getBaseInfos( &GaudiHandleBase::type ); }
82
83const std::vector<std::string> GaudiHandleArrayBase::names() const { return getBaseInfos( &GaudiHandleBase::name ); }
84
85std::string GaudiHandleArrayBase::pythonPropertyClassName() const { return componentType() + "HandleArray"; }
86
88 std::string repr = pythonPropertyClassName() + "([";
89 auto theList = typesAndNames();
90 auto first = theList.begin();
91 auto last = theList.end();
92 if ( first != last ) {
93 repr += "'" + *first + "'";
94 ++first;
95 }
96 for ( ; first != last; ++first ) repr += ",'" + *first + "'";
97 repr += "])";
98 return repr;
99}
100
101//
102// Public functions
103//
104std::ostream& operator<<( std::ostream& os, const GaudiHandleInfo& handle ) {
105 std::string msg;
106 const auto& propName = handle.propertyName();
107 if ( !propName.empty() ) msg += propName + " = ";
108 msg += handle.pythonRepr();
109 os << msg;
110 return os;
111}
std::ostream & operator<<(std::ostream &os, const GaudiHandleInfo &handle)
virtual bool push_back(const std::string &myHandleTypeAndName)=0
Add a handle to the array with "type/name" given in <myHandleTypeAndName>.
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::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.
const std::vector< std::string > names() const
Return a vector with "type/name" strings of all handles in the array.
std::string pythonPropertyClassName() const override
Name of the componentType with "HandleArray" appended.
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
std::string name() const
The instance name: the part after the '/'.
std::string messageName() const
name used for printing messages
std::string pythonRepr() const override
Python representation of handle, i.e.
void setName(std::string_view myName)
Set the instance name (part after the '/') without changing the class type.
const std::string & typeAndName() const
The full type and name: "type/name".
std::string pythonPropertyClassName() const override
Name of the componentType with "Handle" appended.
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 '/'.
const std::string & componentType() const
Definition GaudiHandle.h:53
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
const std::string & parentName() const
The name of the parent.
Definition GaudiHandle.h:62
virtual std::string pythonRepr() const =0
Python representation of handle, i.e.