The Gaudi Framework  v29r0 (ff2e7097)
PropertyMgr.cpp
Go to the documentation of this file.
1 // hide deprecation warnings here... the whole file is deprecated
2 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD& STL
7 // ============================================================================
8 #include <algorithm>
9 #include <functional>
10 #include <iostream>
11 #include <sstream>
12 #include <stdexcept>
13 #include <string>
14 // ============================================================================
15 // GaudiKernel
16 // ============================================================================
17 #include "GaudiKernel/Bootstrap.h"
23 // ============================================================================
24 
26 
27 namespace
28 {
29  // ==========================================================================
31  constexpr struct NoCaseCmp_t {
32  inline bool operator()( const std::string& v1, const std::string& v2 ) const
33  {
34  return v1.size() == v2.size() && std::equal( std::begin( v1 ), std::end( v1 ), std::begin( v2 ),
35  []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
36  }
37  } noCaseCmp{};
38  // ==========================================================================
40  struct PropByName {
41  std::string m_name;
42 
43  inline bool operator()( const PropertyBase* p ) const { return p && noCaseCmp( p->name(), m_name ); }
44  };
45  // ==========================================================================
46 }
47 // ====================================================================
48 // constructor from the interface
49 // ====================================================================
50 PropertyMgr::PropertyMgr( IInterface* iface ) : m_pOuter( iface )
51 {
52  addRef(); // initial reference count set to 1
53 }
54 // ====================================================================
55 // Declare a remote property
56 // ====================================================================
58 {
59  if ( !rsvc ) {
60  return nullptr;
61  }
62  const std::string& nam = rname.empty() ? name : rname;
63  PropertyBase* p = property( nam, rsvc->getProperties() );
64  m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
65  return p;
66 }
67 // ============================================================================
68 // Declare a property
69 // ============================================================================
71 {
72  assertUniqueName( name );
73  m_todelete.emplace_back( new GaudiHandleProperty( name, ref ) );
74  Property* p = m_todelete.back().get();
75  //
76  p->setDocumentation( doc );
78  //
79  return p;
80 }
81 // ============================================================================
83 {
84  assertUniqueName( name );
86  Property* p = m_todelete.back().get();
87  //
88  p->setDocumentation( doc );
90  //
91  return p;
92 }
93 
94 // ============================================================================
96 {
97  assertUniqueName( name );
99  Property* p = m_todelete.back().get();
100  //
101  p->setDocumentation( doc );
102  m_properties.push_back( p );
103  //
104  return p;
105 }
106 // ====================================================================
107 // get the property by name form the proposed list
108 // ====================================================================
110 {
111  auto it = std::find_if( props.begin(), props.end(), PropByName{name} );
112  return ( it != props.end() ) ? *it : nullptr; // RETURN
113 }
114 // ====================================================================
115 // retrieve the property by name
116 // ====================================================================
117 PropertyBase* PropertyMgr::property( const std::string& name ) const
118 {
119  // local property ?
120  PropertyBase* lp = property( name, m_properties );
121  if ( lp ) {
122  return lp;
123  } // RETURN
124  // look for remote property
125  for ( const auto& it : m_remoteProperties ) {
126  if ( !noCaseCmp( it.first, name ) ) {
127  continue;
128  } // CONTINUE
129  const IProperty* p = it.second.first;
130  if ( !p ) {
131  continue;
132  } // CONTINUE
133  return property( it.second.second, p->getProperties() ); // RETURN
134  }
135  return nullptr; // RETURN
136 }
137 // ====================================================================
138 /* set a property from another property
139  * Implementation of IProperty::setProperty
140  */
141 // =====================================================================
143 {
144  PropertyBase* pp = property( p.name() );
145  try {
146  if ( pp && pp->assign( p ) ) {
147  return StatusCode::SUCCESS;
148  }
149  } // RETURN
150  catch ( ... ) {
151  }
152  //
153  return StatusCode::FAILURE;
154 }
155 // ====================================================================
156 /* set a property from the stream
157  * Implementation of IProperty::setProperty
158  */
159 // =====================================================================
161 {
163  std::string value;
164  StatusCode sc = Gaudi::Parsers::parse( name, value, i );
165  if ( sc.isFailure() ) {
166  return sc;
167  }
168  return setProperty( name, value );
169 }
170 // =====================================================================
171 /* set a property from the string
172  * Implementation of IProperty::setProperty
173  */
174 // =====================================================================
176 {
177  PropertyBase* p = property( n );
178  return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
179 }
180 // =====================================================================
181 /* Retrieve the value of a property
182  * Implementation of IProperty::getProperty
183  */
184 // =====================================================================
186 {
187  try {
188  const PropertyBase* pp = property( p->name() );
189  if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS; // RETURN
190  } catch ( ... ) {
191  }
192  return StatusCode::FAILURE; // RETURN
193 }
194 // =====================================================================
195 /* Get the property by name
196  * Implementation of IProperty::getProperty
197  */
198 // =====================================================================
199 const PropertyBase& PropertyMgr::getProperty( const std::string& name ) const
200 {
201  const PropertyBase* p = property( name );
202  if ( !p ) throw std::out_of_range( "Property " + name + " not found." ); // Not found
203  return *p; // RETURN
204 }
205 // =====================================================================
206 /* Get the property by name
207  * Implementation of IProperty::getProperty
208  */
209 // =====================================================================
211 {
212  // get the property
213  const PropertyBase* p = property( n );
214  if ( !p ) {
215  return StatusCode::FAILURE;
216  }
217  // convert the value into the string
218  v = p->toString();
219  //
220  return StatusCode::SUCCESS;
221 }
222 // =====================================================================
223 // Implementation of IProperty::getProperties
224 // =====================================================================
226 // =====================================================================
227 // Implementation of IInterface::queryInterface
228 // =====================================================================
229 StatusCode PropertyMgr::queryInterface( const InterfaceID& iid, void** pinterface )
230 {
231  // try local interfaces
232  StatusCode sc = base_class::queryInterface( iid, pinterface );
233  if ( sc.isSuccess() ) return sc;
234  // fall back on the owner
235  return m_pOuter ? m_pOuter->queryInterface( iid, pinterface ) : sc; // FAILURE
236 }
237 // =====================================================================
238 // Implementation of IProperty::hasProperty
239 // =====================================================================
240 bool PropertyMgr::hasProperty( const std::string& name ) const
241 {
242  return any_of( begin( m_properties ), end( m_properties ),
243  [&name]( const PropertyBase* prop ) { return noCaseCmp( prop->name(), name ); } );
244 }
245 void PropertyMgr::assertUniqueName( const std::string& name ) const
246 {
247  if ( UNLIKELY( hasProperty( name ) ) ) {
248  auto owner = SmartIF<INamedInterface>( m_pOuter );
249  auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
250  if ( !msgSvc ) {
251  std::cerr << "error: cannot get MessageSvc!" << std::endl;
252  }
253  MsgStream log( msgSvc, owner ? owner->name() : "PropertyMgr" );
254  log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
255  << endmsg;
256  }
257 }
258 // =====================================================================
259 // The END
260 // =====================================================================
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
#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
virtual bool load(PropertyBase &dest) const =0
export the property value to the destination
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
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
T endl(T...args)
Gaudi::Details::PropertyBase * property(const std::string &name) const
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
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
Gaudi::Details::PropertyBase Property
backward compatibility hack for old Property base class
Definition: PropertyFwd.h:28
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
virtual StatusCode fromString(const std::string &value)=0
string -> value
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:58
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
STL class.
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)
Interface ID class.
Definition: IInterface.h:29
GAUDI_API ISvcLocator * svcLocator()
class GAUDI_API[[deprecated("will be removed in v29r0, consider using PropertyHolder instead")]] PropertyMgr
Definition: PropertyMgr.h:32
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:28
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Definition of the basic interface.
Definition: IInterface.h:277
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
T make_pair(T...args)
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
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)
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:348
STL class.
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
T begin(T...args)
T any_of(T...args)
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:139
T back(T...args)
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
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
T equal(T...args)
void toupper(std::string &s)
IInterface * m_pOuter
Interface hub reference (ApplicationMgr)
Definition: PropertyMgr.h:145
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
virtual const std::string & name() const =0
Retrieve the name of the instance.
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:141
GAUDI_API Gaudi::Details::PropertyBase * getProperty(const IProperty *p, const std::string &name)
simple function which gets the property with given name from the component
Definition: Property.cpp:223
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
T emplace_back(T...args)