00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef EO_PARSER_H
00024 #define EO_PARSER_H
00025
00026 #include <map>
00027 #include <sstream>
00028 #include <string>
00029
00030 #include "eoParam.h"
00031 #include "eoObject.h"
00032 #include "eoPersistent.h"
00033
00040 class eoParameterLoader
00041 {
00042 public :
00043
00045 virtual ~eoParameterLoader();
00046
00052 virtual void processParam(eoParam& param, std::string section = "") = 0;
00053
00057 virtual bool isItThere(eoParam& _param) const = 0;
00058
00069 template <class ValueType>
00070 eoValueParam<ValueType>& createParam(ValueType _defaultValue,
00071 std::string _longName,
00072 std::string _description,
00073 char _shortHand = 0,
00074 std::string _section = "",
00075 bool _required = false)
00076 {
00077 eoValueParam<ValueType>* p = new eoValueParam<ValueType>(_defaultValue,
00078 _longName,
00079 _description,
00080 _shortHand,
00081 _required);
00082 ownedParams.push_back(p);
00083 processParam(*p, _section);
00084 return *p;
00085 }
00086
00087
00088 private :
00089
00090 std::vector<eoParam*> ownedParams;
00091 };
00092
00093
00094
00100 class eoParser : public eoParameterLoader, public eoObject, public eoPersistent
00101 {
00102
00103 public:
00104
00119 eoParser ( unsigned _argc, char **_argv , std::string _programDescription = "",
00120 std::string _lFileParamName = "param-file", char _shortHand = 'p');
00121
00125 void processParam(eoParam& param, std::string section = "");
00126
00127 void readFrom(std::istream& is);
00128
00129 void printOn(std::ostream& os) const;
00130
00132 std::string className(void) const { return "Parser"; }
00133
00135 bool userNeedsHelp(void);
00140 void printHelp(std::ostream& os);
00141
00142 std::string ProgramName() { return programName; }
00143
00148 virtual bool isItThere(eoParam& _param) const
00149 { return getValue(_param).first; }
00150
00159 eoParam * getParamWithLongName(const std::string& _name) const;
00160
00161
00162
00172 template <class ValueType>
00173 eoValueParam<ValueType>& getORcreateParam(ValueType _defaultValue,
00174 std::string _longName,
00175 std::string _description,
00176 char _shortHand = 0,
00177 std::string _section = "",
00178 bool _required = false)
00179 {
00180 eoParam* ptParam = getParamWithLongName(_longName);
00181 if (ptParam) {
00182
00183 eoValueParam<ValueType>* ptTypedParam(
00184 dynamic_cast<eoValueParam<ValueType>*>(ptParam));
00185 return *ptTypedParam;
00186 } else {
00187
00188 return createParam(_defaultValue, _longName, _description,
00189 _shortHand, _section, _required);
00190 }
00191 }
00192
00193
00194
00211 template <class ValueType>
00212 eoValueParam<ValueType>& setORcreateParam(ValueType _defaultValue,
00213 std::string _longName,
00214 std::string _description,
00215 char _shortHand = 0,
00216 std::string _section = "",
00217 bool _required = false)
00218 {
00219 eoValueParam<ValueType>& param = createParam(_defaultValue, _longName, _description,
00220 _shortHand, _section, _required);
00221 std::ostringstream os;
00222 os << _defaultValue;
00223 if(isItThere(param)) {
00224 param.setValue(os.str());
00225 } else {
00226 longNameMap[_longName] = os.str();
00227 shortNameMap[_shortHand] = os.str();
00228 }
00229 return param;
00230 }
00231
00232
00233
00235 void setStopOnUnknownParam(bool _b) {stopOnUnknownParam.value()=_b;}
00236 bool getStopOnUnknownParam() {return stopOnUnknownParam.value();}
00237
00239 void setPrefix(const std:: string & _prefix) {prefix = _prefix;}
00240
00241 void resetPrefix() {prefix = "";}
00242
00243 std::string getPrefix() {return prefix;}
00244
00245 private:
00246
00247 void doRegisterParam(eoParam& param) const;
00248
00249 std::pair<bool, std::string> getValue(eoParam& _param) const;
00250
00251 void updateParameters() const;
00252
00253 typedef std::multimap<std::string, eoParam*> MultiMapType;
00254
00255
00256 MultiMapType params;
00257
00258 std::string programName;
00259 std::string programDescription;
00260
00261 typedef std::map<char, std::string> ShortNameMapType;
00262 ShortNameMapType shortNameMap;
00263
00264 typedef std::map<std::string, std::string> LongNameMapType;
00265 LongNameMapType longNameMap;
00266
00267 eoValueParam<bool> needHelp;
00268 eoValueParam<bool> stopOnUnknownParam;
00269
00270 mutable std::vector<std::string> messages;
00271
00272 std::string prefix;
00273
00274 };
00275
00276
00277
00278 #endif // EO_PARSER_H
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288