EvolvingObjects
|
00001 /* (c) Marc Schoenauer, Maarten Keijzer, GeNeura Team, Thales group 00002 00003 This library is free software; you can redistribute it and/or modify it under 00004 the terms of the GNU Lesser General Public License as published by the Free 00005 Software Foundation; either version 2 of the License, or (at your option) any 00006 later version. 00007 00008 This library is distributed in the hope that it will be useful, but WITHOUT ANY 00009 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 00010 PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 00011 00012 You should have received a copy of the GNU Lesser General Public License along 00013 with this library; if not, write to the Free Software Foundation, Inc., 59 00014 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00015 00016 Contact: http://eodev.sourceforge.net 00017 Authors: 00018 todos@geneura.ugr.es, http://geneura.ugr.es 00019 Marc.Schoenauer@polytechnique.fr 00020 mkeijzer@dhi.dk 00021 Johann Dréo <johann.dreo@thalesgroup.com> 00022 */ 00023 00024 00025 #ifndef EO_PARSER_H 00026 #define EO_PARSER_H 00027 00028 #include <map> 00029 #include <sstream> 00030 #include <string> 00031 00032 #include "eoParam.h" 00033 #include "eoObject.h" 00034 #include "eoPersistent.h" 00035 #include "eoExceptions.h" 00036 00045 class eoParameterLoader 00046 { 00047 public : 00048 00050 virtual ~eoParameterLoader(); 00051 00057 virtual void processParam(eoParam& param, std::string section = "") = 0; 00058 00062 virtual bool isItThere(eoParam& _param) const = 0; 00063 00074 template <class ValueType> 00075 eoValueParam<ValueType>& createParam(ValueType _defaultValue, 00076 std::string _longName, 00077 std::string _description, 00078 char _shortHand = 0, 00079 std::string _section = "", 00080 bool _required = false) 00081 { 00082 eoValueParam<ValueType>* p = new eoValueParam<ValueType>(_defaultValue, 00083 _longName, 00084 _description, 00085 _shortHand, 00086 _required); 00087 ownedParams.push_back(p); 00088 processParam(*p, _section); 00089 return *p; 00090 } 00091 00092 00093 private : 00094 00095 std::vector<eoParam*> ownedParams; 00096 }; 00097 00110 class eoParser : public eoParameterLoader, public eoObject, public eoPersistent 00111 { 00112 00113 public: 00114 00129 eoParser ( unsigned _argc, char **_argv , std::string _programDescription = "", 00130 std::string _lFileParamName = "param-file", char _shortHand = 'p'); 00131 00135 void processParam(eoParam& param, std::string section = ""); 00136 00140 void readFrom(std::istream& is); 00141 00145 void printOn(std::ostream& os) const; 00146 00148 std::string className(void) const { return "Parser"; } 00149 00151 bool userNeedsHelp(void); 00156 void printHelp(std::ostream& os); 00157 00158 std::string ProgramName() { return programName; } 00159 00164 virtual bool isItThere(eoParam& _param) const 00165 { return getValue(_param).first; } 00166 00167 00168 std::string get( const std::string & name) const; 00169 00170 00179 eoParam * getParamWithLongName(const std::string& _name) const; 00180 00181 00186 eoParam * getParam(const std::string& _name) const; 00187 00188 00198 template<class ValueType> 00199 ValueType valueOf(const std::string& _name) const 00200 { 00201 eoParam* param = getParam(_name); 00202 00203 // Note: eoParam is the polymorphic base class of eoValueParam, thus we can do a dynamix cast 00204 eoValueParam<ValueType>* vparam = dynamic_cast< eoValueParam<ValueType>* >(param); 00205 00206 if( vparam == NULL ) { 00207 // if the dynamic cast has failed, chances are that ValueType 00208 // is not the same than the one used at declaration. 00209 throw eoWrongParamTypeException( _name ); 00210 } else { 00211 return vparam->value(); 00212 } 00213 } 00214 00215 00216 00223 template <class ValueType> 00224 eoValueParam<ValueType>& getORcreateParam(ValueType _defaultValue, 00225 std::string _longName, 00226 std::string _description, 00227 char _shortHand = 0, 00228 std::string _section = "", 00229 bool _required = false) 00230 { 00231 eoParam* ptParam = getParamWithLongName(_longName); 00232 if (ptParam) { 00233 // found 00234 eoValueParam<ValueType>* ptTypedParam( 00235 dynamic_cast<eoValueParam<ValueType>*>(ptParam)); 00236 return *ptTypedParam; 00237 } else { 00238 // not found -> create it 00239 return createParam(_defaultValue, _longName, _description, 00240 _shortHand, _section, _required); 00241 } 00242 } 00243 00244 00245 00262 template <class ValueType> 00263 eoValueParam<ValueType>& setORcreateParam(ValueType _defaultValue, 00264 std::string _longName, 00265 std::string _description, 00266 char _shortHand = 0, 00267 std::string _section = "", 00268 bool _required = false) 00269 { 00270 eoValueParam<ValueType>& param = createParam(_defaultValue, _longName, _description, 00271 _shortHand, _section, _required); 00272 std::ostringstream os; 00273 os << _defaultValue; 00274 if(isItThere(param)) { 00275 param.setValue(os.str()); 00276 } else { 00277 longNameMap[_longName] = os.str(); 00278 shortNameMap[_shortHand] = os.str(); 00279 } 00280 return param; 00281 } 00282 00283 00284 00286 void setStopOnUnknownParam(bool _b) {stopOnUnknownParam.value()=_b;} 00287 bool getStopOnUnknownParam() {return stopOnUnknownParam.value();} 00288 00290 void setPrefix(const std:: string & _prefix) {prefix = _prefix;} 00291 00292 void resetPrefix() {prefix = "";} 00293 00294 std::string getPrefix() {return prefix;} 00295 00296 private: 00297 00298 void doRegisterParam(eoParam& param); 00299 00300 std::pair<bool, std::string> getValue(eoParam& _param) const; 00301 00302 void updateParameters(); 00303 00304 typedef std::multimap<std::string, eoParam*> MultiMapType; 00305 00306 // used to store all parameters that are processed 00307 MultiMapType params; 00308 00309 std::string programName; 00310 std::string programDescription; 00311 00312 typedef std::map<char, std::string> ShortNameMapType; 00313 ShortNameMapType shortNameMap; 00314 00315 typedef std::map<std::string, std::string> LongNameMapType; 00316 LongNameMapType longNameMap; 00317 00318 // flag that marks if the user need to know that there was a problem 00319 // used to display the message about "-h" only once 00320 bool needHelpMessage; 00321 00322 eoValueParam<bool> needHelp; 00323 eoValueParam<bool> stopOnUnknownParam; 00324 00325 mutable std::vector<std::string> messages; 00326 00327 std::string prefix; // used for all created params - in processParam 00328 00329 }; 00330 00331 00332 00333 #endif // EO_PARSER_H 00334 00335 00336 00337 // Local Variables: 00338 // coding: iso-8859-1 00339 // mode:C++ 00340 // c-file-style: "Stroustrup" 00341 // comment-column: 35 00342 // fill-column: 80 00343 // End: