Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
21 #include "GaudiKernel/IProperty.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>
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>
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 {
73  "PropertyHolder template argument must inherit from IProperty and INamedInterface" );
74 
75 public:
83 
84  PropertyHolder() = default;
85 
88  PropertyHolder( const PropertyHolder& ) = delete;
89  PropertyHolder& operator=( const PropertyHolder& ) = delete;
91 
92 public:
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>>
105  const std::string& doc = "none" ) {
106  m_todelete.push_back( std::make_unique<Gaudi::Details::PropertyType<TYPE&>>( name, value ) );
108 
109  p->setDocumentation( doc );
110  return &declareProperty( *p );
111  }
112 
115  template <class TYPE, class VERIFIER, class HANDLERS>
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 
129  const std::string& rname = "" ) {
130  if ( !rsvc ) return nullptr;
131  const std::string& nam = rname.empty() ? name : rname;
133  m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
134  return p;
135  }
136 
138 
139  // ==========================================================================
140  // IProperty implementation
141  // ==========================================================================
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 {
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 {
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  // ==========================================================================
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  // ==========================================================================
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
265  // local property ?
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:
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 
303 
305  Properties m_properties;
307  RemoteProperties m_remoteProperties;
310 };
311 #endif
const Gaudi::Details::PropertyBase & getProperty(const std::string &name) const override
get the property by name
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:1178
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
#define UNLIKELY(x)
Definition: Kernel.h:89
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
T empty(T...args)
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:86
StatusCode setProperty(const std::string &name, const TYPE &value)
set the property form the value
Gaudi::Details::PropertyBase * property(const std::string &name) const
Implementation of property with value of concrete type.
Definition: Property.h:352
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
virtual bool load(PropertyBase &dest) const =0
export the property value to the destination
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
virtual bool assign(const PropertyBase &source)=0
import the property value form the source
const std::string name() const
property name
Definition: Property.h:36
Properties m_properties
Collection of all declared properties.
StatusCode getProperty(const std::string &n, std::string &v) const override
convert the property to the string
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
Gaudi::cpp17::detected_or_t< Gaudi::Property< T >, PropertyType_t, T > PropertyType
T endl(T...args)
Gaudi::Details::PropertyBase * property(const std::string &name) const
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
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
T end(T...args)
void assertUniqueName(const std::string &name) const
Throw an exception if the name is already present in the list of properties (see GAUDI-1023).
virtual std::string toString() const =0
value -> string
bool isFailure() const
Definition: StatusCode.h:130
std::vector< Gaudi::Details::PropertyBase * > Properties
virtual StatusCode fromString(const std::string &value)=0
string -> value
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)...
std::vector< RemProperty > RemoteProperties
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
typename std::remove_reference_t< T >::PropertyType PropertyType_t
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
T push_back(T...args)
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
GAUDI_API ISvcLocator * svcLocator()
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
bool iequal(const std::string &v1, const std::string &v2)
Helper for case insensitive string comparison.
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.
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
T make_pair(T...args)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
std::pair< std::string, std::pair< IProperty *, std::string > > RemProperty
void setName(std::string value)
set the new value for the property name
Definition: Property.h:84
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
T get(T...args)
T find_if(T...args)
T size(T...args)
T begin(T...args)
StatusCode setProperty(const std::string &n, const std::string &v) override
set the property from name and the value
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:137
Helper class to implement the IProperty interface.
T back(T...args)
string s
Definition: gaudirun.py:312
constexpr static const auto FAILURE
Definition: StatusCode.h:86
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:141
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties owned by PropertyHolder, to be deleted.
#define GAUDI_API
Definition: Kernel.h:71
StatusCode setProperty(const std::string &s) override
set the property from the formatted string
T equal(T...args)
void toupper(std::string &s)
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:139
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.
T emplace_back(T...args)