openModeller  Version 1.5.0
Configuration.hh
Go to the documentation of this file.
1 
28 #ifndef _CONFIGURATION_HH_
29 #define _CONFIGURATION_HH_
30 
31 #include <openmodeller/refcount.hh>
32 
33 #include <string>
34 #include <vector>
35 #include <utility>
36 
37 class Sample;
38 
40 
43 
44 struct dllexp Configuration {
45 
46  typedef std::vector<ConfigurationPtr> subsection_list;
47  typedef std::pair<std::string,std::string> attribute;
48  typedef std::vector<attribute> attribute_list;
49 
50  static ConfigurationPtr readXml( char const *filename );
51  static ConfigurationPtr readXml( std::istream& is );
52 
53  static void writeXml( const ConstConfigurationPtr& config, char const *fileaname );
54  static void writeXml( const ConstConfigurationPtr& config, std::ostream& os );
55 
56 };
57 
59 {
62 
63 public:
64  ConfigurationImpl( char const * name );
66 
67 public:
68 
69  // Access to the Name of this Configuarion Section
70  std::string getName() const;
71  void setName( const std::string& );
72 
73  void setValue( const std::string& );
74  std::string getValue() const;
75 
76  // Get the configuration for the named Subsection
77  // If throws is true, will throw SubsectionNotFound.
78  // If throws is false, will not throw but return null configuration object.
79  ConstConfigurationPtr getSubsection( const std::string& name, bool throws = true ) const;
80  ConfigurationPtr getSubsection( const std::string& name, bool throws = true );
81 
82  // This call violates const correctness because the elements in
83  // the container are mutable.
84  const Configuration::subsection_list & getAllSubsections() const;
85 
86  void addSubsection( const ConfigurationPtr & config );
87 
88  // This will throw AttributeNotFound exception
89  std::string getAttribute( const std::string & name ) const;
90 
91  // If the attribute is not found, this will return defaultValue.
92  std::string getAttribute( const std::string & name, const std::string & defaultValue ) const ;
93 
94  template< typename T>
95  std::vector<T> getAttributeAsVec( const std::string& name ) const;
96 
97  int getAttributeAsInt( const std::string & name, int defaultValue ) const;
98  double getAttributeAsDouble( const std::string & name, double defaultValue ) const;
99  std::vector<double> getAttributeAsVecDouble( const std::string & name ) const;
100  void getAttributeAsDoubleArray( const std::string & name, double **, int * ) const;
101  std::vector<int> getAttributeAsVecInt( const std::string & name ) const;
102  void getAttributeAsIntArray( const std::string & name, int **, int * ) const;
103  Sample getAttributeAsSample( const std::string & name ) const;
104  void getAttributeAsByteArray( const std::string & name, unsigned char **, int * ) const;
105 
106  // This call violates const correctness because the elements in
107  // the container are mutable.
108  const Configuration::attribute_list & getAllAttributes() const;
109 
110  void addNameValue( const std::string & name, const std::string & value );
111  void addNameValue( const std::string & name, char const * value );
112  void addNameValue( const std::string & name, int value );
113  void addNameValue( const std::string & name, double value, int precision=25 );
114  void addNameValue( const std::string & name, const Sample& value );
115  void addNameValue( const std::string & name, double const *values, int count, int precision=25 );
116  void addNameValue( const std::string & name, int const *values, int count );
117  void addNameValue( const std::string & name, unsigned char const *values, int count );
118 
119  // Method to get int from string
120  static int getInt( const std::string& str, int defaultValue );
121  // Method to get double from string
122  static double getDouble( const std::string& str, double defaultValue );
123  // Method to get Sample from string
124  static Sample getSample( const std::string& str );
125 private:
126  std::string name;
127  std::string value;
130 
132 
133 };
134 
135 
136 inline void
137 ConfigurationImpl::setName( const std::string& name ) {
138  this->name = name;
139 }
140 
141 inline const Configuration::subsection_list &
143  return subsections;
144 }
145 
146 inline const Configuration::attribute_list &
148  return attributes;
149 }
150 
151 #endif // _CONFIGURATION_HH_
std::vector< ConfigurationPtr > subsection_list
ReferenceCountedPointer< const ConfigurationImpl > ConstConfigurationPtr
void setName(const std::string &)
ReferenceCountedPointer< ConfigurationImpl > ConfigurationPtr
const Configuration::attribute_list & getAllAttributes() const
Configuration::subsection_list subsections
std::vector< attribute > attribute_list
std::pair< std::string, std::string > attribute
const Configuration::subsection_list & getAllSubsections() const
Configuration::attribute_list attributes
Definition: Sample.hh:25