tesseract  5.0.0-alpha-619-ge9db
params.h
Go to the documentation of this file.
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 
19 #ifndef PARAMS_H
20 #define PARAMS_H
21 
23 #include <tesseract/strngs.h>
24 
25 #include <cstdio>
26 
27 namespace tesseract {
28 
29 class IntParam;
30 class BoolParam;
31 class StringParam;
32 class DoubleParam;
33 
34 // Enum for constraints on what kind of params should be set by SetParam().
35 enum SetParamConstraint {
40 };
41 
42 struct ParamsVectors {
47 };
48 
49 // Utility functions for working with Tesseract parameters.
50 class ParamUtils {
51  public:
52  // Reads a file of parameter definitions and set/modify the values therein.
53  // If the filename begins with a + or -, the BoolVariables will be
54  // ORed or ANDed with any current values.
55  // Blank lines and lines beginning # are ignored.
56  // Values may have any whitespace after the name and are the rest of line.
57  static bool ReadParamsFile(const char* file, // filename to read
58  SetParamConstraint constraint,
59  ParamsVectors* member_params);
60 
61  // Read parameters from the given file pointer.
62  static bool ReadParamsFromFp(SetParamConstraint constraint, TFile* fp,
63  ParamsVectors* member_params);
64 
65  // Set a parameters to have the given value.
66  static bool SetParam(const char* name, const char* value,
67  SetParamConstraint constraint,
68  ParamsVectors* member_params);
69 
70  // Returns the pointer to the parameter with the given name (of the
71  // appropriate type) if it was found in the vector obtained from
72  // GlobalParams() or in the given member_params.
73  template <class T>
74  static T* FindParam(const char* name, const GenericVector<T*>& global_vec,
75  const GenericVector<T*>& member_vec) {
76  int i;
77  for (i = 0; i < global_vec.size(); ++i) {
78  if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
79  }
80  for (i = 0; i < member_vec.size(); ++i) {
81  if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
82  }
83  return nullptr;
84  }
85  // Removes the given pointer to the param from the given vector.
86  template <class T>
87  static void RemoveParam(T* param_ptr, GenericVector<T*>* vec) {
88  for (int i = 0; i < vec->size(); ++i) {
89  if ((*vec)[i] == param_ptr) {
90  vec->remove(i);
91  return;
92  }
93  }
94  }
95  // Fetches the value of the named param as a STRING. Returns false if not
96  // found.
97  static bool GetParamAsString(const char* name,
98  const ParamsVectors* member_params,
99  STRING* value);
100 
101  // Print parameters to the given file.
102  static void PrintParams(FILE* fp, const ParamsVectors* member_params);
103 
104  // Resets all parameters back to default values;
105  static void ResetToDefaults(ParamsVectors* member_params);
106 };
107 
108 // Definition of various parameter types.
109 class Param {
110  public:
111  ~Param() = default;
112 
113  const char* name_str() const { return name_; }
114  const char* info_str() const { return info_; }
115  bool is_init() const { return init_; }
116  bool is_debug() const { return debug_; }
117  bool constraint_ok(SetParamConstraint constraint) const {
118  return (
119  constraint == SET_PARAM_CONSTRAINT_NONE ||
120  (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY && this->is_debug()) ||
121  (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
122  !this->is_debug()) ||
123  (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY && !this->is_init()));
124  }
125 
126  protected:
127  Param(const char* name, const char* comment, bool init)
128  : name_(name), info_(comment), init_(init) {
129  debug_ = (strstr(name, "debug") != nullptr) || (strstr(name, "display"));
130  }
131 
132  const char* name_; // name of this parameter
133  const char* info_; // for menus
134  bool init_; // needs to be set before init
135  bool debug_;
136 };
137 
138 class IntParam : public Param {
139  public:
140  IntParam(int32_t value, const char* name, const char* comment, bool init,
142  : Param(name, comment, init) {
143  value_ = value;
144  default_ = value;
145  params_vec_ = &(vec->int_params);
146  vec->int_params.push_back(this);
147  }
148  ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
149  operator int32_t() const { return value_; }
150  void operator=(int32_t value) { value_ = value; }
151  void set_value(int32_t value) { value_ = value; }
152  void ResetToDefault() { value_ = default_; }
153  void ResetFrom(const ParamsVectors* vec) {
154  for (int i = 0; i < vec->int_params.size(); ++i) {
155  if (strcmp(vec->int_params[i]->name_str(), name_) == 0) {
156  // printf("overriding param %s=%d by =%d\n", name_, value_,
157  // *vec->int_params[i]);
158  value_ = *vec->int_params[i];
159  break;
160  }
161  }
162  }
163 
164  private:
165  int32_t value_;
166  int32_t default_;
167  // Pointer to the vector that contains this param (not owned by this class).
168  GenericVector<IntParam*>* params_vec_;
169 };
170 
171 class BoolParam : public Param {
172  public:
173  BoolParam(bool value, const char* name, const char* comment, bool init,
174  ParamsVectors* vec)
175  : Param(name, comment, init) {
176  value_ = value;
177  default_ = value;
178  params_vec_ = &(vec->bool_params);
179  vec->bool_params.push_back(this);
180  }
181  ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
182  operator bool() const { return value_; }
183  void operator=(bool value) { value_ = value; }
184  void set_value(bool value) { value_ = value; }
185  void ResetToDefault() { value_ = default_; }
186  void ResetFrom(const ParamsVectors* vec) {
187  for (int i = 0; i < vec->bool_params.size(); ++i) {
188  if (strcmp(vec->bool_params[i]->name_str(), name_) == 0) {
189  // printf("overriding param %s=%s by =%s\n", name_, value_ ? "true" :
190  // "false", *vec->bool_params[i] ? "true" : "false");
191  value_ = *vec->bool_params[i];
192  break;
193  }
194  }
195  }
196 
197  private:
198  bool value_;
199  bool default_;
200  // Pointer to the vector that contains this param (not owned by this class).
201  GenericVector<BoolParam*>* params_vec_;
202 };
203 
204 class StringParam : public Param {
205  public:
206  StringParam(const char* value, const char* name, const char* comment,
207  bool init, ParamsVectors* vec)
208  : Param(name, comment, init) {
209  value_ = value;
210  default_ = value;
211  params_vec_ = &(vec->string_params);
212  vec->string_params.push_back(this);
213  }
214  ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
215  operator STRING&() { return value_; }
216  const char* c_str() const { return value_.c_str(); }
217  bool empty() { return value_.length() <= 0; }
218  bool operator==(const STRING& other) { return value_ == other; }
219  void operator=(const STRING& value) { value_ = value; }
220  void set_value(const STRING& value) { value_ = value; }
221  void ResetToDefault() { value_ = default_; }
222  void ResetFrom(const ParamsVectors* vec) {
223  for (int i = 0; i < vec->string_params.size(); ++i) {
224  if (strcmp(vec->string_params[i]->name_str(), name_) == 0) {
225  // printf("overriding param %s=%s by =%s\n", name_, value_,
226  // vec->string_params[i]->c_str());
227  value_ = *vec->string_params[i];
228  break;
229  }
230  }
231  }
232 
233  private:
234  STRING value_;
235  STRING default_;
236  // Pointer to the vector that contains this param (not owned by this class).
237  GenericVector<StringParam*>* params_vec_;
238 };
239 
240 class DoubleParam : public Param {
241  public:
242  DoubleParam(double value, const char* name, const char* comment, bool init,
243  ParamsVectors* vec)
244  : Param(name, comment, init) {
245  value_ = value;
246  default_ = value;
247  params_vec_ = &(vec->double_params);
248  vec->double_params.push_back(this);
249  }
250  ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
251  operator double() const { return value_; }
252  void operator=(double value) { value_ = value; }
253  void set_value(double value) { value_ = value; }
254  void ResetToDefault() { value_ = default_; }
255  void ResetFrom(const ParamsVectors* vec) {
256  for (int i = 0; i < vec->double_params.size(); ++i) {
257  if (strcmp(vec->double_params[i]->name_str(), name_) == 0) {
258  // printf("overriding param %s=%f by =%f\n", name_, value_,
259  // *vec->double_params[i]);
260  value_ = *vec->double_params[i];
261  break;
262  }
263  }
264  }
265 
266  private:
267  double value_;
268  double default_;
269  // Pointer to the vector that contains this param (not owned by this class).
270  GenericVector<DoubleParam*>* params_vec_;
271 };
272 
273 } // namespace tesseract
274 
275 // Global parameter lists.
276 //
277 // To avoid the problem of undetermined order of static initialization
278 // global_params are accessed through the GlobalParams function that
279 // initializes the static pointer to global_params only on the first time
280 // GlobalParams() is called.
281 //
282 // TODO(daria): remove GlobalParams() when all global Tesseract
283 // parameters are converted to members.
285 
286 /*************************************************************************
287  * Note on defining parameters.
288  *
289  * The values of the parameters defined with *_INIT_* macros are guaranteed
290  * to be loaded from config files before Tesseract initialization is done
291  * (there is no such guarantee for parameters defined with the other macros).
292  *************************************************************************/
293 
294 #define INT_VAR_H(name, val, comment) tesseract::IntParam name
295 
296 #define BOOL_VAR_H(name, val, comment) tesseract::BoolParam name
297 
298 #define STRING_VAR_H(name, val, comment) tesseract::StringParam name
299 
300 #define double_VAR_H(name, val, comment) tesseract::DoubleParam name
301 
302 #define INT_VAR(name, val, comment) \
303  tesseract::IntParam name(val, #name, comment, false, GlobalParams())
304 
305 #define BOOL_VAR(name, val, comment) \
306  tesseract::BoolParam name(val, #name, comment, false, GlobalParams())
307 
308 #define STRING_VAR(name, val, comment) \
309  tesseract::StringParam name(val, #name, comment, false, GlobalParams())
310 
311 #define double_VAR(name, val, comment) \
312  tesseract::DoubleParam name(val, #name, comment, false, GlobalParams())
313 
314 #define INT_MEMBER(name, val, comment, vec) \
315  name(val, #name, comment, false, vec)
316 
317 #define BOOL_MEMBER(name, val, comment, vec) \
318  name(val, #name, comment, false, vec)
319 
320 #define STRING_MEMBER(name, val, comment, vec) \
321  name(val, #name, comment, false, vec)
322 
323 #define double_MEMBER(name, val, comment, vec) \
324  name(val, #name, comment, false, vec)
325 
326 #define INT_INIT_MEMBER(name, val, comment, vec) \
327  name(val, #name, comment, true, vec)
328 
329 #define BOOL_INIT_MEMBER(name, val, comment, vec) \
330  name(val, #name, comment, true, vec)
331 
332 #define STRING_INIT_MEMBER(name, val, comment, vec) \
333  name(val, #name, comment, true, vec)
334 
335 #define double_INIT_MEMBER(name, val, comment, vec) \
336  name(val, #name, comment, true, vec)
337 
338 #endif
tesseract::BoolParam::set_value
void set_value(bool value)
Definition: params.h:198
GenericVector::remove
void remove(int index)
Definition: genericvector.h:765
tesseract::ParamUtils::ReadParamsFile
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:39
strngs.h
tesseract::Param::is_init
bool is_init() const
Definition: params.h:129
tesseract::StringParam::c_str
const char * c_str() const
Definition: params.h:230
tesseract::IntParam
Definition: params.h:152
tesseract::SET_PARAM_CONSTRAINT_NON_INIT_ONLY
Definition: params.h:53
tesseract::StringParam::empty
bool empty()
Definition: params.h:231
tesseract::BoolParam::BoolParam
BoolParam(bool value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:187
tesseract::DoubleParam::set_value
void set_value(double value)
Definition: params.h:267
tesseract::StringParam::~StringParam
~StringParam()
Definition: params.h:228
tesseract::SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY
Definition: params.h:52
tesseract::Param::~Param
~Param()=default
tesseract::StringParam
Definition: params.h:218
tesseract::IntParam::set_value
void set_value(int32_t value)
Definition: params.h:165
STRING
Definition: strngs.h:45
tesseract::DoubleParam::operator=
void operator=(double value)
Definition: params.h:266
tesseract::SetParamConstraint
SetParamConstraint
Definition: params.h:49
tesseract::ParamsVectors::int_params
GenericVector< IntParam * > int_params
Definition: params.h:57
tesseract::Param
Definition: params.h:123
tesseract::ParamUtils::PrintParams
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:168
tesseract::StringParam::ResetFrom
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:236
tesseract::Param::Param
Param(const char *name, const char *comment, bool init)
Definition: params.h:141
tesseract::SET_PARAM_CONSTRAINT_DEBUG_ONLY
Definition: params.h:51
tesseract::Param::name_
const char * name_
Definition: params.h:146
tesseract::ParamsVectors::double_params
GenericVector< DoubleParam * > double_params
Definition: params.h:60
genericvector.h
tesseract::Param::name_str
const char * name_str() const
Definition: params.h:127
tesseract::ParamsVectors::string_params
GenericVector< StringParam * > string_params
Definition: params.h:59
tesseract::BoolParam::~BoolParam
~BoolParam()
Definition: params.h:195
tesseract::Param::constraint_ok
bool constraint_ok(SetParamConstraint constraint) const
Definition: params.h:131
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
file
Definition: include_gunit.h:22
tesseract::Param::info_
const char * info_
Definition: params.h:147
tesseract::Param::debug_
bool debug_
Definition: params.h:149
tesseract::IntParam::~IntParam
~IntParam()
Definition: params.h:162
tesseract::IntParam::ResetFrom
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:167
tesseract::TFile
Definition: serialis.h:75
tesseract::ParamsVectors
Definition: params.h:56
tesseract::ParamUtils::GetParamAsString
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:129
tesseract::Param::is_debug
bool is_debug() const
Definition: params.h:130
tesseract
Definition: baseapi.h:65
tesseract::ParamUtils::RemoveParam
static void RemoveParam(T *param_ptr, GenericVector< T * > *vec)
Definition: params.h:101
GenericVector
Definition: baseapi.h:40
tesseract::IntParam::operator=
void operator=(int32_t value)
Definition: params.h:164
tesseract::Param::info_str
const char * info_str() const
Definition: params.h:128
tesseract::StringParam::ResetToDefault
void ResetToDefault()
Definition: params.h:235
STRING::length
int32_t length() const
Definition: strngs.cpp:187
tesseract::ParamUtils::ReadParamsFromFp
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:50
tesseract::DoubleParam
Definition: params.h:254
tesseract::StringParam::operator=
void operator=(const STRING &value)
Definition: params.h:233
tesseract::Param::init_
bool init_
Definition: params.h:148
tesseract::ParamUtils::FindParam
static T * FindParam(const char *name, const GenericVector< T * > &global_vec, const GenericVector< T * > &member_vec)
Definition: params.h:88
tesseract::DoubleParam::ResetToDefault
void ResetToDefault()
Definition: params.h:268
tesseract::StringParam::operator==
bool operator==(const STRING &other)
Definition: params.h:232
tesseract::BoolParam
Definition: params.h:185
tesseract::IntParam::IntParam
IntParam(int32_t value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:154
tesseract::BoolParam::ResetToDefault
void ResetToDefault()
Definition: params.h:199
tesseract::ParamUtils
Definition: params.h:64
tesseract::BoolParam::operator=
void operator=(bool value)
Definition: params.h:197
tesseract::ParamUtils::SetParam
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:79
tesseract::IntParam::ResetToDefault
void ResetToDefault()
Definition: params.h:166
tesseract::SET_PARAM_CONSTRAINT_NONE
Definition: params.h:50
tesseract::DoubleParam::ResetFrom
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:269
GlobalParams
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
GenericVector::size
int size() const
Definition: genericvector.h:71
tesseract::BoolParam::ResetFrom
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:200
tesseract::DoubleParam::DoubleParam
DoubleParam(double value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:256
tesseract::StringParam::StringParam
StringParam(const char *value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:220
tesseract::ParamUtils::ResetToDefaults
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:199
tesseract::DoubleParam::~DoubleParam
~DoubleParam()
Definition: params.h:264
tesseract::StringParam::set_value
void set_value(const STRING &value)
Definition: params.h:234
tesseract::ParamsVectors::bool_params
GenericVector< BoolParam * > bool_params
Definition: params.h:58