00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef FST_LIB_FLAGS_H__
00021 #define FST_LIB_FLAGS_H__
00022
00023 #include <iostream>
00024 #include <map>
00025 #include <string>
00026
00027 #include <fst/lock.h>
00028
00029 using std::string;
00030
00031
00032 typedef signed char int8;
00033 typedef short int16;
00034 typedef int int32;
00035 typedef long long int64;
00036
00037 typedef char uint8;
00038 typedef unsigned short uint16;
00039 typedef unsigned int uint32;
00040 typedef unsigned long long uint64;
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #define DECLARE_bool(name) extern bool FLAGS_ ## name
00062 #define DECLARE_string(name) extern string FLAGS_ ## name
00063 #define DECLARE_int32(name) extern int32 FLAGS_ ## name
00064 #define DECLARE_int64(name) extern int64 FLAGS_ ## name
00065 #define DECLARE_double(name) extern double FLAGS_ ## name
00066
00067 template <typename T>
00068 struct FlagDescription {
00069 FlagDescription(T *addr, const char *doc, const char *type, const T val)
00070 : address(addr), doc_string(doc), type_name(type), default_value(val) {}
00071
00072 T *address;
00073 const char *doc_string;
00074 const char *type_name;
00075 const T default_value;
00076 };
00077
00078 template <typename T>
00079 class FlagRegister {
00080 public:
00081 static FlagRegister<T> *GetRegister() {
00082 fst::FstOnceInit(®ister_init_, &FlagRegister<T>::Init);
00083 return register_;
00084 }
00085
00086 const FlagDescription<T> &GetFlagDescription(const string &name) const {
00087 fst::MutexLock l(register_lock_);
00088 typename std::map< string, FlagDescription<T> >::const_iterator it =
00089 flag_table_.find(name);
00090 return it != flag_table_.end() ? it->second : 0;
00091 }
00092 void SetDescription(const string &name,
00093 const FlagDescription<T> &desc) {
00094 fst::MutexLock l(register_lock_);
00095 flag_table_.insert(make_pair(name, desc));
00096 }
00097
00098 bool SetFlag(const string &val, bool *address) const {
00099 if (val == "true" || val == "1" || val.empty()) {
00100 *address = true;
00101 return true;
00102 } else if (val == "false" || val == "0") {
00103 *address = false;
00104 return true;
00105 }
00106 else {
00107 return false;
00108 }
00109 }
00110 bool SetFlag(const string &val, string *address) const {
00111 *address = val;
00112 return true;
00113 }
00114 bool SetFlag(const string &val, int32 *address) const {
00115 char *p = 0;
00116 *address = strtol(val.c_str(), &p, 0);
00117 return !val.empty() && *p == '\0';
00118 }
00119 bool SetFlag(const string &val, int64 *address) const {
00120 char *p = 0;
00121 *address = strtoll(val.c_str(), &p, 0);
00122 return !val.empty() && *p == '\0';
00123 }
00124 bool SetFlag(const string &val, double *address) const {
00125 char *p = 0;
00126 *address = strtod(val.c_str(), &p);
00127 return !val.empty() && *p == '\0';
00128 }
00129
00130 bool SetFlag(const string &arg, const string &val) const {
00131 for (typename std::map< string,
00132 FlagDescription<T> >::const_iterator it =
00133 flag_table_.begin();
00134 it != flag_table_.end();
00135 ++it) {
00136 const string &name = it->first;
00137 const FlagDescription<T> &desc = it->second;
00138 if (arg == name)
00139 return SetFlag(val, desc.address);
00140 }
00141 return false;
00142 }
00143
00144 void ShowDefault(bool default_value) const {
00145 std::cout << ", default = ";
00146 std::cout << (default_value ? "true" : "false");
00147 }
00148 void ShowDefault(const string &default_value) const {
00149 std::cout << ", default = ";
00150 std::cout << "\"" << default_value << "\"";
00151 }
00152 template<typename V> void ShowDefault(const V& default_value) const {
00153 std::cout << ", default = ";
00154 std::cout << default_value;
00155 }
00156 void ShowUsage() const {
00157 for (typename std::map< string,
00158 FlagDescription<T> >::const_iterator it =
00159 flag_table_.begin();
00160 it != flag_table_.end();
00161 ++it) {
00162 const string &name = it->first;
00163 const FlagDescription<T> &desc = it->second;
00164 std::cout << " --" << name
00165 << ": type = " << desc.type_name;
00166 ShowDefault(desc.default_value);
00167 std::cout << "\n " << desc.doc_string << "\n";
00168 }
00169 }
00170
00171 private:
00172 static void Init() {
00173 register_lock_ = new fst::Mutex;
00174 register_ = new FlagRegister<T>;
00175 }
00176 static fst::FstOnceType register_init_;
00177 static fst::Mutex* register_lock_;
00178 static FlagRegister<T> *register_;
00179
00180 std::map< string, FlagDescription<T> > flag_table_;
00181 };
00182
00183 template <class T>
00184 fst::FstOnceType FlagRegister<T>::register_init_ = fst::FST_ONCE_INIT;
00185
00186 template <class T>
00187 fst::Mutex *FlagRegister<T>::register_lock_ = 0;
00188
00189 template <class T>
00190 FlagRegister<T> *FlagRegister<T>::register_ = 0;
00191
00192
00193 template <typename T>
00194 class FlagRegisterer {
00195 public:
00196 FlagRegisterer(const string &name, const FlagDescription<T> &desc) {
00197 FlagRegister<T> *registr = FlagRegister<T>::GetRegister();
00198 registr->SetDescription(name, desc);
00199 }
00200
00201 private:
00202 DISALLOW_COPY_AND_ASSIGN(FlagRegisterer);
00203 };
00204
00205
00206 #define DEFINE_VAR(type, name, value, doc) \
00207 type FLAGS_ ## name = value; \
00208 static FlagRegisterer<type> \
00209 name ## _flags_registerer(#name, FlagDescription<type>(&FLAGS_ ## name, \
00210 doc, \
00211 #type, \
00212 value))
00213
00214 #define DEFINE_bool(name, value, doc) DEFINE_VAR(bool, name, value, doc)
00215 #define DEFINE_string(name, value, doc) \
00216 DEFINE_VAR(string, name, value, doc)
00217 #define DEFINE_int32(name, value, doc) DEFINE_VAR(int32, name, value, doc)
00218 #define DEFINE_int64(name, value, doc) DEFINE_VAR(int64, name, value, doc)
00219 #define DEFINE_double(name, value, doc) DEFINE_VAR(double, name, value, doc)
00220
00221
00222
00223 DECLARE_string(tmpdir);
00224
00225 void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags);
00226
00227
00228 inline void InitFst(const char *usage, int *argc, char ***argv, bool rmflags) {
00229 return SetFlags(usage, argc, argv, rmflags);
00230 }
00231
00232 void ShowUsage();
00233
00234 #endif // FST_LIB_FLAGS_H__