EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inputParser.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file inputParser.h
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 //
21 //
22 // File and Version Information:
23 // $Rev:: 208 $: revision of last commit
24 // $Author:: jnystrand $: author of last commit
25 // $Date:: 2015-08-09 21:29:36 +#$: date of last commit
26 //
27 // Description:
28 //
29 //
30 //
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 
45 {
46 public:
47 
49  inputParser();
50 
52  ~inputParser();
53 
55  int parseFile(std::string filename);
56 
58  int parseString(std::string str);
59 
61  void addIntParameter(std::string name, int *var, bool required = true);
62 
64  void addUintParameter(std::string name, unsigned int *var, bool required = true);
65 
67  void addFloatParameter(std::string name, float *var, bool required = true);
68 
70  void addDoubleParameter(std::string name, double *var, bool required = true);
71 
73  void addBoolParameter(std::string name, bool *var, bool required = true);
74 
76  void addStringParameter(std::string name, std::string *var, bool required = true);
77 
79  void printParameterInfo(std::ostream &out = std::cout);
80 
82  bool validateParameters(std::ostream &errOut = std::cerr);
83 
85  template<typename S>
86  inline void addParameter(S &param);
87 
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>
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 template<>
149 inline void inputParser::addParameter(const std::string& name, int * varPtr, bool required)
150 {
151  addIntParameter(name, varPtr, required);
152 }
153 
154 template<>
155 inline void inputParser::addParameter(const std::string& name, unsigned int * varPtr, bool required)
156 {
157  addUintParameter(name, varPtr, required);
158 }
159 
160 template<>
161 inline void inputParser::addParameter(const std::string& name, float * varPtr, bool required)
162 {
163  addFloatParameter(name, varPtr, required);
164 }
165 
166 template<>
167 inline void inputParser::addParameter(const std::string& name, double * varPtr, bool required)
168 {
169  addDoubleParameter(name, varPtr, required);
170 }
171 
172 template<>
173 inline void inputParser::addParameter(const std::string& name, bool * varPtr, bool required)
174 {
175  addBoolParameter(name, varPtr, required);
176 }
177 
178 template<>
179 inline void inputParser::addParameter(const std::string& name, std::string * varPtr, bool required)
180 {
181  addStringParameter(name, varPtr, required);
182 }
183 
184 #endif // INPUTPARSER_H