1 #ifndef CORE_OptionsList_H
2 #define CORE_OptionsList_H
5 #include "CORE_Object.h"
16 #include "shared_pointer.h"
24 #include "functions_string.h"
25 #include "functions_numeric.h"
45 std::map<tString,tString> mOptions;
46 std::map<tString,std::map<tString,tString> >mManOptions;
47 tBoolean mIsOptionsNameCaseSensitive;
67 static inline CORE_UniquePointer<SelfClass>
New() {
94 for(
const auto& opt:mOptions) mem+=(opt.first.length()+opt.second.length())*
sizeof(tChar);
96 for(
const auto& manOpt:mManOptions) {
97 for(
const auto& opt:manOpt.second) {
98 mem+=(opt.first.length()+opt.second.length())*
sizeof(tChar);
100 mem+=manOpt.first.length()*
sizeof(tChar);
110 std::map<tString,tString>::iterator
begin() {
111 return mOptions.begin();
116 std::map<tString,tString>::iterator
end() {
117 return mOptions.end();
121 std::map<tString,tString>::const_iterator
cbegin()
const {
122 return mOptions.cbegin();
127 std::map<tString,tString>::const_iterator
cend()
const {
128 return mOptions.cend();
139 mIsOptionsNameCaseSensitive=isOptNameCS;
146 return mIsOptionsNameCaseSensitive;
171 auto iter=options.
cbegin();
172 auto endIter=options.
cend();
173 while (iter!=endIter) {
174 mOptions[iter->first]=iter->second;
187 inline void setManOption(
const tString& category,
const tString& optName,
const tString& man) {
188 mManOptions[category][optName]=man;
196 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
198 if (value) mOptions[key]=
"true";
199 else mOptions[key]=
"false";
210 auto iOption=mOptions.find(optName);
211 if (iOption==mOptions.end()) {
212 mOptions[optName]=
"";
213 iOption=mOptions.find(optName);
216 return iOption->second;
228 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
230 std::stringstream sstr;
233 mOptions[key]=sstr.str();
240 inline void setOptionValue(
const tString& optName,
const std::initializer_list<T>& values) {
242 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
244 std::stringstream sstr;
246 for(
const auto& v:values) sstr<<v<<
",";
247 if (values.size()>0) sstr.seekp(-1,sstr.cur);
249 mOptions[key]=sstr.str();
256 inline void setOptionValue(
const tString& optName,
const std::valarray<T>& values) {
258 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
260 std::stringstream sstr;
262 for(
const auto& v:values) sstr<<v<<
",";
263 if (values.size()>0) sstr.seekp(-1,sstr.cur);
265 mOptions[key]=sstr.str();
272 inline void setOptionValue(
const tString& optName,
const std::vector<T>& values) {
274 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
276 std::stringstream sstr;
278 for(
const auto& v:values) sstr<<v<<
",";
279 if (values.size()>0) sstr.seekp(-1,sstr.cur);
281 mOptions[key]=sstr.str();
287 template<
typename T,tIndex N>
288 inline void setOptionValue(
const tString& optName,
const std::array<T,N>& values) {
290 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
292 std::stringstream sstr;
294 for(
const auto& v:values) sstr<<v<<
",";
295 if (values.size()>0) sstr.seekp(-1,sstr.cur);
297 mOptions[key]=sstr.str();
322 std::map<tString,tString>::const_iterator iOptions=mOptions.find(optName);
323 if (iOptions!=mOptions.end()) {
324 mOptions.erase(iOptions);
337 if (!mIsOptionsNameCaseSensitive) functions_string::toLower(key);
339 std::map<tString,tString>::const_iterator iOptions=mOptions.find(key);
340 if (iOptions==mOptions.end())
return false;
342 optValue=iOptions->second;
353 template<
typename T>
requires (functions_type::isRealType<T>)
354 inline tBoolean
getOptionValue(
const tString& optName,T& optValue)
const {
359 std::istringstream iss;
372 template<
typename T>
requires (functions_type::isIntegerType<T>)
373 inline tBoolean
getOptionValue(
const tString& optName,T& optValue)
const {
377 std::from_chars(strValue.data(), strValue.data() + strValue.size(), optValue);
389 inline tBoolean
getOptionValue(
const tString& optName,tBoolean& optValue)
const {
393 functions_string::trim(strValue);
394 if (strValue.length()==0) {
398 switch(strValue[0]) {
421 inline tBoolean
getOptionValue(
const tString& optName,std::vector<T>& optValues)
const {
426 if (!ok)
return false;
429 tIndex lmax=strValue.length();
436 tIndex indexOB=strValue.find(
"[");
437 if (indexOB!=tString::npos) {
438 strValue=strValue.substr(indexOB+1);
440 indexOB=strValue.find(
"(");
441 if (indexOB!=tString::npos) {
442 strValue=strValue.substr(indexOB+1);
449 tIndex indexMax=strValue.find(
"]");
450 if (indexMax==tString::npos) {
451 indexMax=strValue.find(
")");
452 if (indexMax==tString::npos) {
458 tIndex indexC=strValue.find(
",");
461 while (indexC<indexMax) {
463 functions_numeric::parse<T>(strValue.substr(0,indexC),value);
465 optValues.push_back(value);
468 strValue=strValue.substr(indexC+1,lmax-indexC-1);
471 lmax=strValue.length();
474 indexC=strValue.find(
",");
477 indexMax=strValue.find(
"]");
478 if (indexMax==tString::npos) {
479 indexMax=strValue.find(
")");
480 if (indexMax==tString::npos) {
494 v=strValue.substr(0,indexMax);
496 strValue=strValue.substr(indexMax+1,lmax-indexMax-1);
498 if (strValue[0]==
',') strValue=strValue.substr(1);
504 functions_numeric::parse(v,value);
505 optValues.push_back(value);
518 template<
class T,tIndex N>
519 inline tBoolean
getOptionValue(
const tString& optName,std::array<T,N>& optValues)
const {
523 if (!ok)
return false;
526 for(
auto& v:optValues) v=0;
527 T* iValue=optValues.data();
528 const T* eValue=optValues.data()+N;
531 tIndex lmax=strValue.length();
535 tIndex indexOB=strValue.find(
"[");
536 if (indexOB!=tString::npos) {
537 strValue=strValue.substr(indexOB+1);
539 indexOB=strValue.find(
"(");
540 if (indexOB!=tString::npos) {
541 strValue=strValue.substr(indexOB+1);
547 tIndex indexMax=strValue.find(
"]");
548 if (indexMax==tString::npos) {
549 indexMax=strValue.find(
")");
550 if (indexMax==tString::npos) {
557 tIndex indexC=strValue.find(
",");
558 while ((indexC<indexMax)&&(iValue!=eValue)) {
561 functions_numeric::parse<T>(strValue.substr(0,indexC),(*iValue));
565 strValue=strValue.substr(indexC+1,lmax-indexC-1);
566 lmax=strValue.length();
567 indexC=strValue.find(
",");
570 indexMax=strValue.find(
"]");
571 if (indexMax==tString::npos) {
572 indexMax=strValue.find(
")");
573 if (indexMax==tString::npos) {
585 v=strValue.substr(0,indexMax);
587 strValue=strValue.substr(indexMax+1,lmax-indexMax-1);
589 if (strValue[0]==
',') strValue=strValue.substr(1);
595 while (iValue!=eValue) {
596 functions_numeric::parse(v,(*iValue));
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,
629 if (
ReadOption(nArgs,argv,isCaseSensitive,optionName,strValue)) {
630 std::from_chars(strValue.data(), strValue.data() + strValue.size(), optionValue);
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,
648 if (
ReadOption(nArgs,argv,isCaseSensitive,optionName,strValue)) {
649 std::istringstream iss;
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,
669 if (
ReadOption(nArgs,argv,isCaseSensitive,optionName,strValue)) {
670 functions_string::trim(strValue);
671 if (strValue.length()==0) {
674 switch(strValue[0]) {
712 tBoolean
saveToFile(
const tString& fileName)
const;
726 virtual tString
toString()
const override;
736 void setOption(tString& arg);
746 static void ReadOptionValue(tString& optionValue);
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