]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STARLIGHT/starlight/include/inputParser.h
Update to trunk of hepforge
[u/mrichter/AliRoot.git] / STARLIGHT / starlight / include / inputParser.h
CommitLineData
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:
45d54d9a 23// $Rev:: 174 $: revision of last commit
da32329d 24// $Author:: odjuvsla $: author of last commit
45d54d9a 25// $Date:: 2014-04-03 22:36:25 +#$: date of last commit
da32329d
AM
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
44class inputParser
45{
46public:
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 &param);
87
88 /** Add a parameter */
89 template<typename P>
90 inline void addParameter(const std::string &name, P *varPtr, bool required = false);
45d54d9a 91
da32329d
AM
92private:
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
134template<typename S>
135void inputParser::addParameter(S& param)
136{
137 addParameter(param.name(), param.ptr(), param.required());
45d54d9a 138
139}
140
141template<typename P>
142void inputParser::addParameter(const std::string& name, P* /*varPtr*/, bool /*required*/)
143{
144 printWarn << "Trying to add unknown parameter type with name: " << name;
da32329d
AM
145}
146
45d54d9a 147
da32329d 148template<>
45d54d9a 149inline void inputParser::addParameter(const std::string& name, int * varPtr, bool required)
150{
151 addIntParameter(name, varPtr, required);
152}
da32329d
AM
153
154template<>
45d54d9a 155inline void inputParser::addParameter(const std::string& name, unsigned int * varPtr, bool required)
156{
157 addUintParameter(name, varPtr, required);
158}
da32329d
AM
159
160template<>
45d54d9a 161inline void inputParser::addParameter(const std::string& name, float * varPtr, bool required)
162{
163 addFloatParameter(name, varPtr, required);
164}
da32329d
AM
165
166template<>
45d54d9a 167inline void inputParser::addParameter(const std::string& name, double * varPtr, bool required)
168{
169 addDoubleParameter(name, varPtr, required);
170}
da32329d
AM
171
172template<>
45d54d9a 173inline void inputParser::addParameter(const std::string& name, bool * varPtr, bool required)
174{
175 addBoolParameter(name, varPtr, required);
176}
da32329d
AM
177
178template<>
45d54d9a 179inline void inputParser::addParameter(const std::string& name, std::string * varPtr, bool required)
180{
181 addStringParameter(name, varPtr, required);
182}
da32329d
AM
183
184#endif // INPUTPARSER_H