few minor mods
This commit is contained in:
parent
f290f94301
commit
a2457cf126
5 changed files with 86 additions and 11 deletions
|
|
@ -31,6 +31,10 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef __GNUC__
|
||||
typedef ios ios_base; // not currently defined in GCC
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
/*
|
||||
Maarten: added this code here because Mirkosoft has the
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@
|
|||
#include <fstream>
|
||||
#include <iomanip>
|
||||
|
||||
#include "eoParser.h"
|
||||
#include <utils/compatibility.h>
|
||||
|
||||
#include <utils/eoParser.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
|
@ -18,10 +20,22 @@ void eoWarning(std::string str)
|
|||
|
||||
std::ostream& printSectionHeader(std::ostream& os, std::string section)
|
||||
{
|
||||
if (section == "")
|
||||
section = "General";
|
||||
|
||||
os << '\n' << setw(10) << "###### " << setw(20) << section << setw(10) << " ######\n";
|
||||
return os;
|
||||
}
|
||||
|
||||
eoParameterLoader::~eoParameterLoader()
|
||||
{
|
||||
for (int i = 0; i < ownedParams.size(); ++i)
|
||||
{
|
||||
delete ownedParams[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
eoParser::eoParser ( int _argc, char **_argv , string _programDescription, string _lFileParamName, char _shortHand) :
|
||||
programName( _argv[0]),
|
||||
programDescription( _programDescription),
|
||||
|
|
@ -184,9 +198,13 @@ void eoParser::printOn(ostream& os) const
|
|||
|
||||
string str = "--" + param->longName() + "=" + param->getValue();
|
||||
|
||||
//os.setf(ios_base::left, ios_base::adjustfield);
|
||||
os.setf(ios_base::left, ios_base::adjustfield);
|
||||
os << setw(40) << str;
|
||||
os << " # " << '-' << param->shortName() << " : " << param->description();
|
||||
|
||||
os << setw(0) << " # ";
|
||||
if (param->shortName())
|
||||
os << '-' << param->shortName() << " : ";
|
||||
os << param->description();
|
||||
|
||||
if (param->required())
|
||||
{
|
||||
|
|
@ -205,7 +223,7 @@ void eoParser::printHelp(ostream& os)
|
|||
// print the usage when calling the program from the command line
|
||||
os << "Usage: "<< programName<<" [Options]\n";
|
||||
// only short usage!
|
||||
os << "Options of the form \"-ShortName value\" or \"--LongName value\"" << endl;
|
||||
os << "Options of the form \"-f[Value]\" or \"--Name[=value]\"" << endl;
|
||||
|
||||
os << "Where:"<<endl;
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class eoParameterLoader
|
|||
public :
|
||||
|
||||
/** Need a virtual destructor */
|
||||
virtual ~eoParameterLoader() {}
|
||||
virtual ~eoParameterLoader();
|
||||
|
||||
/**
|
||||
* processParam is used to register a parameter and set its value if it is known
|
||||
|
|
@ -54,6 +54,39 @@ public :
|
|||
* @param section the section where this parameter belongs
|
||||
*/
|
||||
virtual void processParam(eoParam& param, std::string section = "") = 0;
|
||||
|
||||
/**
|
||||
* Construct a Param and sets its value. The loader will own the memory thus created
|
||||
*
|
||||
* @param _defaultValue The default value
|
||||
* @param _longName Long name of the argument
|
||||
* @param _description Description of the parameter. What is useful for.
|
||||
* @param _shortName Short name of the argument (Optional)
|
||||
* @param _section Name of the section where the parameter belongs
|
||||
* @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;
|
||||
}
|
||||
|
||||
private :
|
||||
|
||||
std::vector<eoParam*> ownedParams;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
@ -73,7 +106,8 @@ public:
|
|||
*
|
||||
* myEo --param-file=param.rc will then load using the parameter file param.rc
|
||||
*
|
||||
* @param _argc, _ argv command line arguments
|
||||
* @param _argc command line arguments count
|
||||
* @param _argv command line parameters
|
||||
* @param _programDescription Description of the work the program does
|
||||
* @param _lFileParamName Name of the parameter specifying the configuration file (--param-file)
|
||||
* @param _shortHand Single charachter shorthand for specifying the configuration file
|
||||
|
|
@ -128,4 +162,5 @@ private:
|
|||
eoValueParam<bool> needHelp;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -46,7 +46,16 @@ void eoState::registerObject(eoPersistent& registrant)
|
|||
{
|
||||
string name = createObjectName(dynamic_cast<eoObject*>(®istrant));
|
||||
|
||||
objectMap[name] = ®istrant;
|
||||
pair<ObjectMap::iterator,bool> res = objectMap.insert(make_pair(name, ®istrant));
|
||||
|
||||
if (res.second == true)
|
||||
{
|
||||
creationOrder.push_back(res.first);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw logic_error("Interval error: object already present in the state");
|
||||
}
|
||||
}
|
||||
|
||||
void eoState::load(const string& _filename)
|
||||
|
|
@ -107,13 +116,19 @@ void eoState::load(const string& _filename)
|
|||
}
|
||||
|
||||
void eoState::save(const string& filename)
|
||||
{
|
||||
{ // saves in order of insertion
|
||||
ofstream os(filename.c_str());
|
||||
|
||||
for (ObjectMap::iterator it = objectMap.begin(); it != objectMap.end(); ++it)
|
||||
if (os.fail())
|
||||
{
|
||||
os << "\\section{" << it->first << "}\n";
|
||||
it->second->printOn(os);
|
||||
string msg = "Could not open file: " + filename + " for writing!";
|
||||
throw runtime_error(msg);
|
||||
}
|
||||
|
||||
for (vector<ObjectMap::iterator>::iterator it = creationOrder.begin(); it != creationOrder.end(); ++it)
|
||||
{
|
||||
os << "\\section{" << (*it)->first << "}\n";
|
||||
(*it)->second->printOn(os);
|
||||
os << '\n';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@
|
|||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
class eoObject;
|
||||
class eoPersistent;
|
||||
|
|
@ -79,6 +80,8 @@ private :
|
|||
typedef std::map<std::string, eoPersistent*> ObjectMap;
|
||||
|
||||
ObjectMap objectMap;
|
||||
|
||||
std::vector<ObjectMap::iterator> creationOrder;
|
||||
};
|
||||
|
||||
#endif //eoState_h
|
||||
|
|
|
|||
Reference in a new issue