Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
109 // Definition of various parameter types.
110 class Param {
111  public:
112  ~Param() {}
113 
114  const char *name_str() const { return name_; }
115  const char *info_str() const { return info_; }
116  bool is_init() const { return init_; }
117  bool is_debug() const { return debug_; }
118  bool constraint_ok(SetParamConstraint constraint) const {
119  return (constraint == SET_PARAM_CONSTRAINT_NONE ||
120  (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY &&
121  this->is_debug()) ||
122  (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
123  !this->is_debug()) ||
124  (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY &&
125  !this->is_init()));
126  }
127 
128  protected:
129  Param(const char *name, const char *comment, bool init) :
130  name_(name), info_(comment), init_(init) {
131  debug_ = (strstr(name, "debug") != NULL) || (strstr(name, "display"));
132  }
133 
134  const char *name_; // name of this parameter
135  const char *info_; // for menus
136  bool init_; // needs to be set before init
137  bool debug_;
138 };
139 
140 class IntParam : public Param {
141  public:
142  IntParam(inT32 value, const char *name, const char *comment, bool init,
143  ParamsVectors *vec) : Param(name, comment, init) {
144  value_ = 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() const { return value_; }
150  void set_value(inT32 value) { value_ = value; }
151 
152  private:
153  inT32 value_;
154  // Pointer to the vector that contains this param (not owened by this class).
155  GenericVector<IntParam *> *params_vec_;
156 };
157 
158 class BoolParam : public Param {
159  public:
160  BoolParam(bool value, const char *name, const char *comment, bool init,
161  ParamsVectors *vec) : Param(name, comment, init) {
162  value_ = value;
163  params_vec_ = &(vec->bool_params);
164  vec->bool_params.push_back(this);
165  }
166  ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
167  operator BOOL8() const { return value_; }
168  void set_value(BOOL8 value) { value_ = value; }
169 
170  private:
171  BOOL8 value_;
172  // Pointer to the vector that contains this param (not owned by this class).
173  GenericVector<BoolParam *> *params_vec_;
174 };
175 
176 class StringParam : public Param {
177  public:
178  StringParam(const char *value, const char *name,
179  const char *comment, bool init,
180  ParamsVectors *vec) : Param(name, comment, init) {
181  value_ = value;
182  params_vec_ = &(vec->string_params);
183  vec->string_params.push_back(this);
184  }
185  ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
186  operator STRING &() { return value_; }
187  const char *string() const { return value_.string(); }
188  bool empty() { return value_.length() <= 0; }
189  void set_value(const STRING &value) { value_ = value; }
190 
191  private:
192  STRING value_;
193  // Pointer to the vector that contains this param (not owened by this class).
194  GenericVector<StringParam *> *params_vec_;
195 };
196 
197 class DoubleParam : public Param {
198  public:
199  DoubleParam(double value, const char *name, const char *comment,
200  bool init, ParamsVectors *vec) : Param(name, comment, init) {
201  value_ = value;
202  params_vec_ = &(vec->double_params);
203  vec->double_params.push_back(this);
204  }
205  ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
206  operator double() const { return value_; }
207  void set_value(double value) { value_ = value; }
208 
209  private:
210  double value_;
211  // Pointer to the vector that contains this param (not owned by this class).
212  GenericVector<DoubleParam *> *params_vec_;
213 };
214 
215 } // namespace tesseract
216 
217 // Global parameter lists.
218 //
219 // To avoid the problem of undetermined order of static initialization
220 // global_params are accessed through the GlobalParams function that
221 // initializes the static pointer to global_params only on the first
222 // first time GlobalParams() is called.
223 //
224 // TODO(daria): remove GlobalParams() when all global Tesseract
225 // parameters are converted to members.
227 
228 /*************************************************************************
229  * Note on defining parameters.
230  *
231  * The values of the parameters defined with *_INIT_* macros are guaranteed
232  * to be loaded from config files before Tesseract initialization is done
233  * (there is no such guarantee for parameters defined with the other macros).
234  *************************************************************************/
235 
236 #define INT_VAR_H(name,val,comment)\
237  tesseract::IntParam name
238 
239 #define BOOL_VAR_H(name,val,comment)\
240  tesseract::BoolParam name
241 
242 #define STRING_VAR_H(name,val,comment)\
243  tesseract::StringParam name
244 
245 #define double_VAR_H(name,val,comment)\
246  tesseract::DoubleParam name
247 
248 #define INT_VAR(name,val,comment)\
249  tesseract::IntParam name(val,#name,comment,false,GlobalParams())
250 
251 #define BOOL_VAR(name,val,comment)\
252  tesseract::BoolParam name(val,#name,comment,false,GlobalParams())
253 
254 #define STRING_VAR(name,val,comment)\
255  tesseract::StringParam name(val,#name,comment,false,GlobalParams())
256 
257 #define double_VAR(name,val,comment)\
258  tesseract::DoubleParam name(val,#name,comment,false,GlobalParams())
259 
260 #define INT_INIT_VAR(name,val,comment)\
261  tesseract::IntParam name(val,#name,comment,true,GlobalParams())
262 
263 #define BOOL_INIT_VAR(name,val,comment)\
264  tesseract::BoolParam name(val,#name,comment,true,GlobalParams())
265 
266 #define STRING_INIT_VAR(name,val,comment)\
267  tesseract::StringParam name(val,#name,comment,true,GlobalParams())
268 
269 #define double_INIT_VAR(name,val,comment)\
270  tesseract::DoubleParam name(val,#name,comment,true,GlobalParams())
271 
272 #define INT_MEMBER(name, val, comment, vec)\
273  name(val, #name, comment, false, vec)
274 
275 #define BOOL_MEMBER(name, val, comment, vec)\
276  name(val, #name, comment, false, vec)
277 
278 #define STRING_MEMBER(name, val, comment, vec)\
279  name(val, #name, comment, false, vec)
280 
281 #define double_MEMBER(name, val, comment, vec)\
282  name(val, #name, comment, false, vec)
283 
284 #define INT_INIT_MEMBER(name, val, comment, vec)\
285  name(val, #name, comment, true, vec)
286 
287 #define BOOL_INIT_MEMBER(name, val, comment, vec)\
288  name(val, #name, comment, true, vec)
289 
290 #define STRING_INIT_MEMBER(name, val, comment, vec)\
291  name(val, #name, comment, true, vec)
292 
293 #define double_INIT_MEMBER(name, val, comment, vec)\
294  name(val, #name, comment, true, vec)
295 
296 #endif