Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# eoParser.h

    00001 /* (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
    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          todos@geneura.ugr.es, http://geneura.ugr.es
    00018          Marc.Schoenauer@polytechnique.fr
    00019          mkeijzer@dhi.dk
    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                 // found
    00183                 eoValueParam<ValueType>* ptTypedParam =
    00184                     dynamic_cast<eoValueParam<ValueType>*>(ptParam);
    00185                 return *ptTypedParam;
    00186             }
    00187             // not found -> create it
    00188             return createParam (_defaultValue, _longName, _description,
    00189                                 _shortHand, _section, _required);
    00190         }
    00191
    00192
    00193
    00210     template <class ValueType>
    00211     eoValueParam<ValueType>& setORcreateParam(ValueType _defaultValue,
    00212                                               std::string _longName,
    00213                                               std::string _description,
    00214                                               char _shortHand = 0,
    00215                                               std::string _section = "",
    00216                                               bool _required = false)
    00217         {
    00218             eoValueParam<ValueType>& param = createParam(_defaultValue, _longName, _description,
    00219                                                          _shortHand, _section, _required);
    00220             std::ostringstream os;
    00221             os << _defaultValue;
    00222             if(isItThere(param)) {
    00223                 param.setValue(os.str());
    00224             } else {
    00225                 longNameMap[_longName] = os.str();
    00226                 shortNameMap[_shortHand] = os.str();
    00227             }
    00228             return param;
    00229         }
    00230
    00231
    00232
    00234     void setStopOnUnknownParam(bool _b) {stopOnUnknownParam.value()=_b;}
    00235     bool getStopOnUnknownParam() {return stopOnUnknownParam.value();}
    00236
    00238     void setPrefix(const std:: string & _prefix) {prefix = _prefix;}
    00239
    00240   void resetPrefix() {prefix = "";}
    00241
    00242   std::string getPrefix() {return prefix;}
    00243
    00244 private:
    00245
    00246   void doRegisterParam(eoParam& param) const;
    00247
    00248   std::pair<bool, std::string> getValue(eoParam& _param) const;
    00249
    00250   void updateParameters() const;
    00251
    00252   typedef std::multimap<std::string, eoParam*> MultiMapType;
    00253
    00254   // used to store all parameters that are processed
    00255   MultiMapType params;
    00256
    00257   std::string programName;
    00258   std::string programDescription;
    00259
    00260   typedef std::map<char, std::string> ShortNameMapType;
    00261   ShortNameMapType shortNameMap;
    00262
    00263   typedef std::map<std::string, std::string> LongNameMapType;
    00264   LongNameMapType longNameMap;
    00265
    00266   eoValueParam<bool>   needHelp;
    00267   eoValueParam<bool>   stopOnUnknownParam;
    00268
    00269   mutable std::vector<std::string> messages;
    00270
    00271   std::string prefix;   // used for all created params - in processParam
    00272
    00273 };
    00274
    00275
    00276
    00277 #endif //  EO_PARSER_H
    00278
    00279
    00280
    00281 // Local Variables:
    00282 // coding: iso-8859-1
    00283 // mode:C++
    00284 // c-file-style: "Stroustrup"
    00285 // comment-column: 35
    00286 // fill-column: 80
    00287 // End: