The Gaudi Framework  v29r0 (ff2e7097)
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 
26 // ============================================================================
27 
28 // pre-declaration of GaudiHandles is sufficient
29 template <class T>
30 class ToolHandle;
31 template <class T>
33 template <class T>
35 template <class T>
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 {
76  "PropertyHolder template argument must inherit from IProperty and INamedInterface" );
77 
78 public:
86 
87  PropertyHolder() = default;
88  ~PropertyHolder() override = default;
89 
92  PropertyHolder( const PropertyHolder& ) = delete;
93  PropertyHolder& operator=( const PropertyHolder& ) = delete;
95 
96 public:
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 &&
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 ) );
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 &&
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>
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 
151  const std::string& rname = "" )
152  {
153  if ( !rsvc ) {
154  return nullptr;
155  }
156  const std::string& nam = rname.empty() ? name : rname;
158  m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
159  return p;
160  }
161 
165  const std::string& doc = "none" )
166  {
167  assertUniqueName( name );
168  m_todelete.emplace_back( new GaudiHandleProperty( name, ref ) );
170 
171  p->setDocumentation( doc );
172  m_properties.push_back( p );
173 
174  return p;
175  }
177  const std::string& doc = "none" )
178  {
179  assertUniqueName( name );
182 
183  p->setDocumentation( doc );
184  m_properties.push_back( p );
185 
186  return p;
187  }
189  const std::string& doc = "none" )
190  {
191  assertUniqueName( name );
194 
195  p->setDocumentation( doc );
196  m_properties.push_back( p );
197 
198  return p;
199  }
201 
202  // ==========================================================================
203  // IProperty implementation
204  // ==========================================================================
209  {
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  {
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  {
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  // ==========================================================================
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  // ==========================================================================
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  // ==========================================================================
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
344  {
345  // local property ?
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:
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 
393 
395  Properties m_properties;
397  RemoteProperties m_remoteProperties;
400 };
401 #endif
Handle to be used in lieu of naked pointers to services.
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:1173
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
#define UNLIKELY(x)
Definition: Kernel.h:128
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:92
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:319
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:40
AttribStringParser::Iterator end(const AttribStringParser &)
Properties m_properties
Collection of all declared properties.
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
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.
T endl(T...args)
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none") const
Gaudi::Details::PropertyBase * property(const std::string &name) const
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Details::PropertyBase &prop)
Declare a property.
Array of Handles to be used in lieu of vector of naked pointers to tools.
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
Test for a status code of FAILURE.
Definition: StatusCode.h:86
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)...
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
std::vector< RemProperty > RemoteProperties
PropertyMgr & operator=(const PropertyMgr &)=delete
STL class.
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
T push_back(T...args)
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
GAUDI_API ISvcLocator * svcLocator()
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, GaudiHandleBase &ref, const std::string &doc="none")
Specializations for various GaudiHandles.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
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:38
Array of Handles to be used in lieu of vector of naked pointers to tools.
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:90
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
T get(T...args)
Handle to be used in lieu of naked pointers to tools.
T find_if(T...args)
T size(T...args)
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:348
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
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:139
Helper class to implement the IProperty interface.
T back(T...args)
string s
Definition: gaudirun.py:253
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:143
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:83
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, GaudiHandleArrayBase &ref, const std::string &doc="none")
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:110
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:209
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:141
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, DataObjectHandleBase &ref, const std::string &doc="none")
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)