FST  openfst-1.8.1
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 // FLAGS USAGE:
33 //
34 // Definition example:
35 //
36 // DEFINE_int32(length, 0, "length");
37 //
38 // This defines variable FST_FLAGS_length, initialized to 0.
39 //
40 // Declaration example:
41 //
42 // DECLARE_int32(length);
43 //
44 // SET_FLAGS() can be used to set flags from the command line
45 // using, for example, '--length=2'.
46 //
47 // ShowUsage() can be used to print out command and flag usage.
48 
49 #define DECLARE_bool(name) extern bool FST_FLAGS_ ## name
50 #define DECLARE_string(name) extern std::string FST_FLAGS_##name
51 #define DECLARE_int32(name) extern int32 FST_FLAGS_ ## name
52 #define DECLARE_int64(name) extern int64 FST_FLAGS_ ## name
53 #define DECLARE_uint64(name) extern uint64 FST_FLAGS_##name
54 #define DECLARE_double(name) extern double FST_FLAGS_ ## name
55 
56 template <typename T>
58  FlagDescription(T *addr, const char *doc, const char *type,
59  const char *file, const T val)
60  : address(addr),
61  doc_string(doc),
62  type_name(type),
63  file_name(file),
64  default_value(val) {}
65 
66  T *address;
67  const char *doc_string;
68  const char *type_name;
69  const char *file_name;
70  const T default_value;
71 };
72 
73 template <typename T>
74 class FlagRegister {
75  public:
77  static auto reg = new FlagRegister<T>;
78  return reg;
79  }
80 
81  const FlagDescription<T> &GetFlagDescription(const std::string &name) const {
82  fst::MutexLock l(&flag_lock_);
83  auto it = flag_table_.find(name);
84  return it != flag_table_.end() ? it->second : 0;
85  }
86 
87  void SetDescription(const std::string &name, const FlagDescription<T> &desc) {
88  fst::MutexLock l(&flag_lock_);
89  flag_table_.insert(make_pair(name, desc));
90  }
91 
92  bool SetFlag(const std::string &val, bool *address) const {
93  if (val == "true" || val == "1" || val.empty()) {
94  *address = true;
95  return true;
96  } else if (val == "false" || val == "0") {
97  *address = false;
98  return true;
99  }
100  else {
101  return false;
102  }
103  }
104 
105  bool SetFlag(const std::string &val, std::string *address) const {
106  *address = val;
107  return true;
108  }
109 
110  bool SetFlag(const std::string &val, int32 *address) const {
111  char *p = nullptr;
112  *address = strtol(val.c_str(), &p, 0);
113  return !val.empty() && *p == '\0';
114  }
115 
116  bool SetFlag(const std::string &val, int64 *address) const {
117  char *p = nullptr;
118  *address = strtoll(val.c_str(), &p, 0);
119  return !val.empty() && *p == '\0';
120  }
121 
122  bool SetFlag(const std::string &val, uint64 *address) const {
123  char *p = 0;
124  *address = strtoull(val.c_str(), &p, 0);
125  return !val.empty() && *p == '\0';
126  }
127 
128  bool SetFlag(const std::string &val, double *address) const {
129  char *p = nullptr;
130  *address = strtod(val.c_str(), &p);
131  return !val.empty() && *p == '\0';
132  }
133 
134  bool SetFlag(const std::string &arg, const std::string &val) const {
135  for (const auto &kv : flag_table_) {
136  const auto &name = kv.first;
137  const FlagDescription<T> &desc = kv.second;
138  if (arg == name) return SetFlag(val, desc.address);
139  }
140  return false;
141  }
142 
143  void GetUsage(
144  std::set<std::pair<std::string, std::string>> *usage_set) const {
145  for (auto it = flag_table_.begin(); it != flag_table_.end(); ++it) {
146  const auto &name = it->first;
147  const FlagDescription<T> &desc = it->second;
148  std::string usage = " --" + name;
149  usage += ": type = ";
150  usage += desc.type_name;
151  usage += ", default = ";
152  usage += GetDefault(desc.default_value) + "\n ";
153  usage += desc.doc_string;
154  usage_set->insert(make_pair(desc.file_name, usage));
155  }
156  }
157 
158  private:
159  std::string GetDefault(bool default_value) const {
160  return default_value ? "true" : "false";
161  }
162 
163  std::string GetDefault(const std::string &default_value) const {
164  return "\"" + default_value + "\"";
165  }
166 
167  template <class V>
168  std::string GetDefault(const V &default_value) const {
169  std::ostringstream strm;
170  strm << default_value;
171  return strm.str();
172  }
173 
174  mutable fst::Mutex flag_lock_; // Multithreading lock.
175  std::map<std::string, FlagDescription<T>> flag_table_;
176 };
177 
178 template <typename T>
180  public:
181  FlagRegisterer(const std::string &name, const FlagDescription<T> &desc) {
182  auto registr = FlagRegister<T>::GetRegister();
183  registr->SetDescription(name, desc);
184  }
185 
186  private:
187  FlagRegisterer(const FlagRegisterer &) = delete;
188  FlagRegisterer &operator=(const FlagRegisterer &) = delete;
189 };
190 
191 
192 #define DEFINE_VAR(type, name, value, doc) \
193  type FST_FLAGS_ ## name = value; \
194  static FlagRegisterer<type> \
195  name ## _flags_registerer(#name, FlagDescription<type>(&FST_FLAGS_ ## name, \
196  doc, \
197  #type, \
198  __FILE__, \
199  value))
200 
201 #define DEFINE_bool(name, value, doc) DEFINE_VAR(bool, name, value, doc)
202 #define DEFINE_string(name, value, doc) \
203  DEFINE_VAR(std::string, name, value, doc)
204 #define DEFINE_int32(name, value, doc) DEFINE_VAR(int32, name, value, doc)
205 #define DEFINE_int64(name, value, doc) DEFINE_VAR(int64, name, value, doc)
206 #define DEFINE_uint64(name, value, doc) DEFINE_VAR(uint64, name, value, doc)
207 #define DEFINE_double(name, value, doc) DEFINE_VAR(double, name, value, doc)
208 
209 
210 // Temporary directory.
211 DECLARE_string(tmpdir);
212 
213 void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags,
214  const char *src = "");
215 
216 // This is an unpleasant hack around absl::SetFlag API.
217 template <typename Type, typename Value>
218 void SetFlag(Type *flag, Value value) {
219  *flag = Type(value);
220 }
221 
222 #define SET_FLAGS(usage, argc, argv, rmflags) \
223 SetFlags(usage, argc, argv, rmflags, __FILE__)
224 
225 // Deprecated; for backward compatibility.
226 inline void InitFst(const char *usage, int *argc, char ***argv, bool rmflags) {
227  return SetFlags(usage, argc, argv, rmflags);
228 }
229 
230 void ShowUsage(bool long_usage = true);
231 
232 #endif // FST_LIB_FLAGS_H_
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
void InitFst(const char *usage, int *argc, char ***argv, bool rmflags)
Definition: flags.h:226
const char * type_name
Definition: flags.h:68
const char * file_name
Definition: flags.h:69
T * address
Definition: flags.h:66
uint64_t uint64
Definition: types.h:32
bool SetFlag(const std::string &val, double *address) const
Definition: flags.h:128
bool SetFlag(const std::string &val, std::string *address) const
Definition: flags.h:105
bool SetFlag(const std::string &val, bool *address) const
Definition: flags.h:92
bool SetFlag(const std::string &val, uint64 *address) const
Definition: flags.h:122
const FlagDescription< T > & GetFlagDescription(const std::string &name) const
Definition: flags.h:81
void SetFlags(const char *usage, int *argc, char ***argv, bool remove_flags, const char *src="")
Definition: flags.cc:45
bool SetFlag(const std::string &val, int64 *address) const
Definition: flags.h:116
static FlagRegister< T > * GetRegister()
Definition: flags.h:76
const T default_value
Definition: flags.h:70
bool SetFlag(const std::string &val, int32 *address) const
Definition: flags.h:110
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:58
bool SetFlag(const std::string &arg, const std::string &val) const
Definition: flags.h:134
void GetUsage(std::set< std::pair< std::string, std::string >> *usage_set) const
Definition: flags.h:143
#define DECLARE_string(name)
Definition: flags.h:50
FlagRegisterer(const std::string &name, const FlagDescription< T > &desc)
Definition: flags.h:181
int32_t int32
Definition: types.h:26
void SetDescription(const std::string &name, const FlagDescription< T > &desc)
Definition: flags.h:87
void SetFlag(Type *flag, Value value)
Definition: flags.h:218
const char * doc_string
Definition: flags.h:67