anyoptionwrapper-inl.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef ANYOPTIONWRAPPER_INL__HPP
  2. #define ANYOPTIONWRAPPER_INL__HPP
  3. #include "debug.hpp" // for libcwd
  4. #include "wrappedoption.hpp"
  5. #include "stringutils.h"
  6. /** convert string to vector of numbers (int, float, ...)
  7. *
  8. * @param ValString with space separated numbers: like " 0.3 0.5 0.2444"
  9. * @param Delimiters delimiters
  10. * @param[out] outVect result vector
  11. * @return void
  12. */
  13. template<class T>
  14. void convStrToNumVect(const string& ValString,
  15. const string& Delimiters,
  16. vector<T>* outVect)
  17. {
  18. vector<string> Tokens;
  19. Tokenize(ValString, Tokens, Delimiters);
  20. T Number;
  21. for (vector<string>::iterator it=Tokens.begin();
  22. it!=Tokens.end();
  23. ++it)
  24. {
  25. Dout(dc::anyopt, "Token=" << (*it));
  26. convStrToNum((*it).c_str(), Number);
  27. outVect->push_back(Number);
  28. }
  29. }
  30. template<class T1, class T2>
  31. class pairOption: public WrappedOption
  32. {
  33. public:
  34. pairOption(const char* OptionName,
  35. char ShortCut,
  36. pair<T1,T2> *Value,
  37. const pair<T1,T2>& DefaultValue,
  38. bool _CmdLineOnly=false);
  39. virtual void getOption(AnyOption *opt);
  40. virtual void Save(fstream &fw);
  41. private:
  42. static const int MinNumVals=2;
  43. pair<T1,T2> *Value;
  44. };
  45. ////////////////////////////
  46. template<class T1,class T2>
  47. pairOption<T1,T2>::pairOption(const char* _OptionName,
  48. char _ShortCut,
  49. pair<T1,T2> *_Value,
  50. const pair<T1,T2>& _Default,
  51. bool _CmdLineOnly)
  52. : WrappedOption(_OptionName, _ShortCut, _CmdLineOnly), Value(_Value)
  53. {
  54. *Value = _Default;
  55. }
  56. template<class T1, class T2>
  57. void pairOption<T1,T2>::getOption(AnyOption *opt)
  58. {
  59. char* ValStr = opt->getValue(OptionName.c_str());
  60. if (ValStr != 0) {
  61. Dout(dc::anyopt, "ValStr=" << ValStr);
  62. string ValString = string(ValStr);
  63. vector<string> Tokens;
  64. Tokenize(ValString, Tokens, " ,");
  65. if (Tokens.size()<2) {
  66. Dout(dc::anyopt,
  67. "Number of values too low. Value not changed. Pair needs 2 values");
  68. } else {
  69. Dout(dc::anyopt, "Tokens[0]=" << Tokens[0].c_str());
  70. convStrToNum(Tokens[0].c_str(),Value->first);
  71. convStrToNum(Tokens[1].c_str(),Value->second);
  72. }
  73. Dout(dc::anyopt, "Set " << OptionName << " ="
  74. << Value->first << " " << Value->second);
  75. }
  76. }
  77. template<class T1, class T2>
  78. void pairOption<T1,T2>::Save(fstream &fw)
  79. {
  80. fw << OptionName << " : "
  81. << Value->first << " "
  82. << Value->second << "\n";
  83. }
  84. #endif //ANYOPTIONWRAPPER_INL__HPP