The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
PropertyHolder< BASE > Class Template Reference

Helper class to implement the IProperty interface. More...

#include </builds/gaudi/Gaudi/GaudiKernel/include/GaudiKernel/PropertyHolder.h>

Inheritance diagram for PropertyHolder< BASE >:
Collaboration diagram for PropertyHolder< BASE >:

Classes

struct  RemProperty
 

Public Types

using PropertyHolderImpl = PropertyHolder<BASE>
 Typedef used to refer to this class from derived classes, as in.
 

Public Member Functions

 PropertyHolder ()=default
 
Gaudi::Details::PropertyBasedeclareProperty (Gaudi::Details::PropertyBase &prop)
 Declare a property.
 
template<typename TYPE>
requires ( !Gaudi::Details::is_gaudi_property_v<TYPE> )
Gaudi::Details::PropertyBasedeclareProperty (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.
 
template<class TYPE, class VERIFIER, class HANDLERS>
Gaudi::Details::PropertyBasedeclareProperty (const std::string &name, Gaudi::Property< TYPE, VERIFIER, HANDLERS > &prop, const std::string &doc="none")
 Declare a PropertyBase instance setting name and documentation.
 
Gaudi::Details::PropertyBasedeclareRemoteProperty (const std::string &name, IProperty *rsvc, const std::string &rname="")
 Declare a remote property.
 
StatusCode setProperty (const std::string &name, const Gaudi::Details::PropertyBase &p) override
 set the property from another property with a different name
 
StatusCode setProperty (const std::string &s) override
 set the property from the formatted string
 
StatusCode setPropertyRepr (const std::string &n, const std::string &r) override
 set the property from name and value string representation
 
StatusCode getProperty (Gaudi::Details::PropertyBase *p) const override
 get the property
 
const Gaudi::Details::PropertyBasegetProperty (std::string_view name) const override
 get the property by name
 
StatusCode getProperty (std::string_view n, std::string &v) const override
 convert the property to the string
 
const std::vector< Gaudi::Details::PropertyBase * > & getProperties () const override
 get all properties
 
bool hasProperty (std::string_view name) const override
 Return true if we have a property with the given name.
 
Gaudi::Details::PropertyBaseproperty (std::string_view name) const
 \fixme property and bindPropertiesTo should be protected
 
void bindPropertiesTo (Gaudi::Interfaces::IOptionsSvc &optsSvc)
 
StatusCode setProperty (const Gaudi::Details::PropertyBase &p)
 Set the property from a property.
 
StatusCode setProperty (const std::string &name, const char *v)
 Special case for string literals.
 
StatusCode setProperty (const std::string &name, const std::string &v)
 Special case for std::string.
 
template<class TYPE>
requires ( !std::is_base_of_v<Gaudi::Details::PropertyBase, TYPE> )
StatusCode setProperty (const std::string &name, const TYPE &value)
 set the property form the value
 
 PropertyHolder (const PropertyHolder &)=delete
 
PropertyHolderoperator= (const PropertyHolder &)=delete
 

Private Types

typedef std::vector< Gaudi::Details::PropertyBase * > Properties
 
typedef std::vector< RemPropertyRemoteProperties
 

Private Member Functions

Gaudi::Details::PropertyBaseproperty (std::string_view name, const std::vector< Gaudi::Details::PropertyBase * > &props) const
 get the property by name form the proposed list
 
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).
 

Private Attributes

Properties m_properties
 Collection of all declared properties.
 
RemoteProperties m_remoteProperties
 Collection of all declared remote properties.
 
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
 Properties owned by PropertyHolder, to be deleted.
 

Detailed Description

template<class BASE>
class PropertyHolder< BASE >

Helper class to implement the IProperty interface.

PropertyHolder is used by components base classes (Algorithm, Service, etc.) to provide a default implementation the IProperty interface.

When needing to implement the IProperty interface in a class, it is enough to wrap the base of the class with PropertyHolder, as in

class MyClass : public PropertyHolder<BaseClass> {
// ...
};
PropertyHolder()=default

where BaseClass should inherit from IProperty and INamedInterface.

Author
Paul Maley
David Quarrie
Marco Clemencic

Definition at line 68 of file PropertyHolder.h.

Member Typedef Documentation

◆ Properties

template<class BASE>
typedef std::vector<Gaudi::Details::PropertyBase*> PropertyHolder< BASE >::Properties
private

Definition at line 268 of file PropertyHolder.h.

◆ PropertyHolderImpl

template<class BASE>
using PropertyHolder< BASE >::PropertyHolderImpl = PropertyHolder<BASE>

Typedef used to refer to this class from derived classes, as in.

class MyClass : public PropertyHolder<BaseClass> {
};
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.

Definition at line 79 of file PropertyHolder.h.

◆ RemoteProperties

template<class BASE>
typedef std::vector<RemProperty> PropertyHolder< BASE >::RemoteProperties
private

Definition at line 274 of file PropertyHolder.h.

Constructor & Destructor Documentation

◆ PropertyHolder() [1/2]

template<class BASE>
PropertyHolder< BASE >::PropertyHolder ( )
default

◆ PropertyHolder() [2/2]

template<class BASE>
PropertyHolder< BASE >::PropertyHolder ( const PropertyHolder< BASE > & )
delete

prevent copies

Member Function Documentation

◆ assertUniqueName()

template<class BASE>
void PropertyHolder< BASE >::assertUniqueName ( std::string_view name) const
inlineprivate

Issue a runtime warning if the name is already present in the list of properties (see GAUDI-1023).

Definition at line 258 of file PropertyHolder.h.

258 {
259 if ( hasProperty( name ) ) {
260 auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
261 if ( !msgSvc ) std::cerr << "error: cannot get MessageSvc!" << std::endl;
262 MsgStream log( msgSvc, this->name() );
263 log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
264 << endmsg;
265 }
266 }
virtual SmartIF< IService > & service(const Gaudi::Utils::TypeNameString &typeName, const bool createIf=true)=0
Returns a smart pointer to a service.
Helper class to implement the IProperty interface.
bool hasProperty(std::string_view name) const override
Return true if we have a property with the given name.
GAUDI_API ISvcLocator * svcLocator()

◆ bindPropertiesTo()

template<class BASE>
void PropertyHolder< BASE >::bindPropertiesTo ( Gaudi::Interfaces::IOptionsSvc & optsSvc)
inline

Definition at line 238 of file PropertyHolder.h.

238 {
239 auto set_prop = [&optsSvc, this]( auto prop ) { optsSvc.bind( this->name(), prop ); };
242 if ( rem.owner ) set_prop( this->property( rem.remName, rem.owner->getProperties() ) );
243 } );
244 }
Gaudi::Details::PropertyBase * property(std::string_view name) const
\fixme property and bindPropertiesTo should be protected
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Properties m_properties
Collection of all declared properties.
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties

◆ declareProperty() [1/3]

template<class BASE>
template<class TYPE, class VERIFIER, class HANDLERS>
Gaudi::Details::PropertyBase * PropertyHolder< BASE >::declareProperty ( const std::string & name,
Gaudi::Property< TYPE, VERIFIER, HANDLERS > & prop,
const std::string & doc = "none" )
inline

Declare a PropertyBase instance setting name and documentation.

Deprecated
Prefer the signatures using a fully initialized PropertyBase instance.

Definition at line 121 of file PropertyHolder.h.

123 {
125 p->setName( name );
126
127 p->setDocumentation( doc );
128 return &declareProperty( *p );
129 }

◆ declareProperty() [2/3]

template<class BASE>
template<typename TYPE>
requires ( !Gaudi::Details::is_gaudi_property_v<TYPE> )
Gaudi::Details::PropertyBase * PropertyHolder< BASE >::declareProperty ( const std::string & name,
TYPE & value,
const std::string & doc = "none" )
inline

Helper to wrap a regular data member and use it as a regular property.

Deprecated
Prefer the signatures using a a fully initialized PropertyBase instance.

Definition at line 102 of file PropertyHolder.h.

103 {
104 auto make_property = [&]( const std::string& n, TYPE& v ) {
105 if constexpr ( requires { typename TYPE::PropertyType; } ) {
107 } else {
109 }
110 };
111 m_todelete.push_back( make_property( name, value ) );
113
114 p->setDocumentation( doc );
115 return &declareProperty( *p );
116 }
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties owned by PropertyHolder, to be deleted.

◆ declareProperty() [3/3]

template<class BASE>
Gaudi::Details::PropertyBase & PropertyHolder< BASE >::declareProperty ( Gaudi::Details::PropertyBase & prop)
inline

Declare a property.

Record a PropertyBase instance to be managed by PropertyHolder.

Definition at line 92 of file PropertyHolder.h.

92 {
93 assertUniqueName( prop.name() );
94 m_properties.push_back( &prop );
95 return prop;
96 }
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).

◆ declareRemoteProperty()

template<class BASE>
Gaudi::Details::PropertyBase * PropertyHolder< BASE >::declareRemoteProperty ( const std::string & name,
IProperty * rsvc,
const std::string & rname = "" )
inline

Declare a remote property.

Bind name to the property rname of rsvc.

Definition at line 133 of file PropertyHolder.h.

134 {
135 if ( !rsvc ) return nullptr;
136 const std::string& nam = rname.empty() ? name : rname;
138 m_remoteProperties.emplace_back( RemProperty{ name, rsvc, nam } );
139 return p;
140 }

◆ getProperties()

template<class BASE>
const std::vector< Gaudi::Details::PropertyBase * > & PropertyHolder< BASE >::getProperties ( ) const
inlineoverride

get all properties

See also
IProperty

Definition at line 210 of file PropertyHolder.h.

210{ return m_properties; }

◆ getProperty() [1/3]

template<class BASE>
StatusCode PropertyHolder< BASE >::getProperty ( Gaudi::Details::PropertyBase * p) const
inlineoverride

get the property

See also
IProperty

Definition at line 181 of file PropertyHolder.h.

181 {
182 try {
183 const Gaudi::Details::PropertyBase* pp = property( p->name() );
184 if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS;
185 } catch ( ... ) {}
186 return StatusCode::FAILURE;
187 }

◆ getProperty() [2/3]

template<class BASE>
StatusCode PropertyHolder< BASE >::getProperty ( std::string_view n,
std::string & v ) const
inlineoverride

convert the property to the string

See also
IProperty

Definition at line 199 of file PropertyHolder.h.

199 {
200 // get the property
202 if ( !p ) return StatusCode::FAILURE;
203 // convert the value into the string
204 v = p->toString();
205 return StatusCode::SUCCESS;
206 }

◆ getProperty() [3/3]

template<class BASE>
const Gaudi::Details::PropertyBase & PropertyHolder< BASE >::getProperty ( std::string_view name) const
inlineoverride

get the property by name

See also
IProperty

Definition at line 191 of file PropertyHolder.h.

191 {
193 if ( !p ) throw std::out_of_range( "Property " + std::string{ name } + " not found." );
194 return *p;
195 }

◆ hasProperty()

template<class BASE>
bool PropertyHolder< BASE >::hasProperty ( std::string_view name) const
inlineoverride

Return true if we have a property with the given name.

See also
IProperty

Definition at line 214 of file PropertyHolder.h.

214 {
217 return Gaudi::Utils::iequal( prop->name(), name );
218 } ) ||
220 [&name]( const auto& prop ) { return Gaudi::Utils::iequal( prop.name, name ); } );
221 }
bool iequal(std::string_view v1, std::string_view v2)
Helper for case insensitive string comparison.

◆ operator=()

template<class BASE>
PropertyHolder & PropertyHolder< BASE >::operator= ( const PropertyHolder< BASE > & )
delete

◆ property() [1/2]

template<class BASE>
Gaudi::Details::PropertyBase * PropertyHolder< BASE >::property ( std::string_view name) const
inline

\fixme property and bindPropertiesTo should be protected

Definition at line 224 of file PropertyHolder.h.

224 {
225 // local property ?
227 if ( lp ) return lp;
228 // look for remote property
229 for ( const auto& it : m_remoteProperties ) {
230 if ( !Gaudi::Utils::iequal( it.name, name ) ) continue;
231 const IProperty* p = it.owner;
232 if ( !p ) continue;
233 return property( it.remName, p->getProperties() );
234 }
235 return nullptr;
236 }

◆ property() [2/2]

template<class BASE>
Gaudi::Details::PropertyBase * PropertyHolder< BASE >::property ( std::string_view name,
const std::vector< Gaudi::Details::PropertyBase * > & props ) const
inlineprivate

get the property by name form the proposed list

Definition at line 248 of file PropertyHolder.h.

249 {
250 auto it = std::find_if( props.begin(), props.end(), [name]( Gaudi::Details::PropertyBase* p ) {
251 return p && Gaudi::Utils::iequal( p->name(), name );
252 } );
253 return ( it != props.end() ) ? *it : nullptr;
254 }

◆ setProperty() [1/6]

template<class BASE>
StatusCode IProperty::setProperty ( const Gaudi::Details::PropertyBase & p)
inline

Set the property from a property.

Definition at line 38 of file IProperty.h.

38{ return setProperty( p.name(), p ); }
StatusCode setProperty(const std::string &name, const Gaudi::Details::PropertyBase &p) override
set the property from another property with a different name

◆ setProperty() [2/6]

template<class BASE>
StatusCode IProperty::setProperty ( const std::string & name,
const char * v )
inline

Special case for string literals.

Definition at line 46 of file IProperty.h.

46{ return setProperty( name, std::string{ v } ); }

◆ setProperty() [3/6]

template<class BASE>
StatusCode PropertyHolder< BASE >::setProperty ( const std::string & name,
const Gaudi::Details::PropertyBase & p )
inlineoverride

set the property from another property with a different name

See also
IProperty

Definition at line 151 of file PropertyHolder.h.

151 {
153 if ( pp && pp->assign( p ) ) return StatusCode::SUCCESS;
154 return StatusCode::FAILURE;
155 }

◆ setProperty() [4/6]

template<class BASE>
StatusCode IProperty::setProperty ( const std::string & name,
const std::string & v )
inline

Special case for std::string.

Definition at line 48 of file IProperty.h.

48 {
49 if ( !hasProperty( name ) ) return StatusCode::FAILURE;
50 return setPropertyRepr( name, v );
51 }
StatusCode setPropertyRepr(const std::string &n, const std::string &r) override
set the property from name and value string representation

◆ setProperty() [5/6]

template<class BASE>
template<class TYPE>
requires ( !std::is_base_of_v<Gaudi::Details::PropertyBase, TYPE> )
StatusCode IProperty::setProperty ( const std::string & name,
const TYPE & value )
inline

set the property form the value

std::vector<double> data = ... ;
setProperty( "Data" , data ) ;
std::map<std::string,double> cuts = ... ;
setProperty( "Cuts" , cuts ) ;
std::map<std::string,std::string> dict = ... ;
setProperty( "Dictionary" , dict ) ;

Note: the interface IProperty allows setting of the properties either directly from other properties or from strings only

This is very convenient in resetting of the default properties in the derived classes.

Parameters
namename of the property
valuevalue of the property
See also
Gaudi::Utils::setProperty
Author
Vanya BELYAEV ibely.nosp@m.aev@.nosp@m.physi.nosp@m.cs.s.nosp@m.yr.ed.nosp@m.u
Date
2007-05-13

Definition at line 82 of file IProperty.h.

82 {
84 if ( !hasProperty( name ) ) return StatusCode::FAILURE;
85 return setPropertyRepr( name, toString( value ) );
86 }

◆ setProperty() [6/6]

template<class BASE>
StatusCode PropertyHolder< BASE >::setProperty ( const std::string & s)
inlineoverride

set the property from the formatted string

See also
IProperty

Definition at line 159 of file PropertyHolder.h.

159 {
163 if ( sc.isFailure() ) return sc;
164 return setPropertyRepr( name, value );
165 }
StatusCode parse(GaudiUtils::HashMap< K, V > &result, std::string_view input)
Basic parser for the types of HashMap used in DODBasicMapper.

◆ setPropertyRepr()

template<class BASE>
StatusCode PropertyHolder< BASE >::setPropertyRepr ( const std::string & n,
const std::string & r )
inlineoverride

set the property from name and value string representation

See also
IProperty

@fixme SUCCESS is not required to be checked for compatibility with Gaudi::Utils::setProperty

Definition at line 169 of file PropertyHolder.h.

169 {
170 try {
173 return ( p && p->fromString( r ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
174 } catch ( const std::invalid_argument& err ) {
175 throw GaudiException{ "error setting property " + n, this->name(), StatusCode::FAILURE, err };
176 }
177 }

Member Data Documentation

◆ m_properties

template<class BASE>
Properties PropertyHolder< BASE >::m_properties
private

Collection of all declared properties.

Definition at line 277 of file PropertyHolder.h.

◆ m_remoteProperties

template<class BASE>
RemoteProperties PropertyHolder< BASE >::m_remoteProperties
private

Collection of all declared remote properties.

Definition at line 279 of file PropertyHolder.h.

◆ m_todelete

template<class BASE>
std::vector<std::unique_ptr<Gaudi::Details::PropertyBase> > PropertyHolder< BASE >::m_todelete
private

Properties owned by PropertyHolder, to be deleted.

Definition at line 281 of file PropertyHolder.h.


The documentation for this class was generated from the following file: