The Gaudi Framework  master (37c0b60a)
PropertyHolder.h
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 #ifndef GAUDIKERNEL_PROPERTYHOLDER_H
12 #define GAUDIKERNEL_PROPERTYHOLDER_H
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <algorithm>
19 #include <functional>
20 #include <iostream>
21 #include <stdexcept>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 // ============================================================================
26 // GaudiKernel
27 // ============================================================================
28 #include <Gaudi/Property.h>
29 #include <GaudiKernel/Bootstrap.h>
32 #include <GaudiKernel/IProperty.h>
34 #include <GaudiKernel/MsgStream.h>
35 #include <GaudiKernel/detected.h>
36 
38 // ============================================================================
39 namespace Gaudi {
40  namespace Details {
41 
42  template <typename T>
44 
45  template <typename TYPE, typename VERIFIER, typename HANDLERS>
46  struct is_gaudi_property<Gaudi::Property<TYPE, VERIFIER, HANDLERS>> : std::true_type {};
47 
48  template <typename T>
50  template <typename T>
52  } // namespace Details
53  namespace Utils {
55  inline bool iequal( std::string_view v1, std::string_view v2 ) {
56  return std::equal( begin( v1 ), end( v1 ), begin( v2 ), end( v2 ),
57  []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
58  }
59  } // namespace Utils
60 } // namespace Gaudi
81 template <class BASE>
82 class GAUDI_API PropertyHolder : public BASE {
83  static_assert( std::is_base_of_v<IProperty, BASE> && std::is_base_of_v<INamedInterface, BASE>,
84  "PropertyHolder template argument must inherit from IProperty and INamedInterface" );
85 
86 public:
94 
95  PropertyHolder() = default;
96 
99  PropertyHolder( const PropertyHolder& ) = delete;
102 
103 public:
107  assertUniqueName( prop.name() );
108  m_properties.push_back( &prop );
109  return prop;
110  }
111 
114  template <typename TYPE, typename = std::enable_if_t<!Gaudi::Details::is_gaudi_property<TYPE>::value>>
116  const std::string& doc = "none" ) {
117  m_todelete.push_back( std::make_unique<Gaudi::Details::PropertyType<TYPE&>>( name, value ) );
118  Gaudi::Details::PropertyBase* p = m_todelete.back().get();
119 
120  p->setDocumentation( doc );
121  return &declareProperty( *p );
122  }
123 
126  template <class TYPE, class VERIFIER, class HANDLERS>
129  const std::string& doc = "none" ) {
130  Gaudi::Details::PropertyBase* p = &prop;
131  p->setName( name );
132 
133  p->setDocumentation( doc );
134  return &declareProperty( *p );
135  }
136 
140  const std::string& rname = "" ) {
141  if ( !rsvc ) return nullptr;
142  const std::string& nam = rname.empty() ? name : rname;
143  Gaudi::Details::PropertyBase* p = property( nam, rsvc->getProperties() );
144  m_remoteProperties.emplace_back( RemProperty{ name, rsvc, nam } );
145  return p;
146  }
147 
149 
150  // ==========================================================================
151  // IProperty implementation
152  // ==========================================================================
158  Gaudi::Details::PropertyBase* pp = property( name );
159  if ( pp && pp->assign( p ) ) return StatusCode::SUCCESS;
160  return StatusCode::FAILURE;
161  }
162  // ==========================================================================
166  StatusCode setProperty( const std::string& s ) override {
168  std::string value;
169  StatusCode sc = Gaudi::Parsers::parse( name, value, s );
170  if ( sc.isFailure() ) return sc;
171  return setPropertyRepr( name, value );
172  }
173  // ==========================================================================
177  StatusCode setPropertyRepr( const std::string& n, const std::string& r ) override {
178  try {
179  Gaudi::Details::PropertyBase* p = property( n );
181  return ( p && p->fromString( r ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
182  } catch ( const std::invalid_argument& err ) {
183  throw GaudiException{ "error setting property " + n, this->name(), StatusCode::FAILURE, err };
184  }
185  }
186  // ==========================================================================
191  try {
192  const Gaudi::Details::PropertyBase* pp = property( p->name() );
193  if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS;
194  } catch ( ... ) {}
195  return StatusCode::FAILURE;
196  }
197  // ==========================================================================
201  const Gaudi::Details::PropertyBase& getProperty( std::string_view name ) const override {
202  const Gaudi::Details::PropertyBase* p = property( name );
203  if ( !p ) throw std::out_of_range( "Property " + std::string{ name } + " not found." );
204  return *p;
205  }
206  // ==========================================================================
210  StatusCode getProperty( std::string_view n, std::string& v ) const override {
211  // get the property
212  const Gaudi::Details::PropertyBase* p = property( n );
213  if ( !p ) return StatusCode::FAILURE;
214  // convert the value into the string
215  v = p->toString();
216  return StatusCode::SUCCESS;
217  }
218  // ==========================================================================
222  const std::vector<Gaudi::Details::PropertyBase*>& getProperties() const override { return m_properties; }
223  // ==========================================================================
227  bool hasProperty( std::string_view name ) const override {
228  return any_of( begin( m_properties ), end( m_properties ),
229  [&name]( const Gaudi::Details::PropertyBase* prop ) {
230  return Gaudi::Utils::iequal( prop->name(), name );
231  } ) ||
232  any_of( begin( m_remoteProperties ), end( m_remoteProperties ),
233  [&name]( const auto& prop ) { return Gaudi::Utils::iequal( prop.name, name ); } );
234  }
235  // ==========================================================================
237  // get local or remote property by name
238  Gaudi::Details::PropertyBase* property( std::string_view name ) const {
239  // local property ?
240  Gaudi::Details::PropertyBase* lp = property( name, m_properties );
241  if ( lp ) return lp;
242  // look for remote property
243  for ( const auto& it : m_remoteProperties ) {
244  if ( !Gaudi::Utils::iequal( it.name, name ) ) continue;
245  const IProperty* p = it.owner;
246  if ( !p ) continue;
247  return property( it.remName, p->getProperties() );
248  }
249  return nullptr; // RETURN
250  }
251 
253  auto set_prop = [&optsSvc, this]( auto prop ) { optsSvc.bind( this->name(), prop ); };
254  std::for_each( begin( m_properties ), end( m_properties ), set_prop );
255  std::for_each( begin( m_remoteProperties ), end( m_remoteProperties ), [&set_prop, this]( auto& rem ) {
256  if ( rem.owner ) set_prop( this->property( rem.remName, rem.owner->getProperties() ) );
257  } );
258  }
259 
260 private:
264  auto it = std::find_if( props.begin(), props.end(), [name]( Gaudi::Details::PropertyBase* p ) {
265  return p && Gaudi::Utils::iequal( p->name(), name );
266  } );
267  return ( it != props.end() ) ? *it : nullptr; // RETURN
268  }
269 
272  void assertUniqueName( std::string_view name ) const {
273  if ( hasProperty( name ) ) {
274  auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
275  if ( !msgSvc ) std::cerr << "error: cannot get MessageSvc!" << std::endl;
276  MsgStream log( msgSvc, this->name() );
277  log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
278  << endmsg;
279  }
280  }
281 
283  struct RemProperty {
285  IProperty* owner = nullptr;
287  };
289 
296 };
297 #endif
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: PropertyBase.h:39
std::for_each
T for_each(T... args)
toupper
void toupper(std::string &s)
Definition: ExceptionSvc.cpp:36
std::false_type
std::string
STL class.
std::equal
T equal(T... args)
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, Gaudi::Property< TYPE, VERIFIER, HANDLERS > &prop, const std::string &doc="none")
Declare a PropertyBase instance setting name and documentation.
Definition: PropertyHolder.h:127
IMessageSvc
Definition: IMessageSvc.h:47
Gaudi.Configuration.log
log
Definition: Configuration.py:28
GaudiPartProp.tests.v1
v1
Definition: tests.py:39
DataHandleProperty.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
gaudirun.s
string s
Definition: gaudirun.py:346
PropertyHolder::RemProperty
Definition: PropertyHolder.h:283
std::vector< Gaudi::Details::PropertyBase * >
std::find_if
T find_if(T... args)
check_ParticleID.props
props
Definition: check_ParticleID.py:21
GaudiException
Definition: GaudiException.h:31
PropertyHolder::setProperty
StatusCode setProperty(const std::string &name, const Gaudi::Details::PropertyBase &p) override
set the property from another property with a different name
Definition: PropertyHolder.h:157
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
PropertyHolder::getProperty
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
Definition: PropertyHolder.h:190
Gaudi::Details::PropertyType_t
typename std::remove_reference_t< T >::PropertyType PropertyType_t
Definition: PropertyHolder.h:49
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:82
Gaudi::Details::PropertyBase::fromString
virtual StatusCode fromString(const std::string &value)=0
string -> value
PropertyHolder::assertUniqueName
void assertUniqueName(std::string_view name) const
Issue a runtime warning if the name is already present in the list of properties (see GAUDI-1023).
Definition: PropertyHolder.h:272
PropertyHolder::m_todelete
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties owned by PropertyHolder, to be deleted.
Definition: PropertyHolder.h:295
PropertyHolder::RemProperty::name
std::string name
Definition: PropertyHolder.h:284
PropertyHolder::declareRemoteProperty
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyHolder.h:139
PropertyHolder::bindPropertiesTo
void bindPropertiesTo(Gaudi::Interfaces::IOptionsSvc &optsSvc)
Definition: PropertyHolder.h:252
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
Gaudi::Details::PropertyType
Gaudi::cpp17::detected_or_t< Gaudi::Property< T >, PropertyType_t, T > PropertyType
Definition: PropertyHolder.h:51
PropertyHolder::property
Gaudi::Details::PropertyBase * property(std::string_view name) const
\fixme property and bindPropertiesTo should be protected
Definition: PropertyHolder.h:238
Properties
Definition: Properties.py:1
PropertyHolder::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyHolder.h:222
PropertyHolder::property
Gaudi::Details::PropertyBase * property(std::string_view name, const std::vector< Gaudi::Details::PropertyBase * > &props) const
get the property by name form the proposed list
Definition: PropertyHolder.h:262
IProperty
Definition: IProperty.h:33
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
Gaudi::Interfaces::IOptionsSvc::bind
virtual void bind(const std::string &prefix, Gaudi::Details::PropertyBase *property)=0
Register a Gaudi::Property instance to the option service.
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
StatusCode
Definition: StatusCode.h:65
PropertyHolder::setProperty
StatusCode setProperty(const std::string &s) override
set the property from the formatted string
Definition: PropertyHolder.h:166
std::cerr
PropertyHolder::setPropertyRepr
StatusCode setPropertyRepr(const std::string &n, const std::string &r) override
set the property from name and value string representation
Definition: PropertyHolder.h:177
Gaudi::cpp17::detected_or_t
typename details::detector< Default, void, Op, Args... >::type detected_or_t
Definition: detected.h:50
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
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Helper to wrap a regular data member and use it as a regular property.
Definition: PropertyHolder.h:115
Gaudi::Utils::iequal
bool iequal(std::string_view v1, std::string_view v2)
Helper for case insensitive string comparison.
Definition: PropertyHolder.h:55
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:139
PropertyHolder::PropertyHolder
PropertyHolder(const PropertyHolder &)=delete
std::invalid_argument
STL class.
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
Gaudi::Details::is_gaudi_property
Definition: PropertyHolder.h:43
MsgStream
Definition: MsgStream.h:33
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
cpluginsvc.n
n
Definition: cpluginsvc.py:234
PropertyHolder::PropertyHolder
PropertyHolder()=default
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Definition: PropertyHolder.h:106
PropertyHolder::m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyHolder.h:291
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.
PropertyHolder::getProperty
const Gaudi::Details::PropertyBase & getProperty(std::string_view name) const override
get the property by name
Definition: PropertyHolder.h:201
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
AlgSequencer.c1
c1
Definition: AlgSequencer.py:32
detected.h
Bootstrap.h
std::string::empty
T empty(T... args)
PropertyHolder::Properties
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyHolder.h:282
Properties.v
v
Definition: Properties.py:122
std::out_of_range
STL class.
AlgSequencer.c2
c2
Definition: AlgSequencer.py:33
Gaudi::Details::PropertyBase::setName
void setName(std::string value)
set the new value for the property name
Definition: PropertyBase.h:89
PropertyHolder::RemoteProperties
std::vector< RemProperty > RemoteProperties
Definition: PropertyHolder.h:288
PropertyHolder::getProperty
StatusCode getProperty(std::string_view n, std::string &v) const override
convert the property to the string
Definition: PropertyHolder.h:210
PropertyHolder::RemProperty::remName
std::string remName
Definition: PropertyHolder.h:286
IProperty.h
IOTest.end
end
Definition: IOTest.py:125
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:507
Gaudi::Interfaces::IOptionsSvc
Interface for a component that manages application configuration options.
Definition: IOptionsSvc.h:46
IOptionsSvc.h
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:37
PropertyHolder::operator=
PropertyHolder & operator=(const PropertyHolder &)=delete
Gaudi::Utils::hasProperty
GAUDI_API bool hasProperty(const IProperty *p, std::string_view name)
simple function which check the existence of the property with the given name.
Definition: Property.cpp:106
Property.h
MsgStream.h
PropertyHolder::m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyHolder.h:293
PropertyHolder::hasProperty
bool hasProperty(std::string_view name) const override
Return true if we have a property with the given name.
Definition: PropertyHolder.h:227
IProperty::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p)
Set the property from a property.
Definition: IProperty.h:39