| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
26 // ============================================================================
27
28 // pre-declaration of GaudiHandles is sufficient
29 template <class T>
30 class ToolHandle;
31 template <class T>
32 class ServiceHandle;
33 template <class T>
34 class ToolHandleArray;
35 template <class T>
36 class ServiceHandleArray;
37 template <class T>
38 class DataObjectHandle;
39
40 namespace Gaudi
41 {
42 namespace Utils
43 {
45 inline bool iequal( const std::string& v1, const std::string& v2 )
46 {
47 return v1.size() == v2.size() && std::equal( std::begin( v1 ), std::end( v1 ), std::begin( v2 ),
48 []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
49 }
50 }
51 }
72 template <class BASE>
73 class GAUDI_API PropertyHolder : public BASE
74 {
75 static_assert( std::is_base_of<IProperty, BASE>::value && std::is_base_of<INamedInterface, BASE>::value,
76 "PropertyHolder template argument must inherit from IProperty and INamedInterface" );
77
78 public:
85 using PropertyHolderImpl = PropertyHolder<BASE>;
86
87 PropertyHolder() = default;
88 ~PropertyHolder() override = default;
89
92 PropertyHolder( const PropertyHolder& ) = delete;
93 PropertyHolder& operator=( const PropertyHolder& ) = delete;
95
96 public:
99 inline Gaudi::Details::PropertyBase& declareProperty( Gaudi::Details::PropertyBase& prop )
100 {
101 assertUniqueName( prop.name() );
102 m_properties.push_back( &prop );
103 return prop;
104 }
105
108 template <class TYPE,
109 typename = typename std::enable_if< !std::is_base_of<GaudiHandleBase,TYPE>::value
110 && !std::is_base_of<GaudiHandleArrayBase,TYPE>::value
111 && !std::is_base_of<DataObjectHandleBase,TYPE>::value
112 >::type>
113 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, TYPE& value, const std::string& doc = "none" )
114 {
115 assertUniqueName( name );
116 m_todelete.emplace_back( new Gaudi::Property<TYPE&>( name, value ) );
117 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
118 p->setDocumentation( doc );
119 m_properties.push_back( p );
120 return p;
121 }
122
124 template <class TYPE,
125 typename = typename std::enable_if< !std::is_base_of<GaudiHandleBase,TYPE>::value
126 && !std::is_base_of<GaudiHandleArrayBase,TYPE>::value
127 && !std::is_base_of<DataObjectHandleBase,TYPE>::value
128 >::type>
129 [[deprecated("Kept for backward compatibility, use the non-const version instead, will be removed in v29r0" )]]
130 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, TYPE& value, const std::string& doc = "none" ) const
131 {
132 return const_cast<PropertyHolder*>( this )->declareProperty<TYPE>( name, value, doc );
133 }
134
137 template <class TYPE, class VERIFIER, class HANDLERS>
138 Gaudi::Details::PropertyBase* declareProperty( const std::string& name,
139 Gaudi::Property<TYPE, VERIFIER, HANDLERS>& prop,
140 const std::string& doc = "none" )
141 {
142 assertUniqueName( name );
143 Gaudi::Details::PropertyBase* p = &prop;
144 p->setName( name );
145 p->setDocumentation( doc );
146 m_properties.push_back( p );
147 return p;
148 }
149
152 Gaudi::Details::PropertyBase* declareRemoteProperty( const std::string& name, IProperty* rsvc,
153 const std::string& rname = "" )
154 {
155 if ( !rsvc ) {
156 return nullptr;
157 }
158 const std::string& nam = rname.empty() ? name : rname;
159 Gaudi::Details::PropertyBase* p = property( nam, rsvc->getProperties() );
160 m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
161 return p;
162 }
163
166 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, GaudiHandleBase& ref,
167 const std::string& doc = "none" )
168 {
169 assertUniqueName( name );
170 m_todelete.emplace_back( new GaudiHandleProperty( name, ref ) );
171 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
172
173 p->setDocumentation( doc );
174 m_properties.push_back( p );
175
176 return p;
177 }
178 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, GaudiHandleArrayBase& ref,
179 const std::string& doc = "none" )
180 {
181 assertUniqueName( name );
182 m_todelete.emplace_back( new GaudiHandleArrayProperty( name, ref ) );
183 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
184
185 p->setDocumentation( doc );
186 m_properties.push_back( p );
187
188 return p;
189 }
190 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, DataObjectHandleBase& ref,
191 const std::string& doc = "none" )
192 {
193 assertUniqueName( name );
194 m_todelete.emplace_back( new DataObjectHandleProperty( name, ref ) );
195 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
196
197 p->setDocumentation( doc );
198 m_properties.push_back( p );
199
200 return p;
201 }
203
204 // ==========================================================================
205 // IProperty implementation
206 // ==========================================================================
210 StatusCode setProperty( const Gaudi::Details::PropertyBase& p ) override
211 {
212 Gaudi::Details::PropertyBase* pp = property( p.name() );
213 try {
214 if ( pp && pp->assign( p ) ) {
215 return StatusCode::SUCCESS;
216 }
217 } catch ( ... ) {
218 }
219 return StatusCode::FAILURE;
220 }
221 // ==========================================================================
225 StatusCode setProperty( const std::string& s ) override
226 {
227 std::string name;
228 std::string value;
229 StatusCode sc = Gaudi::Parsers::parse( name, value, s );
230 if ( sc.isFailure() ) {
231 return sc;
232 }
233 return setProperty( name, value );
234 }
235 // ==========================================================================
239 StatusCode setProperty( const std::string& n, const std::string& v ) override
240 {
241 Gaudi::Details::PropertyBase* p = property( n );
242 return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
243 }
284 template <class TYPE>
285 StatusCode setProperty( const std::string& name, const TYPE& value )
286 {
287 return Gaudi::Utils::setProperty( this, name, value );
288 }
289 // ==========================================================================
293 StatusCode getProperty( Gaudi::Details::PropertyBase* p ) const override
294 {
295 try {
296 const Gaudi::Details::PropertyBase* pp = property( p->name() );
297 if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS;
298 } catch ( ... ) {
299 }
300 return StatusCode::FAILURE;
301 }
302 // ==========================================================================
306 const Gaudi::Details::PropertyBase& getProperty( const std::string& name ) const override
307 {
308 const Gaudi::Details::PropertyBase* p = property( name );
309 if ( !p ) throw std::out_of_range( "Property " + name + " not found." );
310 return *p;
311 }
312 // ==========================================================================
316 StatusCode getProperty( const std::string& n, std::string& v ) const override
317 {
318 // get the property
319 const Gaudi::Details::PropertyBase* p = property( n );
320 if ( !p ) {
321 return StatusCode::FAILURE;
322 }
323 // convert the value into the string
324 v = p->toString();
325 return StatusCode::SUCCESS;
326 }
327 // ==========================================================================
331 const std::vector<Gaudi::Details::PropertyBase*>& getProperties() const override { return m_properties; }
332 // ==========================================================================
336 bool hasProperty( const std::string& name ) const override
337 {
338 return any_of( begin( m_properties ), end( m_properties ), [&name]( const Gaudi::Details::PropertyBase* prop ) {
339 return Gaudi::Utils::iequal( prop->name(), name );
340 } );
341 }
342 // ==========================================================================
343 protected:
344 // get local or remote property by name
345 Gaudi::Details::PropertyBase* property( const std::string& name ) const
346 {
347 // local property ?
348 Gaudi::Details::PropertyBase* lp = property( name, m_properties );
349 if ( lp ) {
350 return lp;
351 }
352 // look for remote property
353 for ( const auto& it : m_remoteProperties ) {
354 if ( !Gaudi::Utils::iequal( it.first, name ) ) {
355 continue;
356 }
357 const IProperty* p = it.second.first;
358 if ( !p ) {
359 continue;
360 }
361 return property( it.second.second, p->getProperties() );
362 }
363 return nullptr; // RETURN
364 }
365
366 private:
368 Gaudi::Details::PropertyBase* property( const std::string& name,
369 const std::vector<Gaudi::Details::PropertyBase*>& props ) const
370 {
371 auto it = std::find_if( props.begin(), props.end(), [&name]( Gaudi::Details::PropertyBase* p ) {
372 return p && Gaudi::Utils::iequal( p->name(), name );
373 } );
374 return ( it != props.end() ) ? *it : nullptr; // RETURN
375 }
376
379 void assertUniqueName( const std::string& name ) const
380 {
381 if ( UNLIKELY( hasProperty( name ) ) ) {
382 auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
383 if ( !msgSvc ) {
384 std::cerr << "error: cannot get MessageSvc!" << std::endl;
385 }
386 MsgStream log( msgSvc, this->name() );
387 log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
388 << endmsg;
389 }
390 }
391
392 typedef std::vector<Gaudi::Details::PropertyBase*> Properties;
393 typedef std::pair<std::string, std::pair<IProperty*, std::string>> RemProperty;
394 typedef std::vector<RemProperty> RemoteProperties;
395
397 Properties m_properties;
399 RemoteProperties m_remoteProperties;
401 std::vector<std::unique_ptr<Gaudi::Details::PropertyBase>> m_todelete;
402 };
403 #endif
ServiceHandle
Handle to be used in lieu of naked pointers to services.
Definition: PropertyHolder.h:32
PropertyHolder::getProperty
const Gaudi::Details::PropertyBase & getProperty(const std::string &name) const override
get the property by name
Definition: PropertyHolder.h:306
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:1138
PropertyHolder::getProperty
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
Definition: PropertyHolder.h:293
std::out_of_range
STL class.
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
StatusCode::SUCCESS
Definition: StatusCode.h:30
HiveAlgSequencer.c2
c2
Definition: HiveAlgSequencer.py:26
std::string::empty
T empty(T...args)
DataObjectHandleProperty.h
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
StatusCode::FAILURE
Definition: StatusCode.h:29
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:92
PropertyHolder::setProperty
StatusCode setProperty(const std::string &name, const TYPE &value)
set the property form the value
Definition: PropertyHolder.h:285
PropertyHolder::property
Gaudi::Details::PropertyBase * property(const std::string &name) const
Definition: PropertyHolder.h:345
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:314
PropertyHolder::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
Definition: PropertyHolder.h:210
MsgStream.h
Gaudi::Details::PropertyBase::load
virtual bool load(PropertyBase &dest) const =0
export the property value to the destination
PropertyHolder::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyHolder.h:331
Gaudi::Details::PropertyBase::assign
virtual bool assign(const PropertyBase &source)=0
import the property value form the source
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: Property.h:40
Gaudi::Utils::end
AttribStringParser::Iterator end(const AttribStringParser &)
Definition: AttribStringParser.h:137
PropertyHolder::m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyHolder.h:397
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:100
PropertyHolder::getProperty
StatusCode getProperty(const std::string &n, std::string &v) const override
convert the property to the string
Definition: PropertyHolder.h:316
PropertyHolder::hasProperty
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
Definition: PropertyHolder.h:336
std::endl
T endl(T...args)
Gaudi::Functional::deprecated
struct deprecated("use MergingTransformer instead")]] ListTransformer
Definition: ListTransformer.h:9
property
Gaudi::Details::PropertyBase * property(const std::string &name) const
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
Bootstrap.h
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Definition: PropertyHolder.h:99
ToolHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyHolder.h:34
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
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:368
std::end
T end(T...args)
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).
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
PropertyHolder::Properties
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyHolder.h:392
std::enable_if
Gaudi::Details::PropertyBase::fromString
virtual StatusCode fromString(const std::string &value)=0
string -> value
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:379
DataObjectHandleProperty
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
Definition: DataObjectHandleProperty.h:23
PropertyHolder::RemoteProperties
std::vector< RemProperty > RemoteProperties
Definition: PropertyHolder.h:394
operator=
PropertyMgr & operator=(const PropertyMgr &)=delete
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:217
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
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:78
std::vector::push_back
T push_back(T...args)
INamedInterface.h
PropertyHolder::m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyHolder.h:399
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, GaudiHandleBase &ref, const std::string &doc="none")
Specializations for various GaudiHandles.
Definition: PropertyHolder.h:166
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
ForwardSchedulerSimpleTest.msgSvc
msgSvc
Definition: ForwardSchedulerSimpleTest.py:112
gaudirun.type
type
Definition: gaudirun.py:125
Gaudi::Utils::iequal
bool iequal(const std::string &v1, const std::string &v2)
Helper for case insensitive string comparison.
Definition: PropertyHolder.h:45
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:138
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
ServiceHandleArray
Array of Handles to be used in lieu of vector of naked pointers to tools.
Definition: PropertyHolder.h:36
std::make_pair
T make_pair(T...args)
GaudiPython.Bindings.GaudiHandleArrayProperty
GaudiHandleArrayProperty
Definition: Bindings.py:50
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:393
Gaudi::Details::PropertyBase::setName
void setName(std::string value)
set the new value for the property name
Definition: Property.h:90
IProperty::getProperties
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
std::pair
std::unique_ptr::get
T get(T...args)
ToolHandle
Handle to be used in lieu of naked pointers to tools.
Definition: PropertyHolder.h:30
std::find_if
T find_if(T...args)
std::string::size
T size(T...args)
GaudiHandleArrayBase
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:364
IProperty.h
std::vector< Gaudi::Details::PropertyBase * >
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:24
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:239
Property.h
std::is_base_of
m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:144
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:73
std::vector::back
T back(T...args)
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none") const
Definition: PropertyHolder.h:130
gaudirun.s
string s
Definition: gaudirun.py:245
GaudiPython.Bindings.GaudiHandleProperty
GaudiHandleProperty
Definition: Bindings.py:49
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:417
m_todelete
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:148
MSG::WARNING
Definition: IMessageSvc.h:20
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:94
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:134
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, GaudiHandleArrayBase &ref, const std::string &doc="none")
Definition: PropertyHolder.h:178
PropertyHolder::declareRemoteProperty
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyHolder.h:152
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:401
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
PropertyHolder::setProperty
StatusCode setProperty(const std::string &s) override
set the property from the formatted string
Definition: PropertyHolder.h:225
std::cerr
ISvcLocator.h
std::equal
T equal(T...args)
toupper
void toupper(std::string &s)
Definition: StatusCodeSvc.cpp:10
Gaudi
Helper functions to set/get the application return code.
Definition: __init__.py:1
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:146
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, DataObjectHandleBase &ref, const std::string &doc="none")
Definition: PropertyHolder.h:190
HiveAlgSequencer.c1
c1
Definition: HiveAlgSequencer.py:25
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:113
std::vector::emplace_back
T emplace_back(T...args)