00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef PARAMS_H
00021 #define PARAMS_H
00022
00023 #include <stdio.h>
00024
00025 #include "genericvector.h"
00026 #include "strngs.h"
00027
00028 namespace tesseract {
00029
00030 class IntParam;
00031 class BoolParam;
00032 class StringParam;
00033 class DoubleParam;
00034
00035
00036 enum SetParamConstraint {
00037 SET_PARAM_CONSTRAINT_NONE,
00038 SET_PARAM_CONSTRAINT_DEBUG_ONLY,
00039 SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY,
00040 SET_PARAM_CONSTRAINT_NON_INIT_ONLY,
00041 };
00042
00043 struct ParamsVectors {
00044 GenericVector<IntParam *> int_params;
00045 GenericVector<BoolParam *> bool_params;
00046 GenericVector<StringParam *> string_params;
00047 GenericVector<DoubleParam *> double_params;
00048 };
00049
00050
00051 class ParamUtils {
00052 public:
00053
00054
00055
00056
00057
00058 static bool ReadParamsFile(
00059 const char *file,
00060 SetParamConstraint constraint,
00061 ParamsVectors *member_params);
00062
00063
00064 static bool ReadParamsFromFp(FILE *fp, inT64 end_offset,
00065 SetParamConstraint constraint,
00066 ParamsVectors *member_params);
00067
00068
00069 static bool SetParam(const char *name, const char* value,
00070 SetParamConstraint constraint,
00071 ParamsVectors *member_params);
00072
00073
00074
00075
00076 template<class T>
00077 static T *FindParam(const char *name,
00078 const GenericVector<T *> &global_vec,
00079 const GenericVector<T *> &member_vec) {
00080 int i;
00081 for (i = 0; i < global_vec.size(); ++i) {
00082 if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
00083 }
00084 for (i = 0; i < member_vec.size(); ++i) {
00085 if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
00086 }
00087 return NULL;
00088 }
00089
00090 template<class T>
00091 static void RemoveParam(T *param_ptr, GenericVector<T *> *vec) {
00092 for (int i = 0; i < vec->size(); ++i) {
00093 if ((*vec)[i] == param_ptr) {
00094 vec->remove(i);
00095 return;
00096 }
00097 }
00098 }
00099
00100
00101 static bool GetParamAsString(const char *name,
00102 const ParamsVectors* member_params,
00103 STRING *value);
00104
00105
00106 static void PrintParams(FILE *fp, const ParamsVectors *member_params);
00107 };
00108
00109
00110 class Param {
00111 public:
00112 ~Param() {}
00113
00114 const char *name_str() const { return name_; }
00115 const char *info_str() const { return info_; }
00116 bool is_init() const { return init_; }
00117 bool is_debug() const { return debug_; }
00118 bool constraint_ok(SetParamConstraint constraint) const {
00119 return (constraint == SET_PARAM_CONSTRAINT_NONE ||
00120 (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY &&
00121 this->is_debug()) ||
00122 (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
00123 !this->is_debug()) ||
00124 (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY &&
00125 !this->is_init()));
00126 }
00127
00128 protected:
00129 Param(const char *name, const char *comment, bool init) :
00130 name_(name), info_(comment), init_(init) {
00131 debug_ = (strstr(name, "debug") != NULL) || (strstr(name, "display"));
00132 }
00133
00134 const char *name_;
00135 const char *info_;
00136 bool init_;
00137 bool debug_;
00138 };
00139
00140 class IntParam : public Param {
00141 public:
00142 IntParam(inT32 value, const char *name, const char *comment, bool init,
00143 ParamsVectors *vec) : Param(name, comment, init) {
00144 value_ = value;
00145 params_vec_ = &(vec->int_params);
00146 vec->int_params.push_back(this);
00147 }
00148 ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
00149 operator inT32() const { return value_; }
00150 void set_value(inT32 value) { value_ = value; }
00151
00152 private:
00153 inT32 value_;
00154
00155 GenericVector<IntParam *> *params_vec_;
00156 };
00157
00158 class BoolParam : public Param {
00159 public:
00160 BoolParam(bool value, const char *name, const char *comment, bool init,
00161 ParamsVectors *vec) : Param(name, comment, init) {
00162 value_ = value;
00163 params_vec_ = &(vec->bool_params);
00164 vec->bool_params.push_back(this);
00165 }
00166 ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
00167 operator BOOL8() const { return value_; }
00168 void set_value(BOOL8 value) { value_ = value; }
00169
00170 private:
00171 BOOL8 value_;
00172
00173 GenericVector<BoolParam *> *params_vec_;
00174 };
00175
00176 class StringParam : public Param {
00177 public:
00178 StringParam(const char *value, const char *name,
00179 const char *comment, bool init,
00180 ParamsVectors *vec) : Param(name, comment, init) {
00181 value_ = value;
00182 params_vec_ = &(vec->string_params);
00183 vec->string_params.push_back(this);
00184 }
00185 ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
00186 operator STRING &() { return value_; }
00187 const char *string() const { return value_.string(); }
00188 bool empty() { return value_.length() <= 0; }
00189 void set_value(const STRING &value) { value_ = value; }
00190
00191 private:
00192 STRING value_;
00193
00194 GenericVector<StringParam *> *params_vec_;
00195 };
00196
00197 class DoubleParam : public Param {
00198 public:
00199 DoubleParam(double value, const char *name, const char *comment,
00200 bool init, ParamsVectors *vec) : Param(name, comment, init) {
00201 value_ = value;
00202 params_vec_ = &(vec->double_params);
00203 vec->double_params.push_back(this);
00204 }
00205 ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
00206 operator double() const { return value_; }
00207 void set_value(double value) { value_ = value; }
00208
00209 private:
00210 double value_;
00211
00212 GenericVector<DoubleParam *> *params_vec_;
00213 };
00214
00215 }
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 tesseract::ParamsVectors *GlobalParams();
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 #define INT_VAR_H(name,val,comment)\
00237 tesseract::IntParam name
00238
00239 #define BOOL_VAR_H(name,val,comment)\
00240 tesseract::BoolParam name
00241
00242 #define STRING_VAR_H(name,val,comment)\
00243 tesseract::StringParam name
00244
00245 #define double_VAR_H(name,val,comment)\
00246 tesseract::DoubleParam name
00247
00248 #define INT_VAR(name,val,comment)\
00249 tesseract::IntParam name(val,#name,comment,false,GlobalParams())
00250
00251 #define BOOL_VAR(name,val,comment)\
00252 tesseract::BoolParam name(val,#name,comment,false,GlobalParams())
00253
00254 #define STRING_VAR(name,val,comment)\
00255 tesseract::StringParam name(val,#name,comment,false,GlobalParams())
00256
00257 #define double_VAR(name,val,comment)\
00258 tesseract::DoubleParam name(val,#name,comment,false,GlobalParams())
00259
00260 #define INT_INIT_VAR(name,val,comment)\
00261 tesseract::IntParam name(val,#name,comment,true,GlobalParams())
00262
00263 #define BOOL_INIT_VAR(name,val,comment)\
00264 tesseract::BoolParam name(val,#name,comment,true,GlobalParams())
00265
00266 #define STRING_INIT_VAR(name,val,comment)\
00267 tesseract::StringParam name(val,#name,comment,true,GlobalParams())
00268
00269 #define double_INIT_VAR(name,val,comment)\
00270 tesseract::DoubleParam name(val,#name,comment,true,GlobalParams())
00271
00272 #define INT_MEMBER(name, val, comment, vec)\
00273 name(val, #name, comment, false, vec)
00274
00275 #define BOOL_MEMBER(name, val, comment, vec)\
00276 name(val, #name, comment, false, vec)
00277
00278 #define STRING_MEMBER(name, val, comment, vec)\
00279 name(val, #name, comment, false, vec)
00280
00281 #define double_MEMBER(name, val, comment, vec)\
00282 name(val, #name, comment, false, vec)
00283
00284 #define INT_INIT_MEMBER(name, val, comment, vec)\
00285 name(val, #name, comment, true, vec)
00286
00287 #define BOOL_INIT_MEMBER(name, val, comment, vec)\
00288 name(val, #name, comment, true, vec)
00289
00290 #define STRING_INIT_MEMBER(name, val, comment, vec)\
00291 name(val, #name, comment, true, vec)
00292
00293 #define double_INIT_MEMBER(name, val, comment, vec)\
00294 name(val, #name, comment, true, vec)
00295
00296 #endif