| The Gaudi Framework v32r1 (f65d50dc)

PropertyHolder.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_PROPERTYHOLDER_H
2 #define GAUDIKERNEL_PROPERTYHOLDER_H
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <algorithm>
9 #include <functional>
10 #include <iostream>
11 #include <stdexcept>
12 #include <string>
13 #include <utility>
14 #include <vector>
15 // ============================================================================
16 // GaudiKernel
17 // ============================================================================
18 #include "GaudiKernel/Bootstrap.h"
19 #include "GaudiKernel/DataObjectHandleProperty.h"
20 #include "GaudiKernel/INamedInterface.h"
21 #include "GaudiKernel/IProperty.h"
22 #include "GaudiKernel/ISvcLocator.h"
23 #include "GaudiKernel/MsgStream.h"
24 #include "GaudiKernel/Property.h"
25 #include "GaudiKernel/detected.h"
26
27 // ============================================================================
28 namespace Gaudi {
29 namespace Details {
30
31 template <typename T>
32 struct is_gaudi_property : std::false_type {};
33
34 template <typename TYPE, typename VERIFIER, typename HANDLERS>
35 struct is_gaudi_property<Gaudi::Property<TYPE, VERIFIER, HANDLERS>> : std::true_type {};
36
37 template <typename T>
38 using PropertyType_t = typename std::remove_reference_t<T>::PropertyType;
39 template <typename T>
40 using PropertyType = Gaudi::cpp17::detected_or_t<Gaudi::Property<T>, PropertyType_t, T>;
41 } // namespace Details
42 namespace Utils {
44 inline bool iequal( const std::string& v1, const std::string& v2 ) {
45 return v1.size() == v2.size() && std::equal( std::begin( v1 ), std::end( v1 ), std::begin( v2 ),
46 []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
47 }
48 } // namespace Utils
49 } // namespace Gaudi
70 template <class BASE>
71 class GAUDI_API PropertyHolder : public BASE {
72 static_assert( std::is_base_of_v<IProperty, BASE> && std::is_base_of_v<INamedInterface, BASE>,
73 "PropertyHolder template argument must inherit from IProperty and INamedInterface" );
74
75 public:
82 using PropertyHolderImpl = PropertyHolder<BASE>;
83
84 PropertyHolder() = default;
85
88 PropertyHolder( const PropertyHolder& ) = delete;
89 PropertyHolder& operator=( const PropertyHolder& ) = delete;
91
92 public:
95 inline Gaudi::Details::PropertyBase& declareProperty( Gaudi::Details::PropertyBase& prop ) {
96 assertUniqueName( prop.name() );
97 m_properties.push_back( &prop );
98 return prop;
99 }
100
103 template <typename TYPE, typename = std::enable_if_t<!Gaudi::Details::is_gaudi_property<TYPE>::value>>
104 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, TYPE& value,
105 const std::string& doc = "none" ) {
106 m_todelete.push_back( std::make_unique<Gaudi::Details::PropertyType<TYPE&>>( name, value ) );
107 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
108
109 p->setDocumentation( doc );
110 return &declareProperty( *p );
111 }
112
115 template <class TYPE, class VERIFIER, class HANDLERS>
116 Gaudi::Details::PropertyBase* declareProperty( const std::string& name,
117 Gaudi::Property<TYPE, VERIFIER, HANDLERS>& prop,
118 const std::string& doc = "none" ) {
119 Gaudi::Details::PropertyBase* p = &prop;
120 p->setName( name );
121
122 p->setDocumentation( doc );
123 return &declareProperty( *p );
124 }
125
128 Gaudi::Details::PropertyBase* declareRemoteProperty( const std::string& name, IProperty* rsvc,
129 const std::string& rname = "" ) {
130 if ( !rsvc ) return nullptr;
131 const std::string& nam = rname.empty() ? name : rname;
132 Gaudi::Details::PropertyBase* p = property( nam, rsvc->getProperties() );
133 m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
134 return p;
135 }
136
138
139 // ==========================================================================
140 // IProperty implementation
141 // ==========================================================================
145 StatusCode setProperty( const Gaudi::Details::PropertyBase& p ) override {
146 Gaudi::Details::PropertyBase* pp = property( p.name() );
147 try {
148 if ( pp && pp->assign( p ) ) return StatusCode::SUCCESS;
149 } catch ( ... ) {}
150 return StatusCode::FAILURE;
151 }
152 // ==========================================================================
156 StatusCode setProperty( const std::string& s ) override {
157 std::string name;
158 std::string value;
159 StatusCode sc = Gaudi::Parsers::parse( name, value, s );
160 if ( sc.isFailure() ) return sc;
161 return setProperty( name, value );
162 }
163 // ==========================================================================
167 StatusCode setProperty( const std::string& n, const std::string& v ) override {
168 Gaudi::Details::PropertyBase* p = property( n );
169 return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
170 }
211 template <class TYPE>
212 StatusCode setProperty( const std::string& name, const TYPE& value ) {
213 return Gaudi::Utils::setProperty( this, name, value );
214 }
215 // ==========================================================================
219 StatusCode getProperty( Gaudi::Details::PropertyBase* p ) const override {
220 try {
221 const Gaudi::Details::PropertyBase* pp = property( p->name() );
222 if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS;
223 } catch ( ... ) {}
224 return StatusCode::FAILURE;
225 }
226 // ==========================================================================
230 const Gaudi::Details::PropertyBase& getProperty( const std::string& name ) const override {
231 const Gaudi::Details::PropertyBase* p = property( name );
232 if ( !p ) throw std::out_of_range( "Property " + name + " not found." );
233 return *p;
234 }
235 // ==========================================================================
239 StatusCode getProperty( const std::string& n, std::string& v ) const override {
240 // get the property
241 const Gaudi::Details::PropertyBase* p = property( n );
242 if ( !p ) return StatusCode::FAILURE;
243 // convert the value into the string
244 v = p->toString();
245 return StatusCode::SUCCESS;
246 }
247 // ==========================================================================
251 const std::vector<Gaudi::Details::PropertyBase*>& getProperties() const override { return m_properties; }
252 // ==========================================================================
256 bool hasProperty( const std::string& name ) const override {
257 return any_of( begin( m_properties ), end( m_properties ), [&name]( const Gaudi::Details::PropertyBase* prop ) {
258 return Gaudi::Utils::iequal( prop->name(), name );
259 } );
260 }
261 // ==========================================================================
262 protected:
263 // get local or remote property by name
264 Gaudi::Details::PropertyBase* property( const std::string& name ) const {
265 // local property ?
266 Gaudi::Details::PropertyBase* lp = property( name, m_properties );
267 if ( lp ) return lp;
268 // look for remote property
269 for ( const auto& it : m_remoteProperties ) {
270 if ( !Gaudi::Utils::iequal( it.first, name ) ) continue;
271 const IProperty* p = it.second.first;
272 if ( !p ) continue;
273 return property( it.second.second, p->getProperties() );
274 }
275 return nullptr; // RETURN
276 }
277
278 private:
280 Gaudi::Details::PropertyBase* property( const std::string& name,
281 const std::vector<Gaudi::Details::PropertyBase*>& props ) const {
282 auto it = std::find_if( props.begin(), props.end(), [&name]( Gaudi::Details::PropertyBase* p ) {
283 return p && Gaudi::Utils::iequal( p->name(), name );
284 } );
285 return ( it != props.end() ) ? *it : nullptr; // RETURN
286 }
287
290 void assertUniqueName( const std::string& name ) const {
291 if ( UNLIKELY( hasProperty( name ) ) ) {
292 auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
293 if ( !msgSvc ) std::cerr << "error: cannot get MessageSvc!" << std::endl;
294 MsgStream log( msgSvc, this->name() );
295 log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
296 << endmsg;
297 }
298 }
299
300 typedef std::vector<Gaudi::Details::PropertyBase*> Properties;
301 typedef std::pair<std::string, std::pair<IProperty*, std::string>> RemProperty;
302 typedef std::vector<RemProperty> RemoteProperties;
303
305 Properties m_properties;
307 RemoteProperties m_remoteProperties;
309 std::vector<std::unique_ptr<Gaudi::Details::PropertyBase>> m_todelete;
310 };
311 #endif
Gaudi::Utils::setProperty
StatusCode setProperty(IProperty *component, const std::string &name, const TYPE &value, const std::string &doc)
simple function to set the property of the given object from the value
Definition: Property.h:1179
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
std::out_of_range
STL class.
detected.h
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
std::string::empty
T empty(T... args)
PropertyHolder::property
Gaudi::Details::PropertyBase * property(const std::string &name, const std::vector< Gaudi::Details::PropertyBase * > &props) const
get the property by name form the proposed list
Definition: PropertyHolder.h:280
DataObjectHandleProperty.h
PropertyHolder::property
Gaudi::Details::PropertyBase * property(const std::string &name) const
Definition: PropertyHolder.h:264
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:86
PropertyHolder::setProperty
StatusCode setProperty(const std::string &name, const TYPE &value)
set the property form the value
Definition: PropertyHolder.h:212
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:352
PropertyHolder::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
Definition: PropertyHolder.h:145
MsgStream.h
PropertyHolder::m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyHolder.h:305
Gaudi::Utils::hasProperty
GAUDI_API bool hasProperty(const IProperty *p, const std::string &name)
simple function which check the existence of the property with the given name.
Definition: Property.cpp:95
Gaudi::Details::PropertyType
Gaudi::cpp17::detected_or_t< Gaudi::Property< T >, PropertyType_t, T > PropertyType
Definition: PropertyHolder.h:40
std::endl
T endl(T... args)
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Bootstrap.h
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Definition: PropertyHolder.h:95
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:11
std::end
T end(T... args)
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: Property.h:36
PropertyHolder::hasProperty
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
Definition: PropertyHolder.h:256
Gaudi::Details::is_gaudi_property
Definition: PropertyHolder.h:32
PropertyHolder::Properties
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyHolder.h:300
Gaudi::Details::PropertyBase::fromString
virtual StatusCode fromString(const std::string &value)=0
string -> value
PropertyHolder::RemoteProperties
std::vector< RemProperty > RemoteProperties
Definition: PropertyHolder.h:302
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:67
std::string
STL class.
Gaudi::Details::PropertyType_t
typename std::remove_reference_t< T >::PropertyType PropertyType_t
Definition: PropertyHolder.h:38
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
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:76
INamedInterface.h
PropertyHolder::m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyHolder.h:307
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
PropertyHolder::getProperty
StatusCode getProperty(const std::string &n, std::string &v) const override
convert the property to the string
Definition: PropertyHolder.h:239
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Gaudi::Utils::iequal
bool iequal(const std::string &v1, const std::string &v2)
Helper for case insensitive string comparison.
Definition: PropertyHolder.h:44
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:116
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
setProperty
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:233
std::make_pair
T make_pair(T... args)
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
PropertyHolder::RemProperty
std::pair< std::string, std::pair< IProperty *, std::string > > RemProperty
Definition: PropertyHolder.h:301
IOTest.end
def end
Definition: IOTest.py:112
Gaudi::Details::PropertyBase::setName
void setName(std::string value)
set the new value for the property name
Definition: Property.h:84
std::pair
std::find_if
T find_if(T... args)
std::string::size
T size(T... args)
IProperty.h
std::vector< Gaudi::Details::PropertyBase * >
std::false_type
std::begin
T begin(T... args)
PropertyHolder::setProperty
StatusCode setProperty(const std::string &n, const std::string &v) override
set the property from name and the value
Definition: PropertyHolder.h:167
Property.h
PropertyHolder::assertUniqueName
void assertUniqueName(const std::string &name) const
Issue a runtime warning if the name is already present in the list of properties (see GAUDI-1023).
Definition: PropertyHolder.h:290
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:71
IProperty::getProperties
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
gaudirun.s
string s
Definition: gaudirun.py:316
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:495
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
PropertyHolder::getProperty
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
Definition: PropertyHolder.h:219
PropertyHolder::getProperty
const Gaudi::Details::PropertyBase & getProperty(const std::string &name) const override
get the property by name
Definition: PropertyHolder.h:230
MSG::WARNING
Definition: IMessageSvc.h:15
PropertyHolder::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyHolder.h:251
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:120
PropertyHolder::declareRemoteProperty
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyHolder.h:128
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
PropertyHolder::m_todelete
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties owned by PropertyHolder, to be deleted.
Definition: PropertyHolder.h:309
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
PropertyHolder::setProperty
StatusCode setProperty(const std::string &s) override
set the property from the formatted string
Definition: PropertyHolder.h:156
std::cerr
ISvcLocator.h
std::equal
T equal(T... args)
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
toupper
void toupper(std::string &s)
Definition: StatusCodeSvc.cpp:7
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Gaudi.Configuration.log
log
Definition: Configuration.py:13
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:104