| The Gaudi Framework v29r1 (5cd95fa2)

- 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, typename = typename std::enable_if<!std::is_base_of<GaudiHandleBase, TYPE>::value &&
109 !std::is_base_of<GaudiHandleArrayBase, TYPE>::value &&
110 !std::is_base_of<DataObjectHandleBase, TYPE>::value>::type>
111 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, TYPE& value, const std::string& doc = "none" )
112 {
113 assertUniqueName( name );
114 m_todelete.emplace_back( new Gaudi::Property<TYPE&>( name, value ) );
115 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
116 p->setDocumentation( doc );
117 m_properties.push_back( p );
118 return p;
119 }
120
122 template <class TYPE, typename = typename std::enable_if<!std::is_base_of<GaudiHandleBase, TYPE>::value &&
123 !std::is_base_of<GaudiHandleArrayBase, TYPE>::value &&
124 !std::is_base_of<DataObjectHandleBase, TYPE>::value>::type>
125 [[deprecated(
126 "Kept for backward compatibility, use the non-const version instead, will be removed in v29r0" )]] Gaudi::
127 Details::PropertyBase*
128 declareProperty( const std::string& name, TYPE& value, const std::string& doc = "none" ) const
129 {
130 return const_cast<PropertyHolder*>( this )->declareProperty<TYPE>( name, value, doc );
131 }
132
135 template <class TYPE, class VERIFIER, class HANDLERS>
136 Gaudi::Details::PropertyBase* declareProperty( const std::string& name,
137 Gaudi::Property<TYPE, VERIFIER, HANDLERS>& prop,
138 const std::string& doc = "none" )
139 {
140 assertUniqueName( name );
141 Gaudi::Details::PropertyBase* p = &prop;
142 p->setName( name );
143 p->setDocumentation( doc );
144 m_properties.push_back( p );
145 return p;
146 }
147
150 Gaudi::Details::PropertyBase* declareRemoteProperty( const std::string& name, IProperty* rsvc,
151 const std::string& rname = "" )
152 {
153 if ( !rsvc ) {
154 return nullptr;
155 }
156 const std::string& nam = rname.empty() ? name : rname;
157 Gaudi::Details::PropertyBase* p = property( nam, rsvc->getProperties() );
158 m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
159 return p;
160 }
161
164 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, GaudiHandleBase& ref,
165 const std::string& doc = "none" )
166 {
167 assertUniqueName( name );
168 m_todelete.emplace_back( new GaudiHandleProperty( name, ref ) );
169 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
170
171 p->setDocumentation( doc );
172 m_properties.push_back( p );
173
174 return p;
175 }
176 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, GaudiHandleArrayBase& ref,
177 const std::string& doc = "none" )
178 {
179 assertUniqueName( name );
180 m_todelete.emplace_back( new GaudiHandleArrayProperty( name, ref ) );
181 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
182
183 p->setDocumentation( doc );
184 m_properties.push_back( p );
185
186 return p;
187 }
188 Gaudi::Details::PropertyBase* declareProperty( const std::string& name, DataObjectHandleBase& ref,
189 const std::string& doc = "none" )
190 {
191 assertUniqueName( name );
192 m_todelete.emplace_back( new DataObjectHandleProperty( name, ref ) );
193 Gaudi::Details::PropertyBase* p = m_todelete.back().get();
194
195 p->setDocumentation( doc );
196 m_properties.push_back( p );
197
198 return p;
199 }
201
202 // ==========================================================================
203 // IProperty implementation
204 // ==========================================================================
208 StatusCode setProperty( const Gaudi::Details::PropertyBase& p ) override
209 {
210 Gaudi::Details::PropertyBase* pp = property( p.name() );
211 try {
212 if ( pp && pp->assign( p ) ) {
213 return StatusCode::SUCCESS;
214 }
215 } catch ( ... ) {
216 }
217 return StatusCode::FAILURE;
218 }
219 // ==========================================================================
223 StatusCode setProperty( const std::string& s ) override
224 {
225 std::string name;
226 std::string value;
227 StatusCode sc = Gaudi::Parsers::parse( name, value, s );
228 if ( sc.isFailure() ) {
229 return sc;
230 }
231 return setProperty( name, value );
232 }
233 // ==========================================================================
237 StatusCode setProperty( const std::string& n, const std::string& v ) override
238 {
239 Gaudi::Details::PropertyBase* p = property( n );
240 return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
241 }
282 template <class TYPE>
283 StatusCode setProperty( const std::string& name, const TYPE& value )
284 {
285 return Gaudi::Utils::setProperty( this, name, value );
286 }
287 // ==========================================================================
291 StatusCode getProperty( Gaudi::Details::PropertyBase* p ) const override
292 {
293 try {
294 const Gaudi::Details::PropertyBase* pp = property( p->name() );
295 if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS;
296 } catch ( ... ) {
297 }
298 return StatusCode::FAILURE;
299 }
300 // ==========================================================================
304 const Gaudi::Details::PropertyBase& getProperty( const std::string& name ) const override
305 {
306 const Gaudi::Details::PropertyBase* p = property( name );
307 if ( !p ) throw std::out_of_range( "Property " + name + " not found." );
308 return *p;
309 }
310 // ==========================================================================
314 StatusCode getProperty( const std::string& n, std::string& v ) const override
315 {
316 // get the property
317 const Gaudi::Details::PropertyBase* p = property( n );
318 if ( !p ) {
319 return StatusCode::FAILURE;
320 }
321 // convert the value into the string
322 v = p->toString();
323 return StatusCode::SUCCESS;
324 }
325 // ==========================================================================
329 const std::vector<Gaudi::Details::PropertyBase*>& getProperties() const override { return m_properties; }
330 // ==========================================================================
334 bool hasProperty( const std::string& name ) const override
335 {
336 return any_of( begin( m_properties ), end( m_properties ), [&name]( const Gaudi::Details::PropertyBase* prop ) {
337 return Gaudi::Utils::iequal( prop->name(), name );
338 } );
339 }
340 // ==========================================================================
341 protected:
342 // get local or remote property by name
343 Gaudi::Details::PropertyBase* property( const std::string& name ) const
344 {
345 // local property ?
346 Gaudi::Details::PropertyBase* lp = property( name, m_properties );
347 if ( lp ) {
348 return lp;
349 }
350 // look for remote property
351 for ( const auto& it : m_remoteProperties ) {
352 if ( !Gaudi::Utils::iequal( it.first, name ) ) {
353 continue;
354 }
355 const IProperty* p = it.second.first;
356 if ( !p ) {
357 continue;
358 }
359 return property( it.second.second, p->getProperties() );
360 }
361 return nullptr; // RETURN
362 }
363
364 private:
366 Gaudi::Details::PropertyBase* property( const std::string& name,
367 const std::vector<Gaudi::Details::PropertyBase*>& props ) const
368 {
369 auto it = std::find_if( props.begin(), props.end(), [&name]( Gaudi::Details::PropertyBase* p ) {
370 return p && Gaudi::Utils::iequal( p->name(), name );
371 } );
372 return ( it != props.end() ) ? *it : nullptr; // RETURN
373 }
374
377 void assertUniqueName( const std::string& name ) const
378 {
379 if ( UNLIKELY( hasProperty( name ) ) ) {
380 auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
381 if ( !msgSvc ) {
382 std::cerr << "error: cannot get MessageSvc!" << std::endl;
383 }
384 MsgStream log( msgSvc, this->name() );
385 log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
386 << endmsg;
387 }
388 }
389
390 typedef std::vector<Gaudi::Details::PropertyBase*> Properties;
391 typedef std::pair<std::string, std::pair<IProperty*, std::string>> RemProperty;
392 typedef std::vector<RemProperty> RemoteProperties;
393
395 Properties m_properties;
397 RemoteProperties m_remoteProperties;
399 std::vector<std::unique_ptr<Gaudi::Details::PropertyBase>> m_todelete;
400 };
401 #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:304
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:1173
PropertyHolder::getProperty
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
Definition: PropertyHolder.h:291
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
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:31
HiveAlgSequencer.c2
c2
Definition: HiveAlgSequencer.py:26
std::string::empty
T empty(T...args)
DataObjectHandleProperty.h
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
StatusCode::FAILURE
Definition: StatusCode.h:31
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:283
PropertyHolder::property
Gaudi::Details::PropertyBase * property(const std::string &name) const
Definition: PropertyHolder.h:343
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:319
PropertyHolder::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
Definition: PropertyHolder.h:208
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:329
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:133
PropertyHolder::m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyHolder.h:395
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:314
PropertyHolder::hasProperty
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
Definition: PropertyHolder.h:334
std::endl
T endl(T...args)
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none") const
Definition: PropertyHolder.h:128
property
Gaudi::Details::PropertyBase * property(const std::string &name) const
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:13
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:366
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:86
PropertyHolder::Properties
std::vector< Gaudi::Details::PropertyBase * > Properties
Definition: PropertyHolder.h:390
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:377
DataObjectHandleProperty
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
Definition: DataObjectHandleProperty.h:23
PropertyHolder::RemoteProperties
std::vector< RemProperty > RemoteProperties
Definition: PropertyHolder.h:392
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:228
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:79
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:397
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:164
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
ForwardSchedulerSimpleTest.msgSvc
msgSvc
Definition: ForwardSchedulerSimpleTest.py:112
gaudirun.type
type
Definition: gaudirun.py:133
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:136
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
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:54
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:391
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:348
IProperty.h
std::vector< Gaudi::Details::PropertyBase * >
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:25
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:237
Property.h
std::is_base_of
m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:139
PropertyHolder
Helper class to implement the IProperty interface.
Definition: PropertyHolder.h:73
std::vector::back
T back(T...args)
gaudirun.s
string s
Definition: gaudirun.py:253
GaudiPython.Bindings.GaudiHandleProperty
GaudiHandleProperty
Definition: Bindings.py:53
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:467
m_todelete
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:143
MSG::WARNING
Definition: IMessageSvc.h:16
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:83
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:129
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, GaudiHandleArrayBase &ref, const std::string &doc="none")
Definition: PropertyHolder.h:176
PropertyHolder::declareRemoteProperty
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyHolder.h:150
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:399
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
PropertyHolder::setProperty
StatusCode setProperty(const std::string &s) override
set the property from the formatted string
Definition: PropertyHolder.h:223
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:209
m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:141
PropertyHolder::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, DataObjectHandleBase &ref, const std::string &doc="none")
Definition: PropertyHolder.h:188
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:111
std::vector::emplace_back
T emplace_back(T...args)