Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ParamTypes.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef __ParamTypes_hh__
26 #define __ParamTypes_hh__
27 
28 #include <Base/Common/BIASpragmaStart.hh>
29 
30 #include <Base/Debug/Error.hh>
31 #include <Base/Math/Vector.hh>
32 #include <Base/Common/FileHandling.hh> //< for lowercase
33 
34 
35 #include <string>
36 #include <sstream>
37 #include <map>
38 
39 
40 namespace BIAS {
41  // fwd declaratiion
42  class BIASUtils_EXPORT ParamCallbackInterface;
43 
44  /** @class ParamTypeBase
45  * @brief This file holds the definitions of the parameter types used by BIAS::Param.
46  * @author mdunda 03 2004
47  * @ingroup g_utils
48  */
49  class BIASUtils_EXPORT ParamTypeBase
50  {
51  public:
52  // destructor
53  virtual ~ParamTypeBase();
54 
55  // constructors
56  ParamTypeBase();
57 
58  virtual void SetDefault(){ /* to be overwritten in derived class */ }
59 
60  /**
61  This is a common interface to let a specialized instance read
62  its data from a string. Used from ParseCommandLine() and ReadParam()
63  to be overwritten in derived class
64  */
65  virtual int SetFromString(const std::string &s);
66 
67  virtual std::string GetTypeName(){return "NOTYPE";};
68  virtual std::string GetValueString(){return "NOVAL";};
69  virtual std::string GetHint(){return "NOVAL";};
70 
71  // data members / entries
72  std::string Name_;
73  std::string Help_;
74  char ShortCmd_;
75  bool WriteToFile_; ///< is only written to parameter file if true
76  bool Enhanced_; ///< hide this parameter from the 'naive' user
77  bool Hidden_; ///< hide this parameter from all users
78  int Group_;
79 
80  /// if some one changed a parameter, we should call
81  /// CallbackObject_->ParameterChanged(Name_);
83  };
84 
85 
86  // derived parameter types following...
87 
88  // =========================
89  // === BOOLEAN ===
90  // =========================
91  class BIASUtils_EXPORT ParamTypeBool : public ParamTypeBase
92  {
93  public:
94  // 'structors
96  : ParamTypeBase()
97  {}
98 
99  virtual ~ParamTypeBool()
100  {}
101 
102  // entries
103  bool value_;
104  bool default_;
105 
106  /// resets to default value
107  inline void SetDefault(){ value_=default_; }
108 
109  virtual int SetFromString(const std::string &s)
110  {
111  // be case insensitive, don't construct expression for easier debugging
112  if ((FileHandling::LowerCase(s) == "true" || FileHandling::LowerCase(s) == "=true"))
113  value_ = true;
114  else if ((FileHandling::LowerCase(s) == "on")||(FileHandling::LowerCase(s) == "=on"))
115  value_ = true;
116  else if ((s=="1") || (s=="=1"))
117  value_ = true;
118  else
119  value_ = false;
120 
121  return 0; //< OK
122  };
123 
124  virtual std::string GetTypeName(){return "BOOL";};
125  virtual std::string GetValueString(){return value_?"true":"false";};
126  virtual std::string GetHint(){return "true / false";};
127  };
128 
129 
130  // =========================
131  // === INTEGER ===
132  // =========================
133  class BIASUtils_EXPORT ParamTypeInt : public ParamTypeBase
134  {
135  public:
136  // 'structors
138  : ParamTypeBase()
139  {}
140 
141  virtual ~ParamTypeInt()
142  {}
143 
144  // entries
145  int value_;
146  int default_;
147  int min_;
148  int max_;
149  /// resets to default value
150  inline void SetDefault(){ value_=default_; }
151  virtual int SetFromString(const std::string &s){
152  int newval = atoi(s.c_str());
153  if ( newval < min_ ){
154  BIASWARN("Value for '"<<Name_<<"' clipped to "<<min_<<std::endl);
155  value_ = min_;
156  }
157  else if (newval > max_ ) {
158  BIASWARN("Value for '"<<Name_<<"' clipped to "<<max_<<std::endl);
159  value_ = max_;
160  }
161  else value_ = newval;
162  return 0;
163  };
164  virtual std::string GetTypeName(){return "INT";};
165 
166  virtual std::string GetValueString()
167  {
168  std::ostringstream oss;oss<<value_; return oss.str();
169  };
170 
171  virtual std::string GetHint()
172  {
173  std::ostringstream oss;oss<<"[ "<<min_<<" .. "<<max_<<" ]";
174  return oss.str();
175  };
176 
177  }; // class ParamTypeInt
178 
179 
180  // =========================
181  // === DOUBLE ===
182  // =========================
183  class BIASUtils_EXPORT ParamTypeDouble : public ParamTypeBase
184  {
185  public:
186  // 'structors
188  virtual ~ParamTypeDouble(){}
189  // entries
190  double value_;
191  double default_;
192  double min_;
193  double max_;
194  /// resets to default value
195  inline void SetDefault(){ value_=default_; }
196  virtual int SetFromString(const std::string &s){
197  double newval = atof(s.c_str());
198  if ( newval < min_ ){
199  BIASWARN("Value for '"<<Name_<<"' clipped to "<<min_<<std::endl);
200  value_ = min_;
201  }
202  else if (newval > max_ ) {
203  BIASWARN("Value for '"<<Name_<<"' clipped to "<<max_<<std::endl);
204  value_ = max_;
205  }
206  else value_ = newval;
207  return 0;
208  };
209  virtual std::string GetTypeName(){return "DOUBLE";};
210  virtual std::string GetValueString(){
211  std::ostringstream oss;oss<<value_; return oss.str();};
212  virtual std::string GetHint(){
213  std::ostringstream oss;oss<<"[ "<<min_<<" .. "<<max_<<" ]";
214  return oss.str();};
215  };
216 
217 
218  // =========================
219  // === STRING ===
220  // =========================
221  class BIASUtils_EXPORT ParamTypeString : public ParamTypeBase
222  {
223  public:
224  // 'structors
226  virtual ~ParamTypeString(){}
227  // entries
228  std::string value_;
229  std::string default_;
230  /// resets to default value
231  inline void SetDefault(){ value_=default_; }
232  virtual int SetFromString(const std::string &s){
233  value_ = s;
234  return 0;
235  }
236  virtual std::string GetTypeName(){return "STRING";};
237  virtual std::string GetValueString(){return "\""+value_+"\"";};
238  virtual std::string GetHint(){
239  std::ostringstream oss;oss<<" def: "<<default_;
240  return oss.str();};
241  };
242 
243 
244  // =========================
245  // === VECTOR OF DOUBLE ===
246  // =========================
247  class BIASUtils_EXPORT ParamTypeVecDbl : public ParamTypeBase
248  {
249  public:
250  // 'structors
252  virtual ~ParamTypeVecDbl(){}
253  // entries
256  /// resets to default value
257  inline void SetDefault(){ value_=default_; }
258  virtual int SetFromString(const std::string &s){
259  std::istringstream iss(s);
260  double x;
261  // check for size
262  int length=-1;
263  while (iss){
264  iss >> x; length++;
265  }
266  //std::cerr << Name_ << " has size "<<length<<std::endl;
267  // resize vector
268  value_.newsize(length);
269  // now read it
270  std::istringstream iss2(s);
271  for(unsigned int i=0; i<value_.Size(); i++) {
272  iss2 >> x; value_[i] = x;
273  //std::cerr << x << " ";
274  }
275  return 0;
276  };
277  virtual std::string GetTypeName(){return "VECDBL";};
278  virtual std::string GetValueString(){
279  std::ostringstream oss; oss<<"\"";
280  for (int i=0;i<value_.size(); i++) {
281  oss<<value_[i];
282  if (i<value_.size()-1) oss <<" ";
283  }
284  oss<<"\"";
285  return oss.str();
286  };
287 
288  };
289 
290 
291  // =========================
292  // === VECTOR OF INTEGER ===
293  // =========================
294  class BIASUtils_EXPORT ParamTypeVecInt : public ParamTypeBase
295  {
296  public:
297  // 'structors
299  virtual ~ParamTypeVecInt(){}
300  // entries
303  /// resets to default value
304  inline void SetDefault(){ value_=default_; }
305  virtual int SetFromString(const std::string &s){
306  std::istringstream iss(s);
307  int x;
308  // check for size
309  int length=-1;
310  while (iss){
311  iss >> x; length++;
312  }
313  //std::cerr << Name_ << " has size "<<length<<std::endl;
314  // resize vector
315  value_.newsize(length);
316  // now read it
317  std::istringstream iss2(s);
318  for(unsigned int i=0; i<value_.Size(); i++) {
319  iss2 >> x; value_[i] = x;
320  }
321  return 0;
322  };
323  virtual std::string GetTypeName(){return "VECINT";};
324  virtual std::string GetValueString(){
325  std::ostringstream oss; oss<<"\"";
326  for (int i=0;i<value_.size(); i++) {
327  oss<<value_[i];
328  if (i<value_.size()-1) oss <<" ";
329  }
330  oss<<"\"";
331  return oss.str();
332  };
333 
334  };
335 
336 
337  // =========================
338  // === ENUM ===
339  // =========================
340  class BIASUtils_EXPORT ParamTypeEnum : public ParamTypeBase
341  {
342  public:
343  // 'structors
345  virtual ~ParamTypeEnum(){}
346  // entries
347  int value_;
348  int default_;
349  std::map<std::string,int> Map_;
350  /// resets to default value
351  inline void SetDefault(){ value_=default_; }
352  virtual int SetFromString(const std::string &s){
353  std::map<std::string,int>::iterator it;
354  if ( (it=Map_.find(s)) ==Map_.end() ) {
355  BIASERR("Identifier for '" <<Name_<< "' is not valid. Using default.")
356  value_ = default_;
357  }
358  else value_ = it->second;
359  return 0;
360  }
361  virtual std::string GetTypeName(){return "ENUM";};
362  virtual std::string GetValueString(){
363  std::ostringstream oss;
364  std::map<std::string,int>::iterator it;
365  for (it=Map_.begin();it!=Map_.end(); it++)
366  if ( it->second == value_ ) oss<<it->first;
367  return oss.str();};
368 
369  virtual std::string GetHint(){
370  std::ostringstream oss;
371  std::map<std::string,int>::iterator it;
372  for (it=Map_.begin();it!=Map_.end(); it++){
373  if (it!=Map_.begin()) oss<<", ";
374  oss<<it->first;
375  }
376  return oss.str();};
377  };
378 } // namespace BIAS
379 
380 
381 #include <Base/Common/BIASpragmaEnd.hh>
382 #endif /* __ParamTypes_hh__ */
virtual std::string GetValueString()
Definition: ParamTypes.hh:278
bool Enhanced_
hide this parameter from the 'naive' user
Definition: ParamTypes.hh:76
bool WriteToFile_
is only written to parameter file if true
Definition: ParamTypes.hh:75
virtual std::string GetValueString()
Definition: ParamTypes.hh:125
virtual std::string GetTypeName()
Definition: ParamTypes.hh:323
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:352
void SetDefault()
resets to default value
Definition: ParamTypes.hh:351
virtual std::string GetHint()
Definition: ParamTypes.hh:369
ParamCallbackInterface * CallbackObject_
if some one changed a parameter, we should call CallbackObject_->ParameterChanged(Name_); ...
Definition: ParamTypes.hh:82
virtual ~ParamTypeInt()
Definition: ParamTypes.hh:141
virtual void SetDefault()
Definition: ParamTypes.hh:58
BIAS::Vector< int > value_
Definition: ParamTypes.hh:301
virtual ~ParamTypeBool()
Definition: ParamTypes.hh:99
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:232
This file holds the definitions of the parameter types used by BIAS::Param.
Definition: ParamTypes.hh:49
virtual std::string GetHint()
Definition: ParamTypes.hh:126
virtual std::string GetValueString()
Definition: ParamTypes.hh:237
std::map< std::string, int > Map_
Definition: ParamTypes.hh:349
virtual std::string GetValueString()
Definition: ParamTypes.hh:166
virtual std::string GetHint()
Definition: ParamTypes.hh:238
virtual std::string GetHint()
Definition: ParamTypes.hh:171
virtual ~ParamTypeEnum()
Definition: ParamTypes.hh:345
This class provides an interface to be called if parameter changes occured.
Definition: Param.hh:74
virtual std::string GetTypeName()
Definition: ParamTypes.hh:124
std::string default_
Definition: ParamTypes.hh:229
virtual ~ParamTypeVecDbl()
Definition: ParamTypes.hh:252
virtual std::string GetValueString()
Definition: ParamTypes.hh:362
std::string Help_
Definition: ParamTypes.hh:73
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:258
BIAS::Vector< int > default_
Definition: ParamTypes.hh:302
virtual std::string GetTypeName()
Definition: ParamTypes.hh:361
virtual std::string GetValueString()
Definition: ParamTypes.hh:210
virtual ~ParamTypeVecInt()
Definition: ParamTypes.hh:299
virtual std::string GetValueString()
Definition: ParamTypes.hh:324
virtual std::string GetTypeName()
Definition: ParamTypes.hh:236
virtual ~ParamTypeString()
Definition: ParamTypes.hh:226
BIAS::Vector< double > default_
Definition: ParamTypes.hh:255
BIAS::Vector< double > value_
Definition: ParamTypes.hh:254
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:151
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:305
void SetDefault()
resets to default value
Definition: ParamTypes.hh:150
virtual std::string GetHint()
Definition: ParamTypes.hh:212
virtual std::string GetValueString()
Definition: ParamTypes.hh:68
virtual std::string GetTypeName()
Definition: ParamTypes.hh:209
void SetDefault()
resets to default value
Definition: ParamTypes.hh:257
void SetDefault()
resets to default value
Definition: ParamTypes.hh:107
virtual std::string GetTypeName()
Definition: ParamTypes.hh:277
bool Hidden_
hide this parameter from all users
Definition: ParamTypes.hh:77
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:196
void SetDefault()
resets to default value
Definition: ParamTypes.hh:304
void SetDefault()
resets to default value
Definition: ParamTypes.hh:231
static std::string LowerCase(const std::string &input)
Return lower case string of input.
void SetDefault()
resets to default value
Definition: ParamTypes.hh:195
virtual int SetFromString(const std::string &s)
This is a common interface to let a specialized instance read its data from a string.
Definition: ParamTypes.hh:109
virtual ~ParamTypeDouble()
Definition: ParamTypes.hh:188
virtual std::string GetTypeName()
Definition: ParamTypes.hh:67
virtual std::string GetTypeName()
Definition: ParamTypes.hh:164