C++ mpi module for stochmagnet_main Package
CORE_OptionsList.h
1 #ifndef CORE_OptionsList_H
2 #define CORE_OptionsList_H
3 
4 //base class header
5 #include "CORE_Object.h"
6 
7 
8 //map header
9 #include <map>
10 #include <vector>
11 #include <valarray>
12 #include <array>
13 
14 
15 //shared pointer header
16 #include "shared_pointer.h"
17 
18 //string conversion header
19 #include<charconv>
20 #include <string>
21 
22 
23 //utility headers
24 #include "functions_string.h"
25 #include "functions_numeric.h"
26 
27 class CORE_Out;
28 
36 class CORE_OptionsList : public virtual CORE_Object {
37 
38  //attributes
39 private :
40 
41  //class type
43  typedef CORE_Object SuperClass;
44 
45  std::map<tString,tString> mOptions;
46  std::map<tString,std::map<tString,tString> >mManOptions;
47  tBoolean mIsOptionsNameCaseSensitive;
48 
49 public:
50  // CONSTRUCTORS
53  CORE_OptionsList(void);
54 
55  // DESTRUCTORS
58  virtual ~CORE_OptionsList(void);
59 
60 
61 public :
62  // CREATE class
63 
67  static inline CORE_UniquePointer<SelfClass> New() {
68  CORE_UniquePointer<SelfClass> p(new SelfClass(),SelfClass::Delete());
69  return p;
70  }
71 
72  //options reader
73  //==============
81  virtual tMemSize getMemorySize() const override {
82  return sizeof(*this)+getContentsMemorySize();
83  }
92  virtual tMemSize getContentsMemorySize() const override {
93  tMemSize mem=SuperClass::getContentsMemorySize();
94  for(const auto& opt:mOptions) mem+=(opt.first.length()+opt.second.length())*sizeof(tChar);
95 
96  for(const auto& manOpt:mManOptions) {
97  for(const auto& opt:manOpt.second) {
98  mem+=(opt.first.length()+opt.second.length())*sizeof(tChar);
99  }
100  mem+=manOpt.first.length()*sizeof(tChar);
101  }
102 
103  return mem;
104  }
105 
106  //iterator manager on options
107 
110  std::map<tString,tString>::iterator begin() {
111  return mOptions.begin();
112  }
113 
116  std::map<tString,tString>::iterator end() {
117  return mOptions.end();
118  }
121  std::map<tString,tString>::const_iterator cbegin() const {
122  return mOptions.cbegin();
123  }
124 
127  std::map<tString,tString>::const_iterator cend() const {
128  return mOptions.cend();
129  }
130 
131 
132  //type of options
133  //===============
134 
138  inline void setIsOptionsNameCaseSensitive(const tBoolean& isOptNameCS) {
139  mIsOptionsNameCaseSensitive=isOptNameCS;
140 
141  }
145  inline const tBoolean& isOptionsNameCaseSensitive() const {
146  return mIsOptionsNameCaseSensitive;
147 
148  }
149 
150 
151  //clear the options
152  //=================
155  inline void clear() {
156  mOptions.clear();
157  mManOptions.clear();
158 
159  }
160 
161 
162  //option setting
163  //==============
164 
165 
169  inline CORE_OptionsList& operator=(const CORE_OptionsList& options) {
170 
171  auto iter=options.cbegin();
172  auto endIter=options.cend();
173  while (iter!=endIter) {
174  mOptions[iter->first]=iter->second;
175  iter++;
176  }
177 
178  mIsOptionsNameCaseSensitive=options.isOptionsNameCaseSensitive();
179  return *this;
180  }
181 
187  inline void setManOption(const tString& category,const tString& optName,const tString& man) {
188  mManOptions[category][optName]=man;
189  }
194  inline void setOptionValue(const tString& optName,const tBoolean& value) {
195  tString key=optName;
196  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
197 
198  if (value) mOptions[key]="true";
199  else mOptions[key]="false";
200  }
201 
202  //accessor operator
203  //==================
204 
208  inline tString& operator[](const tString& optName) {
209  //register the default value
210  auto iOption=mOptions.find(optName);
211  if (iOption==mOptions.end()) {
212  mOptions[optName]="";
213  iOption=mOptions.find(optName);
214  }
215  //return the value
216  return iOption->second;
217  }
218 
219 
220 
225  template<typename T>
226  inline void setOptionValue(const tString& optName,const T& value) {
227  tString key=optName;
228  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
229 
230  std::stringstream sstr;
231  sstr<<value;
232 
233  mOptions[key]=sstr.str();
234  }
239  template<typename T>
240  inline void setOptionValue(const tString& optName,const std::initializer_list<T>& values) {
241  tString key=optName;
242  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
243 
244  std::stringstream sstr;
245  sstr<<"[";
246  for(const auto& v:values) sstr<<v<<",";
247  if (values.size()>0) sstr.seekp(-1,sstr.cur);
248  sstr<<"]";
249  mOptions[key]=sstr.str();
250  }
255  template<typename T>
256  inline void setOptionValue(const tString& optName,const std::valarray<T>& values) {
257  tString key=optName;
258  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
259 
260  std::stringstream sstr;
261  sstr<<"[";
262  for(const auto& v:values) sstr<<v<<",";
263  if (values.size()>0) sstr.seekp(-1,sstr.cur);
264  sstr<<"]";
265  mOptions[key]=sstr.str();
266  }
271  template<typename T>
272  inline void setOptionValue(const tString& optName,const std::vector<T>& values) {
273  tString key=optName;
274  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
275 
276  std::stringstream sstr;
277  sstr<<"[";
278  for(const auto& v:values) sstr<<v<<",";
279  if (values.size()>0) sstr.seekp(-1,sstr.cur);
280  sstr<<"]";
281  mOptions[key]=sstr.str();
282  }
287  template<typename T,tIndex N>
288  inline void setOptionValue(const tString& optName,const std::array<T,N>& values) {
289  tString key=optName;
290  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
291 
292  std::stringstream sstr;
293  sstr<<"[";
294  for(const auto& v:values) sstr<<v<<",";
295  if (values.size()>0) sstr.seekp(-1,sstr.cur);
296  sstr<<"]";
297  mOptions[key]=sstr.str();
298  }
299 
300 
301 
302  //option getting
303  //===============
304 
305  //accessor operator
306  //==================
307 
311  inline tString operator[](const tString& optName) const {
312  tString optValue="";
313  getOptionValue(optName,optValue);
314  return optValue;
315  }
316 
321  inline tBoolean removeOption(const tString& optName) {
322  std::map<tString,tString>::const_iterator iOptions=mOptions.find(optName);
323  if (iOptions!=mOptions.end()) {
324  mOptions.erase(iOptions);
325  return true;
326  }
327  return false;
328  }
334  inline tBoolean getOptionValue(const tString& optName,tString& optValue) const {
335 
336  tString key=optName;
337  if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
338 
339  std::map<tString,tString>::const_iterator iOptions=mOptions.find(key);
340  if (iOptions==mOptions.end()) return false;
341 
342  optValue=iOptions->second;
343 
344  // deal with any error bits that may have been set on the stream
345  return true;
346  }
347 
353  template<typename T> requires (functions_type::isRealType<T>)
354  inline tBoolean getOptionValue(const tString& optName,T& optValue) const {
355 
356  tString strValue;
357  tBoolean ok=getOptionValue(optName,strValue);
358  if (ok) {
359  std::istringstream iss;
360  iss.str(strValue);
361  iss >> optValue;
362  }
363  return ok;
364  }
365 
366 
372  template<typename T> requires (functions_type::isIntegerType<T>)
373  inline tBoolean getOptionValue(const tString& optName,T& optValue) const {
374  tString strValue;
375  tBoolean ok=getOptionValue(optName,strValue);
376  if (ok) {
377  std::from_chars(strValue.data(), strValue.data() + strValue.size(), optValue);
378  }
379  return ok;
380 
381  }
382 
383 
389  inline tBoolean getOptionValue(const tString& optName,tBoolean& optValue) const {
390  tString strValue;
391  tBoolean ok=getOptionValue(optName,strValue);
392  if (ok) {
393  functions_string::trim(strValue);
394  if (strValue.length()==0) {
395  optValue=true;
396  } else {
397 
398  switch(strValue[0]) {
399  case 't':
400  case 'T':
401  case '1':
402  optValue=true;
403  break;
404  default:
405  optValue=false;
406  break;
407  }
408  }
409  }
410  return ok;
411  }
412 
413 
420  template<class T>
421  inline tBoolean getOptionValue(const tString& optName,std::vector<T>& optValues) const {
422 
423  tString strValue;
424  tBoolean ok=getOptionValue(optName,strValue);
425 
426  if (!ok) return false;
427 
428  // length of the string
429  tIndex lmax=strValue.length();
430 
431 
432  //clear optValues
433  optValues.clear();
434 
435  // find first open brace ('[' or '(') , strValue is set to the chars after open brace
436  tIndex indexOB=strValue.find("[");
437  if (indexOB!=tString::npos) {
438  strValue=strValue.substr(indexOB+1);
439  } else {
440  indexOB=strValue.find("(");
441  if (indexOB!=tString::npos) {
442  strValue=strValue.substr(indexOB+1);
443  }
444  }
445 
446 
447 
448  //find first close brace (']' or ')') max string to analyse is inside [0,indexMax]
449  tIndex indexMax=strValue.find("]");
450  if (indexMax==tString::npos) {
451  indexMax=strValue.find(")");
452  if (indexMax==tString::npos) {
453  indexMax=lmax;
454  }
455  }
456 
457  // find a coma
458  tIndex indexC=strValue.find(",");
459  tIndex i=0;
460  T value;
461  while (indexC<indexMax) {//the first exists
462  // read the value at index i
463  functions_numeric::parse<T>(strValue.substr(0,indexC),value);
464  // add it
465  optValues.push_back(value);
466 
467  // get the next value
468  strValue=strValue.substr(indexC+1,lmax-indexC-1);
469 
470  //max leng to analyse
471  lmax=strValue.length();
472 
473  //get the next value
474  indexC=strValue.find(",");
475 
476  //get the close brace index
477  indexMax=strValue.find("]");
478  if (indexMax==tString::npos) {
479  indexMax=strValue.find(")");
480  if (indexMax==tString::npos) {
481  indexMax=lmax;
482  }
483  }
484  //next value
485  i++;
486  }
487 
488 
489  //set the strValue to chars after ] it it exists
490 
491  tString v=strValue;
492  if (indexMax<lmax) {
493  // take the string v before ]
494  v=strValue.substr(0,indexMax);
495  // erase from strValue all the string before ]
496  strValue=strValue.substr(indexMax+1,lmax-indexMax-1);
497  // erase the ,
498  if (strValue[0]==',') strValue=strValue.substr(1);
499  }
500 
501 
502 
503  // parse the last value
504  functions_numeric::parse(v,value);
505  optValues.push_back(value);
506 
507 
508  return true;
509  }
510 
518  template<class T,tIndex N>
519  inline tBoolean getOptionValue(const tString& optName,std::array<T,N>& optValues) const {
520 
521  tString strValue;
522  tBoolean ok=getOptionValue(optName,strValue);
523  if (!ok) return false;
524  //initialize
525 
526  for(auto& v:optValues) v=0;
527  T* iValue=optValues.data();
528  const T* eValue=optValues.data()+N;
529 
530  // length of the string
531  tIndex lmax=strValue.length();
532 
533 
534  // find first open brace ('[' or '(') , strValue is set to the chars after open brace
535  tIndex indexOB=strValue.find("[");
536  if (indexOB!=tString::npos) {
537  strValue=strValue.substr(indexOB+1);
538  } else {
539  indexOB=strValue.find("(");
540  if (indexOB!=tString::npos) {
541  strValue=strValue.substr(indexOB+1);
542  }
543  }
544 
545 
546  //find first close brace (']' or ')') max string to analyse is inside [0,indexMax]
547  tIndex indexMax=strValue.find("]");
548  if (indexMax==tString::npos) {
549  indexMax=strValue.find(")");
550  if (indexMax==tString::npos) {
551  indexMax=lmax;
552  }
553  }
554 
555 
556  // find a coma
557  tIndex indexC=strValue.find(",");
558  while ((indexC<indexMax)&&(iValue!=eValue)) {//the first exists
559 
560  // read the value at index i
561  functions_numeric::parse<T>(strValue.substr(0,indexC),(*iValue));
562  iValue++;
563 
564  // get the next value
565  strValue=strValue.substr(indexC+1,lmax-indexC-1);
566  lmax=strValue.length();
567  indexC=strValue.find(",");
568 
569  //find first close brace (']' or ')') max string to analyse is inside [0,indexMax]
570  indexMax=strValue.find("]");
571  if (indexMax==tString::npos) {
572  indexMax=strValue.find(")");
573  if (indexMax==tString::npos) {
574  indexMax=lmax;
575  }
576  }
577 
578  }
579 
580 
581  //set the strValue to chars after ] it it exists
582  tString v=strValue;
583  if (indexMax<lmax) {
584  // take the string v before ]
585  v=strValue.substr(0,indexMax);
586  // erase from strValue all the string before ]
587  strValue=strValue.substr(indexMax+1,lmax-indexMax-1);
588  // erase the ,
589  if (strValue[0]==',') strValue=strValue.substr(1);
590  }
591 
592 
593 
594  // parse the last values
595  while (iValue!=eValue) {
596  functions_numeric::parse(v,(*iValue));
597  iValue++;
598  }
599 
600 
601 
602  return true;
603  }
604 
613  static tBoolean ReadOption(int nArgs,char *argv[],
614  const tBoolean& isCaseSensitive,const tString& optionName,
615  tString& optionValue);
624  template<typename T> requires ( (functions_type::isIntegerType<T>) && !(functions_type::isBooleanType<T>) )
625  inline static tBoolean ReadOption(int nArgs,char *argv[],
626  const tBoolean& isCaseSensitive,const tString& optionName,
627  T& optionValue) {
628  tString strValue="";
629  if (ReadOption(nArgs,argv,isCaseSensitive,optionName,strValue)) {
630  std::from_chars(strValue.data(), strValue.data() + strValue.size(), optionValue);
631  return true;
632  }
633  return false;
634  }
643  template<typename T> requires (functions_type::isRealType<T>)
644  inline static tBoolean ReadOption(int nArgs,char *argv[],
645  const tBoolean& isCaseSensitive,const tString& optionName,
646  T& optionValue) {
647  tString strValue="";
648  if (ReadOption(nArgs,argv,isCaseSensitive,optionName,strValue)) {
649  std::istringstream iss;
650  iss.str(strValue);
651  iss >> optionValue;
652  return true;
653  }
654  return false;
655  }
664  template<typename T> requires (functions_type::isBooleanType<T>)
665  inline static tBoolean ReadOption(int nArgs,char *argv[],
666  const tBoolean& isCaseSensitive,const tString& optionName,
667  T& optionValue) {
668  tString strValue="";
669  if (ReadOption(nArgs,argv,isCaseSensitive,optionName,strValue)) {
670  functions_string::trim(strValue);
671  if (strValue.length()==0) {
672  optionValue=true;
673  } else {
674  switch(strValue[0]) {
675  case 't':
676  case 'T':
677  case '1':
678  optionValue=true;
679  break;
680  default:
681  optionValue=false;
682  break;
683  }
684  }
685  return true;
686  }
687  return false;
688  }
689 
690 
691  //options loader && saver
692  //=======================
693 
698  tBoolean loadFromFile(const tString& fileName);
699 
705  void loadFromCommandLine(int argc,char *argv[]);
706 
707 
708 
712  tBoolean saveToFile(const tString& fileName) const;
713 
716  tBoolean saveToFile() const;
717 
718 
719 
720  // string representation
721  //======================
722 
726  virtual tString toString() const override;
727 
728 
729 private:
736  void setOption(tString& arg);
737 
738 
746  static void ReadOptionValue(tString& optionValue);
747 
748 
749 
750 
751 };
752 
753 
754 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
abstract base class for most classes.
Definition: CORE_Object.h:65
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
This class is an list of options.
Definition: CORE_OptionsList.h:36
void setOptionValue(const tString &optName, const std::array< T, N > &values)
set option
Definition: CORE_OptionsList.h:288
void loadFromCommandLine(int argc, char *argv[])
read all the options from the command line . The options are of the form '–option_name=option_value" ...
Definition: CORE_OptionsList.cpp:94
void setOptionValue(const tString &optName, const T &value)
set option
Definition: CORE_OptionsList.h:226
const tBoolean & isOptionsNameCaseSensitive() const
set if the option name is in lower case
Definition: CORE_OptionsList.h:145
void setOptionValue(const tString &optName, const std::initializer_list< T > &values)
set option
Definition: CORE_OptionsList.h:240
void setOptionValue(const tString &optName, const std::vector< T > &values)
set option
Definition: CORE_OptionsList.h:272
virtual tString toString() const override
turn the class to string
Definition: CORE_Run.cpp:394
virtual ~CORE_OptionsList(void)
destroy
Definition: CORE_OptionsList.cpp:20
requires(functions_type::isIntegerType< T >) inline tBoolean getOptionValue(const tString &optName
get the option value
tString operator[](const tString &optName) const
return the value of the option with name
Definition: CORE_OptionsList.h:311
requires(functions_type::isRealType< T >) inline static tBoolean ReadOption(int nArgs
read the only the option with optionName in command line
void setOptionValue(const tString &optName, const tBoolean &value)
set option
Definition: CORE_OptionsList.h:194
tBoolean saveToFile() const
save the options to the file by theme in outputPath/outputPrefix.opt
Definition: CORE_OptionsList.cpp:322
void setIsOptionsNameCaseSensitive(const tBoolean &isOptNameCS)
set if the option name is in lower case
Definition: CORE_OptionsList.h:138
requires(functions_type::isBooleanType< T >) inline static tBoolean ReadOption(int nArgs
read the only the option with optionName in command line
tBoolean getOptionValue(const tString &optName, std::vector< T > &optValues) const
get the option value
Definition: CORE_OptionsList.h:421
std::map< tString, tString >::const_iterator cbegin() const
begin iterator
Definition: CORE_OptionsList.h:121
std::map< tString, tString >::const_iterator cend() const
end iterator
Definition: CORE_OptionsList.h:127
void clear()
clear the options list
Definition: CORE_OptionsList.h:155
void setOptionValue(const tString &optName, const std::valarray< T > &values)
set option
Definition: CORE_OptionsList.h:256
tBoolean loadFromFile(const tString &fileName)
read the options from a file
Definition: CORE_OptionsList.cpp:24
CORE_OptionsList & operator=(const CORE_OptionsList &options)
copy the options value
Definition: CORE_OptionsList.h:169
tBoolean getOptionValue(const tString &optName, std::array< T, N > &optValues) const
get the option value
Definition: CORE_OptionsList.h:519
requires(functions_type::isRealType< T >) inline tBoolean getOptionValue(const tString &optName
get the option value
std::map< tString, tString >::iterator begin()
begin iterator
Definition: CORE_OptionsList.h:110
CORE_OptionsList(void)
create
Definition: CORE_OptionsList.cpp:16
void setManOption(const tString &category, const tString &optName, const tString &man)
set man option
Definition: CORE_OptionsList.h:187
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_OptionsList.h:92
tBoolean getOptionValue(const tString &optName, tString &optValue) const
get the option value
Definition: CORE_OptionsList.h:334
requires((functions_type::isIntegerType< T >) &&!(functions_type::isBooleanType< T >)) inline static tBoolean ReadOption(int nArgs
read the only the option with optionName in command line
tString & operator[](const tString &optName)
string accessor for options list
Definition: CORE_OptionsList.h:208
static CORE_UniquePointer< SelfClass > New()
create a new unique instance of the class this
Definition: CORE_OptionsList.h:67
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_OptionsList.h:81
static tBoolean ReadOption(int nArgs, char *argv[], const tBoolean &isCaseSensitive, const tString &optionName, tString &optionValue)
read the only the option with optionName in command line
Definition: CORE_OptionsList.cpp:260
tBoolean getOptionValue(const tString &optName, tBoolean &optValue) const
get the option value
Definition: CORE_OptionsList.h:389
tBoolean removeOption(const tString &optName)
remove option
Definition: CORE_OptionsList.h:321
std::map< tString, tString >::iterator end()
end iterator
Definition: CORE_OptionsList.h:116
this class describes the output stream by default write on standart output
Definition: CORE_Out.h:28