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 <iostream>
7 #include <vector>
8 #include <string>
9 #include <utility>
10 #include <algorithm>
11 #include <functional>
12 // ============================================================================
13 // GaudiKernel
14 // ============================================================================
15 #include "GaudiKernel/IProperty.h"
16 #include "GaudiKernel/SmartIF.h"
17 #include "GaudiKernel/Property.h"
18 #include "GaudiKernel/GaudiHandle.h"
19 // ============================================================================
20 // Boost
21 // ============================================================================
22 #include "boost/algorithm/string/compare.hpp"
23 // ============================================================================
29 // ============================================================================
30 // The output operator for friendly printout
31 // ============================================================================
32 std::ostream& operator<<( std::ostream& stream ,
33  const Property& prop )
34 { return prop.fillStream ( stream ) ; }
35 // ============================================================================
36 /* constructor from the property name and the type
37  * @param name proeprty name
38  * @param type property C++/RTTI type
39  */
40 // ============================================================================
42 ( const std::type_info& type ,
43  std::string name )
44  : m_name ( std::move(name) )
45  , m_documentation ( m_name )
46  , m_typeinfo ( &type )
47 {}
48 // ============================================================================
49 /* constructor from the property name and the type
50  * @param type property C++/RTTI type
51  * @param name proeprty name
52  */
53 // ============================================================================
55 ( std::string name ,
56  const std::type_info& type )
57  : m_name ( std::move(name) )
58  , m_documentation ( m_name )
59  , m_typeinfo ( &type )
60 {}
61 // ============================================================================
62 // set new callback for reading
63 // ============================================================================
65 {
66  m_readCallBack = std::move(fun);
67  return *this;
68 }
69 // ============================================================================
70 // set new callback for update
71 // ============================================================================
73 {
74  m_updateCallBack = std::move(fun);
75  return *this;
76 }
77 // ============================================================================
78 // use the call-back function at reading
79 // ============================================================================
81 {
82  if ( !m_readCallBack ) { return ; } // RETURN
83  // avoid infinite loop
84  std::function<void(Property&)> theCallBack;
85  theCallBack.swap(m_readCallBack);
86  theCallBack( const_cast<Property&>(*this) ) ;
87  m_readCallBack.swap(theCallBack);
88 }
89 // ============================================================================
90 // use the call-back function at update
91 // ============================================================================
93 {
94  bool sc(true);
95  if ( !m_updateCallBack ) { return sc; } // RETURN
96  // avoid infinite loop
97  std::function<void(Property&)> theCallBack;
98  theCallBack.swap(m_updateCallBack);
99  try {
100  theCallBack( *this ) ;
101  } catch(...) {
102  sc = false;
103  }
104  m_updateCallBack.swap(theCallBack);
105  return sc;
106 }
107 // ============================================================================
108 // the printout of the property value
109 // ============================================================================
110 std::ostream&
111 Property::fillStream ( std::ostream& stream ) const
112 { return stream << " '" <<name() << "':" << toString() ; }
113 // ============================================================================
114 /* simple function which check the existence of the property with
115  * the given name.
116  *
117  * @code
118  *
119  * IInterface* p = .
120  *
121  * const bool = hasProperty( p , "Context" ) ;
122  *
123  * @endcode
124  *
125  * @param p pointer to IInterface object (any component)
126  * @param name property name (case insensitive)
127  * @return true if "p" has a property with such name
128  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
129  * @date 2006-09-09
130  */
131 // ============================================================================
133 ( const IInterface* p ,
134  const std::string& name )
135 {
136  // delegate to another method after trivial check
137  return p && getProperty ( p , name ) ;
138 }
139 // ============================================================================
140 /* simple function which check the existence of the property with
141  * the given name.
142  *
143  * @code
144  *
145  * const IProperty* p = ... ;
146  *
147  * const bool = hasProperty( p , "Context" ) ;
148  *
149  * @endcode
150  *
151  * @param p pointer to IProperty object
152  * @param name property name (case insensitive)
153  * @return true if "p" has a property with such name
154  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
155  * @date 2006-09-09
156  */
157 // ============================================================================
159 ( const IProperty* p ,
160  const std::string& name )
161 {
162  // delegate the actual work to another method ;
163  return p && getProperty ( p , name ) ;
164 }
165 // ============================================================================
166 //
167 // GaudiHandleProperty implementation
168 //
170 ( std::string name_, GaudiHandleBase& ref )
171  : Property( std::move(name_), typeid( GaudiHandleBase ) ), m_pValue( &ref )
172 {
173  m_pValue->setPropertyName( name() );
174 }
175 
177  m_pValue->setTypeAndName( value.typeAndName() );
178  return useUpdateHandler();
179 }
180 
181 std::string GaudiHandleProperty::toString( ) const {
182  useReadHandler();
183  return m_pValue->typeAndName();
184 }
185 
186 void GaudiHandleProperty::toStream(std::ostream& out) const {
187  useReadHandler();
188  out << m_pValue->typeAndName();
189 }
190 
192  m_pValue->setTypeAndName( s );
194 }
195 
196 
197 //
198 // GaudiHandlePropertyArray implementation
199 //
201  : Property( std::move(name_), typeid( GaudiHandleArrayBase ) ), m_pValue( &ref )
202 {
204 }
205 
208  return useUpdateHandler();
209 }
210 
212  // treat as if a StringArrayProperty
213  useReadHandler();
215 }
216 
217 void GaudiHandleArrayProperty::toStream(std::ostream &out) const {
218  // treat as if a StringArrayProperty
219  useReadHandler();
221 }
222 
223 StatusCode GaudiHandleArrayProperty::fromString( const std::string& source ) {
224  // treat as if a StringArrayProperty
225  std::vector< std::string > tmp;
226  StatusCode sc = Gaudi::Parsers::parse ( tmp , source );
227  if ( sc.isFailure() ) return sc;
228  if ( !m_pValue->setTypesAndNames( std::move(tmp) ) ) return StatusCode::FAILURE;
230 }
231 
232 
233 
234 // ============================================================================
235 namespace
236 {
237  template <typename C, typename BinaryPredicate>
238  bool equal_(const C& c1, const C& c2, BinaryPredicate&& p) {
239  return c1.size() == c2.size() &&
240  std::equal(std::begin(c1), std::end(c1), std::begin(c2),
241  std::forward<BinaryPredicate>(p) );
242  }
243 
244  // match (case insensitive) property by name
245  struct is_iByName
246  {
248  is_iByName ( const std::string& name ) : m_name ( name ) {}
250  bool operator () ( const Property* p ) const
251  {
252  return p && equal_(m_name,p->name(),boost::algorithm::is_iequal{});
253  } ;
254  private:
255  const std::string& m_name ;
256  } ;
257 }
258 // ============================================================================
259 /* simple function which gets the property with given name
260  * from the component
261  *
262  * @code
263  *
264  * const IProperty* p = ... ;
265  *
266  * const Property* pro = getProperty( p , "Context" ) ;
267  *
268  * @endcode
269  *
270  * @param p pointer to IProperty object
271  * @param name property name (case insensitive)
272  * @return property with the given name (if exists), NULL otherwise
273  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
274  * @date 2006-09-09
275  */
276 // ============================================================================
278 ( const IProperty* p ,
279  const std::string& name )
280 {
281  // trivial check
282  if ( !p ) { return nullptr ; } // RETURN
283  // get all properties
284  const auto& props = p->getProperties() ;
285  // comparison criteria:
286  auto ifound = std::find_if ( props.begin(), props.end(), is_iByName{ name } );
287  return ifound != props.end() ? *ifound : nullptr;
288 }
289 // ============================================================================
290 /* simple function which gets the property with given name
291  * from the component
292  *
293  * @code
294  *
295  * const IInterface* p = ... ;
296  *
297  * const Property* pro = getProperty( p , "Context" ) ;
298  *
299  * @endcode
300  *
301  * @param p pointer to IInterface object
302  * @param name property name (case insensitive)
303  * @return property with the given name (if exists), NULL otherwise
304  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
305  * @date 2006-09-09
306  */
307 // ============================================================================
309 ( const IInterface* p , const std::string& name )
310 {
311  // trivial check
312  if ( !p ) { return nullptr ; } // RETURN
313  // remove const-qualifier
314  IInterface* _i = const_cast<IInterface*>( p ) ;
315  if ( !_i ) { return nullptr ; } // RETURN
316  SmartIF<IProperty> property( _i ) ;
317  return property ? getProperty ( property , name ) : nullptr;
318 }
319 // ============================================================================
320 /* check the property by name from the list of the properties
321  *
322  * @code
323  *
324  * IJobOptionsSvc* svc = ... ;
325  *
326  * const std::string client = ... ;
327  *
328  * // get the property:
329  * bool context =
330  * hasProperty ( svc->getProperties( client ) , "Context" )
331  *
332  * @endcode
333  *
334  * @see IJobOptionsSvc
335  *
336  * @param p list of properties
337  * @param name property name (case insensitive)
338  * @return true if the property exists
339  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
340  * @date 2006-09-09
341  */
342 // ============================================================================
344 ( const std::vector<const Property*>* p ,
345  const std::string& name )
346 {
347  // delegate to another method
348  return getProperty ( p , name ) ;
349 }
350 // ============================================================================
351 /* get the property by name from the list of the properties
352  *
353  * @code
354  *
355  * IJobOptionsSvc* svc = ... ;
356  *
357  * const std::string client = ... ;
358  *
359  * // get the property:
360  * const Property* context =
361  * getProperty ( svc->getProperties( client ) , "Context" )
362  *
363  * @endcode
364  *
365  * @see IJobOptionsSvc
366  *
367  * @param p list of properties
368  * @param name property name (case insensitive)
369  * @return property with the given name (if exists), NULL otherwise
370  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
371  * @date 2006-09-09
372  */
373 // ============================================================================
375 ( const std::vector<const Property*>* p ,
376  const std::string& name )
377 {
378  // trivial check
379  if ( !p ) { return nullptr ; } // RETURN
380  auto ifound = std::find_if ( p->begin() , p->end() , is_iByName{ name } ) ;
381  return p->end() != ifound ? *ifound : nullptr ; // RETURN
382 }
383 // ============================================================================
384 /* the full specialization of the
385  * method setProperty( IProperty, std::string, const TYPE&)
386  * for C-strings
387  *
388  * @param component component which needs to be configured
389  * @param name name of the property
390  * @param value value of the property
391  * @param doc the new documentation string
392  *
393  * @see IProperty
394  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
395  * @date 2007-05-13
396  */
397 // ============================================================================
399 ( IProperty* component ,
400  const std::string& name ,
401  const char* value ,
402  const std::string& doc )
403 {
404  return Gaudi::Utils::setProperty ( component , name , std::string{value} , doc ) ;
405 }
406 // ============================================================================
407 /* the full specialization of the
408  * method Gaudi::Utils::setProperty( IProperty, std::string, const TYPE&)
409  * for standard strings
410  *
411  * @param component component which needs to be configured
412  * @param name name of the property
413  * @param value value of the property
414  *
415  * @see IProperty
416  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
417  * @date 2007-05-13
418  */
419 // ============================================================================
421 ( IProperty* component ,
422  const std::string& name ,
423  const std::string& value ,
424  const std::string& doc )
425 {
426  if ( !component ) { return StatusCode::FAILURE ; } // RETURN
427  if ( !hasProperty ( component , name ) ) { return StatusCode::FAILURE ; }
428  StatusCode sc = component -> setProperty ( name , value ) ;
429  if ( !doc.empty() )
430  {
431  Property* p = getProperty( component , name ) ;
432  if ( p ) { p -> setDocumentation ( doc ) ; }
433  }
434  sc.ignore() ;
435  return sc ;
436 }
437 // ============================================================================
438 /* simple function to set the property of the given object from another
439  * property
440  *
441  * @code
442  *
443  * IProperty* component = ... ;
444  *
445  * const Property* prop = ... ;
446  * StatusCode sc = setProperty ( component , "Data" , prop ) ;
447  *
448  * @endcode
449  *
450  * @param component component which needs to be configured
451  * @param name name of the property
452  * @param property the property
453  * @param doc the new documentation string
454  *
455  * @see IProperty
456  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
457  * @date 2007-05-13
458  */
459 // ============================================================================
461 ( IProperty* component ,
462  const std::string& name ,
463  const Property* property ,
464  const std::string& doc )
465 {
466  if ( !component || !property ) return StatusCode::FAILURE ;
467  Property* p = getProperty ( component , name ) ;
468  if ( !p || !p->assign ( *property ) ) return StatusCode::FAILURE ;
469  if ( !doc.empty() ) { p->setDocumentation( doc ) ; }
470  return StatusCode::SUCCESS ;
471 }
472 // ============================================================================
473 /* simple function to set the property of the given object from another
474  * property
475  *
476  * @code
477  *
478  * IProperty* component = ... ;
479  *
480  * const Property& prop = ... ;
481  * StatusCode sc = setProperty ( component , "Data" , prop ) ;
482  *
483  * @endcode
484  *
485  * @param component component which needs to be configured
486  * @param name name of the property
487  * @param property the property
488  * @param doc the new documentation string
489  *
490  * @see IProperty
491  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
492  * @date 2007-05-13
493  */
494 // ============================================================================
496 ( IProperty* component ,
497  const std::string& name ,
498  const Property& property ,
499  const std::string& doc )
500 { return setProperty ( component , name , &property , doc ) ; }
501 // ============================================================================
502 /* the full specialization of the
503  * method setProperty( IInterface , std::string, const TYPE&)
504  * for standard strings
505  *
506  * @param component component which needs to be configured
507  * @param name name of the property
508  * @param value value of the property
509  * @param doc the new documentation string
510  *
511  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
512  * @date 2007-05-13
513  */
514 // ============================================================================
516 ( IInterface* component ,
517  const std::string& name ,
518  const std::string& value ,
519  const std::string& doc )
520 {
521  if ( !component ) { return StatusCode::FAILURE ; }
522  SmartIF<IProperty> property ( component ) ;
523  return property ? setProperty ( property , name , value , doc )
525 }
526 // ============================================================================
527 /* the full specialization of the
528  * method setProperty( IInterface , std::string, const TYPE&)
529  * for C-strings
530  *
531  * @param component component which needs to be configured
532  * @param name name of the property
533  * @param value value of the property
534  * @param doc the new documentation string
535  *
536  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
537  * @date 2007-05-13
538  */
539 // ============================================================================
541 ( IInterface* component ,
542  const std::string& name ,
543  const char* value ,
544  const std::string& doc )
545 {
546  return setProperty ( component , name , std::string{ value } , doc ) ;
547 }
548 // ============================================================================
549 /* simple function to set the property of the given object from another
550  * property
551  *
552  * @code
553  *
554  * IInterface* component = ... ;
555  *
556  * const Property* prop = ... ;
557  * StatusCode sc = setProperty ( component , "Data" , prop ) ;
558  *
559  * @endcode
560  *
561  * @param component component which needs to be configured
562  * @param name name of the property
563  * @param property the property
564  * @param doc the new documentation string
565  *
566  * @see IProperty
567  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
568  * @date 2007-05-13
569  */
570 // ============================================================================
572 ( IInterface* component ,
573  const std::string& name ,
574  const Property* property ,
575  const std::string& doc )
576 {
577  if ( !component ) { return StatusCode::FAILURE ; }
578  SmartIF<IProperty> prop ( component ) ;
579  if ( !prop ) { return StatusCode::FAILURE ; }
580  return setProperty ( prop , name , property , doc ) ;
581 }
582 // ============================================================================
583 /* simple function to set the property of the given object from another
584  * property
585  *
586  * @code
587  *
588  * IInterface* component = ... ;
589  *
590  * const Property& prop = ... ;
591  * StatusCode sc = setProperty ( component , "Data" , prop ) ;
592  *
593  * @endcode
594  *
595  * @param component component which needs to be configured
596  * @param name name of the property
597  * @param property the property
598  * @param doc the new documentation string
599  *
600  * @see IProperty
601  * @author Vanya BELYAEV ibelyaev@physics.syr.edu
602  * @date 2007-05-13
603  */
604 // ============================================================================
606 ( IInterface* component ,
607  const std::string& name ,
608  const Property& property ,
609  const std::string& doc )
610 { return setProperty ( component , name , &property , doc ) ; }
611 // ============================================================================
612 
613 // ============================================================================
614 // The END
615 // ============================================================================
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:1187
virtual void useReadHandler() const
use the call-back function at reading
Definition: Property.cpp:80
bool setValue(const GaudiHandleArrayBase &value)
Definition: Property.cpp:206
virtual bool assign(const Property &source)=0
import the property value form the source
virtual const std::vector< Property * > & getProperties() const =0
Get list of properties.
void setTypeAndName(std::string myTypeAndName)
The component "type/name" string.
Definition: GaudiHandle.cpp:9
virtual std::string toString() const =0
value -> string
virtual Property & declareUpdateHandler(std::function< void(Property &)> fun)
set new callback for update
Definition: Property.cpp:72
const std::string & name() const
property name
Definition: Property.h:45
StatusCode fromString(const std::string &s) override
string -> value
Definition: Property.cpp:191
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:45
std::string toString(const TYPE &obj)
the generic implementation of the type conversion to the string
Definition: ToStream.h:371
void setDocumentation(std::string documentation)
set the documentation string
Definition: Property.h:94
STL namespace.
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:159
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
const char *PyHelper() getProperty(IInterface *p, char *name)
Definition: Bootstrap.cpp:258
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
virtual std::ostream & fillStream(std::ostream &) const
the printout of the property value
Definition: Property.cpp:111
void toStream(std::ostream &out) const override
value -> stream
Definition: Property.cpp:217
virtual Property & declareReadHandler(std::function< void(Property &)> fun)
set new callback for reading
Definition: Property.cpp:64
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:319
GaudiHandleProperty(std::string name, GaudiHandleBase &ref)
Definition: Property.cpp:170
bool setTypesAndNames(const std::vector< std::string > &myTypesAndNamesList)
Set the array of handles from list of "type/name" strings in .
Definition: GaudiHandle.cpp:63
virtual bool useUpdateHandler()
use the call-back function at update
Definition: Property.cpp:92
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:47
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Definition of the basic interface.
Definition: IInterface.h:234
GaudiHandleArrayProperty(std::string name, GaudiHandleArrayBase &ref)
Definition: Property.cpp:200
std::function< void(Property &)> m_readCallBack
Definition: Property.h:120
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:254
std::string toString() const override
value -> string
Definition: Property.cpp:181
GAUDI_API Property * getProperty(const IProperty *p, const std::string &name)
simple function which gets the property with given name from the component
Definition: Property.cpp:278
StatusCode fromString(const std::string &s) override
string -> value
Definition: Property.cpp:223
GaudiHandleArrayBase * m_pValue
Pointer to the real property.
Definition: Property.h:867
Base class of array's of various gaudihandles.
Definition: GaudiHandle.h:320
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
void toStream(std::ostream &out) const override
value -> stream
Definition: Property.cpp:186
void setPropertyName(std::string propName)
set name as used in declareProperty(name,gaudiHandle).
Definition: GaudiHandle.h:44
std::function< void(Property &)> m_updateCallBack
Definition: Property.h:122
double fun(const std::vector< double > &x)
Definition: PFuncTest.cpp:26
string s
Definition: gaudirun.py:245
std::ostream & operator<<(std::ostream &stream, const Property &prop)
The output operator for friendly printout.
Definition: Property.cpp:32
GaudiHandleBase * m_pValue
Pointer to the real property.
Definition: Property.h:808
const std::vector< std::string > typesAndNames() const
Return a vector with "type/name" strings of all handles in the array.
Definition: GaudiHandle.cpp:78
Base class to handles to be used in lieu of naked pointers to various Gaudi components.
Definition: GaudiHandle.h:93
std::string toString() const override
value -> string
Definition: Property.cpp:211
std::string typeAndName() const
The full type and name: "type/name".
Definition: GaudiHandle.h:120
void ignore() const
Definition: StatusCode.h:108
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
bool setValue(const GaudiHandleBase &value)
Definition: Property.cpp:176
string type
Definition: gaudirun.py:151