OpenDaVINCI  4.15.3
KeyValueConfiguration.h
Go to the documentation of this file.
1 
20 #ifndef OPENDAVINCI_CORE_BASE_KEYVALUECONFIGURATION_H_
21 #define OPENDAVINCI_CORE_BASE_KEYVALUECONFIGURATION_H_
22 
23 #include <cerrno>
24 #include <map>
25 #include <iosfwd>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
34 #include "opendavinci/generated/odcore/data/LogMessage.h"
35 
36 namespace odcore {
37  namespace base {
38 
39  using namespace std;
40 
59  public:
61 
68 
69  virtual ~KeyValueConfiguration();
70 
77  KeyValueConfiguration& operator=(const KeyValueConfiguration &obj);
78 
79  virtual int32_t getID() const;
80  virtual const string getShortName() const;
81  virtual const string getLongName() const;
82  virtual const string toString() const;
83 
84  virtual ostream& operator<<(ostream &out) const;
85  virtual istream& operator>>(istream &in);
86 
94  ostream& writeTo(ostream &out) const;
95 
103  istream& readFrom(istream &in);
104 
118  template<class T>
119  inline T getValue(const string &key) const throw (exceptions::ValueForKeyNotFoundException) {
120  string stringValue(getValueFor(key));
121  if (stringValue == "") {
122  stringstream s;
123  s << "Value for key '" << key << "' not found.";
124  errno = 0;
125  OPENDAVINCI_CORE_THROW_EXCEPTION(ValueForKeyNotFoundException, s.str());
126  }
127  stringstream s(stringValue);
128  T value;
129  s >> value;
130  return value;
131  };
132 
146  template<class T>
147  inline T getOptionalValue(const string &key, bool &isFound) const {
148  string stringValue(getValueFor(key));
149  if (stringValue == "") {
150  isFound=false;
151  stringstream s;
152  T value;
153  s << "Value for optional key '" << key << "' not found.";
154  odcore::data::LogMessage lm("odcore::base::KeyValueConfiguration", odcore::data::LogMessage::LogLevel::INFO, s.str());
155  #pragma GCC diagnostic push
156  #pragma GCC diagnostic ignored "-Wuninitialized"
157  #ifndef __APPLE__
158  #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
159  #endif
160  return value;
161  #pragma GCC diagnostic pop
162  }
163  isFound=true;
164  stringstream s(stringValue);
165  T value;
166  s >> value;
167  return value;
168  };
169 
178  KeyValueConfiguration getSubsetForSectionRemoveLeadingSectionName(const string &section) const;
179 
187  KeyValueConfiguration getSubsetForSection(const string &section) const;
188 
194  const vector<string> getListOfKeys() const;
195 
196  private:
197  map<string, string, odcore::strings::StringComparator> m_keyValueConfiguration;
198 
206  string getValueFor(string key) const;
207  };
208 
209  }
210 } // odcore::base
211 
212 #endif /*OPENDAVINCI_CORE_BASE_KEYVALUECONFIGURATION_H_*/
T getValue(const string &key) const
Definition: KeyValueConfiguration.h:119
T getOptionalValue(const string &key, bool &isFound) const
Definition: KeyValueConfiguration.h:147
#define OPENDAVINCI_API
Definition: opendavinci.h:67
Definition: SerializableData.h:36
#define OPENDAVINCI_CORE_THROW_EXCEPTION(ExceptionClass, ExceptionMessage)
Definition: opendavinci.h:209
Definition: KeyValueConfiguration.h:58