Use getORcreateParam instead of createParam when getting values from
the eoParser. Update setORcreateParam.
This commit is contained in:
parent
f948ccda3a
commit
438e8ef6d6
9 changed files with 290 additions and 194 deletions
|
|
@ -123,11 +123,10 @@ void eoParser::processParam(eoParam& param, std::string section)
|
|||
{
|
||||
// this param enters the parser: add the prefix to the long name
|
||||
if (prefix != "")
|
||||
{
|
||||
{
|
||||
param.setLongName(prefix+param.longName());
|
||||
section = prefix + section; // and to section
|
||||
}
|
||||
|
||||
}
|
||||
doRegisterParam(param); // plainly register it
|
||||
params.insert(make_pair(section, ¶m));
|
||||
}
|
||||
|
|
@ -139,9 +138,7 @@ void eoParser::doRegisterParam(eoParam& param) const
|
|||
string msg = "Required parameter: " + param.longName() + " missing";
|
||||
messages.push_back(msg);
|
||||
}
|
||||
|
||||
pair<bool, string> value = getValue(param);
|
||||
|
||||
if (value.first)
|
||||
{
|
||||
param.setValue(value.second);
|
||||
|
|
@ -162,9 +159,7 @@ pair<bool, string> eoParser::getValue(eoParam& _param) const
|
|||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
map<string, string>::const_iterator it = longNameMap.find(_param.longName());
|
||||
|
||||
if (it != longNameMap.end())
|
||||
{
|
||||
result.second = it->second;
|
||||
|
|
@ -172,7 +167,6 @@ pair<bool, string> eoParser::getValue(eoParam& _param) const
|
|||
return result;
|
||||
}
|
||||
// else (TODO: check environment, just long names)
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -188,72 +182,72 @@ void eoParser::updateParameters() const
|
|||
|
||||
void eoParser::readFrom(istream& is)
|
||||
{
|
||||
string str;
|
||||
// we must avoid processing \section{xxx} if xxx is NOT "Parser"
|
||||
bool processing = true;
|
||||
while (is >> str)
|
||||
string str;
|
||||
// we must avoid processing \section{xxx} if xxx is NOT "Parser"
|
||||
bool processing = true;
|
||||
while (is >> str)
|
||||
{
|
||||
if (str.find(string("\\section{"))==0) // found section begin
|
||||
processing = (str.find(string("Parser"))<str.size());
|
||||
if (str.find(string("\\section{"))==0) // found section begin
|
||||
processing = (str.find(string("Parser"))<str.size());
|
||||
|
||||
if (processing) // right \section (or no \section at all)
|
||||
if (processing) // right \section (or no \section at all)
|
||||
{
|
||||
if (str[0] == '#')
|
||||
if (str[0] == '#')
|
||||
{ // skip the rest of the line
|
||||
string tempStr;
|
||||
getline(is, tempStr);
|
||||
string tempStr;
|
||||
getline(is, tempStr);
|
||||
}
|
||||
if (str[0] == '-')
|
||||
if (str[0] == '-')
|
||||
{
|
||||
if (str.size() < 2)
|
||||
if (str.size() < 2)
|
||||
{
|
||||
eoWarning("Missing parameter");
|
||||
needHelp.value() = true;
|
||||
return;
|
||||
eoWarning("Missing parameter");
|
||||
needHelp.value() = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (str[1] == '-') // two consecutive dashes
|
||||
if (str[1] == '-') // two consecutive dashes
|
||||
{
|
||||
string::iterator equalLocation = find(str.begin() + 2, str.end(), '=');
|
||||
string value;
|
||||
string::iterator equalLocation = find(str.begin() + 2, str.end(), '=');
|
||||
string value;
|
||||
|
||||
if (equalLocation == str.end())
|
||||
if (equalLocation == str.end())
|
||||
{ // TODO: it should be the next string
|
||||
value = "";
|
||||
value = "";
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
value = string(equalLocation + 1, str.end());
|
||||
value = string(equalLocation + 1, str.end());
|
||||
}
|
||||
|
||||
string name(str.begin() + 2, equalLocation);
|
||||
longNameMap[name] = value;
|
||||
string name(str.begin() + 2, equalLocation);
|
||||
longNameMap[name] = value;
|
||||
|
||||
}
|
||||
else // it should be a char
|
||||
else // it should be a char
|
||||
{
|
||||
string value = "1"; // flags do not need a special
|
||||
string value = "1"; // flags do not need a special
|
||||
|
||||
if (str.size() >= 2)
|
||||
if (str.size() >= 2)
|
||||
{
|
||||
if (str[2] == '=')
|
||||
if (str[2] == '=')
|
||||
{
|
||||
if (str.size() >= 3)
|
||||
value = string(str.begin() + 3, str.end());
|
||||
if (str.size() >= 3)
|
||||
value = string(str.begin() + 3, str.end());
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
value = string(str.begin() + 2, str.end());
|
||||
value = string(str.begin() + 2, str.end());
|
||||
}
|
||||
}
|
||||
|
||||
shortNameMap[str[1]] = value;
|
||||
shortNameMap[str[1]] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateParameters();
|
||||
updateParameters();
|
||||
}
|
||||
|
||||
void eoParser::printOn(ostream& os) const
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
/**
|
||||
CVS Info: $Date: 2005-08-29 07:50:50 $ $Version$ $Author: kuepper $
|
||||
CVS Info: $Date: 2005-09-12 17:53:24 $ $Version$ $Author: kuepper $
|
||||
*/
|
||||
#ifndef eoParser_h
|
||||
#define eoParser_h
|
||||
|
|
@ -73,29 +73,27 @@ public :
|
|||
* @param _required If it is a necessary parameter or not
|
||||
*/
|
||||
template <class ValueType>
|
||||
eoValueParam<ValueType>& createParam
|
||||
(ValueType _defaultValue,
|
||||
std::string _longName,
|
||||
std::string _description,
|
||||
char _shortHand = 0,
|
||||
std::string _section = "",
|
||||
bool _required = false)
|
||||
{
|
||||
eoValueParam<ValueType>* p = new eoValueParam<ValueType>(_defaultValue,
|
||||
_longName,
|
||||
_description,
|
||||
_shortHand,
|
||||
_required);
|
||||
ownedParams.push_back(p);
|
||||
processParam(*p, _section);
|
||||
return *p;
|
||||
}
|
||||
eoValueParam<ValueType>& createParam(ValueType _defaultValue,
|
||||
std::string _longName,
|
||||
std::string _description,
|
||||
char _shortHand = 0,
|
||||
std::string _section = "",
|
||||
bool _required = false)
|
||||
{
|
||||
eoValueParam<ValueType>* p = new eoValueParam<ValueType>(_defaultValue,
|
||||
_longName,
|
||||
_description,
|
||||
_shortHand,
|
||||
_required);
|
||||
ownedParams.push_back(p);
|
||||
processParam(*p, _section);
|
||||
return *p;
|
||||
}
|
||||
|
||||
|
||||
private :
|
||||
|
||||
std::vector<eoParam*> ownedParams;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -198,7 +196,10 @@ public:
|
|||
|
||||
|
||||
|
||||
/** Make sure parameter has specific value
|
||||
/** Set parameter value or create parameter
|
||||
|
||||
This makes sure that the specified parameter has the given value.
|
||||
If the parameter does not exist yet, it is created.
|
||||
|
||||
This requires that operator<< is defined for ValueType.
|
||||
*/
|
||||
|
|
@ -207,18 +208,19 @@ public:
|
|||
std::string _description, char _shortHand = 0,
|
||||
std::string _section = "", bool _required = false)
|
||||
{
|
||||
eoParam *param = getParamWithLongName(_longName);
|
||||
if(0 == param) {
|
||||
createParam(_defaultValue, _longName, _description,
|
||||
_shortHand, _section, _required);
|
||||
} else {
|
||||
eoValueParam<ValueType>& param = createParam(_defaultValue, _longName, _description,
|
||||
_shortHand, _section, _required);
|
||||
#ifdef HAVE_SSTREAM
|
||||
std::ostringstream os;
|
||||
std::ostringstream os;
|
||||
#else
|
||||
std::ostrstream os;
|
||||
std::ostrstream os;
|
||||
#endif
|
||||
os << _defaultValue;
|
||||
dynamic_cast<eoValueParam<int> *>(param)->setValue(os.str());
|
||||
os << _defaultValue;
|
||||
if(isItThere(param)) {
|
||||
param.setValue(os.str());
|
||||
} else {
|
||||
longNameMap[_longName] = os.str();
|
||||
shortNameMap[_shortHand] = os.str();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifdef _MSC_VER
|
||||
// to avoid long name warnings
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include <config.h>
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
#include "eoRealVectorBounds.h"
|
||||
|
||||
|
||||
// the global dummy bounds
|
||||
// the global dummy bounds
|
||||
// (used for unbounded variables when bounds are required)
|
||||
eoRealNoBounds eoDummyRealNoBounds;
|
||||
eoRealVectorNoBounds eoDummyVectorNoBounds(0);
|
||||
|
|
@ -74,7 +74,7 @@ eoRealVectorBounds::eoRealVectorBounds(const eoRealVectorBounds & _b):
|
|||
|
||||
// the readFrom method of eoRealVectorNoBounds:
|
||||
// only calls the readFrom(string) - for param reading
|
||||
void eoRealVectorBounds::readFrom(std::istream& _is)
|
||||
void eoRealVectorBounds::readFrom(std::istream& _is)
|
||||
{
|
||||
std::string value;
|
||||
_is >> value;
|
||||
|
|
@ -95,7 +95,7 @@ void eoRealVectorBounds::readFrom(std::string _value)
|
|||
ownedBounds.resize(0);
|
||||
factor.resize(0);
|
||||
resize(0);
|
||||
|
||||
|
||||
// now read
|
||||
std::string delim(",; ");
|
||||
while (_value.size()>0)
|
||||
|
|
@ -129,7 +129,7 @@ void eoRealVectorBounds::readFrom(std::string _value)
|
|||
std::string sBounds = _value.substr(posDeb+1, posFin-posDeb-1);
|
||||
// and remove from original string
|
||||
_value = _value.substr(posFin+1);
|
||||
|
||||
|
||||
remove_leading(sBounds, delim);
|
||||
size_t posDelim = sBounds.find_first_of(delim);
|
||||
if (posDelim >= sBounds.size())
|
||||
|
|
@ -193,10 +193,10 @@ void eoRealVectorBounds::adjust_size(unsigned _dim)
|
|||
}
|
||||
}
|
||||
|
||||
/** the constructor for eoGeneralRealBound - from a string
|
||||
* very similar to the eoRealVectorBounds::readFrom above
|
||||
/** the constructor for eoGeneralRealBound - from a string
|
||||
* very similar to the eoRealVectorBounds::readFrom above
|
||||
* but was written much later so the readFrom does not call this one
|
||||
* as it should do
|
||||
* as it should do
|
||||
*/
|
||||
eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value)
|
||||
{
|
||||
|
|
@ -208,7 +208,7 @@ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value)
|
|||
|
||||
// look for opening char
|
||||
size_t posDeb = _value.find_first_of(beginOrClose); // allow ]a,b]
|
||||
if (posDeb >= _value.size()) // nothing left to read
|
||||
if (posDeb >= _value.size()) // nothing left to read
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
|
||||
// ending char: next {}() after posDeb
|
||||
|
|
@ -220,7 +220,7 @@ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value)
|
|||
std::string sBounds = _value.substr(posDeb+1, posFin-posDeb-1);
|
||||
// and remove from original string
|
||||
_value = _value.substr(posFin+1);
|
||||
|
||||
|
||||
remove_leading(sBounds, delim);
|
||||
size_t posDelim = sBounds.find_first_of(delim);
|
||||
if (posDelim >= sBounds.size())
|
||||
|
|
@ -260,7 +260,7 @@ eoRealBounds* eoGeneralRealBounds::getBoundsFromString(std::string _value)
|
|||
if (maxBound <= minBound)
|
||||
throw std::runtime_error("Syntax error in eoGeneralRealBounds Ctor");
|
||||
locBound = new eoRealInterval(minBound, maxBound);
|
||||
}
|
||||
}
|
||||
else if (!minBounded && !maxBounded) // no bound at all
|
||||
locBound = new eoRealNoBounds;
|
||||
else if (!minBounded && maxBounded)
|
||||
|
|
|
|||
Reference in a new issue