Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Property.h

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiKernel/GaudiKernel/Property.h,v 1.1.1.1 2001/04/18 18:14:18 tlindner Exp $
00002 #ifndef GAUDIKERNEL_PROPERTY_H
00003 #define GAUDIKERNEL_PROPERTY_H
00004 
00005 /**********************************************************
00006  *   This component supplies SimpleProperty's, that own
00007  *   their value's memory, and SimplePropertyRef's, that
00008  *   refer to their value's memory, for all fundamental
00009  *   types plus std::string's and std::vector's of all 
00010  *   fundamental types plus std::string's. Both 
00011  *   SimpleProperty's and SimplePropertyRef's include 
00012  *   Verifier's that are BoundedVerifier's by default for
00013  *   SimpleProperty's and NullVerifier's by default for
00014  *   SimplePropertyRef's. Other Verifier's may be specified
00015  *   as optional arguments to creators. All SimplePropery's
00016  *   and SimplePropertyRef's are derived from the abstract
00017  *   class Property so that containers of Property*'s may be
00018  *   "homogeneous".
00019  **********************************************************/
00020 
00021 // Application C++ Class Headers
00022 #include "GaudiKernel/Kernel.h"
00023 #include "GaudiKernel/PropertyCallbackFunctor.h"
00024 #include "GaudiKernel/PropertyVerifier.h"
00025 
00026 // Standard C++ Class Headers
00027 #include <string>
00028 #include <vector>
00029 #include <typeinfo>
00030 #include <iosfwd>
00031 #include <stdexcept>
00032 
00033 // Forward delarations
00034 class Property;
00035 
00036 // PropertyRef is now a synonym for Property
00037 typedef Property PropertyRef;
00038 
00046 class Property {
00047 public:
00048   // Constructors - no public constructors
00049   // Copy Constructor - compiler generated default is ok
00050   // Destructor
00051   virtual ~Property() {
00052       if ( 0 != m_readCallback ) {
00053           delete m_readCallback;
00054           m_readCallback = 0;
00055       }
00056       if ( 0 != m_updateCallback ) {
00057           delete m_updateCallback;
00058           m_updateCallback = 0;
00059       }
00060   };
00061 
00062   // Operators - compiler generated assignment is ok
00063 
00064   // Accessor members (const)
00065   const   std::string&    name()      const  { return m_name; };
00066   const   std::type_info& type_info() const  { return *m_type_info; };
00067   const   std::string     type()      const  { return m_type_info->name(); };
00068   virtual bool            isRef()     const = 0;  // Does this property reference value?
00069   virtual const PropertyCallbackFunctor*
00070                           readCallback()   const { return m_readCallback; };
00071   virtual const PropertyCallbackFunctor*
00072                           updateCallback() const { return m_updateCallback; };
00073 
00074   // Modifier members
00075   void    setName( const std::string& name ) { m_name = name; };
00076   void    setType( const std::string& type ); // Deprecated and unimplemented.
00077 
00078   // Extractors (by abuse of language)
00079   virtual std::istream& nameAndValueFromStream( std::istream& );
00080           std::istream& nameFromStream(  std::istream& );
00081   virtual std::istream& valueFromStream( std::istream& ) = 0;
00082   // Insertor (by abuse of language)
00083   virtual std::ostream& nameAndValueAsStream( std::ostream& ) const = 0;
00084 
00085   // Function members
00086   virtual PropertyRef* cloneRef() const = 0;    // Client owns returned heap PropertyRef.
00087   virtual bool load ( Property& destination ) const = 0;   // from argument
00088   virtual bool assign( const Property& source ) = 0;       // to   argument
00089 
00090   // Callback functions return void and take a Property& as argument
00091   void    declareReadHandler( void ( * callbackFunctionPointer ) ( Property& ) )
00092   {
00093      delete m_readCallback;
00094      m_readCallback = new PropertyCallbackPointerFunctor( callbackFunctionPointer );
00095   }
00096   template< class HT >
00097   void    declareReadHandler( void ( HT::* callbackMemberPointer ) ( Property& ), HT* instance )
00098   {
00099     delete m_readCallback;
00100     m_readCallback = new PropertyCallbackMemberFunctor< HT >( callbackMemberPointer, instance );
00101   }
00102 
00103   void    declareUpdateHandler( void ( * callbackFunctionPointer ) ( Property& ) )
00104   {
00105      delete m_updateCallback;
00106      m_updateCallback = new PropertyCallbackPointerFunctor( callbackFunctionPointer );
00107   }
00108   template< class HT >
00109   void    declareUpdateHandler( void ( HT::* callbackMemberPointer ) ( Property& ), HT* instance )
00110   {
00111     delete m_updateCallback;
00112     m_updateCallback = new PropertyCallbackMemberFunctor< HT >( callbackMemberPointer, instance );
00113   }
00114 
00115   // HACK - DO NOT USE
00116   // Only needed until partial template specialization arrives
00117   virtual std::ostream&    valueElementAsStream(   std::ostream&, size_t index ) const = 0;
00118   virtual std::istream&    valueElementFromStream( std::istream& ) = 0;
00119   struct  bad_call { };    // Exception type
00120 
00121 protected:
00122   // Constructor
00123   Property( const std::string&    name, 
00124             const std::type_info& type_info )    
00125         : m_name( name ),
00126           m_type_info( &type_info ),
00127           m_readCallback( 0 ),
00128           m_updateCallback( 0 ) { };
00129 
00130   // Data members
00131   std::string  m_name;
00132   const std::type_info*  m_type_info;
00133   PropertyCallbackFunctor* m_readCallback;
00134   PropertyCallbackFunctor* m_updateCallback;
00135 };
00136 
00144 template< class T, class VT = BoundedVerifier< T > >
00145 class SimpleProperty : public Property {
00146 public:
00147   // Data and Function Members for Collaborators.
00148   // Constructors
00149   SimpleProperty() : Property( std::string(), typeid( T )) { }
00150   SimpleProperty( const T& value ) : Property( std::string(), typeid( T )) ,
00151                                      m_value( const_cast< T& >( value ) ) { }
00152   SimpleProperty( const std::string& name, 
00153                   const T& value ) : Property( name, typeid( value )) ,
00154                                      m_value( const_cast< T& >( value ) ) { }
00155   SimpleProperty( const std::string& name, 
00156                   const T& value, 
00157                   VT& verifier ) : Property( name, typeid( value ) ), 
00158                                    m_value( const_cast< T& >( value ) ),
00159                                    m_verifier( const_cast< VT& >( verifier ) ) { }
00160 
00161 
00162   // Copy Constructor - compiler generated default is ok
00163   // Destructor
00164   virtual ~SimpleProperty() { };
00165 
00166   // Operators - compiler generated assignment is ok
00167   SimpleProperty< T, VT >& operator=( const T& value ) {
00168     if ( !set( value ) ) {
00169       throw std::out_of_range( "Value not verified" );
00170     }
00171     return *this;
00172   }
00173 
00174   // Convertors
00175   operator const T&() const { return value( ); }
00176 
00178   const T& value() const {
00179     if ( 0 != m_readCallback ) ( *m_readCallback )( const_cast<SimpleProperty<T,VT>&>(*this) );
00180     return m_value;
00181   }
00182 
00184   VT& verifier() { return m_verifier; }            
00185 
00187   bool set( const T& value ) {
00188     bool result = false;
00189     if ( m_verifier.isValid( &value ) ) {
00190       m_value = value;
00191       if ( 0 != m_updateCallback ) ( *m_updateCallback )( *this );
00192       result = true;
00193     }
00194     return result;
00195   }
00197   bool setValue( const T& value ) { return set( value ); }    
00198 
00199   // Accessor members (const)
00200   bool isRef() const { return false; }
00201 
00202   // Extractors (by abuse of language)
00203   std::istream& nameAndValueFromStream( std::istream& );
00204   std::istream& valueFromStream( std::istream& );
00205 
00206   // Insertor (by abuse of language)
00207   std::ostream& nameAndValueAsStream( std::ostream& ) const;
00208 
00209   // Function members
00210   virtual PropertyRef* cloneRef() const;
00211   virtual bool assign( const Property& );
00212   virtual bool load( Property& ) const;
00213 
00214   // HACK - DO NOT USE
00215   // Only needed until partial template specialization arrives
00216   std::ostream& valueElementAsStream(   std::ostream&, size_t index ) const;
00217   std::istream& valueElementFromStream( std::istream& );
00218 
00219     // Data members
00220   T   m_value;
00221   VT  m_verifier;
00222 };
00223 
00231 template< class T, class VT = NullVerifier< T > >
00232 class SimplePropertyRef : public Property {
00233 public:
00234   // Data and Function Members for Collaborators.
00235   // Constructors
00236   SimplePropertyRef( const std::string& name, 
00237                      const T& value, 
00238                      const VT& verifier = NullVerifier<T>::theNullVerifier() )
00239                    : Property( name, typeid( value ) ), 
00240                      m_value( const_cast< T& >( value ) ),
00241                      m_verifier( const_cast< VT& >( verifier ) )  { }
00242 
00243 
00244 
00245   // Copy Constructor - compiler generated default is ok
00246   // Destructor
00247   virtual ~SimplePropertyRef() { };
00248   // Operators - compiler generated assignment is ok
00249   // Operators - compiler generated assignment is ok
00250   SimplePropertyRef< T, VT >& operator=( const T& value ) {
00251     if ( !set( value ) ) {
00252       throw std::out_of_range( "Value not verified" );
00253     }
00254     return *this;
00255   }
00256 
00257   // Convertors
00258   operator const T&() const { return value( ); }
00259 
00261   const T& value() const {
00262     if ( 0 != m_readCallback ) ( *m_readCallback )( const_cast<SimplePropertyRef<T,VT>&>(*this) );
00263     return m_value;
00264   }
00265 
00267   VT& verifier() { return m_verifier; }            
00268 
00270   bool set( const T& value ) {
00271     bool result = false;
00272     if ( m_verifier.isValid( &value ) ) {
00273       if ( 0 != m_updateCallback ) ( *m_updateCallback )( *this );
00274       m_value = value;
00275       result = true;
00276     }
00277     return result;
00278   }
00280   bool setValue( const T& value ) { return set( value ); }    
00281 
00282   // Accessor members (const)
00283   bool isRef() const { return true; }
00284 
00285   // Extractors (by abuse of language)
00286   std::istream& nameAndValueFromStream( std::istream& );
00287   std::istream& valueFromStream(     std::istream& );
00288 
00289   // Insertor (by abuse of language)
00290   std::ostream& nameAndValueAsStream(   std::ostream& ) const;
00291 
00292   // Function members
00293   virtual PropertyRef* cloneRef() const;
00294   virtual bool assign( const Property& );
00295   virtual bool load( Property& ) const;
00296 
00297   // HACK - DO NOT USE
00298   // Only needed until partial template specialization arrives
00299   std::ostream& valueElementAsStream( std::ostream&, size_t index ) const;
00300   std::istream& valueElementFromStream( std::istream& );
00301 
00302       // Data members
00303   T&   m_value;
00304   VT&  m_verifier;
00305 
00306 };
00307 
00308 
00309 // Typedef Properties for built-in types
00310 typedef SimpleProperty< bool >              BooleanProperty;
00311 typedef SimpleProperty< char >              CharProperty;
00312 typedef SimpleProperty< signed char >       SignedCharProperty;
00313 typedef SimpleProperty< unsigned char >     UnsignedCharProperty;
00314 typedef SimpleProperty< short >             ShortProperty;
00315 typedef SimpleProperty< unsigned short >    UnsignedShortProperty;
00316 typedef SimpleProperty< int >               IntegerProperty;
00317 typedef SimpleProperty< unsigned int >      UnsignedIntegerProperty;
00318 typedef SimpleProperty< long >              LongProperty;
00319 typedef SimpleProperty< unsigned long >     UnsignedLongProperty;
00320 typedef SimpleProperty< float >             FloatProperty;
00321 typedef SimpleProperty< double >            DoubleProperty;
00322 typedef SimpleProperty< long double >       LongDoubleProperty;
00323 
00324 typedef SimpleProperty< std::string >       StringProperty;
00325 
00326 
00327 // Typedef PropertyRefs for built-in types
00328 typedef SimplePropertyRef< bool >           BooleanPropertyRef;
00329 typedef SimplePropertyRef< char >           CharPropertyRef;
00330 typedef SimplePropertyRef< signed char >    SignedCharPropertyRef;
00331 typedef SimplePropertyRef< unsigned char >  UnsignedCharPropertyRef;
00332 typedef SimplePropertyRef< short >          ShortPropertyRef;
00333 typedef SimplePropertyRef< unsigned short > UnsignedShortPropertyRef;
00334 typedef SimplePropertyRef< int >            IntegerPropertyRef;
00335 typedef SimplePropertyRef< unsigned int >   UnsignedIntegerPropertyRef;
00336 typedef SimplePropertyRef< long >           LongPropertyRef;
00337 typedef SimplePropertyRef< unsigned long >  UnsignedLongPropertyRef;
00338 typedef SimplePropertyRef< float >          FloatPropertyRef;
00339 typedef SimplePropertyRef< double >         DoublePropertyRef;
00340 typedef SimplePropertyRef< long double >    LongDoublePropertyRef;
00341 
00342 typedef SimplePropertyRef< std::string >    StringPropertyRef;
00343 
00344 
00345 // Typedef "Arrays" of Properties for built-in types
00346 typedef SimpleProperty< std::vector< bool > >           BooleanArrayProperty;
00347 typedef SimpleProperty< std::vector< char > >           CharArrayProperty;
00348 typedef SimpleProperty< std::vector< signed char > >    SignedCharArrayProperty;
00349 typedef SimpleProperty< std::vector< unsigned char > >  UnsignedCharArrayProperty;
00350 typedef SimpleProperty< std::vector< short > >          ShortArrayProperty;
00351 typedef SimpleProperty< std::vector< unsigned short > > UnsignedShortArrayProperty;
00352 typedef SimpleProperty< std::vector< int > >            IntegerArrayProperty;
00353 typedef SimpleProperty< std::vector< unsigned int > >   UnsignedIntegerArrayProperty;
00354 typedef SimpleProperty< std::vector< long > >           LongArrayProperty;
00355 typedef SimpleProperty< std::vector< unsigned long > >  UnsignedLongArrayProperty;
00356 typedef SimpleProperty< std::vector< float > >          FloatArrayProperty;
00357 typedef SimpleProperty< std::vector< double > >         DoubleArrayProperty;
00358 typedef SimpleProperty< std::vector< long double > >    LongDoubleArrayProperty;
00359 
00360 typedef SimpleProperty< std::vector< std::string > >    StringArrayProperty;
00361 
00362 
00363 // Typedef "Arrays" of PropertyRefs for built-in types
00364 typedef SimplePropertyRef< std::vector< bool > >            BooleanArrayPropertyRef;
00365 typedef SimplePropertyRef< std::vector< char > >            CharArrayPropertyRef;
00366 typedef SimplePropertyRef< std::vector< signed char > >     SignedCharArrayPropertyRef;
00367 typedef SimplePropertyRef< std::vector< unsigned char > >   UnsignedCharArrayPropertyRef;
00368 typedef SimplePropertyRef< std::vector< short > >           ShortArrayPropertyRef;
00369 typedef SimplePropertyRef< std::vector< unsigned short > >  UnsignedShortArrayPropertyRef;
00370 typedef SimplePropertyRef< std::vector< int > >             IntegerArrayPropertyRef;
00371 typedef SimplePropertyRef< std::vector< unsigned int > >    UnsignedIntegerArrayPropertyRef;
00372 typedef SimplePropertyRef< std::vector< long > >            LongArrayPropertyRef;
00373 typedef SimplePropertyRef< std::vector< unsigned long > >   UnsignedLongArrayPropertyRef;
00374 typedef SimplePropertyRef< std::vector< float > >           FloatArrayPropertyRef;
00375 typedef SimplePropertyRef< std::vector< double > >          DoubleArrayPropertyRef;
00376 typedef SimplePropertyRef< std::vector< long double > >     LongDoubleArrayPropertyRef;
00377 
00378 typedef SimplePropertyRef< std::vector< std::string > >     StringArrayPropertyRef;
00379 
00380 
00381 #endif // GAUDIKERNEL_PROPERTY_H

Generated at Wed Nov 21 12:22:05 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000