tesseract 4.1.1
Loading...
Searching...
No Matches
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
22#include <cstdio>
23
24#include "genericvector.h"
25#include "strngs.h"
26
27namespace tesseract {
28
29class IntParam;
30class BoolParam;
31class StringParam;
32class DoubleParam;
33
34// Enum for constraints on what kind of params should be set by SetParam().
40};
41
47};
48
49// Utility functions for working with Tesseract parameters.
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.
109class 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()) ||
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
138class IntParam : public Param {
139 public:
140 IntParam(int32_t value, const char* name, const char* comment, bool init,
141 ParamsVectors* vec)
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
171class 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
204class 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* string() const { return value_.string(); }
217 const char* c_str() const { return value_.string(); }
218 bool empty() { return value_.length() <= 0; }
219 bool operator==(const STRING& other) { return value_ == other; }
220 void operator=(const STRING& value) { value_ = value; }
221 void set_value(const STRING& value) { value_ = value; }
222 void ResetToDefault() { value_ = default_; }
223 void ResetFrom(const ParamsVectors* vec) {
224 for (int i = 0; i < vec->string_params.size(); ++i) {
225 if (strcmp(vec->string_params[i]->name_str(), name_) == 0) {
226 // printf("overriding param %s=%s by =%s\n", name_, value_,
227 // vec->string_params[i]->c_str());
228 value_ = *vec->string_params[i];
229 break;
230 }
231 }
232 }
233
234 private:
235 STRING value_;
236 STRING default_;
237 // Pointer to the vector that contains this param (not owned by this class).
238 GenericVector<StringParam*>* params_vec_;
239};
240
241class DoubleParam : public Param {
242 public:
243 DoubleParam(double value, const char* name, const char* comment, bool init,
244 ParamsVectors* vec)
245 : Param(name, comment, init) {
246 value_ = value;
247 default_ = value;
248 params_vec_ = &(vec->double_params);
249 vec->double_params.push_back(this);
250 }
251 ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
252 operator double() const { return value_; }
253 void operator=(double value) { value_ = value; }
254 void set_value(double value) { value_ = value; }
255 void ResetToDefault() { value_ = default_; }
256 void ResetFrom(const ParamsVectors* vec) {
257 for (int i = 0; i < vec->double_params.size(); ++i) {
258 if (strcmp(vec->double_params[i]->name_str(), name_) == 0) {
259 // printf("overriding param %s=%f by =%f\n", name_, value_,
260 // *vec->double_params[i]);
261 value_ = *vec->double_params[i];
262 break;
263 }
264 }
265 }
266
267 private:
268 double value_;
269 double default_;
270 // Pointer to the vector that contains this param (not owned by this class).
271 GenericVector<DoubleParam*>* params_vec_;
272};
273
274} // namespace tesseract
275
276// Global parameter lists.
277//
278// To avoid the problem of undetermined order of static initialization
279// global_params are accessed through the GlobalParams function that
280// initializes the static pointer to global_params only on the first time
281// GlobalParams() is called.
282//
283// TODO(daria): remove GlobalParams() when all global Tesseract
284// parameters are converted to members.
286
287/*************************************************************************
288 * Note on defining parameters.
289 *
290 * The values of the parameters defined with *_INIT_* macros are guaranteed
291 * to be loaded from config files before Tesseract initialization is done
292 * (there is no such guarantee for parameters defined with the other macros).
293 *************************************************************************/
294
295#define INT_VAR_H(name, val, comment) tesseract::IntParam name
296
297#define BOOL_VAR_H(name, val, comment) tesseract::BoolParam name
298
299#define STRING_VAR_H(name, val, comment) tesseract::StringParam name
300
301#define double_VAR_H(name, val, comment) tesseract::DoubleParam name
302
303#define INT_VAR(name, val, comment) \
304 tesseract::IntParam name(val, #name, comment, false, GlobalParams())
305
306#define BOOL_VAR(name, val, comment) \
307 tesseract::BoolParam name(val, #name, comment, false, GlobalParams())
308
309#define STRING_VAR(name, val, comment) \
310 tesseract::StringParam name(val, #name, comment, false, GlobalParams())
311
312#define double_VAR(name, val, comment) \
313 tesseract::DoubleParam name(val, #name, comment, false, GlobalParams())
314
315#define INT_MEMBER(name, val, comment, vec) \
316 name(val, #name, comment, false, vec)
317
318#define BOOL_MEMBER(name, val, comment, vec) \
319 name(val, #name, comment, false, vec)
320
321#define STRING_MEMBER(name, val, comment, vec) \
322 name(val, #name, comment, false, vec)
323
324#define double_MEMBER(name, val, comment, vec) \
325 name(val, #name, comment, false, vec)
326
327#define INT_INIT_MEMBER(name, val, comment, vec) \
328 name(val, #name, comment, true, vec)
329
330#define BOOL_INIT_MEMBER(name, val, comment, vec) \
331 name(val, #name, comment, true, vec)
332
333#define STRING_INIT_MEMBER(name, val, comment, vec) \
334 name(val, #name, comment, true, vec)
335
336#define double_INIT_MEMBER(name, val, comment, vec) \
337 name(val, #name, comment, true, vec)
338
339#endif
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:32
SetParamConstraint
Definition: params.h:35
@ SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY
Definition: params.h:38
@ SET_PARAM_CONSTRAINT_NONE
Definition: params.h:36
@ SET_PARAM_CONSTRAINT_NON_INIT_ONLY
Definition: params.h:39
@ SET_PARAM_CONSTRAINT_DEBUG_ONLY
Definition: params.h:37
int size() const
Definition: genericvector.h:72
void remove(int index)
GenericVector< IntParam * > int_params
Definition: params.h:43
GenericVector< DoubleParam * > double_params
Definition: params.h:46
GenericVector< BoolParam * > bool_params
Definition: params.h:44
GenericVector< StringParam * > string_params
Definition: params.h:45
static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp, ParamsVectors *member_params)
Definition: params.cpp:50
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:39
static T * FindParam(const char *name, const GenericVector< T * > &global_vec, const GenericVector< T * > &member_vec)
Definition: params.h:74
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:129
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:199
static void RemoveParam(T *param_ptr, GenericVector< T * > *vec)
Definition: params.h:87
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:168
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:79
bool is_init() const
Definition: params.h:115
const char * name_
Definition: params.h:132
const char * name_str() const
Definition: params.h:113
const char * info_
Definition: params.h:133
Param(const char *name, const char *comment, bool init)
Definition: params.h:127
bool constraint_ok(SetParamConstraint constraint) const
Definition: params.h:117
const char * info_str() const
Definition: params.h:114
bool is_debug() const
Definition: params.h:116
~Param()=default
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:153
void ResetToDefault()
Definition: params.h:152
void operator=(int32_t value)
Definition: params.h:150
IntParam(int32_t value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:140
void set_value(int32_t value)
Definition: params.h:151
void ResetToDefault()
Definition: params.h:185
void set_value(bool value)
Definition: params.h:184
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:186
void operator=(bool value)
Definition: params.h:183
BoolParam(bool value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:173
bool operator==(const STRING &other)
Definition: params.h:219
void set_value(const STRING &value)
Definition: params.h:221
void operator=(const STRING &value)
Definition: params.h:220
StringParam(const char *value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:206
const char * string() const
Definition: params.h:216
const char * c_str() const
Definition: params.h:217
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:223
void operator=(double value)
Definition: params.h:253
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:256
void set_value(double value)
Definition: params.h:254
DoubleParam(double value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:243
Definition: strngs.h:45
int32_t length() const
Definition: strngs.cpp:189
const char * string() const
Definition: strngs.cpp:194