| The Gaudi Framework v32r1 (f65d50dc)

Property.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // Include files
3 // ============================================================================
4 // STD & STL
5 // ============================================================================
6 #include <algorithm>
7 #include <functional>
8 #include <iostream>
9 #include <string>
10 #include <unordered_set>
11 #include <utility>
12 #include <vector>
13 // ============================================================================
14 // GaudiKernel
15 // ============================================================================
16 #include "GaudiKernel/GaudiHandle.h"
17 #include "GaudiKernel/IProperty.h"
18 #include "GaudiKernel/Property.h"
19 #include "GaudiKernel/PropertyHolder.h"
20 #include "GaudiKernel/SmartIF.h"
21 // ============================================================================
22 // Boost
23 // ============================================================================
24 #include "boost/algorithm/string/compare.hpp"
25 // ============================================================================
26 namespace {
28 struct PtrCmp {
29 bool operator()( const std::unique_ptr<std::string>& a, const std::unique_ptr<std::string>& b ) const {
30 return *a == *b;
31 }
32 };
33 struct PtrHash {
34 std::size_t operator()( const std::unique_ptr<std::string>& s ) const { return std::hash<std::string>()( *s ); }
35 };
37 std::unordered_set<std::unique_ptr<std::string>, PtrHash, PtrCmp> all_strings;
38 } // namespace
39
40 using Gaudi::Details::PropertyBase;
41
42 std::string_view PropertyBase::to_view( std::string str ) {
43 return **( all_strings.insert( std::make_unique<std::string>( std::move( str ) ) ).first );
44 }
45
46 // ============================================================================
47 // the printout of the property value
48 // ============================================================================
49 std::ostream& PropertyBase::fillStream( std::ostream& stream ) const {
50 return stream << " '" << name() << "':" << toString();
51 }
52
53 // ============================================================================
54 /* simple function which check the existence of the property with
55 * the given name.
56 *
57 * @code
58 *
59 * IInterface* p = .
60 *
61 * const bool = hasProperty( p , "Context" ) ;
62 *
63 * @endcode
64 *
65 * @param p pointer to IInterface object (any component)
66 * @param name property name (case insensitive)
67 * @return true if "p" has a property with such name
68 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
69 * @date 2006-09-09
70 */
71 // ============================================================================
72 bool Gaudi::Utils::hasProperty( const IInterface* p, const std::string& name ) {
73 // delegate to another method after trivial check
74 return p && getProperty( p, name );
75 }
76 // ============================================================================
77 /* simple function which check the existence of the property with
78 * the given name.
79 *
80 * @code
81 *
82 * const IProperty* p = ... ;
83 *
84 * const bool = hasProperty( p , "Context" ) ;
85 *
86 * @endcode
87 *
88 * @param p pointer to IProperty object
89 * @param name property name (case insensitive)
90 * @return true if "p" has a property with such name
91 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
92 * @date 2006-09-09
93 */
94 // ============================================================================
95 bool Gaudi::Utils::hasProperty( const IProperty* p, const std::string& name ) {
96 // delegate the actual work to another method ;
97 return p && getProperty( p, name );
98 }
99 // ============================================================================
100 //
101 // GaudiHandleProperty implementation
102 //
103 GaudiHandleProperty::GaudiHandleProperty( std::string name_, GaudiHandleBase& ref )
104 : PropertyWithHandlers( std::move( name_ ), typeid( GaudiHandleBase ) ), m_pValue( &ref ) {
105 m_pValue->setPropertyName( name() );
106 }
107
108 bool GaudiHandleProperty::setValue( const GaudiHandleBase& value ) {
109 m_pValue->setTypeAndName( value.typeAndName() );
110 return useUpdateHandler();
111 }
112
113 std::string GaudiHandleProperty::toString() const {
114 useReadHandler();
115 return m_pValue->typeAndName();
116 }
117
118 void GaudiHandleProperty::toStream( std::ostream& out ) const {
119 useReadHandler();
120 out << m_pValue->typeAndName();
121 }
122
123 StatusCode GaudiHandleProperty::fromString( const std::string& s ) {
124 m_pValue->setTypeAndName( s );
125 useUpdateHandler();
126 return StatusCode::SUCCESS;
127 }
128
129 //
130 // GaudiHandlePropertyArray implementation
131 //
132 GaudiHandleArrayProperty::GaudiHandleArrayProperty( std::string name_, GaudiHandleArrayBase& ref )
133 : PropertyWithHandlers( std::move( name_ ), typeid( GaudiHandleArrayBase ) ), m_pValue( &ref ) {
134 m_pValue->setPropertyName( name() );
135 }
136
137 bool GaudiHandleArrayProperty::setValue( const GaudiHandleArrayBase& value ) {
138 m_pValue->setTypesAndNames( value.typesAndNames() );
139 return useUpdateHandler();
140 }
141
142 std::string GaudiHandleArrayProperty::toString() const {
143 // treat as if a Gaudi::Property<std::vector<std::string>>
144 useReadHandler();
145 return Gaudi::Utils::toString( m_pValue->typesAndNames() );
146 }
147
148 void GaudiHandleArrayProperty::toStream( std::ostream& out ) const {
149 // treat as if a Gaudi::Property<std::vector<std::string>>
150 useReadHandler();
151 Gaudi::Utils::toStream( m_pValue->typesAndNames(), out );
152 }
153
154 StatusCode GaudiHandleArrayProperty::fromString( const std::string& source ) {
155 // treat as if a Gaudi::Property<std::vector<std::string>>
156 std::vector<std::string> tmp;
157 StatusCode sc = Gaudi::Parsers::parse( tmp, source );
158 if ( sc.isFailure() ) return sc;
159 if ( !m_pValue->setTypesAndNames( std::move( tmp ) ) ) return StatusCode::FAILURE;
160 useUpdateHandler();
161 return StatusCode::SUCCESS;
162 }
163
164 // ============================================================================
165 namespace {
166 template <typename C, typename BinaryPredicate>
167 bool equal_( const C& c1, const C& c2, BinaryPredicate&& p ) {
168 return c1.size() == c2.size() &&
169 std::equal( std::begin( c1 ), std::end( c1 ), std::begin( c2 ), std::forward<BinaryPredicate>( p ) );
170 }
171
172 // match (case insensitive) property by name
173 struct is_iByName {
175 is_iByName( const std::string& name ) : m_name( name ) {}
177 bool operator()( const PropertyBase* p ) const {
178 return p && equal_( m_name, p->name(), boost::algorithm::is_iequal{} );
179 };
180
181 private:
182 const std::string& m_name;
183 };
184 } // namespace
185 // ============================================================================
186 /* simple function which gets the property with given name
187 * from the component
188 *
189 * @code
190 *
191 * const IProperty* p = ... ;
192 *
193 * auto pro = getProperty( p , "Context" ) ;
194 *
195 * @endcode
196 *
197 * @param p pointer to IProperty object
198 * @param name property name (case insensitive)
199 * @return property with the given name (if exists), NULL otherwise
200 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
201 * @date 2006-09-09
202 */
203 // ============================================================================
204 PropertyBase* Gaudi::Utils::getProperty( const IProperty* p, const std::string& name ) {
205 // trivial check
206 if ( !p ) { return nullptr; } // RETURN
207 // get all properties
208 const auto& props = p->getProperties();
209 // comparison criteria:
210 auto ifound = std::find_if( props.begin(), props.end(), is_iByName{name} );
211 return ifound != props.end() ? *ifound : nullptr;
212 }
213 // ============================================================================
214 /* simple function which gets the property with given name
215 * from the component
216 *
217 * @code
218 *
219 * const IInterface* p = ... ;
220 *
221 * auto pro = getProperty( p , "Context" ) ;
222 *
223 * @endcode
224 *
225 * @param p pointer to IInterface object
226 * @param name property name (case insensitive)
227 * @return property with the given name (if exists), NULL otherwise
228 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
229 * @date 2006-09-09
230 */
231 // ============================================================================
232 PropertyBase* Gaudi::Utils::getProperty( const IInterface* p, const std::string& name ) {
233 // trivial check
234 if ( !p ) { return nullptr; } // RETURN
235 // remove const-qualifier
236 IInterface* _i = const_cast<IInterface*>( p );
237 if ( !_i ) { return nullptr; } // RETURN
238 SmartIF<IProperty> property( _i );
239 return property ? getProperty( property, name ) : nullptr;
240 }
241 // ============================================================================
242 /* check the property by name from the list of the properties
243 *
244 * @code
245 *
246 * IJobOptionsSvc* svc = ... ;
247 *
248 * const std::string client = ... ;
249 *
250 * // get the property:
251 * bool context =
252 * hasProperty ( svc->getProperties( client ) , "Context" )
253 *
254 * @endcode
255 *
256 * @see IJobOptionsSvc
257 *
258 * @param p list of properties
259 * @param name property name (case insensitive)
260 * @return true if the property exists
261 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
262 * @date 2006-09-09
263 */
264 // ============================================================================
265 bool Gaudi::Utils::hasProperty( const std::vector<const PropertyBase*>* p, const std::string& name ) {
266 // delegate to another method
267 return getProperty( p, name );
268 }
269 // ============================================================================
270 /* get the property by name from the list of the properties
271 *
272 * @code
273 *
274 * IJobOptionsSvc* svc = ... ;
275 *
276 * const std::string client = ... ;
277 *
278 * // get the property:
279 * auto context = getProperty ( svc->getProperties( client ) , "Context" )
280 *
281 * @endcode
282 *
283 * @see IJobOptionsSvc
284 *
285 * @param p list of properties
286 * @param name property name (case insensitive)
287 * @return property with the given name (if exists), NULL otherwise
288 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
289 * @date 2006-09-09
290 */
291 // ============================================================================
292 const PropertyBase* Gaudi::Utils::getProperty( const std::vector<const PropertyBase*>* p, const std::string& name ) {
293 // trivial check
294 if ( !p ) { return nullptr; } // RETURN
295 auto ifound = std::find_if( p->begin(), p->end(), is_iByName{name} );
296 return p->end() != ifound ? *ifound : nullptr; // RETURN
297 }
298 // ============================================================================
299 /* the full specialization of the
300 * method setProperty( IProperty, std::string, const TYPE&)
301 * for C-strings
302 *
303 * @param component component which needs to be configured
304 * @param name name of the property
305 * @param value value of the property
306 * @param doc the new documentation string
307 *
308 * @see IProperty
309 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
310 * @date 2007-05-13
311 */
312 // ============================================================================
313 StatusCode Gaudi::Utils::setProperty( IProperty* component, const std::string& name, const char* value,
314 const std::string& doc ) {
315 return Gaudi::Utils::setProperty( component, name, std::string{value}, doc );
316 }
317 // ============================================================================
318 /* the full specialization of the
319 * method Gaudi::Utils::setProperty( IProperty, std::string, const TYPE&)
320 * for standard strings
321 *
322 * @param component component which needs to be configured
323 * @param name name of the property
324 * @param value value of the property
325 *
326 * @see IProperty
327 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
328 * @date 2007-05-13
329 */
330 // ============================================================================
331 StatusCode Gaudi::Utils::setProperty( IProperty* component, const std::string& name, const std::string& value,
332 const std::string& doc ) {
333 if ( !component ) { return StatusCode::FAILURE; } // RETURN
334 if ( !hasProperty( component, name ) ) { return StatusCode::FAILURE; }
335 StatusCode sc = component->setProperty( name, value );
336 if ( !doc.empty() ) {
337 PropertyBase* p = getProperty( component, name );
338 if ( p ) { p->setDocumentation( doc ); }
339 }
340 sc.ignore();
341 return sc;
342 }
343 // ============================================================================
344 /* simple function to set the property of the given object from another
345 * property
346 *
347 * @code
348 *
349 * IProperty* component = ... ;
350 *
351 * const Gaudi::Details::PropertyBase* prop = ... ;
352 * StatusCode sc = setProperty ( component , "Data" , prop ) ;
353 *
354 * @endcode
355 *
356 * @param component component which needs to be configured
357 * @param name name of the property
358 * @param property the property
359 * @param doc the new documentation string
360 *
361 * @see IProperty
362 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
363 * @date 2007-05-13
364 */
365 // ============================================================================
366 StatusCode Gaudi::Utils::setProperty( IProperty* component, const std::string& name, const PropertyBase* property,
367 const std::string& doc ) {
368 if ( !component || !property ) return StatusCode::FAILURE;
369 PropertyBase* p = getProperty( component, name );
370 if ( !p || !p->assign( *property ) ) return StatusCode::FAILURE;
371 if ( !doc.empty() ) { p->setDocumentation( doc ); }
372 return StatusCode::SUCCESS;
373 }
374 // ============================================================================
375 /* simple function to set the property of the given object from another
376 * property
377 *
378 * @code
379 *
380 * IProperty* component = ... ;
381 *
382 * const Gaudi::Details::PropertyBase& prop = ... ;
383 * StatusCode sc = setProperty ( component , "Data" , prop ) ;
384 *
385 * @endcode
386 *
387 * @param component component which needs to be configured
388 * @param name name of the property
389 * @param property the property
390 * @param doc the new documentation string
391 *
392 * @see IProperty
393 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
394 * @date 2007-05-13
395 */
396 // ============================================================================
397 StatusCode Gaudi::Utils::setProperty( IProperty* component, const std::string& name, const PropertyBase& property,
398 const std::string& doc ) {
399 return setProperty( component, name, &property, doc );
400 }
401 // ============================================================================
402 /* the full specialization of the
403 * method setProperty( IInterface , std::string, const TYPE&)
404 * for standard strings
405 *
406 * @param component component which needs to be configured
407 * @param name name of the property
408 * @param value value of the property
409 * @param doc the new documentation string
410 *
411 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
412 * @date 2007-05-13
413 */
414 // ============================================================================
415 StatusCode Gaudi::Utils::setProperty( IInterface* component, const std::string& name, const std::string& value,
416 const std::string& doc ) {
417 if ( !component ) { return StatusCode::FAILURE; }
418 SmartIF<IProperty> property( component );
419 return property ? setProperty( property, name, value, doc ) : StatusCode::FAILURE;
420 }
421 // ============================================================================
422 /* the full specialization of the
423 * method setProperty( IInterface , std::string, const TYPE&)
424 * for C-strings
425 *
426 * @param component component which needs to be configured
427 * @param name name of the property
428 * @param value value of the property
429 * @param doc the new documentation string
430 *
431 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
432 * @date 2007-05-13
433 */
434 // ============================================================================
435 StatusCode Gaudi::Utils::setProperty( IInterface* component, const std::string& name, const char* value,
436 const std::string& doc ) {
437 return setProperty( component, name, std::string{value}, doc );
438 }
439 // ============================================================================
440 /* simple function to set the property of the given object from another
441 * property
442 *
443 * @code
444 *
445 * IInterface* component = ... ;
446 *
447 * const Gaudi::Details::PropertyBase* prop = ... ;
448 * StatusCode sc = setProperty ( component , "Data" , prop ) ;
449 *
450 * @endcode
451 *
452 * @param component component which needs to be configured
453 * @param name name of the property
454 * @param property the property
455 * @param doc the new documentation string
456 *
457 * @see IProperty
458 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
459 * @date 2007-05-13
460 */
461 // ============================================================================
462 StatusCode Gaudi::Utils::setProperty( IInterface* component, const std::string& name, const PropertyBase* property,
463 const std::string& doc ) {
464 if ( !component ) { return StatusCode::FAILURE; }
465 SmartIF<IProperty> prop( component );
466 if ( !prop ) { return StatusCode::FAILURE; }
467 return setProperty( prop, name, property, doc );
468 }
469 // ============================================================================
470 /* simple function to set the property of the given object from another
471 * property
472 *
473 * @code
474 *
475 * IInterface* component = ... ;
476 *
477 * const Gaudi::Details::PropertyBase& prop = ... ;
478 * StatusCode sc = setProperty ( component , "Data" , prop ) ;
479 *
480 * @endcode
481 *
482 * @param component component which needs to be configured
483 * @param name name of the property
484 * @param property the property
485 * @param doc the new documentation string
486 *
487 * @see IProperty
488 * @author Vanya BELYAEV ibelyaev@physics.syr.edu
489 * @date 2007-05-13
490 */
491 // ============================================================================
492 StatusCode Gaudi::Utils::setProperty( IInterface* component, const std::string& name, const PropertyBase& property,
493 const std::string& doc ) {
494 return setProperty( component, name, &property, doc );
495 }
496 // ============================================================================
497
498 // ============================================================================
499 // The END
500 // ============================================================================
Gaudi::Utils::setProperty
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:1179
PropertyHolder.h
SmartIF< IProperty >
std::string::empty
T empty(T... args)
Gaudi::Utils::toStream
std::ostream & toStream(ITERATOR first, ITERATOR last, std::ostream &s, const std::string &open, const std::string &close, const std::string &delim)
the helper function to print the sequence
Definition: ToStream.h:284
GaudiHandleArrayProperty::setValue
bool setValue(const GaudiHandleArrayBase &value)
Definition: Property.cpp:137
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
Gaudi::Details::PropertyBase::setDocumentation
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:86
GaudiHandleBase::setTypeAndName
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
GaudiHandleArrayProperty::value
const GaudiHandleArrayBase & value() const
Definition: Property.h:903
Gaudi::Details::PropertyBase::assign
virtual bool assign(const PropertyBase &source)=0
import the property value form the source
GaudiHandleBase::typeAndName
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:115
GaudiHandleArrayBase::typesAndNames
const std::vector< std::string > typesAndNames() const
Return a vector with "type/name" strings of all handles in the array.
Definition: GaudiHandle.cpp:68
GaudiHandleProperty::fromString
StatusCode fromString(const std::string &s) override
string -> value
Definition: Property.cpp:123
Gaudi::Utils::hasProperty
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:95
Gaudi::Utils::toString
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:334
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
std
STL namespace.
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
std::size_t
getProperty
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:237
GaudiHandleArrayProperty::toStream
void toStream(std::ostream &out) const override
value -> stream
Definition: Property.cpp:148
PropertyWithHandlers
Helper class to simplify the migration old properties deriving directly from PropertyBase.
Definition: Property.h:806
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
IProperty::setProperty
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
GaudiHandleProperty::m_pValue
GaudiHandleBase * m_pValue
Pointer to the real property.
Definition: Property.h:876
GaudiHandleProperty::GaudiHandleProperty
GaudiHandleProperty(std::string name, GaudiHandleBase &ref)
Definition: Property.cpp:103
std::hash
GaudiHandleArrayBase::setTypesAndNames
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in <myTypesAndNamesList>.
Definition: GaudiHandle.cpp:53
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
IInterface
Definition of the basic interface.
Definition: IInterface.h:244
SmartIF.h
GaudiHandleArrayProperty::GaudiHandleArrayProperty
GaudiHandleArrayProperty(std::string name, GaudiHandleArrayBase &ref)
Definition: Property.cpp:132
setProperty
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:233
std::unordered_set
STL class.
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
GaudiHandleProperty::toString
std::string toString() const override
value -> string
Definition: Property.cpp:113
Gaudi::Details::PropertyBase::fillStream
virtual std::ostream & fillStream(std::ostream &) const
the printout of the property value
Definition: Property.cpp:49
std::move
T move(T... args)
GaudiHandleArrayProperty::fromString
StatusCode fromString(const std::string &s) override
string -> value
Definition: Property.cpp:154
std::find_if
T find_if(T... args)
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
GaudiHandleArrayBase
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:330
IProperty.h
std::vector< std::string >
std::unique_ptr
STL class.
GaudiHandleProperty::toStream
void toStream(std::ostream &out) const override
value -> stream
Definition: Property.cpp:118
GaudiHandleInfo::setPropertyName
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:50
PropertyWithHandlers<>::useUpdateHandler
bool useUpdateHandler() override
use the call-back function at update, if available
Definition: Property.h:832
std::begin
T begin(T... args)
Property.h
GaudiHandleArrayProperty::m_pValue
GaudiHandleArrayBase * m_pValue
Pointer to the real property.
Definition: Property.h:913
IProperty::getProperties
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
gaudirun.s
string s
Definition: gaudirun.py:316
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
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
GaudiHandleArrayProperty::toString
std::string toString() const override
value -> string
Definition: Property.cpp:142
GaudiHandleProperty::value
const GaudiHandleBase & value() const
Definition: Property.h:866
IProperty
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition: IProperty.h:20
std::ostream
STL class.
std::equal
T equal(T... args)
Gaudi::Details::PropertyBase::toString
virtual std::string toString() const =0
value -> string
GaudiHandle.h
GaudiHandleProperty::setValue
bool setValue(const GaudiHandleBase &value)
Definition: Property.cpp:108
Gaudi::Utils::getProperty
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:204
PropertyWithHandlers<>::useReadHandler
void useReadHandler() const
use the call-back function at reading, if available
Definition: Property.h:829