Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v29r3 (fa547fc2)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 // ============================================================================
17 #include "GaudiKernel/IProperty.h"
18 #include "GaudiKernel/Property.h"
20 #include "GaudiKernel/SmartIF.h"
21 // ============================================================================
22 // Boost
23 // ============================================================================
24 #include "boost/algorithm/string/compare.hpp"
25 // ============================================================================
26 namespace
27 {
29  struct PtrCmp {
31  {
32  return *a == *b;
33  }
34  };
35  struct PtrHash {
37  };
39  std::unordered_set<std::unique_ptr<std::string>, PtrHash, PtrCmp> all_strings;
40 }
41 
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 // ============================================================================
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 // ============================================================================
101 {
102  // delegate the actual work to another method ;
103  return p && getProperty( p, name );
104 }
105 // ============================================================================
106 //
107 // GaudiHandleProperty implementation
108 //
110  : PropertyWithHandlers( std::move( name_ ), typeid( GaudiHandleBase ) ), m_pValue( &ref )
111 {
113 }
114 
116 {
117  m_pValue->setTypeAndName( value.typeAndName() );
118  return useUpdateHandler();
119 }
120 
122 {
123  useReadHandler();
124  return m_pValue->typeAndName();
125 }
126 
128 {
129  useReadHandler();
130  out << m_pValue->typeAndName();
131 }
132 
134 {
135  m_pValue->setTypeAndName( s );
137  return StatusCode::SUCCESS;
138 }
139 
140 //
141 // GaudiHandlePropertyArray implementation
142 //
144  : PropertyWithHandlers( std::move( name_ ), typeid( GaudiHandleArrayBase ) ), m_pValue( &ref )
145 {
147 }
148 
150 {
152  return useUpdateHandler();
153 }
154 
156 {
157  // treat as if a Gaudi::Property<std::vector<std::string>>
158  useReadHandler();
160 }
161 
163 {
164  // treat as if a Gaudi::Property<std::vector<std::string>>
165  useReadHandler();
167 }
168 
170 {
171  // treat as if a Gaudi::Property<std::vector<std::string>>
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;
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 // ============================================================================
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 // ============================================================================
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
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
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 // ============================================================================
545  const std::string& doc )
546 {
547  return setProperty( component, name, &property, doc );
548 }
549 // ============================================================================
550 
551 // ============================================================================
552 // The END
553 // ============================================================================
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:1173
T empty(T...args)
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
bool setValue(const GaudiHandleArrayBase &value)
Definition: Property.cpp:149
void setDocumentation(std::string value)
set the documentation string
Definition: Property.h:92
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
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
StatusCode fromString(const std::string &s) override
string -> value
Definition: Property.cpp:133
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
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:346
Gaudi::Details::PropertyBase * property(const std::string &name) const
STL namespace.
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)
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:61
std::string toString() const override
value -> string
Definition: Property.cpp:121
Helper class to simplify the migration old properties deriving directly from PropertyBase.
Definition: Property.h:786
STL class.
virtual StatusCode setProperty(const Gaudi::Details::PropertyBase &p)=0
Set the property by property.
GaudiHandleBase * m_pValue
Pointer to the real property.
Definition: Property.h:863
GaudiHandleProperty(std::string name, GaudiHandleBase &ref)
Definition: Property.cpp:109
boost::string_ref m_name
property name
Definition: Property.h:137
bool hasProperty(const std::string &name) const override
Return true if we have a property with the given name.
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
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:277
GaudiHandleArrayProperty(std::string name, GaudiHandleArrayBase &ref)
Definition: Property.cpp:143
const GaudiHandleBase & value() const
Definition: Property.h:852
void toStream(std::ostream &out) const override
value -> stream
Definition: Property.cpp:162
const GaudiHandleArrayBase & value() const
Definition: Property.h:892
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: Property.h:32
T move(T...args)
virtual const std::vector< Gaudi::Details::PropertyBase * > & getProperties() const =0
Get list of properties.
StatusCode fromString(const std::string &s) override
string -> value
Definition: Property.cpp:169
T find_if(T...args)
Base class of array&#39;s of various gaudihandles.
Definition: GaudiHandle.h:348
void useReadHandler() const
use the call-back function at reading, if available
Definition: Property.h:812
STL class.
virtual Out operator()(const vector_of_const_< In > &inputs) const =0
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:44
bool useUpdateHandler() override
use the call-back function at update, if available
Definition: Property.h:815
T begin(T...args)
std::string toString() const override
value -> string
Definition: Property.cpp:155
GaudiHandleArrayBase * m_pValue
Pointer to the real property.
Definition: Property.h:903
string s
Definition: gaudirun.py:253
const std::vector< std::string > typesAndNames() const
Return a vector with "type/name" strings of all handles in the array.
Definition: GaudiHandle.cpp:75
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:83
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:111
void ignore() const
Definition: StatusCode.h:84
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
STL class.
T equal(T...args)
void toStream(std::ostream &out) const override
value -> stream
Definition: Property.cpp:127
std::string toString(const Type &)
bool setValue(const GaudiHandleBase &value)
Definition: Property.cpp:115
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