]>
Commit | Line | Data |
---|---|---|
da32329d AM |
1 | /////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // Copyright 2011 | |
4 | // | |
5 | // This file is part of starlight. | |
6 | // | |
7 | // starlight is free software: you can redistribute it and/or modify | |
8 | // it under the terms of the GNU General Public License as published by | |
9 | // the Free Software Foundation, either version 3 of the License, or | |
10 | // (at your option) any later version. | |
11 | // | |
12 | // starlight is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | // | |
17 | // You should have received a copy of the GNU General Public License | |
18 | // along with starlight. If not, see <http://www.gnu.org/licenses/>. | |
19 | // | |
20 | /////////////////////////////////////////////////////////////////////////// | |
21 | // | |
22 | // File and Version Information: | |
23 | // $Rev:: $: revision of last commit | |
24 | // $Author:: $: author of last commit | |
25 | // $Date:: $: date of last commit | |
26 | // | |
27 | // Description: | |
28 | // | |
29 | // | |
30 | // | |
31 | /////////////////////////////////////////////////////////////////////////// | |
32 | ||
33 | ||
34 | #ifndef INPUTPARSER_H | |
35 | #define INPUTPARSER_H | |
36 | ||
37 | #include <string> | |
38 | #include <typeinfo> | |
39 | #include <iostream> | |
40 | #include <map> | |
41 | ||
42 | #include <reportingUtils.h> | |
43 | ||
44 | class inputParser | |
45 | { | |
46 | public: | |
47 | ||
48 | /** Constructor */ | |
49 | inputParser(); | |
50 | ||
51 | /** Destructor */ | |
52 | ~inputParser(); | |
53 | ||
54 | /** Parse a file */ | |
55 | int parseFile(std::string filename); | |
56 | ||
57 | /** Parse a file */ | |
58 | int parseString(std::string str); | |
59 | ||
60 | /** Add parameter to pass */ | |
61 | void addIntParameter(std::string name, int *var, bool required = true); | |
62 | ||
63 | /** Add parameter to pass */ | |
64 | void addUintParameter(std::string name, unsigned int *var, bool required = true); | |
65 | ||
66 | /** Add parameter to pass */ | |
67 | void addFloatParameter(std::string name, float *var, bool required = true); | |
68 | ||
69 | /** Add parameter to pass */ | |
70 | void addDoubleParameter(std::string name, double *var, bool required = true); | |
71 | ||
72 | /** Add parameter to pass */ | |
73 | void addBoolParameter(std::string name, bool *var, bool required = true); | |
74 | ||
75 | /** Add parameter to pass */ | |
76 | void addStringParameter(std::string name, std::string *var, bool required = true); | |
77 | ||
78 | /** Print info */ | |
79 | void printParameterInfo(std::ostream &out = std::cout); | |
80 | ||
81 | /** Validate */ | |
82 | bool validateParameters(std::ostream &warnOut = std::cout, std::ostream &errOut = std::cerr); | |
83 | ||
84 | /** Add a parameter */ | |
85 | template<typename S> | |
86 | inline void addParameter(S ¶m); | |
87 | ||
88 | /** Add a parameter */ | |
89 | template<typename P> | |
90 | inline void addParameter(const std::string &name, P *varPtr, bool required = false); | |
91 | ||
92 | private: | |
93 | ||
94 | template <class T> | |
95 | class _parameter | |
96 | { | |
97 | public: | |
98 | _parameter(std::string name, T *val, bool required = true, bool found = false) : _name(name), _val(val), _required(required), _found(found){} | |
99 | ||
100 | bool operator==(const _parameter &rhs) const { return _name == rhs._name; } | |
101 | ||
102 | bool operator<(const _parameter &rhs) const { return _name.c_str()[0] < rhs._name.c_str()[0]; } | |
103 | ||
104 | void printParameterInfo(std::ostream &out = std::cout) | |
105 | { | |
106 | out << std::boolalpha << _name << "\t\t"; | |
107 | if(_found) | |
108 | { | |
109 | out << *_val << std::endl; | |
110 | } | |
111 | else | |
112 | { | |
113 | out << "NOT FOUND" << std::endl; | |
114 | } | |
115 | out << std::noboolalpha; | |
116 | } | |
117 | ||
118 | ||
119 | std::string _name; | |
120 | T *_val; | |
121 | bool _required; | |
122 | bool _found; | |
123 | }; | |
124 | ||
125 | std::map<std::string, _parameter<int> > _intParameters; | |
126 | std::map<std::string, _parameter<unsigned int> > _uintParameters; | |
127 | std::map<std::string, _parameter<float> > _floatParameters; | |
128 | std::map<std::string, _parameter<double> > _doubleParameters; | |
129 | std::map<std::string, _parameter<bool> > _boolParameters; | |
130 | std::map<std::string, _parameter<std::string> > _stringParameters; | |
131 | ||
132 | }; | |
133 | ||
134 | template<typename S> | |
135 | void inputParser::addParameter(S& param) | |
136 | { | |
137 | addParameter(param.name(), param.ptr(), param.required()); | |
138 | ||
139 | } | |
140 | ||
141 | template<typename P> | |
142 | void inputParser::addParameter(const std::string& name, P* /*varPtr*/, bool /*required*/) | |
143 | { | |
144 | printWarn << "Trying to add unknown parameter type with name: " << name; | |
145 | } | |
146 | ||
147 | ||
148 | #endif // INPUTPARSER_H |