You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
7.2 KiB

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