charon-core  0.3.1
ParameterFile.h
Go to the documentation of this file.
1 /* This file is part of Charon.
2 
3  Charon is free software: you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as published by
5  the Free Software Foundation, either version 3 of the License, or
6  (at your option) any later version.
7 
8  Charon is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU Lesser General Public License for more details.
12 
13  You should have received a copy of the GNU Lesser General Public License
14  along with Charon. If not, see <http://www.gnu.org/licenses/>.
15 */
41 #ifndef _ParameterFile_H
42 #define _ParameterFile_H
43 
44 #include <vector>
45 #include <map>
46 #include <iostream>
47 #include <stdexcept>
48 #include "DllEx.h"
49 
69 {
70 private:
76  void _set(std::string parameter, std::string value);
77 
81  inline void _toLower(std::string& input) const;
82 
84  // @{
85 
89  std::vector<std::string> _parameterLines;
90 
92  std::map<std::string, std::string> _params;
93 
96  std::map<std::string, std::string> _setParams;
97 
98  // @}
99 
101  // @{
102 
105 
109 
112 
113  // @}
114 
115 public:
117  class charon_core_DLL_PUBLIC IoError : public std::runtime_error {
118  public:
121  explicit IoError(const std::string& msg);
122  };
123 
125  class charon_core_DLL_PUBLIC Unset : public std::invalid_argument {
126  public:
129  explicit Unset(const std::string& msg);
130  };
131 
133  ParameterFile();
134 
137  ParameterFile(std::string fileName);
138 
140  ~ParameterFile();
141 
146  static std::string parName(std::string fullName);
147 
152  static std::string objName(std::string fullName);
153 
156  void setDelimiter(char delimiter);
157 
160  void setConvertSlashes(bool convertSlashes);
161 
165  bool isSet(std::string parameter) const;
166 
170  template<class T>
171  void set(std::string parameter, const T& value = T());
172 
177  template<class T>
178  void set(std::string parameter, const std::vector<T>& value);
179 
185  template<class T>
186  T get(std::string parameter) const;
187 
192  template<class T>
193  T get(std::string parameter, T defaultValue);
194 
202  template<class T>
203  std::vector<T> getList(std::string parameter) const;
204 
211  template<class T>
212  inline std::vector<T> getList(
213  std::string parameter, std::string defaultValue);
214 
220  std::vector<std::string> getKeyList(std::string beginsWith = "") const;
221 
232  std::vector<std::string> getEveryParameter(const std::string& param) const;
233 
235  void showSetParams() const;
236 
238  void clear();
239 
245  void erase(std::string parameter);
246 
249  void resetSetParams();
250 
253  void setNotFoundWarningsOn(bool noFoundWarnings);
254 
260  bool save(std::string fileName) const;
261 
266  bool load(std::string fileName);
267 
270  void toStream(std::ostream &strm) const;
271 
274  void fromStream(std::istream& strm);
275 };
276 
277 #endif // _ParameterFile_H
std::map< std::string, std::string > _params
maps parameters to their respective values
Definition: ParameterFile.h:92
#define charon_DEPRECATED
handle deprecated messages on GCC and MSVC
Definition: DllEx.h:87
This class serves to store parameters used within the Charon Project.
Definition: ParameterFile.h:68
Exception thrown when trying to access unset parameters.
std::map< std::string, std::string > _setParams
stores modified and new parameters in a seperate map (see showSetParams())
Definition: ParameterFile.h:96
bool _convertSlashes
convert linux to windows slashes and vice versa (depends on define CHARON_LINUX/CHARON_WINDOWS in Str...
std::vector< std::string > _parameterLines
this vector keeps a copy of all parameters in order to preserve their ordering in the actual file it ...
Definition: ParameterFile.h:89
char _delimiter
delimiter for lists of values (default is ';')
#define charon_core_DLL_PUBLIC
Preprocessor macro to define this class/function as public visible, i.e.
Definition: DllEx.h:82
I/O error exception thrown by ParameterFile instances.
Dynamic library API import/export prprocessor macros.
bool _noFoundWarnings
warn if parameter was not found