![]() |
|
|
Generated: 8 Jan 2009 |
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 , m_refcount ( 1 ) 00058 {} 00059 // ==================================================================== 00060 // copy constructor 00061 // ==================================================================== 00062 PropertyMgr::PropertyMgr ( const PropertyMgr& right ) 00063 : IInterface(right), IProperty(right) 00064 , m_properties ( right.m_properties ) 00065 , m_remoteProperties ( right.m_remoteProperties ) 00066 , m_todelete ( right.m_todelete ) 00067 , m_pOuter ( right.m_pOuter ) 00068 , m_refcount ( 1 ) 00069 { 00070 std::transform 00071 ( m_todelete.begin() , m_todelete.end () , 00072 m_todelete.begin() , std::mem_fun(&Property::clone)); 00073 } 00074 // ==================================================================== 00075 // destructor 00076 // ==================================================================== 00077 PropertyMgr::~PropertyMgr() 00078 { 00080 for ( Properties::iterator ip = m_todelete.begin() ; 00081 m_todelete.end() != ip ; ++ip ) { delete *ip ; } 00082 } 00083 // ==================================================================== 00084 // assignement operator 00085 // ==================================================================== 00086 PropertyMgr& PropertyMgr::operator=( const PropertyMgr& right ) 00087 { 00088 if ( &right == this ) { return *this ; } 00089 // 00090 for ( Properties::iterator ip = m_todelete.begin() ; 00091 m_todelete.end() != ip ; ++ip ) { delete *ip ; } 00092 // 00093 m_properties = right.m_properties ; 00094 m_remoteProperties = right.m_remoteProperties ; 00095 m_todelete = right.m_todelete ; 00096 m_pOuter = right.m_pOuter ; 00097 // 00098 std::transform 00099 ( m_todelete.begin() , m_todelete.end () , 00100 m_todelete.begin() , std::mem_fun(&Property::clone)); 00101 // 00102 return *this ; 00103 } 00104 // ==================================================================== 00105 // Declare a remote property 00106 // ==================================================================== 00107 Property* PropertyMgr::declareRemoteProperty 00108 ( const std::string& name , 00109 IProperty* rsvc , 00110 const std::string& rname ) 00111 { 00112 if ( 0 == rsvc ) { return 0 ; } 00113 const std::string& nam = rname.empty() ? name : rname ; 00114 Property* p = property ( nam , rsvc->getProperties() ) ; 00115 m_remoteProperties.push_back ( RemProperty ( name , std::make_pair ( rsvc , nam ) ) ) ; 00116 return p ; 00117 } 00118 // ==================================================================== 00119 // get the property by name form the proposed list 00120 // ==================================================================== 00121 Property* PropertyMgr::property 00122 ( const std::string& name , 00123 const std::vector<Property*>& props ) const 00124 { 00125 Properties::const_iterator it = 00126 std::find_if ( props.begin() , props.end() , PropByName( name ) ) ; 00127 if ( props.end() != it ) { return *it ; } // RETURN 00128 return 0 ; // RETURN 00129 } 00130 // ==================================================================== 00131 // retrive the property by name 00132 // ==================================================================== 00133 Property* PropertyMgr::property 00134 ( const std::string& name ) const 00135 { 00136 // local property ? 00137 Property* lp = property ( name , m_properties ) ; 00138 if ( 0 != lp ) { return lp ; } // RETURN 00139 // look for remote property 00140 Nocase cmp ; 00141 for ( RemoteProperties::const_iterator it = m_remoteProperties.begin() ; 00142 m_remoteProperties.end() != it ; ++it ) 00143 { 00144 if ( !cmp(it->first,name) ) { continue ; } // CONTINUE 00145 const IProperty* p = it->second.first ; 00146 if ( 0 == p ) { continue ; } // CONITNUE 00147 return property ( it->second.second , p->getProperties() ) ; // RETURN 00148 } 00149 return 0 ; // RETURN 00150 } 00151 // ==================================================================== 00152 /* set a property from another property 00153 * Implementation of IProperty::setProperty 00154 */ 00155 // ===================================================================== 00156 StatusCode PropertyMgr::setProperty( const Property& p ) 00157 { 00158 Property* pp = property( p.name() ) ; 00159 if ( 0 == pp ) { return StatusCode::FAILURE ; } // RETURN 00160 // 00161 try 00162 { if ( !pp->assign(p) ) { return StatusCode::FAILURE ; } } // RETURN 00163 catch(...) { return StatusCode::FAILURE ; } // RETURN 00164 // 00165 return StatusCode::SUCCESS; // Property value set 00166 } 00167 // ==================================================================== 00168 /* set a property from the stream 00169 * Implementation of IProperty::setProperty 00170 */ 00171 // ===================================================================== 00172 StatusCode 00173 PropertyMgr::setProperty( const std::string& i ) 00174 { 00175 std::string name ; 00176 std::string value ; 00177 StatusCode sc = Gaudi::Parsers::parse( name , value , i ) ; 00178 if ( sc.isFailure() ) { return sc ; } 00179 return setProperty ( name , value ) ; 00180 } 00181 // ===================================================================== 00182 /* set a property from the string 00183 * Implementation of IProperty::setProperty 00184 */ 00185 // ===================================================================== 00186 StatusCode 00187 PropertyMgr::setProperty( const std::string& n, const std::string& v ) 00188 { 00189 Property* p = property( n ) ; 00190 if ( 0 == p ) { return StatusCode::FAILURE ; } // RETURN 00191 bool result = p->fromString( v ) != 0 ; 00192 return result ? StatusCode::SUCCESS : StatusCode::FAILURE ; 00193 } 00194 // ===================================================================== 00195 /* Retrieve the value of a property 00196 * Implementation of IProperty::getProperty 00197 */ 00198 // ===================================================================== 00199 StatusCode PropertyMgr::getProperty(Property* p) const 00200 { 00201 try 00202 { 00203 const Property* pp = property( p->name() ) ; 00204 if ( 0 == pp ) { return StatusCode::FAILURE ; } // RETURN 00205 if ( !pp->load( *p ) ) { return StatusCode::FAILURE ; } // RETURN 00206 } 00207 catch ( ... ) { return StatusCode::FAILURE; } // RETURN 00208 return StatusCode::SUCCESS ; // RETURN 00209 } 00210 // ===================================================================== 00211 /* Get the property by name 00212 * Implementation of IProperty::getProperty 00213 */ 00214 // ===================================================================== 00215 const Property& PropertyMgr::getProperty( const std::string& name) const 00216 { 00217 const Property* p = property( name ) ; 00218 if ( 0 != p ) { return *p ; } // RETURN 00219 // 00220 throw std::out_of_range( "Property "+name+" not found." ); // Not found 00221 } 00222 // ===================================================================== 00223 /* Get the property by name 00224 * Implementation of IProperty::getProperty 00225 */ 00226 // ===================================================================== 00227 StatusCode PropertyMgr::getProperty 00228 ( const std::string& n , 00229 std::string& v ) const 00230 { 00231 // get the property 00232 const Property* p = property( n ) ; 00233 if ( 0 == p ) { return StatusCode::FAILURE ; } 00234 // convert the value into the string 00235 v = p->toString() ; 00236 // 00237 return StatusCode::SUCCESS ; 00238 } 00239 // ===================================================================== 00240 // Implementation of IProperty::getProperties 00241 // ===================================================================== 00242 const std::vector<Property*>& 00243 PropertyMgr::getProperties( ) const { return m_properties; } 00244 // ===================================================================== 00245 // Implementation of IInterface::queryInterface 00246 // ===================================================================== 00247 StatusCode PropertyMgr::queryInterface 00248 ( const InterfaceID& iid , 00249 void** pinterface ) 00250 { 00251 // invalid destination 00252 if ( 0 == pinterface ) { return StatusCode::FAILURE ; } 00253 00254 if ( IInterface ::interfaceID () == iid ) 00255 { *pinterface = static_cast<IInterface*> ( this ) ; } 00256 else if ( IProperty ::interfaceID () == iid ) 00257 { *pinterface = static_cast<IProperty*> ( this ) ; } 00258 else if ( 0 != m_pOuter ) // get the interface from the hub 00259 { return m_pOuter->queryInterface(iid, pinterface) ; } // RETURN 00260 else { return StatusCode::FAILURE ; } // RETURN 00261 // 00262 addRef() ; 00263 return StatusCode::SUCCESS ; 00264 } 00265 // ===================================================================== 00266 // Implementation of IInterface::addRef 00267 // ===================================================================== 00268 unsigned long PropertyMgr::addRef() { return ++m_refcount; } 00269 // ===================================================================== 00270 // Implementation of IInterface::release 00271 // ===================================================================== 00272 unsigned long PropertyMgr::release() 00273 { 00274 unsigned long count = --m_refcount; 00275 if ( 0 == count ) { delete this; } 00276 return count; 00277 } 00278 // ===================================================================== 00279 // The END 00280 // =====================================================================