params.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**********************************************************************
  2. * File: params.h
  3. * Description: Class definitions of the *_VAR classes for tunable constants.
  4. * Author: Ray Smith
  5. *
  6. * (C) Copyright 1991, Hewlett-Packard Ltd.
  7. ** Licensed under the Apache License, Version 2.0 (the "License");
  8. ** you may not use this file except in compliance with the License.
  9. ** You may obtain a copy of the License at
  10. ** http://www.apache.org/licenses/LICENSE-2.0
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. *
  17. **********************************************************************/
  18. #ifndef PARAMS_H
  19. #define PARAMS_H
  20. #include <cstdio>
  21. #include "genericvector.h"
  22. #include "strngs.h"
  23. namespace tesseract {
  24. class IntParam;
  25. class BoolParam;
  26. class StringParam;
  27. class DoubleParam;
  28. // Enum for constraints on what kind of params should be set by SetParam().
  29. enum SetParamConstraint {
  30. SET_PARAM_CONSTRAINT_NONE,
  31. SET_PARAM_CONSTRAINT_DEBUG_ONLY,
  32. SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY,
  33. SET_PARAM_CONSTRAINT_NON_INIT_ONLY,
  34. };
  35. struct ParamsVectors {
  36. GenericVector<IntParam*> int_params;
  37. GenericVector<BoolParam*> bool_params;
  38. GenericVector<StringParam*> string_params;
  39. GenericVector<DoubleParam*> double_params;
  40. };
  41. // Utility functions for working with Tesseract parameters.
  42. class ParamUtils {
  43. public:
  44. // Reads a file of parameter definitions and set/modify the values therein.
  45. // If the filename begins with a + or -, the BoolVariables will be
  46. // ORed or ANDed with any current values.
  47. // Blank lines and lines beginning # are ignored.
  48. // Values may have any whitespace after the name and are the rest of line.
  49. static bool ReadParamsFile(const char* file, // filename to read
  50. SetParamConstraint constraint,
  51. ParamsVectors* member_params);
  52. // Read parameters from the given file pointer.
  53. static bool ReadParamsFromFp(SetParamConstraint constraint, TFile* fp,
  54. ParamsVectors* member_params);
  55. // Set a parameters to have the given value.
  56. static bool SetParam(const char* name, const char* value,
  57. SetParamConstraint constraint,
  58. ParamsVectors* member_params);
  59. // Returns the pointer to the parameter with the given name (of the
  60. // appropriate type) if it was found in the vector obtained from
  61. // GlobalParams() or in the given member_params.
  62. template <class T>
  63. static T* FindParam(const char* name, const GenericVector<T*>& global_vec,
  64. const GenericVector<T*>& member_vec) {
  65. int i;
  66. for (i = 0; i < global_vec.size(); ++i) {
  67. if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
  68. }
  69. for (i = 0; i < member_vec.size(); ++i) {
  70. if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
  71. }
  72. return nullptr;
  73. }
  74. // Removes the given pointer to the param from the given vector.
  75. template <class T>
  76. static void RemoveParam(T* param_ptr, GenericVector<T*>* vec) {
  77. for (int i = 0; i < vec->size(); ++i) {
  78. if ((*vec)[i] == param_ptr) {
  79. vec->remove(i);
  80. return;
  81. }
  82. }
  83. }
  84. // Fetches the value of the named param as a STRING. Returns false if not
  85. // found.
  86. static bool GetParamAsString(const char* name,
  87. const ParamsVectors* member_params,
  88. STRING* value);
  89. // Print parameters to the given file.
  90. static void PrintParams(FILE* fp, const ParamsVectors* member_params);
  91. // Resets all parameters back to default values;
  92. static void ResetToDefaults(ParamsVectors* member_params);
  93. };
  94. // Definition of various parameter types.
  95. class Param {
  96. public:
  97. ~Param() = default;
  98. const char* name_str() const { return name_; }
  99. const char* info_str() const { return info_; }
  100. bool is_init() const { return init_; }
  101. bool is_debug() const { return debug_; }
  102. bool constraint_ok(SetParamConstraint constraint) const {
  103. return (
  104. constraint == SET_PARAM_CONSTRAINT_NONE ||
  105. (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY && this->is_debug()) ||
  106. (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
  107. !this->is_debug()) ||
  108. (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY && !this->is_init()));
  109. }
  110. protected:
  111. Param(const char* name, const char* comment, bool init)
  112. : name_(name), info_(comment), init_(init) {
  113. debug_ = (strstr(name, "debug") != nullptr) || (strstr(name, "display"));
  114. }
  115. const char* name_; // name of this parameter
  116. const char* info_; // for menus
  117. bool init_; // needs to be set before init
  118. bool debug_;
  119. };
  120. class IntParam : public Param {
  121. public:
  122. IntParam(int32_t value, const char* name, const char* comment, bool init,
  123. ParamsVectors* vec)
  124. : Param(name, comment, init) {
  125. value_ = value;
  126. default_ = value;
  127. params_vec_ = &(vec->int_params);
  128. vec->int_params.push_back(this);
  129. }
  130. ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
  131. operator int32_t() const { return value_; }
  132. void operator=(int32_t value) { value_ = value; }
  133. void set_value(int32_t value) { value_ = value; }
  134. void ResetToDefault() { value_ = default_; }
  135. void ResetFrom(const ParamsVectors* vec) {
  136. for (int i = 0; i < vec->int_params.size(); ++i) {
  137. if (strcmp(vec->int_params[i]->name_str(), name_) == 0) {
  138. // printf("overriding param %s=%d by =%d\n", name_, value_,
  139. // *vec->int_params[i]);
  140. value_ = *vec->int_params[i];
  141. break;
  142. }
  143. }
  144. }
  145. private:
  146. int32_t value_;
  147. int32_t default_;
  148. // Pointer to the vector that contains this param (not owned by this class).
  149. GenericVector<IntParam*>* params_vec_;
  150. };
  151. class BoolParam : public Param {
  152. public:
  153. BoolParam(bool value, const char* name, const char* comment, bool init,
  154. ParamsVectors* vec)
  155. : Param(name, comment, init) {
  156. value_ = value;
  157. default_ = value;
  158. params_vec_ = &(vec->bool_params);
  159. vec->bool_params.push_back(this);
  160. }
  161. ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
  162. operator bool() const { return value_; }
  163. void operator=(bool value) { value_ = value; }
  164. void set_value(bool value) { value_ = value; }
  165. void ResetToDefault() { value_ = default_; }
  166. void ResetFrom(const ParamsVectors* vec) {
  167. for (int i = 0; i < vec->bool_params.size(); ++i) {
  168. if (strcmp(vec->bool_params[i]->name_str(), name_) == 0) {
  169. // printf("overriding param %s=%s by =%s\n", name_, value_ ? "true" :
  170. // "false", *vec->bool_params[i] ? "true" : "false");
  171. value_ = *vec->bool_params[i];
  172. break;
  173. }
  174. }
  175. }
  176. private:
  177. bool value_;
  178. bool default_;
  179. // Pointer to the vector that contains this param (not owned by this class).
  180. GenericVector<BoolParam*>* params_vec_;
  181. };
  182. class StringParam : public Param {
  183. public:
  184. StringParam(const char* value, const char* name, const char* comment,
  185. bool init, ParamsVectors* vec)
  186. : Param(name, comment, init) {
  187. value_ = value;
  188. default_ = value;
  189. params_vec_ = &(vec->string_params);
  190. vec->string_params.push_back(this);
  191. }
  192. ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
  193. operator STRING&() { return value_; }
  194. const char* string() const { return value_.string(); }
  195. const char* c_str() const { return value_.string(); }
  196. bool empty() { return value_.length() <= 0; }
  197. bool operator==(const STRING& other) { return value_ == other; }
  198. void operator=(const STRING& value) { value_ = value; }
  199. void set_value(const STRING& value) { value_ = value; }
  200. void ResetToDefault() { value_ = default_; }
  201. void ResetFrom(const ParamsVectors* vec) {
  202. for (int i = 0; i < vec->string_params.size(); ++i) {
  203. if (strcmp(vec->string_params[i]->name_str(), name_) == 0) {
  204. // printf("overriding param %s=%s by =%s\n", name_, value_,
  205. // vec->string_params[i]->c_str());
  206. value_ = *vec->string_params[i];
  207. break;
  208. }
  209. }
  210. }
  211. private:
  212. STRING value_;
  213. STRING default_;
  214. // Pointer to the vector that contains this param (not owned by this class).
  215. GenericVector<StringParam*>* params_vec_;
  216. };
  217. class DoubleParam : public Param {
  218. public:
  219. DoubleParam(double value, const char* name, const char* comment, bool init,
  220. ParamsVectors* vec)
  221. : Param(name, comment, init) {
  222. value_ = value;
  223. default_ = value;
  224. params_vec_ = &(vec->double_params);
  225. vec->double_params.push_back(this);
  226. }
  227. ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
  228. operator double() const { return value_; }
  229. void operator=(double value) { value_ = value; }
  230. void set_value(double value) { value_ = value; }
  231. void ResetToDefault() { value_ = default_; }
  232. void ResetFrom(const ParamsVectors* vec) {
  233. for (int i = 0; i < vec->double_params.size(); ++i) {
  234. if (strcmp(vec->double_params[i]->name_str(), name_) == 0) {
  235. // printf("overriding param %s=%f by =%f\n", name_, value_,
  236. // *vec->double_params[i]);
  237. value_ = *vec->double_params[i];
  238. break;
  239. }
  240. }
  241. }
  242. private:
  243. double value_;
  244. double default_;
  245. // Pointer to the vector that contains this param (not owned by this class).
  246. GenericVector<DoubleParam*>* params_vec_;
  247. };
  248. } // namespace tesseract
  249. // Global parameter lists.
  250. //
  251. // To avoid the problem of undetermined order of static initialization
  252. // global_params are accessed through the GlobalParams function that
  253. // initializes the static pointer to global_params only on the first time
  254. // GlobalParams() is called.
  255. //
  256. // TODO(daria): remove GlobalParams() when all global Tesseract
  257. // parameters are converted to members.
  258. tesseract::ParamsVectors* GlobalParams();
  259. /*************************************************************************
  260. * Note on defining parameters.
  261. *
  262. * The values of the parameters defined with *_INIT_* macros are guaranteed
  263. * to be loaded from config files before Tesseract initialization is done
  264. * (there is no such guarantee for parameters defined with the other macros).
  265. *************************************************************************/
  266. #define INT_VAR_H(name, val, comment) tesseract::IntParam name
  267. #define BOOL_VAR_H(name, val, comment) tesseract::BoolParam name
  268. #define STRING_VAR_H(name, val, comment) tesseract::StringParam name
  269. #define double_VAR_H(name, val, comment) tesseract::DoubleParam name
  270. #define INT_VAR(name, val, comment) \
  271. tesseract::IntParam name(val, #name, comment, false, GlobalParams())
  272. #define BOOL_VAR(name, val, comment) \
  273. tesseract::BoolParam name(val, #name, comment, false, GlobalParams())
  274. #define STRING_VAR(name, val, comment) \
  275. tesseract::StringParam name(val, #name, comment, false, GlobalParams())
  276. #define double_VAR(name, val, comment) \
  277. tesseract::DoubleParam name(val, #name, comment, false, GlobalParams())
  278. #define INT_MEMBER(name, val, comment, vec) \
  279. name(val, #name, comment, false, vec)
  280. #define BOOL_MEMBER(name, val, comment, vec) \
  281. name(val, #name, comment, false, vec)
  282. #define STRING_MEMBER(name, val, comment, vec) \
  283. name(val, #name, comment, false, vec)
  284. #define double_MEMBER(name, val, comment, vec) \
  285. name(val, #name, comment, false, vec)
  286. #define INT_INIT_MEMBER(name, val, comment, vec) \
  287. name(val, #name, comment, true, vec)
  288. #define BOOL_INIT_MEMBER(name, val, comment, vec) \
  289. name(val, #name, comment, true, vec)
  290. #define STRING_INIT_MEMBER(name, val, comment, vec) \
  291. name(val, #name, comment, true, vec)
  292. #define double_INIT_MEMBER(name, val, comment, vec) \
  293. name(val, #name, comment, true, vec)
  294. #endif