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

PropertyMgr.cpp

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/GaudiKernel/src/Lib/PropertyMgr.cpp,v 1.1.1.1 2001/04/18 18:14:18 tlindner Exp $
00002 
00003 // Include files
00004 
00005 #include "GaudiKernel/PropertyMgr.h"
00006 
00007 // strings.h does not exist on WIN32. Needed for case insensitive comparison
00008 #ifdef WIN32
00009 #  include <string.h>
00010 #  define strcasecmp _stricmp
00011 #else
00012 #  include <strings.h>
00013 #endif
00014 #include <iostream>
00015 #include <strstream>
00016 #include <stdexcept>
00017 
00018 PropertyMgr::PropertyMgr()  { }
00019 
00020 PropertyMgr::~PropertyMgr() {
00021 
00022     // This is a kludge in order to avoid problems with a Property having
00023     // been deleted defore it's entry in the list is removed. Either we need
00024     // to clone a Property prior to the clone being inserted in the list, or
00025     // we need to indicate whether the Property is owned by this PropertyMgr.
00026     // CHris Day originally did the former, but that had template problems
00027     // with the Solaris compiler. This implementation uses the second
00028     // approach. We should probably revisit this.
00029     PropertyCollectionType::iterator it;
00030     std::vector<bool>::const_iterator itb = m_isOwned.begin( );
00031     for(it = m_properties.begin(); it != m_properties.end(); it++) {
00032         if ( (*itb) ) {
00033             delete (*it);
00034         }
00035         itb++;
00036     }
00037 }
00038 
00039 // Set the value of a property
00040 StatusCode PropertyMgr::setProperty(const Property& p) {
00041   PropertyCollectionType::iterator it;
00042   // Locate the property by name
00043   for(it = m_properties.begin(); it != m_properties.end(); it++) {
00044     if( strcasecmp(((*it)->name()).c_str(), (p.name()).c_str()) == 0 ) {
00045       // Property found: assign value
00046       // An exception is thrown in the event of type mismatch
00047       // catch(...) is used because catch(std::bad_cast) doesn't work
00048       try {
00049         (*it)->assign(p);
00050       } catch(...) {
00051         return StatusCode::FAILURE;    // Type mismatch
00052       }
00053       return StatusCode::SUCCESS;        // Property value set
00054     }
00055   }
00056   return StatusCode::FAILURE;
00057 }
00058 
00059 
00060 StatusCode
00061 PropertyMgr::setProperty( std::istream& i )
00062 {
00063     SimpleProperty< int > modelProperty;
00064     modelProperty.nameFromStream( i );
00065 
00066     try {
00067         Property& actualProperty = const_cast<Property&>(getProperty( modelProperty.name( ) ));
00068         actualProperty.valueFromStream( i );
00069     } catch ( std::out_of_range ) {
00070         return StatusCode::FAILURE;     // Property not found
00071     }
00072 
00073     return StatusCode::SUCCESS;
00074 }
00075 
00076 
00077 StatusCode
00078 PropertyMgr::setProperty( const std::string& n, const std::string& v )
00079 {
00080     try {
00081         Property& property = const_cast<Property&>(getProperty( n ));
00082         std::strstream s;
00083         s << v ;  //<< std::ends;
00084         property.valueFromStream( s );
00085         s.freeze(false);
00086     } catch ( std::out_of_range ) {
00087         return StatusCode::FAILURE;     // Property not found
00088     }
00089     return StatusCode::SUCCESS;
00090 }
00091 
00092 
00093 // Retrieve the value of a property
00094 StatusCode PropertyMgr::getProperty(Property* p) const {
00095   try {
00096     const Property& source = getProperty( p->name() );
00097     source.load( *p );
00098     return StatusCode::SUCCESS;
00099   }
00100   catch ( ... ) {
00101     return StatusCode::FAILURE;        // Property not found
00102   }
00103 }
00104 
00105 // Get the property by name
00106 const Property& PropertyMgr::getProperty( const std::string& name) const {
00107   PropertyCollectionType::const_iterator it;
00108   // Locate the property by name
00109   for(it = m_properties.begin(); it != m_properties.end(); it++) {
00110     if( strcasecmp(((*it)->name()).c_str(), name.c_str()) == 0 ) {
00111       // Property found
00112       return **it;
00113     }
00114   }
00115   throw std::out_of_range( "Property "+name+" not found." );    // Not found
00116 }
00117 
00118 // Get the property by name
00119 StatusCode PropertyMgr::getProperty( const std::string& n, std::string& v ) const {
00120   // This method is not implementable since the return argument is const !!!! 
00121   std::strstream s;
00122   v = "";
00123   try {
00124     const Property& property = getProperty( n );
00125     property.nameAndValueAsStream(s);
00126     while( s.get() != ':' && !s.fail() ) { }
00127     while( !s.fail() ) v += s.get(); 
00128     return StatusCode::SUCCESS;
00129   }
00130   catch ( ... ) {
00131     return StatusCode::FAILURE;        // Property not found
00132   }
00133 }
00134 
00135 const std::vector<Property*>& PropertyMgr::getProperties( ) const {
00136   return m_properties;
00137 }
00138 
00139 
00140 // Implementation (or not) of IInterface
00141 StatusCode PropertyMgr::queryInterface(const IID&, void**) { return StatusCode::FAILURE; }
00142 unsigned long PropertyMgr::addRef() { return 0; }
00143 unsigned long PropertyMgr::release() { return 0; }

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