Gaudi Framework, version v22r4

Home   Generated: Fri Sep 2 2011

PropertyMgr.cpp

Go to the documentation of this file.
00001 // $Id: PropertyMgr.cpp,v 1.23 2008/04/03 17:27:01 marcocle Exp $
00002 // ============================================================================
00003 // CVS tag $Name:  $, version $Revision: 1.23 $
00004 // ============================================================================
00005 // Include files
00006 // ============================================================================
00007 // STD& STL
00008 // ============================================================================
00009 #include <iostream>
00010 #include <sstream>
00011 #include <string>
00012 #include <functional>
00013 #include <stdexcept>
00014 #include <algorithm>
00015 // ============================================================================
00016 // GaudiKernel
00017 // ============================================================================
00018 #include "GaudiKernel/PropertyMgr.h"
00019 // ============================================================================
00020 namespace
00021 {
00022   // ==========================================================================
00024   struct Nocase : public std::binary_function<std::string,std::string,bool>
00025   {
00026     inline bool operator() ( const std::string& v1 ,
00027                              const std::string& v2 ) const
00028     {
00029       std::string::const_iterator i1 = v1.begin() ;
00030       std::string::const_iterator i2 = v2.begin() ;
00031       for ( ; v1.end() != i1 && v2.end() != i2 ; ++i1 , ++i2 )
00032       { if ( toupper(*i1) != toupper(*i2) ) { return false ; } }
00033       return v1.size() == v2.size() ;
00034     }
00035   };
00036   // ==========================================================================
00038   struct PropByName : public std::unary_function<const Property*,bool>
00039   {
00040     PropByName ( const std::string& name ) : m_name ( name ) {} ;
00041     inline bool operator() ( const Property* p ) const
00042     { return ( 0 == p ) ? false : m_cmp ( p->name() , m_name ) ; }
00043   private:
00044     std::string m_name ;
00045     Nocase      m_cmp  ;
00046   } ;
00047   // ==========================================================================
00048 }
00049 // ====================================================================
00050 // constructor from the interface
00051 // ====================================================================
00052 PropertyMgr::PropertyMgr(IInterface* iface)
00053   : m_properties       ()
00054   , m_remoteProperties ()
00055   , m_todelete         ()
00056   , m_pOuter           ( iface )
00057 {
00058   addRef(); // initial reference count set to 1
00059 }
00060 // ====================================================================
00061 // copy constructor
00062 // ====================================================================
00063 PropertyMgr::PropertyMgr ( const PropertyMgr& right )
00064   : IInterface(right),
00065     IProperty(right),
00066     extend_interfaces_base(right),
00067     base_class(right)
00068   , m_properties       ( right.m_properties       )
00069   , m_remoteProperties ( right.m_remoteProperties )
00070   , m_todelete         ( right.m_todelete         )
00071   , m_pOuter           ( right.m_pOuter )
00072 {
00073   addRef(); // initial reference count set to 1
00074   std::transform
00075     ( m_todelete.begin() , m_todelete.end  () ,
00076       m_todelete.begin() , std::mem_fun(&Property::clone));
00077 }
00078 // ====================================================================
00079 // destructor
00080 // ====================================================================
00081 PropertyMgr::~PropertyMgr()
00082 {
00084   for ( Properties::iterator ip  = m_todelete.begin() ;
00085         m_todelete.end() != ip ; ++ip ) { delete *ip ; }
00086 }
00087 // ====================================================================
00088 // assignment operator
00089 // ====================================================================
00090 PropertyMgr& PropertyMgr::operator=( const PropertyMgr& right )
00091 {
00092   if  ( &right == this ) { return *this ; }
00093   //
00094   for ( Properties::iterator ip  = m_todelete.begin() ;
00095         m_todelete.end() != ip ; ++ip ) { delete *ip ; }
00096   //
00097   m_properties       = right.m_properties       ;
00098   m_remoteProperties = right.m_remoteProperties ;
00099   m_todelete         = right.m_todelete         ;
00100   m_pOuter           = right.m_pOuter           ;
00101   //
00102   std::transform
00103     ( m_todelete.begin() , m_todelete.end  () ,
00104       m_todelete.begin() , std::mem_fun(&Property::clone));
00105   //
00106   return *this ;
00107 }
00108 // ====================================================================
00109 // Declare a remote property
00110 // ====================================================================
00111 Property* PropertyMgr::declareRemoteProperty
00112 ( const std::string& name  ,
00113   IProperty*         rsvc  ,
00114   const std::string& rname )
00115 {
00116   if ( 0 == rsvc ) { return 0 ; }
00117   const std::string& nam = rname.empty() ? name : rname ;
00118   Property* p = property ( nam , rsvc->getProperties() )  ;
00119   m_remoteProperties.push_back ( RemProperty ( name , std::make_pair ( rsvc , nam ) ) ) ;
00120   return p ;
00121 }
00122 // ====================================================================
00123 // get the property by name form the proposed list
00124 // ====================================================================
00125 Property* PropertyMgr::property
00126 ( const std::string&            name  ,
00127   const std::vector<Property*>& props ) const
00128 {
00129   Properties::const_iterator it =
00130     std::find_if ( props.begin() , props.end() , PropByName( name ) ) ;
00131   if ( props.end() != it ) { return *it ; }                // RETURN
00132   return 0 ;                                               // RETURN
00133 }
00134 // ====================================================================
00135 // retrieve the property by name
00136 // ====================================================================
00137 Property* PropertyMgr::property
00138 ( const std::string& name  ) const
00139 {
00140   // local property ?
00141   Property* lp = property ( name , m_properties ) ;
00142   if ( 0 != lp ) { return lp ; }                       // RETURN
00143   // look for remote property
00144   Nocase cmp ;
00145   for ( RemoteProperties::const_iterator it = m_remoteProperties.begin() ;
00146         m_remoteProperties.end() != it ; ++it )
00147   {
00148     if ( !cmp(it->first,name) ) { continue ; }   // CONTINUE
00149     const IProperty* p = it->second.first ;
00150     if ( 0 == p               ) { continue ; }   // CONITNUE
00151     return property ( it->second.second , p->getProperties() ) ; // RETURN
00152   }
00153   return 0 ;                                           // RETURN
00154 }
00155 // ====================================================================
00156 /* set a property from another property
00157  *  Implementation of IProperty::setProperty
00158  */
00159 // =====================================================================
00160 StatusCode PropertyMgr::setProperty( const Property& p )
00161 {
00162   Property* pp = property( p.name() ) ;
00163   if ( 0 == pp            ) { return StatusCode::FAILURE ; } // RETURN
00164   //
00165   try
00166   { if ( !pp->assign(p) ) { return StatusCode::FAILURE ; } } // RETURN
00167   catch(...)              { return StatusCode::FAILURE ;   } // RETURN
00168   //
00169   return StatusCode::SUCCESS;      // Property value set
00170 }
00171 // ====================================================================
00172 /* set a property from the stream
00173  *  Implementation of IProperty::setProperty
00174  */
00175 // =====================================================================
00176 StatusCode
00177 PropertyMgr::setProperty( const std::string& i )
00178 {
00179   std::string name  ;
00180   std::string value ;
00181   StatusCode sc = Gaudi::Parsers::parse( name , value , i ) ;
00182   if ( sc.isFailure() ) { return sc ; }
00183   return setProperty ( name , value ) ;
00184 }
00185 // =====================================================================
00186 /* set a property from the string
00187  *  Implementation of IProperty::setProperty
00188  */
00189 // =====================================================================
00190 StatusCode
00191 PropertyMgr::setProperty( const std::string& n, const std::string& v )
00192 {
00193   Property* p = property( n ) ;
00194   if ( 0 == p ) { return StatusCode::FAILURE ; }  // RETURN
00195   bool result =  p->fromString( v ) != 0 ;
00196   return result ? StatusCode::SUCCESS : StatusCode::FAILURE ;
00197 }
00198 // =====================================================================
00199 /* Retrieve the value of a property
00200  *  Implementation of IProperty::getProperty
00201  */
00202 // =====================================================================
00203 StatusCode PropertyMgr::getProperty(Property* p) const
00204 {
00205   try
00206   {
00207     const Property* pp = property( p->name() ) ;
00208     if ( 0 == pp         ) { return StatusCode::FAILURE ; }   // RETURN
00209     if ( !pp->load( *p ) ) { return StatusCode::FAILURE ; }   // RETURN
00210   }
00211   catch ( ... )            { return StatusCode::FAILURE;  }   // RETURN
00212   return StatusCode::SUCCESS ;                                // RETURN
00213 }
00214 // =====================================================================
00215 /* Get the property by name
00216  *  Implementation of IProperty::getProperty
00217  */
00218 // =====================================================================
00219 const Property& PropertyMgr::getProperty( const std::string& name) const
00220 {
00221   const Property* p = property( name ) ;
00222   if ( 0 != p ) { return *p ; }                        // RETURN
00223   //
00224   throw std::out_of_range( "Property "+name+" not found." );    // Not found
00225 }
00226 // =====================================================================
00227 /* Get the property by name
00228  *  Implementation of IProperty::getProperty
00229  */
00230 // =====================================================================
00231 StatusCode PropertyMgr::getProperty
00232 ( const std::string& n ,
00233   std::string&       v ) const
00234 {
00235   // get the property
00236   const Property* p = property( n ) ;
00237   if ( 0 == p ) { return StatusCode::FAILURE ; }
00238   // convert the value into the string
00239   v = p->toString() ;
00240   //
00241   return StatusCode::SUCCESS ;
00242 }
00243 // =====================================================================
00244 // Implementation of IProperty::getProperties
00245 // =====================================================================
00246 const std::vector<Property*>&
00247 PropertyMgr::getProperties( ) const { return m_properties; }
00248 // =====================================================================
00249 // Implementation of IInterface::queryInterface
00250 // =====================================================================
00251 StatusCode PropertyMgr::queryInterface(const InterfaceID& iid, void** pinterface) {
00252   // try local interfaces
00253   StatusCode sc= base_class::queryInterface(iid, pinterface);
00254   if (sc.isSuccess()) return sc;
00255   // fall back on the owner
00256   return (0 != m_pOuter)? m_pOuter->queryInterface(iid, pinterface)
00257                         : sc; // FAILURE
00258 }
00259 // =====================================================================
00260 // The END
00261 // =====================================================================
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Fri Sep 2 2011 16:24:44 for Gaudi Framework, version v22r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004