All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 // get the property by name form the proposed list
69 // ====================================================================
71 {
72  auto it = std::find_if( props.begin(), props.end(), PropByName{name} );
73  return ( it != props.end() ) ? *it : nullptr; // RETURN
74 }
75 // ====================================================================
76 // retrieve the property by name
77 // ====================================================================
79 {
80  // local property ?
81  PropertyBase* lp = property( name, m_properties );
82  if ( lp ) {
83  return lp;
84  } // RETURN
85  // look for remote property
86  for ( const auto& it : m_remoteProperties ) {
87  if ( !noCaseCmp( it.first, name ) ) {
88  continue;
89  } // CONTINUE
90  const IProperty* p = it.second.first;
91  if ( !p ) {
92  continue;
93  } // CONTINUE
94  return property( it.second.second, p->getProperties() ); // RETURN
95  }
96  return nullptr; // RETURN
97 }
98 // ====================================================================
99 /* set a property from another property
100  * Implementation of IProperty::setProperty
101  */
102 // =====================================================================
104 {
105  PropertyBase* pp = property( p.name() );
106  try {
107  if ( pp && pp->assign( p ) ) {
108  return StatusCode::SUCCESS;
109  }
110  } // RETURN
111  catch ( ... ) {
112  }
113  //
114  return StatusCode::FAILURE;
115 }
116 // ====================================================================
117 /* set a property from the stream
118  * Implementation of IProperty::setProperty
119  */
120 // =====================================================================
122 {
124  std::string value;
125  StatusCode sc = Gaudi::Parsers::parse( name, value, i );
126  if ( sc.isFailure() ) {
127  return sc;
128  }
129  return setProperty( name, value );
130 }
131 // =====================================================================
132 /* set a property from the string
133  * Implementation of IProperty::setProperty
134  */
135 // =====================================================================
137 {
138  PropertyBase* p = property( n );
139  return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
140 }
141 // =====================================================================
142 /* Retrieve the value of a property
143  * Implementation of IProperty::getProperty
144  */
145 // =====================================================================
147 {
148  try {
149  const PropertyBase* pp = property( p->name() );
150  if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS; // RETURN
151  } catch ( ... ) {
152  }
153  return StatusCode::FAILURE; // RETURN
154 }
155 // =====================================================================
156 /* Get the property by name
157  * Implementation of IProperty::getProperty
158  */
159 // =====================================================================
160 const PropertyBase& PropertyMgr::getProperty( const std::string& name ) const
161 {
162  const PropertyBase* p = property( name );
163  if ( !p ) throw std::out_of_range( "Property " + name + " not found." ); // Not found
164  return *p; // RETURN
165 }
166 // =====================================================================
167 /* Get the property by name
168  * Implementation of IProperty::getProperty
169  */
170 // =====================================================================
172 {
173  // get the property
174  const PropertyBase* p = property( n );
175  if ( !p ) {
176  return StatusCode::FAILURE;
177  }
178  // convert the value into the string
179  v = p->toString();
180  //
181  return StatusCode::SUCCESS;
182 }
183 // =====================================================================
184 // Implementation of IProperty::getProperties
185 // =====================================================================
187 // =====================================================================
188 // Implementation of IInterface::queryInterface
189 // =====================================================================
190 StatusCode PropertyMgr::queryInterface( const InterfaceID& iid, void** pinterface )
191 {
192  // try local interfaces
193  StatusCode sc = base_class::queryInterface( iid, pinterface );
194  if ( sc.isSuccess() ) return sc;
195  // fall back on the owner
196  return m_pOuter ? m_pOuter->queryInterface( iid, pinterface ) : sc; // FAILURE
197 }
198 // =====================================================================
199 // Implementation of IProperty::hasProperty
200 // =====================================================================
201 bool PropertyMgr::hasProperty( const std::string& name ) const
202 {
203  return any_of( begin( m_properties ), end( m_properties ),
204  [&name]( const PropertyBase* prop ) { return noCaseCmp( prop->name(), name ); } );
205 }
206 void PropertyMgr::assertUniqueName( const std::string& name ) const
207 {
208  if ( UNLIKELY( hasProperty( name ) ) ) {
209  auto owner = SmartIF<INamedInterface>( m_pOuter );
210  auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
211  if ( !msgSvc ) {
212  std::cerr << "error: cannot get MessageSvc!" << std::endl;
213  }
214  MsgStream log( msgSvc, owner ? owner->name() : "PropertyMgr" );
215  log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
216  << endmsg;
217  }
218 }
219 // =====================================================================
220 // The END
221 // =====================================================================
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:1123
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
T empty(T...args)
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:74
T endl(T...args)
Gaudi::Details::PropertyBase * property(const std::string &name) const
#define UNLIKELY(x)
Definition: Kernel.h:126
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
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
virtual StatusCode fromString(const std::string &value)=0
string -> value
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:47
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:78
Interface ID class.
Definition: IInterface.h:30
GAUDI_API ISvcLocator * svcLocator()
class GAUDI_API[[deprecated("will be removed in v28r1, consider using PropertyHolder instead")]] PropertyMgr
Definition: PropertyMgr.h:44
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:26
Definition of the basic interface.
Definition: IInterface.h:234
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
T make_pair(T...args)
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:49
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 find_if(T...args)
T size(T...args)
STL class.
T begin(T...args)
T any_of(T...args)
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:159
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:165
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
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:161
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)