| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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