rambrain
configreader.h
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef CONFIGREADER_H
21 #define CONFIGREADER_H
22 
23 #include <string>
24 #include <fstream>
25 #include <istream>
26 #include <vector>
27 
28 #include "common.h"
29 #include "regexmatcher.h"
30 
31 using namespace std;
32 
33 //Test classes
34 #ifdef BUILD_TESTS
35 class configReader_Unit_ParseProgramName_Test;
36 #endif
37 
38 namespace rambrain
39 {
40 
47 enum class swapPolicy {
48  fixed,
51 };
52 
53 
58 {
59 
60 public:
66  configLineBase ( const string &name, const int matchType ) : name ( name ), matchType ( matchType ) {}
70  virtual ~configLineBase() {}
75  virtual void setValue ( const string &str ) = 0;
76 
77  const string name;
78  const int matchType;
79 
80 };
81 
82 
86 template<typename T>
87 class configLine : public configLineBase
88 {
89 public:
96  configLine ( const string &name, const T &value, const int matchType ) : configLineBase ( name, matchType ), value ( value ) {}
100  virtual ~configLine() {}
101 
106  virtual void setValue ( const string &str ) {
107  value = str;
108  }
109 
110  T value;
111 };
112 
114 template<>
115 void configLine<global_bytesize>::setValue ( const string &str );
116 
118 template<>
119 void configLine<bool>::setValue ( const string &str );
120 
122 template<>
123 void configLine<swapPolicy>::setValue ( const string &str );
124 
125 
132 
136  configuration();
137 
138  configLine<string> memoryManager, swap, swapfiles;
142 
143  vector<configLineBase *> configOptions;
144 };
145 
146 
164 {
165 
166 public:
171  configReader();
172 
180  bool readConfig();
181 
185  inline void setCustomConfigPath ( const string &path ) {
186  customConfigPath = path;
187  }
188 
193  if ( !readSuccess ) {
194  readConfig();
195  }
196  return config;
197  }
198 
202  inline bool readSuccessfully() const {
203  return readSuccess;
204  }
208  inline void setReread() {
209  readSuccess = false;
210  }
211 
212 private:
219  bool reopenStreams();
220 
230  bool parseConfigFile ( istream &stream, vector<configLineBase *> &readLines );
239  bool parseConfigBlock ( istream &stream, vector<configLineBase *> &readLines );
240 
245  string getApplicationName() const;
246 
251  string getHomeDir() const;
252 
257  void stripLeadingTrailingWhitespace ( string &str ) const;
258 
259  const string globalConfigPath = "/etc/rambrain.conf";
260  string localConfigPath = "";
261  string customConfigPath = "";
262 
264 
265  ifstream streams[3];
266 
268 
269  bool readSuccess = false;
270 
271  //Test classes
272 #ifdef BUILD_TESTS
273  friend class ::configReader_Unit_ParseProgramName_Test;
274 #endif
275 
276 };
277 
278 }
279 
280 #endif // CONFIGREADER_H
281 
virtual void setValue(const string &str)
Reset the value from a string.
Definition: configreader.h:106
swapPolicy
An enumeration to regulate how the swap should define when it approaches it's set boundary...
Definition: configreader.h:47
configLine< swapPolicy > policy
Definition: configreader.h:141
configLine< bool > enableDMA
Definition: configreader.h:140
virtual ~configLine()
Destructor.
Definition: configreader.h:100
configLine(const string &name, const T &value, const int matchType)
Create a new config option.
Definition: configreader.h:96
Class for config key value pairs represented by a line in a config file.
Definition: configreader.h:87
configLine< string > swapfiles
Definition: configreader.h:138
configuration & getConfig()
Simple getter.
Definition: configreader.h:192
Main struct to save configuration variables.
Definition: configreader.h:131
const regexMatcher regex
Definition: configreader.h:263
void setReread()
Simple setter.
Definition: configreader.h:208
rambrainConfig config
You will find the object in managedMemory.cpp as we have to define it in some 'used' file in the link...
Class to handle regex matching used for parsing configuration files.
Definition: regexmatcher.h:35
virtual ~configLineBase()
Destructor.
Definition: configreader.h:70
STL namespace.
void setCustomConfigPath(const string &path)
Simple setter.
Definition: configreader.h:185
bool readSuccessfully() const
Simple getter.
Definition: configreader.h:202
Reader class to read in and properly parse config files.
Definition: configreader.h:163
configuration config
Definition: configreader.h:267
configLineBase(const string &name, const int matchType)
Create a new config line.
Definition: configreader.h:66
configLine< global_bytesize > swapMemory
Definition: configreader.h:139
vector< configLineBase * > configOptions
Definition: configreader.h:143
Base class for config lines.
Definition: configreader.h:57