1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #ifndef ANYOPTIONWRAPPER_INL__HPP
- #define ANYOPTIONWRAPPER_INL__HPP
- #include "debug.hpp" // for libcwd
- #include "wrappedoption.hpp"
- #include "stringutils.h"
- /** convert string to vector of numbers (int, float, ...)
- *
- * @param ValString with space separated numbers: like " 0.3 0.5 0.2444"
- * @param Delimiters delimiters
- * @param[out] outVect result vector
- * @return void
- */
- template<class T>
- void convStrToNumVect(const string& ValString,
- const string& Delimiters,
- vector<T>* outVect)
- {
- vector<string> Tokens;
- Tokenize(ValString, Tokens, Delimiters);
- T Number;
- for (vector<string>::iterator it=Tokens.begin();
- it!=Tokens.end();
- ++it)
- {
- Dout(dc::anyopt, "Token=" << (*it));
- convStrToNum((*it).c_str(), Number);
- outVect->push_back(Number);
- }
- }
- template<class T1, class T2>
- class pairOption: public WrappedOption
- {
- public:
- pairOption(const char* OptionName,
- char ShortCut,
- pair<T1,T2> *Value,
- const pair<T1,T2>& DefaultValue,
- bool _CmdLineOnly=false);
- virtual void getOption(AnyOption *opt);
- virtual void Save(fstream &fw);
- private:
- static const int MinNumVals=2;
- pair<T1,T2> *Value;
-
- };
- ////////////////////////////
- template<class T1,class T2>
- pairOption<T1,T2>::pairOption(const char* _OptionName,
- char _ShortCut,
- pair<T1,T2> *_Value,
- const pair<T1,T2>& _Default,
- bool _CmdLineOnly)
- : WrappedOption(_OptionName, _ShortCut, _CmdLineOnly), Value(_Value)
- {
- *Value = _Default;
- }
- template<class T1, class T2>
- void pairOption<T1,T2>::getOption(AnyOption *opt)
- {
- char* ValStr = opt->getValue(OptionName.c_str());
- if (ValStr != 0) {
- Dout(dc::anyopt, "ValStr=" << ValStr);
- string ValString = string(ValStr);
- vector<string> Tokens;
- Tokenize(ValString, Tokens, " ,");
- if (Tokens.size()<2) {
- Dout(dc::anyopt,
- "Number of values too low. Value not changed. Pair needs 2 values");
- } else {
- Dout(dc::anyopt, "Tokens[0]=" << Tokens[0].c_str());
- convStrToNum(Tokens[0].c_str(),Value->first);
- convStrToNum(Tokens[1].c_str(),Value->second);
- }
-
- Dout(dc::anyopt, "Set " << OptionName << " ="
- << Value->first << " " << Value->second);
- }
- }
- template<class T1, class T2>
- void pairOption<T1,T2>::Save(fstream &fw)
- {
- fw << OptionName << " : "
- << Value->first << " "
- << Value->second << "\n";
- }
- #endif //ANYOPTIONWRAPPER_INL__HPP
|