FST  openfst-1.6.9
OpenFst Library
flags.h
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 //
13 // See www.openfst.org for extensive documentation on this weighted
14 // finite-state transducer library.
15 //
16 // Google-style flag handling declarations and inline definitions.
17 
18 #ifndef FST_LIB_FLAGS_H_
19 #define FST_LIB_FLAGS_H_
20 
21 #include <cstdlib>
22 
23 #include <iostream>
24 #include <map>
25 #include <set>
26 #include <sstream>
27 #include <string>
28 
29 #include <fst/types.h>
30 #include <fst/lock.h>
31 
32 using std::string;
33 
34 // FLAGS USAGE:
35 //
36 // Definition example:
37 //
38 // DEFINE_int32(length, 0, "length");
39 //
40 // This defines variable FLAGS_length, initialized to 0.
41 //
42 // Declaration example:
43 //
44 // DECLARE_int32(length);
45 //
46 // SET_FLAGS() can be used to set flags from the command line
47 // using, for example, '--length=2'.
48 //
49 // ShowUsage() can be used to print out command and flag usage.
50 
51 #define DECLARE_bool(name) extern bool FLAGS_ ## name
52 #define DECLARE_string(name) extern string FLAGS_ ## name
53 #define DECLARE_int32(name) extern int32 FLAGS_ ## name
54 #define DECLARE_int64(name) extern int64 FLAGS_ ## name
55 #define DECLARE_double(name) extern double FLAGS_ ## name
56 
57 template <typename T>
59  FlagDescription(T *addr, const char *doc, const char *type,
60  const char *file, const T val)
61  : address(addr),
62  doc_string(doc),
63  type_name(type),
64  file_name(file),
65  default_value(val) {}
66 
67  T *address;
68  const char *doc_string;
69  const char *type_name;
70  const char *file_name;
71  const T default_value;
72 };
73 
74 template <typename T>
75 class FlagRegister {
76  public:
78  static auto reg = new FlagRegister<T>;
79  return reg;
80  }
81 
82  const FlagDescription<T> &GetFlagDescription(const string &name) const {
83  fst::MutexLock l(&flag_lock_);
84  auto it = flag_table_.find(name);
85  return it != flag_table_.end() ? it->second : 0;
86  }
87 
88  void SetDescription(const string &name,
89  const FlagDescription<T> &desc) {
90  fst::MutexLock l(&flag_lock_);
91  flag_table_.insert(make_pair(name, desc));
92  }
93 
94  bool SetFlag(const string &val, bool *address) const {
95  if (val == "true" || val == "1" || val.empty()) {
96  *address = true;
97  return true;
98  } else if (val == "false" || val == "0") {
99  *address = false;
100  return true;
101  }
102  else {
103  return false;
104  }
105  }
106 
107  bool SetFlag(const string &val, string *address) const {
108  *address = val;
109  return true;
110  }
111 
112  bool SetFlag(const string &val, int32 *address) const {
113  char *p = 0;
114  *address = strtol(val.c_str(), &p, 0);
115  return !val.empty() && *p == '\0';
116  }
117 
118  bool SetFlag(const string &val, int64 *address) const {
119  char *p = 0;
120  *address = strtoll(val.c_str(), &p, 0);
121  return !val.empty() && *p == '\0';
122  }
123 
124  bool SetFlag(const string &val, double *address) const {
125  char *p = 0;
126  *address = strtod(val.c_str(), &p);
127  return !val.empty() && *p == '\0';
128  }
129 
130  bool SetFlag(const string &arg, const string &val) const {
131  for (typename std::map< string, FlagDescription<T> >::const_iterator it =
132  flag_table_.begin();
133  it != flag_table_.end();
134  ++it) {
135  const string &name = it->first;
136  const FlagDescription<T> &desc = it->second;
137  if (arg == name)
138  return SetFlag(val, desc.address);
139  }
140  return false;
141  }
142 
143  void GetUsage(std::set<std::pair<string, string>> *usage_set) const {
144  for (auto it = flag_table_.begin(); it != flag_table_.end(); ++it) {
145  const string &name = it->first;
146  const FlagDescription<T> &desc = it->second;
147  string usage = " --" + name;
148  usage += ": type = ";
149  usage += desc.type_name;
150  usage += ", default = ";
151  usage += GetDefault(desc.default_value) + "\n ";
152  usage += desc.doc_string;
153  usage_set->insert(make_pair(desc.file_name, usage));
154  }
155  }
156 
157  private:
158  string GetDefault(bool default_value) const {
159  return default_value ? "true" : "false";
160  }
161 
162  string GetDefault(const string &default_value) const {
163  return "\"" + default_value + "\"";
164  }
165 
166  template <class V>
167  string GetDefault(const V &default_value) const {
168  std::ostringstream strm;
169  strm << default_value;
170  return strm.str();
171  }
172 
173  mutable fst::Mutex flag_lock_; // Multithreading lock.
174  std::map<string, FlagDescription<T>> flag_table_;
175 };
176 
177 template <typename T>
179  public:
180  FlagRegisterer(const string &name, const FlagDescription<T> &desc) {
181  auto registr = FlagRegister<T>::GetRegister();
182  registr->SetDescription(name, desc);
183  }
184 
185  private:
186  FlagRegisterer(const FlagRegisterer &) = delete;
187  FlagRegisterer &operator=(const FlagRegisterer &) = delete;
188 };
189 
190 
191 #define DEFINE_VAR(type, name, value, doc) \
192  type FLAGS_ ## name = value; \
193  static FlagRegisterer<type> \
194  name ## _flags_registerer(#name, FlagDescription<type>(&FLAGS_ ## name, \
195  doc, \
196  #type, \
197  __FILE__, \
198  value))
199 
200 #define DEFINE_bool(name, value, doc) DEFINE_VAR(bool, name, value, doc)
201 #define DEFINE_string(name, value, doc) \
202  DEFINE_VAR(string, name, value, doc)
203 #define DEFINE_int32(name, value, doc) DEFINE_VAR(int32, name, value, doc)
204 #define DEFINE_int64(name, value, doc) DEFINE_VAR(int64, name, value, doc)
205 #define DEFINE_double(name, value, doc) DEFINE_VAR(double, name, value, doc)
206 
207 
208 // Temporary directory.
209 DECLARE_string(tmpdir);
210 
211 void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags,
212  const char *src = "");
213 
214 #define SET_FLAGS(usage, argc, argv, rmflags) \
215 SetFlags(usage, argc, argv, rmflags, __FILE__)
216 
217 // Deprecated; for backward compatibility.
218 inline void InitFst(const char *usage, int *argc, char ***argv, bool rmflags) {
219  return SetFlags(usage, argc, argv, rmflags);
220 }
221 
222 void ShowUsage(bool long_usage = true);
223 
224 #endif // FST_LIB_FLAGS_H_
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
void InitFst(const char *usage, int *argc, char ***argv, bool rmflags)
Definition: flags.h:218
const char * type_name
Definition: flags.h:69
const char * file_name
Definition: flags.h:70
T * address
Definition: flags.h:67
bool SetFlag(const string &val, double *address) const
Definition: flags.h:124
bool SetFlag(const string &val, int64 *address) const
Definition: flags.h:118
void SetDescription(const string &name, const FlagDescription< T > &desc)
Definition: flags.h:88
void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags, const char *src="")
Definition: flags.cc:46
void GetUsage(std::set< std::pair< string, string >> *usage_set) const
Definition: flags.h:143
static FlagRegister< T > * GetRegister()
Definition: flags.h:77
bool SetFlag(const string &val, bool *address) const
Definition: flags.h:94
const T default_value
Definition: flags.h:71
bool SetFlag(const string &val, int32 *address) const
Definition: flags.h:112
FlagRegisterer(const string &name, const FlagDescription< T > &desc)
Definition: flags.h:180
int64_t int64
Definition: types.h:27
FlagDescription(T *addr, const char *doc, const char *type, const char *file, const T val)
Definition: flags.h:59
bool SetFlag(const string &arg, const string &val) const
Definition: flags.h:130
#define DECLARE_string(name)
Definition: flags.h:52
bool SetFlag(const string &val, string *address) const
Definition: flags.h:107
int32_t int32
Definition: types.h:26
const char * doc_string
Definition: flags.h:68
const FlagDescription< T > & GetFlagDescription(const string &name) const
Definition: flags.h:82