Gaudi Framework, version v23r7

Home   Generated: Wed Mar 20 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Property.h
Go to the documentation of this file.
1 // $Id: Property.h,v 1.26 2008/10/27 16:41:34 marcocle Exp $
2 // ============================================================================
3 // CVS tag $Name: $
4 // ============================================================================
5 #ifndef GAUDIKERNEL_PROPERTY_H
6 #define GAUDIKERNEL_PROPERTY_H
7 // ============================================================================
8 // STD & STL
9 // ============================================================================
10 #include <string>
11 #include <stdexcept>
12 #include <typeinfo>
13 // ============================================================================
14 // Application C++ Class Headers
15 // ============================================================================
16 #include "GaudiKernel/Kernel.h"
18 #include "GaudiKernel/Parsers.h"
19 #include "GaudiKernel/ToStream.h"
20 #include "GaudiKernel/SmartIF.h"
21 // ============================================================================
22 
23 // ============================================================================
24 class Property ;
26 class IProperty ;
27 class IInterface ;
28 // ============================================================================
29 
30 // ============================================================================
32 // ============================================================================
34 // ============================================================================
44 {
45 public:
47  const std::string& name () const { return m_name ; }
49  const std::string& documentation() const { return m_documentation; }
51  const std::type_info* type_info () const { return m_typeinfo ; }
53  std::string type () const { return m_typeinfo->name() ; }
55  virtual bool load ( Property& dest ) const = 0 ;
57  virtual bool assign ( const Property& source ) = 0 ;
58 public:
60  virtual std::string toString () const = 0 ;
62  virtual void toStream(std::ostream& out) const = 0;
64  virtual StatusCode fromString ( const std::string& value ) = 0 ;
65 public:
67  const PropertyCallbackFunctor* readCallBack () const ;
69  const PropertyCallbackFunctor* updateCallBack () const ;
71  virtual void declareReadHandler ( PropertyCallbackFunctor* pf ) ;
73  virtual void declareUpdateHandler ( PropertyCallbackFunctor* pf ) ;
74  template< class HT >
75  void declareReadHandler
76  ( void ( HT::* MF ) ( Property& ) , HT* instance ) ;
77  template< class HT >
78  void declareUpdateHandler
79  ( void ( HT::* MF ) ( Property& ) , HT* instance ) ;
81  virtual void useReadHandler () const ;
83  virtual bool useUpdateHandler () ;
84 public:
86  virtual ~Property() ;
88  virtual Property* clone () const = 0 ;
90  void setName ( const std::string& value ) { m_name = value ; }
92  void setDocumentation( const std::string& documentation ) {
93  m_documentation = documentation; }
95  virtual std::ostream& fillStream ( std::ostream& ) const ;
96 protected:
98  Property
99  ( const std::type_info& type ,
100  const std::string& name = "" ) ;
102  Property
103  ( const std::string& name ,
104  const std::type_info& type ) ;
106  Property ( const Property& right ) ;
108  Property& operator=( const Property& right ) ;
109 private:
110  // the default constructor is disabled
111  Property() ;
112 private:
113  // property name
115  // property doc string
117  // property type
119 protected:
120  // call back functor for reading
122  // call back functor for update
124 };
125 // ============================================================================
127 // ============================================================================
128 template< class HT >
130 ( void ( HT::* MF ) ( Property& ) , HT* obj )
131 { declareReadHandler ( new PropertyCallbackMemberFunctor< HT >( MF , obj ) ) ; }
132 // ============================================================================
133 template< class HT >
135 ( void ( HT::* MF ) ( Property& ) , HT* obj )
136 { declareUpdateHandler ( new PropertyCallbackMemberFunctor< HT >( MF , obj ) ) ; }
137 // ============================================================================
145 // ============================================================================
146 template <class TYPE>
148 {
149 public:
150  // ==========================================================================
152  typedef Gaudi::Utils::PropertyTypeTraits<TYPE> Traits ;
154  typedef typename Traits::PVal PVal ;
155  // ==========================================================================
156 protected:
157  // ==========================================================================
159  inline PropertyWithValue
160  ( const std::string& name ,
161  PVal value ,
162  const bool owner ) ;
164  inline PropertyWithValue ( const PropertyWithValue& rhs ) ;
166  template <class OTHER>
167  inline PropertyWithValue ( const PropertyWithValue<OTHER>& right ) ;
169  virtual inline ~PropertyWithValue() ;
171  PropertyWithValue& operator=( const TYPE& value ) ;
172  // assignment operator (don't let the compiler generate a buggy one)
174  // assignment operator
175  template <class OTHER>
177  // ==========================================================================
178 public:
179  // ==========================================================================
181  operator const TYPE& () const { return value() ;}
183  inline const TYPE& value() const ;
184  // ==========================================================================
185 public:
186  // ==========================================================================
188  virtual bool setValue ( const TYPE& value ) = 0 ;
190  virtual bool assign ( const Property& source ) ;
192  virtual bool load ( Property& dest ) const ;
194  virtual StatusCode fromString ( const std::string& s ) ;
196  virtual std::string toString () const ;
198  virtual void toStream (std::ostream& out) const ;
199  // ==========================================================================
200 protected:
201  // ==========================================================================
203  inline void i_set ( const TYPE& value ) {
204  Traits::assign(*m_value, value);
205  }
207  inline PVal i_get () const {
208  return m_value;
209  }
210  // ==========================================================================
211 private:
212  // ==========================================================================
214  PVal m_value ; // the actual property value
216  bool m_own ; // owner of the storage
217  // ==========================================================================
218 };
219 
220 // ============================================================================
222 // ============================================================================
223 template <class TYPE>
224 inline
226 ( const std::string& name ,
227  PVal value ,
228  const bool own )
229  : Property ( typeid( TYPE ) , name )
230  , m_value ( value )
231  , m_own ( own )
232 {}
233 // ============================================================================
234 // copy constructor
235 // ============================================================================
236 template <class TYPE>
238 ( const PropertyWithValue& right )
239  : Property( right )
240  , m_value ( right.m_value )
241  , m_own ( right.m_own )
242 {
243  m_value = Traits::copy ( right.value() , m_own ) ;
244 }
245 // ============================================================================
246 // "copy" constructor form any other type
247 // ============================================================================
248 template <class TYPE>
249 template <class OTHER>
251 ( const PropertyWithValue<OTHER>& right )
252  : Property( right )
253  , m_value ( right.m_value )
254  , m_own ( right.m_own )
255 {
256  m_value = Traits::copy ( right.value() , m_own ) ;
257 }
258 // ============================================================================
260 // ============================================================================
261 template <class TYPE>
263 {
264  Traits::dele ( m_value , m_own ) ;
265  m_value = 0 ;
266 }
267 // ============================================================================
269 // ============================================================================
270 template <class TYPE>
273 {
274  if ( !setValue ( value ) )
275  { throw std::out_of_range( "Value not verified" ) ; }
276  return *this ;
277 }
278 // ============================================================================
280 // ============================================================================
281 template <class TYPE>
282 inline bool
284 {
285  // 1) Is the property of "the same" type?
286  const PropertyWithValue<TYPE>* p =
287  dynamic_cast<const PropertyWithValue<TYPE>*> ( &source ) ;
288  if ( 0 != p ) { return setValue ( p->value() ) ; } // RETURN
289  // 2) Else use the string representation
290  return this->fromString( source.toString() ).isSuccess() ;
291 }
292 // ============================================================================
294 // ============================================================================
295 template <class TYPE>
296 inline bool
298 {
299  // delegate to the 'opposite' method ;
300  return dest.assign( *this ) ;
301 }
302 // ============================================================================
304 // ============================================================================
305 template <class TYPE>
306 inline std::string
308 {
309  useReadHandler();
310  return Gaudi::Utils::toString( *m_value ) ;
311 }
312 // ============================================================================
314 // ============================================================================
315 template <class TYPE>
316 inline void
318 {
319  useReadHandler();
320  Gaudi::Utils::toStream( *m_value, out ) ;
321 }
322 // ============================================================================
324 // ============================================================================
325 template <class TYPE>
326 inline StatusCode
328 {
329  TYPE tmp ;
330  StatusCode sc = Gaudi::Parsers::parse ( tmp , source ) ;
331  if ( sc.isFailure() ) { return sc ; }
332  return setValue ( tmp ) ? StatusCode::SUCCESS : StatusCode::FAILURE ;
333 }
334 // ============================================================================
336 // ============================================================================
337 template <>
338 inline std::string
340 {
341  useReadHandler();
342  return this->value() ;
343 }
344 // ============================================================================
345 template <>
347 { return this->fromString( source.toString() ).isSuccess() ; }
348 // ============================================================================
349 
350 // ============================================================================
352 // ============================================================================
353 template <class TYPE>
354 inline const TYPE&
356 { useReadHandler() ; return *m_value ; }
357 // ============================================================================
358 // assignment operator
359 // ============================================================================
360 template <class TYPE>
362 ( const PropertyWithValue& right )
363 {
364  // assign the base class
365  Property::operator=( right ) ;
366  // assign the value
368  return *this ;
369 }
370 // ============================================================================
371 // templated assignment operator
372 // ============================================================================
373 template <class TYPE>
374 template <class OTHER>
375 PropertyWithValue<TYPE>& PropertyWithValue<TYPE>::operator=
376 ( const PropertyWithValue<OTHER>& right )
377 {
378  // assign the base class
379  Property::operator=( right ) ;
380  // assign the value
382  return *this ;
383 }
384 // ============================================================================
385 
386 // ============================================================================
394 // ============================================================================
395 template<class TYPE,class VERIFIER>
397  : public PropertyWithValue<TYPE>
398 {
399 protected:
400  // ==========================================================================
403  ( const std::string& name ,
404  typename Gaudi::Utils::PropertyTypeTraits<TYPE>::PVal value ,
405  const bool owner ,
406  const VERIFIER& verifier )
407  : PropertyWithValue<TYPE> ( name , value , owner )
408  , m_verifier ( verifier )
409  {}
412  // ==========================================================================
413 public:
414  // ==========================================================================
415  inline VERIFIER& verifier() { return m_verifier ; }
416  inline const VERIFIER& verifier() const { return m_verifier ; }
418  bool set( const TYPE& value ) ;
420  virtual bool setValue( const TYPE& value ) { return set( value ) ; }
422  template <class OTHER,class OTHERVERIFIER>
423  PropertyWithVerifier& operator=
426  template <class OTHER>
429  PropertyWithVerifier& operator=( const TYPE& right ) ;
430  // ==========================================================================
431 private:
432  // ==========================================================================
437  // ==========================================================================
438 private:
439  // ==========================================================================
441  VERIFIER m_verifier ; // the verifier itself
442  // ==========================================================================
443 } ;
444 // ============================================================================
446 // ============================================================================
447 template <class TYPE,class VERIFIER>
448 inline bool
450 {
452  if ( !m_verifier.isValid( &value ) ) { return false ; }
454  this->i_set( value ) ;
456  return this->useUpdateHandler() ;
457 }
458 // ============================================================================
460 // ============================================================================
461 template <class TYPE,class VERIFIER>
464 {
466  return *this ;
467 }
468 // ============================================================================
470 // ============================================================================
471 template <class TYPE,class VERIFIER>
472 template <class OTHER>
475 {
477  return *this ;
478 }
479 // ============================================================================
481 // ============================================================================
482 template <class TYPE,class VERIFIER>
483 template <class OTHER,class OTHERVERIFIER>
487 {
489  return *this ;
490 }
491 // ============================================================================
492 
493 // ============================================================================
503 // ============================================================================
504 template <class TYPE,class VERIFIER = BoundedVerifier<TYPE> >
505 class SimpleProperty
506  : public PropertyWithVerifier<TYPE,VERIFIER>
507 {
508 protected:
509  // ==========================================================================
510  typedef Gaudi::Utils::PropertyTypeTraits<TYPE> Traits ;
511  // ==========================================================================
512 public:
513  // ==========================================================================
516  ( VERIFIER verifier = VERIFIER() ) ;
519  ( const TYPE& value ,
520  VERIFIER verifier = VERIFIER() ) ;
523  ( const std::string& name ,
524  const TYPE& value ,
525  VERIFIER verifier = VERIFIER() ) ;
527  template <class OTHER>
528  SimpleProperty ( const PropertyWithValue<OTHER>& right ) ;
530  SimpleProperty ( const SimpleProperty& right ) ;
532  virtual ~SimpleProperty() ;
534  virtual SimpleProperty* clone() const ;
536  SimpleProperty& operator=( const TYPE& value ) ;
538  template <class OTHER>
540  // ==========================================================================
541 };
542 // ============================================================================
544 // ============================================================================
545 template <class TYPE,class VERIFIER>
547 ( VERIFIER verifier )
549 ( "" , Traits::new_() , true , verifier )
550 {}
551 // ============================================================================
553 // ============================================================================
554 template <class TYPE,class VERIFIER>
556 ( const TYPE& value ,
557  VERIFIER verifier )
559 ( "" , Traits::new_(value) , true , verifier )
560 {}
561 // ============================================================================
563 // ============================================================================
564 template <class TYPE,class VERIFIER>
566 ( const std::string& name ,
567  const TYPE& value ,
568  VERIFIER verifier )
570 ( name , Traits::new_(value) , true , verifier )
571 {}
572 // ============================================================================
574 // ============================================================================
575 template <class TYPE,class VERIFIER>
576 template <class OTHER>
578 ( const PropertyWithValue<OTHER>& right )
580 ( right.name() , Traits::new_( right.value() ) , true , VERIFIER() )
581 {}
582 // ============================================================================
584 // ============================================================================
585 template <class TYPE,class VERIFIER>
587 ( const SimpleProperty& right )
589 ( right.name() , Traits::new_( right.value() ) , true , right.verifier() )
590 {}
591 // ============================================================================
593 // ============================================================================
594 template <class TYPE,class VERIFIER>
596 // ============================================================================
598 // ============================================================================
599 template <class TYPE,class VERIFIER>
600 inline
603 { return new SimpleProperty(*this) ; }
604 // ============================================================================
606 // ============================================================================
607 template <class TYPE,class VERIFIER>
608 inline
611 {
613  return *this ;
614 }
615 // ============================================================================
617 // ============================================================================
618 template <class TYPE,class VERIFIER>
619 template <class OTHER>
620 inline
623 ( const PropertyWithValue<OTHER>& right )
624 {
626  return *this ;
627 }
628 // ============================================================================
629 
630 // ============================================================================
639 // ============================================================================
640 template< class TYPE, class VERIFIER = NullVerifier<TYPE> >
641 class SimplePropertyRef :
642  public PropertyWithVerifier<TYPE,VERIFIER>
643 {
644 public:
647  ( const std::string& name ,
648  TYPE& value ,
649  VERIFIER verifier = VERIFIER() ) ;
651  SimplePropertyRef ( const SimplePropertyRef& right ) ;
653  virtual ~SimplePropertyRef() ;
655  virtual SimplePropertyRef* clone() const ;
657  SimplePropertyRef& operator=( const TYPE& value ) ;
659  template <class OTHER>
661 private:
662  // the default constructor is disabled
664 };
665 // ============================================================================
667 // ============================================================================
668 template <class TYPE,class VERIFIER>
670 ( const std::string& name ,
671  TYPE& value ,
672  VERIFIER verifier )
673  : PropertyWithVerifier<TYPE,VERIFIER> ( name , &value , false , verifier )
674 {}
675 // ============================================================================
677 // ============================================================================
678 template <class TYPE,class VERIFIER>
680 ( const SimplePropertyRef& right )
682 ( right.name() , right.i_get() , false , right.verifier() )
683 {}
684 // ============================================================================
686 // ============================================================================
687 template <class TYPE,class VERIFIER>
689 // ============================================================================
691 // ============================================================================
692 template <class TYPE,class VERIFIER>
693 inline
696 { return new SimplePropertyRef(*this) ; }
697 // ============================================================================
699 // ============================================================================
700 template <class TYPE,class VERIFIER>
701 inline
704 {
706  return *this ;
707 }
708 // ============================================================================
710 // ============================================================================
711 template <class TYPE,class VERIFIER>
712 template <class OTHER>
713 inline
716 ( const PropertyWithValue<OTHER>& right )
717 {
719  return *this ;
720 }
721 // ============================================================================
722 
723 
724 
725 
726 // Typedef Properties for built-in types
742 
744 
745 
746 // Typedef PropertyRefs for built-in types
762 
764 
765 
766 // Typedef "Arrays" of Properties for built-in types
782 
784 
785 
786 // Typedef "Arrays" of PropertyRefs for built-in types
802 
804 
805 // pre-declaration is sufficient here
806 class GaudiHandleBase;
807 
809 public:
811 
813 
814  virtual GaudiHandleProperty* clone() const;
815 
816  virtual bool load( Property& destination ) const;
817 
818  virtual bool assign( const Property& source );
819 
820  virtual std::string toString() const;
821 
822  virtual void toStream(std::ostream& out) const;
823 
824  virtual StatusCode fromString(const std::string& s);
825 
826  const GaudiHandleBase& value() const;
827 
828  bool setValue( const GaudiHandleBase& value );
829 
830 private:
834 };
835 
836 // implementation in header file only where the GaudiHandleBase class
837 // definition is not needed. The rest goes into the .cpp file.
838 // The goal is to decouple the header files, to avoid that the whole
839 // world depends on GaudiHandle.h
841  setValue( value );
842  return *this;
843 }
844 
846  return new GaudiHandleProperty( *this );
847 }
848 
849 inline bool GaudiHandleProperty::load( Property& destination ) const {
850  return destination.assign( *this );
851 }
852 
853 inline bool GaudiHandleProperty::assign( const Property& source ) {
854  return fromString( source.toString() ).isSuccess();
855 }
856 
858  useReadHandler();
859  return *m_pValue;
860 }
861 
862 
863 // pre-declaration is sufficient here
865 
867 public:
868 
870 
872 
873  virtual GaudiHandleArrayProperty* clone() const;
874 
875  virtual bool load( Property& destination ) const;
876 
877  virtual bool assign( const Property& source );
878 
879  virtual std::string toString() const;
880 
881  virtual void toStream(std::ostream& out) const;
882 
883  virtual StatusCode fromString(const std::string& s);
884 
885  const GaudiHandleArrayBase& value() const;
886 
887  bool setValue( const GaudiHandleArrayBase& value );
888 
889 private:
893 
894 };
895 
896 // implementation in header file only where the GaudiHandleBase class
897 // definition is not needed. The rest goes into the .cpp file.
898 // The goal is to decouple the header files, to avoid that the whole
899 // world depends on GaudiHandle.h
901  setValue( value );
902  return *this;
903 }
904 
906  return new GaudiHandleArrayProperty( *this );
907 }
908 
909 inline bool GaudiHandleArrayProperty::load( Property& destination ) const {
910  return destination.assign( *this );
911 }
912 
913 inline bool GaudiHandleArrayProperty::assign( const Property& source ) {
914  return fromString( source.toString() ) != 0;
915 }
916 
918  useReadHandler();
919  return *m_pValue;
920 }
921 
922 
923 namespace Gaudi
924 {
925  namespace Utils
926  {
927  // ========================================================================
945  GAUDI_API bool hasProperty ( const IProperty* p , const std::string& name ) ;
946  // ========================================================================
964  GAUDI_API bool hasProperty ( const IInterface* p , const std::string& name ) ;
965  // ========================================================================
984  ( const IProperty* p , const std::string& name ) ;
985  // ========================================================================
1004  ( const IInterface* p , const std::string& name ) ;
1005  // ========================================================================
1028  GAUDI_API bool hasProperty
1029  ( const std::vector<const Property*>* p ,
1030  const std::string& name ) ;
1031  // ========================================================================
1055  ( const std::vector<const Property*>* p ,
1056  const std::string& name ) ;
1057  // ========================================================================
1081  template <class TYPE>
1083  ( IProperty* component ,
1084  const std::string& name ,
1085  const TYPE& value ,
1086  const std::string& doc ) ;
1087  // ========================================================================
1110  template <class TYPE>
1112  ( IProperty* component ,
1113  const std::string& name ,
1114  const TYPE& value )
1115  { return setProperty ( component , name , value , std::string() ) ; }
1116  // ========================================================================
1131  ( IProperty* component ,
1132  const std::string& name ,
1133  const std::string& value ,
1134  const std::string& doc = "" ) ;
1135  // ========================================================================
1150  ( IProperty* component ,
1151  const std::string& name ,
1152  const char* value ,
1153  const std::string& doc = "" ) ;
1154  // ========================================================================
1168  template <unsigned N>
1170  ( IProperty* component ,
1171  const std::string& name ,
1172  const char (&value)[N] ,
1173  const std::string& doc = "" )
1174  {
1175  if ( 0 == component ) { return StatusCode::FAILURE ; }
1176  const std::string val = std::string ( value , value + N ) ;
1177  return setProperty ( component , name , val , doc ) ;
1178  }
1179  // ========================================================================
1210  template <class TYPE>
1212  ( IProperty* component ,
1213  const std::string& name ,
1214  const TYPE& value ,
1215  const std::string& doc )
1216  {
1217  if ( 0 == component ) { return StatusCode::FAILURE ; } // RETURN
1218  if ( !hasProperty ( component , name ) ) { return StatusCode::FAILURE ; }
1219  const std::string val = Gaudi::Utils::toString ( value ) ;
1220  return Gaudi::Utils::setProperty ( component , name , val , doc ) ;
1221  }
1222  // ========================================================================
1245  ( IProperty* component ,
1246  const std::string& name ,
1247  const Property* property ,
1248  const std::string& doc = "" ) ;
1249  // ========================================================================
1272  ( IProperty* component ,
1273  const std::string& name ,
1274  const Property& property ,
1275  const std::string& doc = "" ) ;
1276  // ========================================================================
1299  template <class TYPE>
1301  ( IProperty* component ,
1302  const std::string& name ,
1303  const SimpleProperty<TYPE>& value ,
1304  const std::string& doc = "" )
1305  {
1306  const Property* property = &value ;
1307  return setProperty ( component , name , property , doc ) ;
1308  }
1309  // ========================================================================
1330  template <class TYPE>
1332  ( IInterface* component ,
1333  const std::string& name ,
1334  const TYPE& value ,
1335  const std::string& doc = "" )
1336  {
1337  if ( 0 == component ) { return StatusCode::FAILURE ; }
1338  SmartIF<IProperty> property ( component ) ;
1339  if ( !property ) { return StatusCode::FAILURE ; }
1340  return setProperty ( property , name , value , doc ) ;
1341  }
1342  // ========================================================================
1356  ( IInterface* component ,
1357  const std::string& name ,
1358  const std::string& value ,
1359  const std::string& doc = "" ) ;
1360  // ========================================================================
1374  ( IInterface* component ,
1375  const std::string& name ,
1376  const char* value ,
1377  const std::string& doc = "" ) ;
1378  // ========================================================================
1392  template <unsigned N>
1394  ( IInterface* component ,
1395  const std::string& name ,
1396  const char (&value)[N] ,
1397  const std::string& doc = "" )
1398  {
1399  if ( 0 == component ) { return StatusCode::FAILURE ; }
1400  const std::string val = std::string ( value , value + N ) ;
1401  return setProperty ( component , name , val , doc ) ;
1402  }
1403  // ========================================================================
1426  ( IInterface* component ,
1427  const std::string& name ,
1428  const Property* property ,
1429  const std::string& doc = "" ) ;
1430  // ========================================================================
1453  ( IInterface* component ,
1454  const std::string& name ,
1455  const Property& property ,
1456  const std::string& doc = "" ) ;
1457  // ========================================================================
1480  template <class TYPE>
1481  StatusCode
1482  setProperty
1483  ( IInterface* component ,
1484  const std::string& name ,
1485  const SimpleProperty<TYPE>& value ,
1486  const std::string& doc = "" )
1487  {
1488  const Property* property = &value ;
1489  return setProperty ( component , name , property , doc ) ;
1490  }
1491  // ========================================================================
1492  } // end of namespace Gaudi::Utils
1493 } // end of namespace Gaudi
1494 
1495 
1496 // ============================================================================
1497 // The END
1498 // ============================================================================
1499 #endif // GAUDIKERNEL_PROPERTY_H
1500 // ============================================================================

Generated at Wed Mar 20 2013 17:59:37 for Gaudi Framework, version v23r7 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004