| The Gaudi Framework v32r1 (f65d50dc)

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"
18 #include "GaudiKernel/GaudiException.h"
19 #include "GaudiKernel/IMessageSvc.h"
20 #include "GaudiKernel/INamedInterface.h"
21 #include "GaudiKernel/ISvcLocator.h"
22 #include "GaudiKernel/PropertyMgr.h"
23 // ============================================================================
24
25 using Gaudi::Details::PropertyBase;
26
27 namespace {
28 // ==========================================================================
30 constexpr struct NoCaseCmp_t {
31 inline bool operator()( const std::string& v1, const std::string& v2 ) const {
32 return v1.size() == v2.size() && std::equal( std::begin( v1 ), std::end( v1 ), std::begin( v2 ),
33 []( char c1, char c2 ) { return toupper( c1 ) == toupper( c2 ); } );
34 }
35 } noCaseCmp{};
36 // ==========================================================================
38 struct PropByName {
39 std::string m_name;
40
41 inline bool operator()( const PropertyBase* p ) const { return p && noCaseCmp( p->name(), m_name ); }
42 };
43 // ==========================================================================
44 } // namespace
45 // ====================================================================
46 // constructor from the interface
47 // ====================================================================
48 PropertyMgr::PropertyMgr( IInterface* iface ) : m_pOuter( iface ) {
49 addRef(); // initial reference count set to 1
50 }
51 // ====================================================================
52 // Declare a remote property
53 // ====================================================================
54 PropertyBase* PropertyMgr::declareRemoteProperty( const std::string& name, IProperty* rsvc, const std::string& rname ) {
55 if ( !rsvc ) return nullptr;
56 const std::string& nam = rname.empty() ? name : rname;
57 PropertyBase* p = property( nam, rsvc->getProperties() );
58 m_remoteProperties.emplace_back( name, std::make_pair( rsvc, nam ) );
59 return p;
60 }
61 // ============================================================================
62 // Declare a property
63 // ============================================================================
64 PropertyBase* PropertyMgr::declareProperty( const std::string& name, GaudiHandleBase& ref, const std::string& doc ) {
65 assertUniqueName( name );
66 m_todelete.emplace_back( new typename GaudiHandleBase::PropertyType( name, ref ) );
67 Property* p = m_todelete.back().get();
68 //
69 p->setDocumentation( doc );
70 m_properties.push_back( p );
71 //
72 return p;
73 }
74 // ============================================================================
75 PropertyBase* PropertyMgr::declareProperty( const std::string& name, GaudiHandleArrayBase& ref,
76 const std::string& doc ) {
77 assertUniqueName( name );
78 m_todelete.emplace_back( new typename GaudiHandleArrayBase::PropertyType( name, ref ) );
79 Property* p = m_todelete.back().get();
80 //
81 p->setDocumentation( doc );
82 m_properties.push_back( p );
83 //
84 return p;
85 }
86
87 // ============================================================================
88 PropertyBase* PropertyMgr::declareProperty( const std::string& name, DataObjectHandleBase& ref,
89 const std::string& doc ) {
90 assertUniqueName( name );
91 m_todelete.emplace_back( new typename DataObjectHandleBase::PropertyType( name, ref ) );
92 Property* p = m_todelete.back().get();
93 //
94 p->setDocumentation( doc );
95 m_properties.push_back( p );
96 //
97 return p;
98 }
99 // ====================================================================
100 // get the property by name form the proposed list
101 // ====================================================================
102 PropertyBase* PropertyMgr::property( const std::string& name, const std::vector<PropertyBase*>& props ) const {
103 auto it = std::find_if( props.begin(), props.end(), PropByName{name} );
104 return ( it != props.end() ) ? *it : nullptr; // RETURN
105 }
106 // ====================================================================
107 // retrieve the property by name
108 // ====================================================================
109 PropertyBase* PropertyMgr::property( const std::string& name ) const {
110 // local property ?
111 PropertyBase* lp = property( name, m_properties );
112 if ( lp ) return lp; // RETURN
113 // look for remote property
114 for ( const auto& it : m_remoteProperties ) {
115 if ( !noCaseCmp( it.first, name ) ) continue; // CONTINUE
116 const IProperty* p = it.second.first;
117 if ( !p ) continue;
118 return property( it.second.second, p->getProperties() ); // RETURN
119 }
120 return nullptr; // RETURN
121 }
122 // ====================================================================
123 /* set a property from another property
124 * Implementation of IProperty::setProperty
125 */
126 // =====================================================================
127 StatusCode PropertyMgr::setProperty( const PropertyBase& p ) {
128 PropertyBase* pp = property( p.name() );
129 try {
130 if ( pp && pp->assign( p ) ) return StatusCode::SUCCESS;
131 } // RETURN
132 catch ( ... ) {}
133 //
134 return StatusCode::FAILURE;
135 }
136 // ====================================================================
137 /* set a property from the stream
138 * Implementation of IProperty::setProperty
139 */
140 // =====================================================================
141 StatusCode PropertyMgr::setProperty( const std::string& i ) {
142 std::string name, value;
143 StatusCode sc = Gaudi::Parsers::parse( name, value, i );
144 return sc.isFailure() ? sc : setProperty( name, value );
145 }
146 // =====================================================================
147 /* set a property from the string
148 * Implementation of IProperty::setProperty
149 */
150 // =====================================================================
151 StatusCode PropertyMgr::setProperty( const std::string& n, const std::string& v ) {
152 PropertyBase* p = property( n );
153 return ( p && p->fromString( v ) ) ? StatusCode::SUCCESS : StatusCode::FAILURE;
154 }
155 // =====================================================================
156 /* Retrieve the value of a property
157 * Implementation of IProperty::getProperty
158 */
159 // =====================================================================
160 StatusCode PropertyMgr::getProperty( PropertyBase* p ) const {
161 try {
162 const PropertyBase* pp = property( p->name() );
163 if ( pp && pp->load( *p ) ) return StatusCode::SUCCESS; // RETURN
164 } catch ( ... ) {}
165 return StatusCode::FAILURE; // RETURN
166 }
167 // =====================================================================
168 /* Get the property by name
169 * Implementation of IProperty::getProperty
170 */
171 // =====================================================================
172 const PropertyBase& PropertyMgr::getProperty( const std::string& name ) const {
173 const PropertyBase* p = property( name );
174 if ( !p ) throw std::out_of_range( "Property " + name + " not found." ); // Not found
175 return *p; // RETURN
176 }
177 // =====================================================================
178 /* Get the property by name
179 * Implementation of IProperty::getProperty
180 */
181 // =====================================================================
182 StatusCode PropertyMgr::getProperty( const std::string& n, std::string& v ) const {
183 // get the property
184 const PropertyBase* p = property( n );
185 if ( !p ) return StatusCode::FAILURE;
186 // convert the value into the string
187 v = p->toString();
188 //
189 return StatusCode::SUCCESS;
190 }
191 // =====================================================================
192 // Implementation of IProperty::getProperties
193 // =====================================================================
194 const std::vector<PropertyBase*>& PropertyMgr::getProperties() const { return m_properties; }
195 // =====================================================================
196 // Implementation of IInterface::queryInterface
197 // =====================================================================
198 StatusCode PropertyMgr::queryInterface( const InterfaceID& iid, void** pinterface ) {
199 // try local interfaces
200 StatusCode sc = base_class::queryInterface( iid, pinterface );
201 if ( sc.isSuccess() ) return sc;
202 // fall back on the owner
203 return m_pOuter ? m_pOuter->queryInterface( iid, pinterface ) : sc; // FAILURE
204 }
205 // =====================================================================
206 // Implementation of IProperty::hasProperty
207 // =====================================================================
208 bool PropertyMgr::hasProperty( const std::string& name ) const {
209 return any_of( begin( m_properties ), end( m_properties ),
210 [&name]( const PropertyBase* prop ) { return noCaseCmp( prop->name(), name ); } );
211 }
212 void PropertyMgr::assertUniqueName( const std::string& name ) const {
213 if ( LIKELY( !hasProperty( name ) ) ) return;
214 auto msgSvc = Gaudi::svcLocator()->service<IMessageSvc>( "MessageSvc" );
215 if ( !msgSvc ) std::cerr << "error: cannot get MessageSvc!" << std::endl;
216 auto owner = SmartIF<INamedInterface>( m_pOuter );
217 MsgStream log( msgSvc, owner ? owner->name() : "PropertyMgr" );
218 log << MSG::WARNING << "duplicated property name '" << name << "', see https://its.cern.ch/jira/browse/GAUDI-1023"
219 << endmsg;
220 }
221 // =====================================================================
222 // The END
223 // =====================================================================
std::out_of_range
STL class.
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
SmartIF< INamedInterface >
std::string::empty
T empty(T... args)
PropertyMgr::m_pOuter
IInterface * m_pOuter
Interface hub reference (ApplicationMgr)
Definition: PropertyMgr.h:143
PropertyMgr::m_todelete
std::vector< std::unique_ptr< Gaudi::Details::PropertyBase > > m_todelete
Properties to be deleted.
Definition: PropertyMgr.h:141
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:86
PropertyMgr::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
Definition: PropertyMgr.cpp:127
PropertyMgr::queryInterface
StatusCode queryInterface(const InterfaceID &iid, void **pinterface) override
Definition: PropertyMgr.cpp:198
PropertyMgr::declareRemoteProperty
Gaudi::Details::PropertyBase * declareRemoteProperty(const std::string &name, IProperty *rsvc, const std::string &rname="")
Declare a remote property.
Definition: PropertyMgr.cpp:54
PropertyMgr::getProperty
StatusCode getProperty(Gaudi::Details::PropertyBase *p) const override
get the property
Definition: PropertyMgr.cpp:160
std::endl
T endl(T... args)
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Bootstrap.h
GaudiHandleProperty
Definition: Property.h:845
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:11
std::end
T end(T... args)
Gaudi::Details::PropertyBase::name
const std::string name() const
property name
Definition: Property.h:36
Property
Gaudi::Details::PropertyBase Property
\fixme backward compatibility hack for old Property base class
Definition: PropertyFwd.h:25
PropertyMgr::assertUniqueName
void assertUniqueName(const std::string &name) const
Throw an exception if the name is already present in the list of properties (see GAUDI-1023).
Definition: PropertyMgr.cpp:212
Gaudi::Details::PropertyBase::fromString
virtual StatusCode fromString(const std::string &value)=0
string -> value
DataObjectHandleProperty
DataObjectHandleProperty.h GaudiKernel/DataObjectHandleProperty.h.
Definition: DataObjectHandleProperty.h:23
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:67
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
ISvcLocator::service
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:76
std::vector::push_back
T push_back(T... args)
InterfaceID
Interface ID class.
Definition: IInterface.h:29
INamedInterface.h
PropertyMgr::m_properties
Properties m_properties
Collection of all declared properties.
Definition: PropertyMgr.h:137
Gaudi::svcLocator
GAUDI_API ISvcLocator * svcLocator()
PropertyMgr::m_remoteProperties
RemoteProperties m_remoteProperties
Collection of all declared remote properties.
Definition: PropertyMgr.h:139
implements< IProperty >::addRef
unsigned long addRef() override
Reference Interface instance.
Definition: implements.h:38
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
GaudiException.h
IInterface
Definition of the basic interface.
Definition: IInterface.h:244
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
std::make_pair
T make_pair(T... args)
IMessageSvc.h
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
IOTest.end
def end
Definition: IOTest.py:112
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
LIKELY
#define LIKELY(x)
Definition: Kernel.h:95
PropertyMgr::property
Gaudi::Details::PropertyBase * property(const std::string &name) const
Definition: PropertyMgr.cpp:109
std::unique_ptr::get
T get(T... args)
std::find_if
T find_if(T... args)
std::string::size
T size(T... args)
GaudiHandleArrayBase
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:330
std::vector
STL class.
PropertyMgr::declareProperty
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Definition: PropertyMgr.h:149
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:26
std::begin
T begin(T... args)
PropertyMgr.h
std::vector::back
T back(T... args)
IProperty::getProperties
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
PropertyMgr::hasProperty
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
Definition: PropertyMgr.cpp:208
PropertyMgr::getProperties
const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const override
get all properties
Definition: PropertyMgr.cpp:194
compareOutputFiles.pp
pp
Definition: compareOutputFiles.py:495
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
MSG::WARNING
Definition: IMessageSvc.h:15
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
GaudiHandleBase
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:89
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:120
implements::queryInterface
StatusCode queryInterface(const InterfaceID &ti, void **pp) override
Implementation of IInterface::queryInterface.
Definition: implements.h:20
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
GaudiHandleArrayProperty
Definition: Property.h:882
std::cerr
ISvcLocator.h
std::equal
T equal(T... args)
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
toupper
void toupper(std::string &s)
Definition: StatusCodeSvc.cpp:7
PropertyMgr::PropertyMgr
PropertyMgr(IInterface *iface=nullptr)
constructor from the interface
Definition: PropertyMgr.cpp:48
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Gaudi.Configuration.log
log
Definition: Configuration.py:13
IInterface::queryInterface
virtual StatusCode queryInterface(const InterfaceID &ti, void **pp)=0
Set the void** to the pointer to the requested interface of the instance.
std::vector::emplace_back
T emplace_back(T... args)