The Gaudi Framework  v33r1 (b1225454)
PropertyMgr.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // hide deprecation warnings here... the whole file is deprecated
12 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD& STL
17 // ============================================================================
18 #include <algorithm>
19 #include <functional>
20 #include <iostream>
21 #include <sstream>
22 #include <stdexcept>
23 #include <string>
24 // ============================================================================
25 // GaudiKernel
26 // ============================================================================
27 #include "GaudiKernel/Bootstrap.h"
33 // ============================================================================
34 
36 
37 namespace {
38  // ==========================================================================
40  constexpr struct NoCaseCmp_t {
41  inline bool operator()( const std::string& v1, const std::string& v2 ) const {
42  return v1.size() == v2.size() && std::equal( std::begin( v1 ), std::end( v1 ), std::begin( v2 ),
43  []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
44  }
45  } noCaseCmp{};
46  // ==========================================================================
48  struct PropByName {
49  std::string m_name;
50 
51  inline bool operator()( const PropertyBase* p ) const { return p && noCaseCmp( p->name(), m_name ); }
52  };
53  // ==========================================================================
54 } // namespace
55 // ====================================================================
56 // constructor from the interface
57 // ====================================================================
58 PropertyMgr::PropertyMgr( IInterface* iface ) : m_pOuter( iface ) {
59  addRef(); // initial reference count set to 1
60 }
61 // ====================================================================
62 // Declare a remote property
63 // ====================================================================
65  if ( !rsvc ) return nullptr;
66  const std::string& nam = rname.empty() ? name : rname;
67  PropertyBase* p = property( nam, rsvc->getProperties() );
69  return p;
70 }
71 // ============================================================================
72 // Declare a property
73 // ============================================================================
77  Property* p = m_todelete.back().get();
78  //
79  p->setDocumentation( doc );
81  //
82  return p;
83 }
84 // ============================================================================
86  const std::string& doc ) {
89  Property* p = m_todelete.back().get();
90  //
91  p->setDocumentation( doc );
93  //
94  return p;
95 }
96 
97 // ============================================================================
99  const std::string& doc ) {
102  Property* p = m_todelete.back().get();
103  //
104  p->setDocumentation( doc );
105  m_properties.push_back( p );
106  //
107  return p;
108 }
109 // ====================================================================
110 // get the property by name form the proposed list
111 // ====================================================================
113  auto it = std::find_if( props.begin(), props.end(), PropByName{name} );
114  return ( it != props.end() ) ? *it : nullptr; // RETURN
115 }
116 // ====================================================================
117 // retrieve the property by name
118 // ====================================================================
120  // local property ?
122  if ( lp ) return lp; // RETURN
123  // look for remote property
124  for ( const auto& it : m_remoteProperties ) {
125  if ( !noCaseCmp( it.first, name ) ) continue; // CONTINUE
126  const IProperty* p = it.second.first;
127  if ( !p ) continue;
128  return property( it.second.second, p->getProperties() ); // RETURN
129  }
130  return nullptr; // RETURN
131 }
132 // ====================================================================
133 /* set a property from another property
134  * Implementation of IProperty::setProperty
135  */
136 // =====================================================================
138  PropertyBase* pp = property( p.name() );
139  try {
140  if ( pp && pp->assign( p ) ) return StatusCode::SUCCESS;
141  } // RETURN
142  catch ( ... ) {}
143  //
144  return StatusCode::FAILURE;
145 }
146 // ====================================================================
147 /* set a property from the stream
148  * Implementation of IProperty::setProperty
149  */
150 // =====================================================================
152  std::string name, value;
153  StatusCode sc = Gaudi::Parsers::parse( name, value, i );
154  return sc.isFailure() ? sc : setProperty( name, value );
155 }
156 // =====================================================================
157 /* set a property from the string
158  * Implementation of IProperty::setProperty
159  */
160 // =====================================================================
162  PropertyBase* p = property( n );
163  return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
164 }
165 // =====================================================================
166 /* Retrieve the value of a property
167  * Implementation of IProperty::getProperty
168  */
169 // =====================================================================
171  try {
172  const PropertyBase* pp = property( p->name() );
173  if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS; // RETURN
174  } catch ( ... ) {}
175  return StatusCode::FAILURE; // RETURN
176 }
177 // =====================================================================
178 /* Get the property by name
179  * Implementation of IProperty::getProperty
180  */
181 // =====================================================================
183  const PropertyBase* p = property( name );
184  if ( !p ) throw std::out_of_range( "Property " + name + " not found." ); // Not found
185  return *p; // RETURN
186 }
187 // =====================================================================
188 /* Get the property by name
189  * Implementation of IProperty::getProperty
190  */
191 // =====================================================================
193  // get the property
194  const PropertyBase* p = property( n );
195  if ( !p ) return StatusCode::FAILURE;
196  // convert the value into the string
197  v = p->toString();
198  //
199  return StatusCode::SUCCESS;
200 }
201 // =====================================================================
202 // Implementation of IProperty::getProperties
203 // =====================================================================
205 // =====================================================================
206 // Implementation of IInterface::queryInterface
207 // =====================================================================
208 StatusCode PropertyMgr::queryInterface( const InterfaceID& iid, void** pinterface ) {
209  // try local interfaces
210  StatusCode sc = base_class::queryInterface( iid, pinterface );
211  if ( sc.isSuccess() ) return sc;
212  // fall back on the owner
213  return m_pOuter ? m_pOuter->queryInterface( iid, pinterface ) : sc; // FAILURE
214 }
215 // =====================================================================
216 // Implementation of IProperty::hasProperty
217 // =====================================================================
219  return any_of( begin( m_properties ), end( m_properties ),
220  [&name]( const PropertyBase* prop ) { return noCaseCmp( prop->name(), name ); } );
221 }
223  if ( LIKELY( !hasProperty( name ) ) ) return;
224  auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
225  if ( !msgSvc ) std::cerr << "error: cannot get MessageSvc!" << std::endl;
226  auto owner = SmartIF<INamedInterface>( m_pOuter );
227  MsgStream log( msgSvc, owner ? owner->name() : "PropertyMgr" );
228  log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
229  << endmsg;
230 }
231 // =====================================================================
232 // The END
233 // =====================================================================
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
T empty(T... args)
IInterface * m_pOuter
Interface hub reference (ApplicationMgr)
Definition: PropertyMgr.h:153
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:151
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:98
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyMgr.cpp:64
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
T endl(T... args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
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)
const std::string name() const
property name
Definition: Property.h:46
Gaudi::Details::PropertyBase Property
\fixme backward compatibility hack for old Property base class
Definition: PropertyFwd.h:35
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 StatusCode fromString(const std::string &value)=0
string -> value
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:86
T push_back(T... args)
Interface ID class.
Definition: IInterface.h:39
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:147
GAUDI_API ISvcLocator * svcLocator()
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:149
unsigned long addRef() override
Reference Interface instance.
Definition: implements.h:48
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Definition of the basic interface.
Definition: IInterface.h:254
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:47
T make_pair(T... args)
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:42
def end
Definition: IOTest.py:123
bool isSuccess() const
Definition: StatusCode.h:365
#define LIKELY(x)
Definition: Kernel.h:105
Gaudi::Details::PropertyBase * property(const std::string &name) const
T get(T... args)
T find_if(T... args)
T size(T... args)
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:340
STL class.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Definition: PropertyMgr.h:159
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
T begin(T... args)
T back(T... args)
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
constexpr static const auto FAILURE
Definition: StatusCode.h:101
bool isFailure() const
Definition: StatusCode.h:145
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:99
AttribStringParser::Iterator begin(const AttribStringParser &parser)
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: implements.h:30
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:30
T equal(T... args)
virtual std::string toString() const =0
value -> string
void toupper(std::string &s)
PropertyMgr(IInterface *iface=nullptr)
constructor from the interface
Definition: PropertyMgr.cpp:58
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
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)