All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * 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 <stdio.h>
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 (stop at end_offset).
64  static bool ReadParamsFromFp(FILE *fp, inT64 end_offset,
65  SetParamConstraint constraint,
66  ParamsVectors *member_params);
67 
68  // Set a parameters to have the given value.
69  static bool SetParam(const char *name, const char* value,
70  SetParamConstraint constraint,
71  ParamsVectors *member_params);
72 
73  // Returns the pointer to the parameter with the given name (of the
74  // appropriate type) if it was found in the vector obtained from
75  // GlobalParams() or in the given member_params.
76  template<class T>
77  static T *FindParam(const char *name,
78  const GenericVector<T *> &global_vec,
79  const GenericVector<T *> &member_vec) {
80  int i;
81  for (i = 0; i < global_vec.size(); ++i) {
82  if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
83  }
84  for (i = 0; i < member_vec.size(); ++i) {
85  if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
86  }
87  return NULL;
88  }
89  // Removes the given pointer to the param from the given vector.
90  template<class T>
91  static void RemoveParam(T *param_ptr, GenericVector<T *> *vec) {
92  for (int i = 0; i < vec->size(); ++i) {
93  if ((*vec)[i] == param_ptr) {
94  vec->remove(i);
95  return;
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,
102  const ParamsVectors* member_params,
103  STRING *value);
104 
105  // Print parameters to the given file.
106  static void PrintParams(FILE *fp, const ParamsVectors *member_params);
107 
108  // Resets all parameters back to default values;
109  static void ResetToDefaults(ParamsVectors* member_params);
110 };
111 
112 // Definition of various parameter types.
113 class Param {
114  public:
115  ~Param() {}
116 
117  const char *name_str() const { return name_; }
118  const char *info_str() const { return info_; }
119  bool is_init() const { return init_; }
120  bool is_debug() const { return debug_; }
121  bool constraint_ok(SetParamConstraint constraint) const {
122  return (constraint == SET_PARAM_CONSTRAINT_NONE ||
123  (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY &&
124  this->is_debug()) ||
125  (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
126  !this->is_debug()) ||
127  (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY &&
128  !this->is_init()));
129  }
130 
131  protected:
132  Param(const char *name, const char *comment, bool init) :
133  name_(name), info_(comment), init_(init) {
134  debug_ = (strstr(name, "debug") != NULL) || (strstr(name, "display"));
135  }
136 
137  const char *name_; // name of this parameter
138  const char *info_; // for menus
139  bool init_; // needs to be set before init
140  bool debug_;
141 };
142 
143 class IntParam : public Param {
144  public:
145  IntParam(inT32 value, const char *name, const char *comment, bool init,
146  ParamsVectors *vec) : Param(name, comment, init) {
147  value_ = value;
148  default_ = value;
149  params_vec_ = &(vec->int_params);
150  vec->int_params.push_back(this);
151  }
152  ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
153  operator inT32() const { return value_; }
154  void operator=(inT32 value) { value_ = value; }
155  void set_value(inT32 value) { value_ = value; }
156  void ResetToDefault() {
157  value_ = default_;
158  }
159 
160  private:
161  inT32 value_;
162  inT32 default_;
163  // Pointer to the vector that contains this param (not owened by this class).
164  GenericVector<IntParam *> *params_vec_;
165 };
166 
167 class BoolParam : public Param {
168  public:
169  BoolParam(bool value, const char *name, const char *comment, bool init,
170  ParamsVectors *vec) : Param(name, comment, init) {
171  value_ = value;
172  default_ = value;
173  params_vec_ = &(vec->bool_params);
174  vec->bool_params.push_back(this);
175  }
176  ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
177  operator BOOL8() const { return value_; }
178  void operator=(BOOL8 value) { value_ = value; }
179  void set_value(BOOL8 value) { value_ = value; }
180  void ResetToDefault() {
181  value_ = default_;
182  }
183 
184  private:
185  BOOL8 value_;
186  BOOL8 default_;
187  // Pointer to the vector that contains this param (not owned by this class).
188  GenericVector<BoolParam *> *params_vec_;
189 };
190 
191 class StringParam : public Param {
192  public:
193  StringParam(const char *value, const char *name,
194  const char *comment, bool init,
195  ParamsVectors *vec) : Param(name, comment, init) {
196  value_ = value;
197  default_ = value;
198  params_vec_ = &(vec->string_params);
199  vec->string_params.push_back(this);
200  }
201  ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
202  operator STRING &() { return value_; }
203  const char *string() const { return value_.string(); }
204  const char *c_str() const { return value_.string(); }
205  bool empty() { return value_.length() <= 0; }
206  bool operator==(const STRING& other) { return value_ == other; }
207  void operator=(const STRING& value) { value_ = value; }
208  void set_value(const STRING& value) { value_ = value; }
209  void ResetToDefault() {
210  value_ = default_;
211  }
212 
213  private:
214  STRING value_;
215  STRING default_;
216  // Pointer to the vector that contains this param (not owened by this class).
217  GenericVector<StringParam *> *params_vec_;
218 };
219 
220 class DoubleParam : public Param {
221  public:
222  DoubleParam(double value, const char *name, const char *comment,
223  bool init, ParamsVectors *vec) : Param(name, comment, init) {
224  value_ = value;
225  default_ = value;
226  params_vec_ = &(vec->double_params);
227  vec->double_params.push_back(this);
228  }
229  ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
230  operator double() const { return value_; }
231  void operator=(double value) { value_ = value; }
232  void set_value(double value) { value_ = value; }
233  void ResetToDefault() {
234  value_ = default_;
235  }
236 
237  private:
238  double value_;
239  double default_;
240  // Pointer to the vector that contains this param (not owned by this class).
241  GenericVector<DoubleParam *> *params_vec_;
242 };
243 
244 } // namespace tesseract
245 
246 // Global parameter lists.
247 //
248 // To avoid the problem of undetermined order of static initialization
249 // global_params are accessed through the GlobalParams function that
250 // initializes the static pointer to global_params only on the first
251 // first time GlobalParams() is called.
252 //
253 // TODO(daria): remove GlobalParams() when all global Tesseract
254 // parameters are converted to members.
256 
257 /*************************************************************************
258  * Note on defining parameters.
259  *
260  * The values of the parameters defined with *_INIT_* macros are guaranteed
261  * to be loaded from config files before Tesseract initialization is done
262  * (there is no such guarantee for parameters defined with the other macros).
263  *************************************************************************/
264 
265 #define INT_VAR_H(name,val,comment)\
266  tesseract::IntParam name
267 
268 #define BOOL_VAR_H(name,val,comment)\
269  tesseract::BoolParam name
270 
271 #define STRING_VAR_H(name,val,comment)\
272  tesseract::StringParam name
273 
274 #define double_VAR_H(name,val,comment)\
275  tesseract::DoubleParam name
276 
277 #define INT_VAR(name,val,comment)\
278  tesseract::IntParam name(val,#name,comment,false,GlobalParams())
279 
280 #define BOOL_VAR(name,val,comment)\
281  tesseract::BoolParam name(val,#name,comment,false,GlobalParams())
282 
283 #define STRING_VAR(name,val,comment)\
284  tesseract::StringParam name(val,#name,comment,false,GlobalParams())
285 
286 #define double_VAR(name,val,comment)\
287  tesseract::DoubleParam name(val,#name,comment,false,GlobalParams())
288 
289 #define INT_INIT_VAR(name,val,comment)\
290  tesseract::IntParam name(val,#name,comment,true,GlobalParams())
291 
292 #define BOOL_INIT_VAR(name,val,comment)\
293  tesseract::BoolParam name(val,#name,comment,true,GlobalParams())
294 
295 #define STRING_INIT_VAR(name,val,comment)\
296  tesseract::StringParam name(val,#name,comment,true,GlobalParams())
297 
298 #define double_INIT_VAR(name,val,comment)\
299  tesseract::DoubleParam name(val,#name,comment,true,GlobalParams())
300 
301 #define INT_MEMBER(name, val, comment, vec)\
302  name(val, #name, comment, false, vec)
303 
304 #define BOOL_MEMBER(name, val, comment, vec)\
305  name(val, #name, comment, false, vec)
306 
307 #define STRING_MEMBER(name, val, comment, vec)\
308  name(val, #name, comment, false, vec)
309 
310 #define double_MEMBER(name, val, comment, vec)\
311  name(val, #name, comment, false, vec)
312 
313 #define INT_INIT_MEMBER(name, val, comment, vec)\
314  name(val, #name, comment, true, vec)
315 
316 #define BOOL_INIT_MEMBER(name, val, comment, vec)\
317  name(val, #name, comment, true, vec)
318 
319 #define STRING_INIT_MEMBER(name, val, comment, vec)\
320  name(val, #name, comment, true, vec)
321 
322 #define double_INIT_MEMBER(name, val, comment, vec)\
323  name(val, #name, comment, true, vec)
324 
325 #endif
static void ResetToDefaults(ParamsVectors *member_params)
Definition: params.cpp:205
int size() const
Definition: genericvector.h:72
static bool ReadParamsFromFp(FILE *fp, inT64 end_offset, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:66
const char * info_
Definition: params.h:138
bool constraint_ok(SetParamConstraint constraint) const
Definition: params.h:121
bool is_debug() const
Definition: params.h:120
SetParamConstraint
Definition: params.h:36
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:180
unsigned char BOOL8
Definition: host.h:113
inT32 length() const
Definition: strngs.cpp:188
static bool GetParamAsString(const char *name, const ParamsVectors *member_params, STRING *value)
Definition: params.cpp:142
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:33
void operator=(const STRING &value)
Definition: params.h:207
void set_value(BOOL8 value)
Definition: params.h:179
void operator=(double value)
Definition: params.h:231
const char * name_str() const
Definition: params.h:117
void set_value(double value)
Definition: params.h:232
StringParam(const char *value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:193
bool operator==(const STRING &other)
Definition: params.h:206
const char * c_str() const
Definition: params.h:204
IntParam(inT32 value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:145
name_table name
GenericVector< IntParam * > int_params
Definition: params.h:44
DoubleParam(double value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:222
void operator=(inT32 value)
Definition: params.h:154
static bool SetParam(const char *name, const char *value, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:98
const char * info_str() const
Definition: params.h:118
GenericVector< BoolParam * > bool_params
Definition: params.h:45
static T * FindParam(const char *name, const GenericVector< T * > &global_vec, const GenericVector< T * > &member_vec)
Definition: params.h:77
bool is_init() const
Definition: params.h:119
const char * name_
Definition: params.h:137
void remove(int index)
Param(const char *name, const char *comment, bool init)
Definition: params.h:132
GenericVector< DoubleParam * > double_params
Definition: params.h:47
void set_value(inT32 value)
Definition: params.h:155
void ResetToDefault()
Definition: params.h:180
BoolParam(bool value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:169
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:41
Definition: strngs.h:44
#define NULL
Definition: host.h:144
void ResetToDefault()
Definition: params.h:156
const char * string() const
Definition: strngs.cpp:193
GenericVector< StringParam * > string_params
Definition: params.h:46
const char * string() const
Definition: params.h:203
void set_value(const STRING &value)
Definition: params.h:208
void operator=(BOOL8 value)
Definition: params.h:178
static void RemoveParam(T *param_ptr, GenericVector< T * > *vec)
Definition: params.h:91
int inT32
Definition: host.h:102
long long int inT64
Definition: host.h:108