From 53eac8838bdd4d82bb2be5ad3133347c63acffa8 Mon Sep 17 00:00:00 2001 From: evomarc Date: Fri, 8 Feb 2002 07:49:32 +0000 Subject: [PATCH] Added the method getORcreateParam. It has exactly the same syntax than the createParam method but it first checks whether the param has been laready entered before creating it --- eo/src/utils/eoParser.cpp | 148 ++++++++++++++++++++++++++------------ eo/src/utils/eoParser.h | 29 +++++++- 2 files changed, 131 insertions(+), 46 deletions(-) diff --git a/eo/src/utils/eoParser.cpp b/eo/src/utils/eoParser.cpp index d7a138d3..d0360859 100644 --- a/eo/src/utils/eoParser.cpp +++ b/eo/src/utils/eoParser.cpp @@ -1,3 +1,29 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoParser.cpp +// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000 +/* + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact: todos@geneura.ugr.es, http://geneura.ugr.es + Marc.Schoenauer@inria.fr + mkeijzer@dhi.dk + */ +//----------------------------------------------------------------------------- + #ifdef _MSC_VER #pragma warning(disable:4786) #endif @@ -39,7 +65,8 @@ eoParameterLoader::~eoParameterLoader() eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription, string _lFileParamName, char _shortHand) : programName( _argv[0]), programDescription( _programDescription), - needHelp(false, "help", "Prints this message", 'h') + needHelp(false, "help", "Prints this message", 'h'), + stopOnUnknownParam(true, "stopOnUnknownParam", "Stop if unkonwn param entered", '\0') { // need to process the param file first // if we want command-line to have highest priority @@ -77,6 +104,7 @@ eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription, readFrom(stream); processParam(needHelp); + processParam(stopOnUnknownParam); } /** @@ -99,6 +127,34 @@ eoParam* eoParser::getParamWithLongName(std::string _name) return 0; } +/** it seems finally that the easiest use of the above method is + through the following, whose interface is similar to that of the + widely-used createParam + For some (probably very stupid) reason, I failed to put it in + the .cpp. Any hint??? +*/ +/* +template +eoValueParam& eoParser::getORcreateParam ( + ValueType _defaultValue, + std::string _longName, + std::string _description, + char _shortHand, + std::string _section, + bool _required) +{ + eoParam* ptParam = getParamWithLongName(_longName); + if (ptParam) { // found + eoValueParam* ptTypedParam = + dynamic_cast*>(ptParam); + return *ptTypedParam; + } + // not found -> create it + return createParam (_defaultValue, _longName, _description, + _shortHand, _section, _required); +} +*/ + void eoParser::processParam(eoParam& param, std::string section) { doRegisterParam(param); // plainly register it @@ -328,55 +384,57 @@ void eoParser::printHelp(ostream& os) bool eoParser::userNeedsHelp(void) { - /* - check whether there are long or short names entered - without a corresponding parameter - */ - - for (LongNameMapType::const_iterator lIt = longNameMap.begin(); lIt != longNameMap.end(); ++lIt) + /* + check whether there are long or short names entered + without a corresponding parameter + */ + // first, check if we want to check that ! + if (stopOnUnknownParam.value()) { - string entry = lIt->first; + for (LongNameMapType::const_iterator lIt = longNameMap.begin(); lIt != longNameMap.end(); ++lIt) + { + string entry = lIt->first; - MultiMapType::const_iterator it; + MultiMapType::const_iterator it; - for (it = params.begin(); it != params.end(); ++it) - { - if (entry == it->second->longName()) - { - break; - } - } + for (it = params.begin(); it != params.end(); ++it) + { + if (entry == it->second->longName()) + { + break; + } + } - if (it == params.end()) - { - string msg = "Unknown parameter: --" + entry + " entered, type -h or --help to see available parameters"; - messages.push_back(msg); - } + if (it == params.end()) + { + string msg = "Unknown parameter: --" + entry + " entered, type -h or --help to see available parameters"; + messages.push_back(msg); + } + } + + for (ShortNameMapType::const_iterator sIt = shortNameMap.begin(); sIt != shortNameMap.end(); ++sIt) + { + char entry = sIt->first; + + MultiMapType::const_iterator it; + + for (it = params.begin(); it != params.end(); ++it) + { + if (entry == it->second->shortName()) + { + break; + } + } + + if (it == params.end()) + { + string entryString(1, entry); + string msg = "Unknown parameter: -" + entryString + " entered, type -h or --help to see available parameters"; + messages.push_back(msg); + } + } } - - for (ShortNameMapType::const_iterator sIt = shortNameMap.begin(); sIt != shortNameMap.end(); ++sIt) - { - char entry = sIt->first; - - MultiMapType::const_iterator it; - - for (it = params.begin(); it != params.end(); ++it) - { - if (entry == it->second->shortName()) - { - break; - } - } - - if (it == params.end()) - { - string entryString(1, entry); - string msg = "Unknown parameter: -" + entryString + " entered, type -h or --help to see available parameters"; - messages.push_back(msg); - } - } - - return needHelp.value() || !messages.empty(); + return needHelp.value() || !messages.empty(); } ///////////////// I put these here at the moment diff --git a/eo/src/utils/eoParser.h b/eo/src/utils/eoParser.h index 52ef152b..a676259d 100644 --- a/eo/src/utils/eoParser.h +++ b/eo/src/utils/eoParser.h @@ -24,7 +24,7 @@ */ //----------------------------------------------------------------------------- /** -CVS Info: $Date: 2001-11-05 16:38:51 $ $Version$ $Author: evomarc $ +CVS Info: $Date: 2002-02-08 07:49:32 $ $Version$ $Author: evomarc $ */ #ifndef eoParser_h #define eoParser_h @@ -159,6 +159,32 @@ public: */ eoParam* getParamWithLongName(std::string _name); + /** it seems finally that the easiest use of the above method is + through the following, whose interface is similar to that of the + widely-used createParam + For some (probably very stupid) reason, I failed to put it in + the .cpp. Any hint??? + */ + template + eoValueParam& getORcreateParam + (ValueType _defaultValue, + std::string _longName, + std::string _description, + char _shortHand = 0, + std::string _section = "", + bool _required = false) +{ + eoParam* ptParam = getParamWithLongName(_longName); + if (ptParam) { // found + eoValueParam* ptTypedParam = + dynamic_cast*>(ptParam); + return *ptTypedParam; + } + // not found -> create it + return createParam (_defaultValue, _longName, _description, + _shortHand, _section, _required); +} + private: void doRegisterParam(eoParam& param) const; @@ -182,6 +208,7 @@ private: LongNameMapType longNameMap; eoValueParam needHelp; + eoValueParam stopOnUnknownParam; mutable std::vector messages; };