File indexing completed on 2024-09-27 07:03:37
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #ifndef INPUTPARSER_H
0035 #define INPUTPARSER_H
0036
0037 #include <string>
0038 #include <typeinfo>
0039 #include <iostream>
0040 #include <map>
0041
0042 #include <reportingUtils.h>
0043
0044 class inputParser
0045 {
0046 public:
0047
0048
0049 inputParser();
0050
0051
0052 ~inputParser();
0053
0054
0055 int parseFile(std::string filename);
0056
0057
0058 int parseString(std::string str);
0059
0060
0061 void addIntParameter(std::string name, int *var, bool required = true);
0062
0063
0064 void addUintParameter(std::string name, unsigned int *var, bool required = true);
0065
0066
0067 void addFloatParameter(std::string name, float *var, bool required = true);
0068
0069
0070 void addDoubleParameter(std::string name, double *var, bool required = true);
0071
0072
0073 void addBoolParameter(std::string name, bool *var, bool required = true);
0074
0075
0076 void addStringParameter(std::string name, std::string *var, bool required = true);
0077
0078
0079 void printParameterInfo(std::ostream &out = std::cout);
0080
0081
0082 bool validateParameters(std::ostream &errOut = std::cerr);
0083
0084
0085 template<typename S>
0086 inline void addParameter(S ¶m);
0087
0088
0089 template<typename P>
0090 inline void addParameter(const std::string &name, P *varPtr, bool required = false);
0091
0092 private:
0093
0094 template <class T>
0095 class _parameter
0096 {
0097 public:
0098 _parameter(std::string name, T *val, bool required = true, bool found = false) : _name(name), _val(val), _required(required), _found(found){}
0099
0100 bool operator==(const _parameter &rhs) const { return _name == rhs._name; }
0101
0102 bool operator<(const _parameter &rhs) const { return _name.c_str()[0] < rhs._name.c_str()[0]; }
0103
0104 void printParameterInfo(std::ostream &out = std::cout)
0105 {
0106 out << std::boolalpha << _name << "\t\t";
0107 if(_found)
0108 {
0109 out << *_val << std::endl;
0110 }
0111 else
0112 {
0113 out << "NOT FOUND" << std::endl;
0114 }
0115 out << std::noboolalpha;
0116 }
0117
0118
0119 std::string _name;
0120 T *_val;
0121 bool _required;
0122 bool _found;
0123 };
0124
0125 std::map<std::string, _parameter<int> > _intParameters;
0126 std::map<std::string, _parameter<unsigned int> > _uintParameters;
0127 std::map<std::string, _parameter<float> > _floatParameters;
0128 std::map<std::string, _parameter<double> > _doubleParameters;
0129 std::map<std::string, _parameter<bool> > _boolParameters;
0130 std::map<std::string, _parameter<std::string> > _stringParameters;
0131
0132 };
0133
0134 template<typename S>
0135 void inputParser::addParameter(S& param)
0136 {
0137 addParameter(param.name(), param.ptr(), param.required());
0138
0139 }
0140
0141 template<typename P>
0142 void inputParser::addParameter(const std::string& name, P* , bool )
0143 {
0144 printWarn << "Trying to add unknown parameter type with name: " << name;
0145 }
0146
0147
0148 template<>
0149 inline void inputParser::addParameter(const std::string& name, int * varPtr, bool required)
0150 {
0151 addIntParameter(name, varPtr, required);
0152 }
0153
0154 template<>
0155 inline void inputParser::addParameter(const std::string& name, unsigned int * varPtr, bool required)
0156 {
0157 addUintParameter(name, varPtr, required);
0158 }
0159
0160 template<>
0161 inline void inputParser::addParameter(const std::string& name, float * varPtr, bool required)
0162 {
0163 addFloatParameter(name, varPtr, required);
0164 }
0165
0166 template<>
0167 inline void inputParser::addParameter(const std::string& name, double * varPtr, bool required)
0168 {
0169 addDoubleParameter(name, varPtr, required);
0170 }
0171
0172 template<>
0173 inline void inputParser::addParameter(const std::string& name, bool * varPtr, bool required)
0174 {
0175 addBoolParameter(name, varPtr, required);
0176 }
0177
0178 template<>
0179 inline void inputParser::addParameter(const std::string& name, std::string * varPtr, bool required)
0180 {
0181 addStringParameter(name, varPtr, required);
0182 }
0183
0184 #endif