openModeller  Version 1.4.0
Configuration.hh
Go to the documentation of this file.
00001 
00028 #ifndef _CONFIGURATION_HH_
00029 #define _CONFIGURATION_HH_
00030 
00031 #include <openmodeller/refcount.hh>
00032 
00033 #include <string>
00034 #include <vector>
00035 #include <utility>
00036 
00037 class Sample;
00038 
00039 class ConfigurationImpl;
00040 
00041 typedef ReferenceCountedPointer<ConfigurationImpl> ConfigurationPtr;
00042 typedef ReferenceCountedPointer<const ConfigurationImpl> ConstConfigurationPtr;
00043 
00044 struct dllexp Configuration {
00045 
00046   typedef std::vector<ConfigurationPtr> subsection_list;
00047   typedef std::pair<std::string,std::string> attribute;
00048   typedef std::vector<attribute> attribute_list;
00049 
00050   static ConfigurationPtr readXml( char const *filename );
00051   static ConfigurationPtr readXml( std::istream& is );
00052 
00053   static void writeXml( const ConstConfigurationPtr& config, char const *fileaname );
00054   static void writeXml( const ConstConfigurationPtr& config, std::ostream& os );
00055 
00056 };
00057 
00058 class dllexp ConfigurationImpl : private ReferenceCountedObject
00059 {
00060   friend class ReferenceCountedPointer<ConfigurationImpl>;
00061   friend class ReferenceCountedPointer<const ConfigurationImpl>;
00062 
00063 public:
00064   ConfigurationImpl( char const * name );
00065   ConfigurationImpl();
00066 
00067 public:
00068   
00069   // Access to the Name of this Configuarion Section
00070   std::string getName() const;
00071   void setName( const std::string& );
00072 
00073   void setValue( const std::string& );
00074   std::string getValue() const;
00075 
00076   // Get the configuration for the named Subsection
00077   // If throws is true, will throw SubsectionNotFound.
00078   // If throws is false, will not throw but return null configuration object.
00079   ConstConfigurationPtr getSubsection( const std::string& name, bool throws = true ) const;
00080   ConfigurationPtr getSubsection( const std::string& name, bool throws = true );
00081 
00082   // This call violates const correctness because the elements in
00083   // the container are mutable.
00084   const Configuration::subsection_list & getAllSubsections() const;
00085   
00086   void addSubsection( const ConfigurationPtr & config );
00087 
00088   // This will throw AttributeNotFound exception
00089   std::string getAttribute( const std::string & name ) const;
00090 
00091   // If the attribute is not found, this will return defaultValue.
00092   std::string getAttribute( const std::string & name, const std::string & defaultValue ) const ;
00093 
00094   template< typename T>
00095   std::vector<T> getAttributeAsVec( const std::string& name ) const;
00096 
00097   int getAttributeAsInt( const std::string & name, int defaultValue ) const;
00098   double getAttributeAsDouble( const std::string & name, double defaultValue ) const;
00099   std::vector<double> getAttributeAsVecDouble( const std::string & name ) const;
00100   void getAttributeAsDoubleArray( const std::string & name, double **, int * ) const;
00101   std::vector<int> getAttributeAsVecInt( const std::string & name ) const;
00102   void getAttributeAsIntArray( const std::string & name, int **, int * ) const;
00103   Sample getAttributeAsSample( const std::string & name ) const;
00104   void getAttributeAsByteArray( const std::string & name, unsigned char **, int * ) const;
00105   
00106   // This call violates const correctness because the elements in
00107   // the container are mutable.
00108   const Configuration::attribute_list & getAllAttributes() const;
00109   
00110   void addNameValue( const std::string & name, const std::string & value );
00111   void addNameValue( const std::string & name, char const * value );
00112   void addNameValue( const std::string & name, int value );
00113   void addNameValue( const std::string & name, double value, int precision=25 );
00114   void addNameValue( const std::string & name, const Sample& value );
00115   void addNameValue( const std::string & name, double const *values, int count, int precision=25 );
00116   void addNameValue( const std::string & name, int const *values, int count );
00117   void addNameValue( const std::string & name, unsigned char const *values, int count );
00118   
00119   // Method to get int from string
00120   static int getInt( const std::string& str, int defaultValue );
00121   // Method to get double from string
00122   static double getDouble( const std::string& str, double defaultValue );
00123   // Method to get Sample from string
00124   static Sample getSample( const std::string& str ); 
00125 private:
00126   std::string name;
00127   std::string value;
00128   Configuration::subsection_list subsections;
00129   Configuration::attribute_list attributes;
00130 
00131   ~ConfigurationImpl();
00132   
00133 };
00134 
00135 
00136 inline void
00137 ConfigurationImpl::setName( const std::string& name ) {
00138   this->name = name;
00139 }
00140 
00141 inline const Configuration::subsection_list &
00142 ConfigurationImpl::getAllSubsections() const {
00143   return subsections;
00144 }
00145 
00146 inline const Configuration::attribute_list &
00147 ConfigurationImpl::getAllAttributes() const {
00148   return attributes;
00149 }
00150 
00151 #endif // _CONFIGURATION_HH_