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