| 1 |
#ifndef OPTIONS_H |
| 2 |
#define OPTIONS_H |
| 3 |
|
| 4 |
#include <iostream> |
| 5 |
#include <sstream> |
| 6 |
#include <string> |
| 7 |
#include <map> |
| 8 |
#include <algorithm> |
| 9 |
#include <cctype> |
| 10 |
|
| 11 |
#include "StrUtils.h" |
| 12 |
|
| 13 |
using std::string; |
| 14 |
using std::cout; |
| 15 |
using std::endl; |
| 16 |
using std::stringstream; |
| 17 |
using std::map; |
| 18 |
using std::ostream; |
| 19 |
|
| 20 |
namespace FCUtils { |
| 21 |
|
| 22 |
struct Option { |
| 23 |
Option(): name("error"), val("false"), |
| 24 |
help("no description"){}; |
| 25 |
Option( |
| 26 |
const string &n, |
| 27 |
const string &v="false", |
| 28 |
const string &h="no description") |
| 29 |
: name(ToLower(n)), val(v), help("-"+ToLower(n)+" : "+h){}; |
| 30 |
Option(const char *n, |
| 31 |
const char *v="false", |
| 32 |
const char *h="no description") |
| 33 |
: name(ToLower(n)), val(v){ SetHelp(h); } |
| 34 |
|
| 35 |
void SetHelp(const string &h){help="-"+name+" : "+h;} |
| 36 |
void SetHelp(const char *h){SetHelp(string(h));} |
| 37 |
|
| 38 |
void Help() const { cout << help << endl; } |
| 39 |
void Print() const { |
| 40 |
cout << " Option : " << name << ", value : " << val << endl; |
| 41 |
cout << " help : " << help << endl; |
| 42 |
} |
| 43 |
|
| 44 |
void operator >> ( int & v) { |
| 45 |
stringstream temp(val); |
| 46 |
temp >> v; |
| 47 |
} |
| 48 |
|
| 49 |
void operator >> ( float & v) const { |
| 50 |
stringstream temp(val); |
| 51 |
temp >> v; |
| 52 |
} |
| 53 |
|
| 54 |
void operator >> ( double & v) const { |
| 55 |
stringstream temp(val); |
| 56 |
temp >> v; |
| 57 |
} |
| 58 |
|
| 59 |
void operator >> ( bool & v) const { |
| 60 |
stringstream temp(val); |
| 61 |
temp << std::boolalpha; |
| 62 |
temp >> v; |
| 63 |
} |
| 64 |
|
| 65 |
void operator >> ( string & v) const { |
| 66 |
v=val; |
| 67 |
} |
| 68 |
|
| 69 |
string name; |
| 70 |
string val; |
| 71 |
string help; |
| 72 |
|
| 73 |
}; // struct Option |
| 74 |
|
| 75 |
|
| 76 |
typedef map<string, Option > OptMap; |
| 77 |
|
| 78 |
class Options { |
| 79 |
|
| 80 |
public: |
| 81 |
|
| 82 |
static Options * Get(); |
| 83 |
|
| 84 |
bool IsOptionDef( const string &name); |
| 85 |
bool IsOptionDef( const char *name){ |
| 86 |
return IsOptionDef( string(name)); |
| 87 |
} |
| 88 |
|
| 89 |
void SetOption(const string &name, |
| 90 |
const string &val="false", |
| 91 |
const string &help="No description") |
| 92 |
{ options_[ToLower(name)] = Option(name,val,help); } |
| 93 |
|
| 94 |
void SetOption(const char *name, |
| 95 |
const char *val="false", |
| 96 |
const char *help="No description") |
| 97 |
{ SetOption(string(name),val,help); } |
| 98 |
|
| 99 |
bool UpdateOption( const string &name, const string &val); |
| 100 |
bool UpdateOption( const char *name, const char *val){ |
| 101 |
return UpdateOption(string(name),val); |
| 102 |
} |
| 103 |
|
| 104 |
//Option const & GetOption(string const &name); |
| 105 |
Option & GetOption(string const &name); |
| 106 |
|
| 107 |
Option & operator[] ( string const & name) { return options_[name]; } |
| 108 |
void Help() const; |
| 109 |
void Print() const; |
| 110 |
|
| 111 |
private: |
| 112 |
static Options * instance_; |
| 113 |
OptMap options_; |
| 114 |
Option error_; |
| 115 |
|
| 116 |
Options(); |
| 117 |
|
| 118 |
}; // class Options |
| 119 |
|
| 120 |
}; // namespace FCUtils |
| 121 |
#endif // OPTIONS_H |