The Gaudi Framework  master (37c0b60a)
PropertyMgr.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 \***********************************************************************************/
11 // hide deprecation warnings here... the whole file is deprecated
12 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD& STL
17 // ============================================================================
18 #include <algorithm>
19 #include <functional>
20 #include <iostream>
21 #include <sstream>
22 #include <stdexcept>
23 #include <string>
24 // ============================================================================
25 // GaudiKernel
26 // ============================================================================
27 #include <GaudiKernel/Bootstrap.h>
33 // ============================================================================
34 
36 
37 namespace {
38  // ==========================================================================
40  constexpr struct NoCaseCmp_t {
41  bool operator()( std::string_view v1, std::string_view v2 ) const {
42  return std::equal( begin( v1 ), end( v1 ), begin( v2 ), end( v2 ),
43  []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
44  }
45  } noCaseCmp{};
46  // ==========================================================================
47 } // namespace
48 // ====================================================================
49 // constructor from the interface
50 // ====================================================================
51 PropertyMgr::PropertyMgr( IInterface* iface ) : m_pOuter( iface ) {
52  addRef(); // initial reference count set to 1
53 }
54 // ====================================================================
55 // Declare a remote property
56 // ====================================================================
58  if ( !rsvc ) return nullptr;
59  const std::string& nam = rname.empty() ? name : rname;
60  PropertyBase* p = property( nam, rsvc->getProperties() );
62  return p;
63 }
64 // ============================================================================
65 // Declare a property
66 // ============================================================================
69  auto p = m_todelete.emplace_back( std::make_unique<typename GaudiHandleBase::PropertyType>( name, ref ) ).get();
70  //
71  p->setDocumentation( doc );
73  //
74  return p;
75 }
76 // ============================================================================
78  const std::string& doc ) {
80  auto p = m_todelete.emplace_back( std::make_unique<typename GaudiHandleArrayBase::PropertyType>( name, ref ) ).get();
81  //
82  p->setDocumentation( doc );
84  //
85  return p;
86 }
87 
88 // ============================================================================
91  auto p = m_todelete.emplace_back( std::make_unique<typename Gaudi::DataHandle::PropertyType>( name, ref ) ).get();
92  //
93  p->setDocumentation( doc );
95  //
96  return p;
97 }
98 // ====================================================================
99 // get the property by name form the proposed list
100 // ====================================================================
102  auto it = std::find_if( props.begin(), props.end(),
103  [=]( const PropertyBase* p ) { return p && noCaseCmp( p->name(), name ); } );
104  return ( it != props.end() ) ? *it : nullptr; // RETURN
105 }
106 // ====================================================================
107 // retrieve the property by name
108 // ====================================================================
109 PropertyBase* PropertyMgr::property( std::string_view name ) const {
110  // local property ?
112  if ( lp ) return lp; // RETURN
113  // look for remote property
114  for ( const auto& it : m_remoteProperties ) {
115  if ( !noCaseCmp( it.first, name ) ) continue; // CONTINUE
116  const IProperty* p = it.second.first;
117  if ( !p ) continue;
118  return property( it.second.second, p->getProperties() ); // RETURN
119  }
120  return nullptr; // RETURN
121 }
122 // ====================================================================
123 /* set a property from another property
124  * Implementation of IProperty::setProperty
125  */
126 // =====================================================================
129  try {
130  if ( pp && pp->assign( p ) ) return StatusCode::SUCCESS;
131  } // RETURN
132  catch ( ... ) {}
133  //
134  return StatusCode::FAILURE;
135 }
136 // ====================================================================
137 /* set a property from the stream
138  * Implementation of IProperty::setProperty
139  */
140 // =====================================================================
142  std::string name, value;
143  StatusCode sc = Gaudi::Parsers::parse( name, value, i );
144  return sc.isFailure() ? sc : setPropertyRepr( name, value );
145 }
146 // =====================================================================
147 /* set a property from the string
148  * Implementation of IProperty::setProperty
149  */
150 // =====================================================================
152  PropertyBase* p = property( n );
153  return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
154 }
155 // =====================================================================
156 /* Retrieve the value of a property
157  * Implementation of IProperty::getProperty
158  */
159 // =====================================================================
161  try {
162  const PropertyBase* pp = property( p->name() );
163  if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS; // RETURN
164  } catch ( ... ) {}
165  return StatusCode::FAILURE; // RETURN
166 }
167 // =====================================================================
168 /* Get the property by name
169  * Implementation of IProperty::getProperty
170  */
171 // =====================================================================
172 const PropertyBase& PropertyMgr::getProperty( std::string_view name ) const {
173  const PropertyBase* p = property( name );
174  if ( !p ) throw std::out_of_range( "Property " + std::string{ name } + " not found." ); // Not found
175  return *p; // RETURN
176 }
177 // =====================================================================
178 /* Get the property by name
179  * Implementation of IProperty::getProperty
180  */
181 // =====================================================================
182 StatusCode PropertyMgr::getProperty( std::string_view n, std::string& v ) const {
183  // get the property
184  const PropertyBase* p = property( n );
185  if ( !p ) return StatusCode::FAILURE;
186  // convert the value into the string
187  v = p->toString();
188  //
189  return StatusCode::SUCCESS;
190 }
191 // =====================================================================
192 // Implementation of IProperty::getProperties
193 // =====================================================================
195 // =====================================================================
196 // Implementation of IInterface::queryInterface
197 // =====================================================================
198 StatusCode PropertyMgr::queryInterface( const InterfaceID& iid, void** pinterface ) {
199  // try local interfaces
200  StatusCode sc = base_class::queryInterface( iid, pinterface );
201  if ( sc.isSuccess() ) return sc;
202  // fall back on the owner
203  return m_pOuter ? m_pOuter->queryInterface( iid, pinterface ) : sc; // FAILURE
204 }
205 // =====================================================================
206 // Implementation of IProperty::hasProperty
207 // =====================================================================
208 bool PropertyMgr::hasProperty( std::string_view name ) const {
209  return any_of( begin( m_properties ), end( m_properties ),
210  [name]( const PropertyBase* prop ) { return noCaseCmp( prop->name(), name ); } );
211 }
213  if ( !hasProperty( name ) ) return;
214  auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
215  if ( !msgSvc ) std::cerr << "error: cannot get MessageSvc!" << std::endl;
216  auto owner = SmartIF<INamedInterface>( m_pOuter );
217  MsgStream log( msgSvc, owner ? owner->name() : "PropertyMgr" );
218  log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
219  << endmsg;
220 }
221 // =====================================================================
222 // The END
223 // =====================================================================
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
implements::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: implements.h:30
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: PropertyBase.h:39
toupper
void toupper(std::string &s)
Definition: ExceptionSvc.cpp:36
std::string
STL class.
std::equal
T equal(T... args)
IMessageSvc
Definition: IMessageSvc.h:47
Gaudi.Configuration.log
log
Definition: Configuration.py:28
PropertyMgr::property
Gaudi::Details::PropertyBase * property(std::string_view name) const
Definition: PropertyMgr.cpp:109
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
GaudiPartProp.tests.v1
v1
Definition: tests.py:39
PropertyMgr::getProperty
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
Definition: PropertyMgr.cpp:160
GaudiException.h
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:21
std::vector
STL class.
std::find_if
T find_if(T... args)
check_ParticleID.props
props
Definition: check_ParticleID.py:21
PropertyMgr::m_pOuter
IInterface * m_pOuter
Interface hub reference (ApplicationMgr)
Definition: PropertyMgr.h:153
Gaudi::DataHandle
Definition: DataHandle.h:38
std::unique_ptr::get
T get(T... args)
PropertyMgr::PropertyMgr
PropertyMgr(IInterface *iface=nullptr)
constructor from the interface
Definition: PropertyMgr.cpp:51
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
PropertyMgr::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Definition: PropertyMgr.h:159
IInterface::queryInterface
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
Gaudi::Details::PropertyBase::fromString
virtual StatusCode fromString(const std::string &value)=0
string -> value
IMessageSvc.h
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
GaudiHandleBase
Definition: GaudiHandle.h:105
std::vector::push_back
T push_back(T... args)
IProperty
Definition: IProperty.h:33
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
INamedInterface.h
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: PropertyBase.h:91
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
PropertyMgr::assertUniqueName
void assertUniqueName(const std::string &name) const
Throw an exception if the name is already present in the list of properties (see GAUDI-1023).
Definition: PropertyMgr.cpp:212
StatusCode
Definition: StatusCode.h:65
PropertyMgr::hasProperty
bool hasProperty(std::string_view name) const override
Return true if we have a property with the given name.
Definition: PropertyMgr.cpp:208
PropertyMgr::m_todelete
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:151
std::cerr
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:98
PropertyMgr::setPropertyRepr
StatusCode setPropertyRepr(const std::string &n, const std::string &v) override
set the property from name and the value
Definition: PropertyMgr.cpp:151
SmartIF
Definition: IConverter.h:25
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
MsgStream
Definition: MsgStream.h:33
GaudiHandleArrayBase
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:348
implements< IProperty >::addRef
unsigned long addRef() override
Reference Interface instance
Definition: implements.h:48
cpluginsvc.n
n
Definition: cpluginsvc.py:234
PropertyMgr.h
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
std::vector::emplace_back
T emplace_back(T... args)
PropertyMgr::declareRemoteProperty
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyMgr.cpp:57
PropertyMgr::m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:149
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std::endl
T endl(T... args)
IProperty::getProperties
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
PropertyMgr::m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:147
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
AlgSequencer.c1
c1
Definition: AlgSequencer.py:32
IInterface
Definition: IInterface.h:239
Bootstrap.h
std::string::empty
T empty(T... args)
Properties.v
v
Definition: Properties.py:122
std::out_of_range
STL class.
AlgSequencer.c2
c2
Definition: AlgSequencer.py:33
std::make_pair
T make_pair(T... args)
InterfaceID
Definition: IInterface.h:39
IOTest.end
end
Definition: IOTest.py:125
compareRootHistos.ref
ref
Definition: compareRootHistos.py:27
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:507
PropertyMgr::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyMgr.cpp:194
PropertyMgr::setProperty
StatusCode setProperty(const std::string &name, const Gaudi::Details::PropertyBase &p) override
set the property form another property
Definition: PropertyMgr.cpp:127
PropertyMgr::queryInterface
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
Definition: PropertyMgr.cpp:198