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