Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <iostream>
00010 #include <sstream>
00011 #include <string>
00012 #include <functional>
00013 #include <stdexcept>
00014 #include <algorithm>
00015
00016
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
00051
00052 PropertyMgr::PropertyMgr(IInterface* iface)
00053 : m_properties ()
00054 , m_remoteProperties ()
00055 , m_todelete ()
00056 , m_pOuter ( iface )
00057 {
00058 addRef();
00059 }
00060
00061
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();
00074 std::transform
00075 ( m_todelete.begin() , m_todelete.end () ,
00076 m_todelete.begin() , std::mem_fun(&Property::clone));
00077 }
00078
00079
00080
00081 PropertyMgr::~PropertyMgr()
00082 {
00084 for ( Properties::iterator ip = m_todelete.begin() ;
00085 m_todelete.end() != ip ; ++ip ) { delete *ip ; }
00086 }
00087
00088
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
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
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 ; }
00132 return 0 ;
00133 }
00134
00135
00136
00137 Property* PropertyMgr::property
00138 ( const std::string& name ) const
00139 {
00140
00141 Property* lp = property ( name , m_properties ) ;
00142 if ( 0 != lp ) { return lp ; }
00143
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 ; }
00149 const IProperty* p = it->second.first ;
00150 if ( 0 == p ) { continue ; }
00151 return property ( it->second.second , p->getProperties() ) ;
00152 }
00153 return 0 ;
00154 }
00155
00156
00157
00158
00159
00160 StatusCode PropertyMgr::setProperty( const Property& p )
00161 {
00162 Property* pp = property( p.name() ) ;
00163 if ( 0 == pp ) { return StatusCode::FAILURE ; }
00164
00165 try
00166 { if ( !pp->assign(p) ) { return StatusCode::FAILURE ; } }
00167 catch(...) { return StatusCode::FAILURE ; }
00168
00169 return StatusCode::SUCCESS;
00170 }
00171
00172
00173
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
00187
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 ; }
00195 bool result = p->fromString( v ) != 0 ;
00196 return result ? StatusCode::SUCCESS : StatusCode::FAILURE ;
00197 }
00198
00199
00200
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 ; }
00209 if ( !pp->load( *p ) ) { return StatusCode::FAILURE ; }
00210 }
00211 catch ( ... ) { return StatusCode::FAILURE; }
00212 return StatusCode::SUCCESS ;
00213 }
00214
00215
00216
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 ; }
00223
00224 throw std::out_of_range( "Property "+name+" not found." );
00225 }
00226
00227
00228
00229
00230
00231 StatusCode PropertyMgr::getProperty
00232 ( const std::string& n ,
00233 std::string& v ) const
00234 {
00235
00236 const Property* p = property( n ) ;
00237 if ( 0 == p ) { return StatusCode::FAILURE ; }
00238
00239 v = p->toString() ;
00240
00241 return StatusCode::SUCCESS ;
00242 }
00243
00244
00245
00246 const std::vector<Property*>&
00247 PropertyMgr::getProperties( ) const { return m_properties; }
00248
00249
00250
00251 StatusCode PropertyMgr::queryInterface(const InterfaceID& iid, void** pinterface) {
00252
00253 StatusCode sc= base_class::queryInterface(iid, pinterface);
00254 if (sc.isSuccess()) return sc;
00255
00256 return (0 != m_pOuter)? m_pOuter->queryInterface(iid, pinterface)
00257 : sc;
00258 }
00259
00260
00261