[go: up one dir, main page]

Menu

[r999]: / mia2 / mia / core / parameter.hh  Maximize  Restore  History

Download this file

340 lines (286 with data), 9.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- mona-c++ -*-
*
* Copyright (c) Leipzig, Madrid 2004 - 2009
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PUcRPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef ph_parameters_hh
#define ph_parameters_hh
#include <string>
#include <map>
#include <ostream>
#include <sstream>
#include <mia/core/dictmap.hh>
#include <mia/core/msgstream.hh>
NS_MIA_BEGIN
/**
The base class for parameters that might be set based on strings.
The main use-case is command line parameters for plug-ins.
*/
class EXPORT_CORE CParameter {
public:
/**
Base parameter type constructor
\param type a type description string that will show up in the help
\param required set to true if the parameter has to be set by the user
\param descr a short description of the parameter
*/
CParameter(const char type[], bool required, const char *descr);
/**
ensure virtual destructor and avoid warnings because we have a virtual function
*/
virtual ~CParameter();
/**
\returns the type string of the parameter
*/
const char *type() const;
/**
\returns the description string of the parameter
*/
void descr(std::ostream& os) const;
/**
\returns wheather the required flag is (still) set
*/
bool required_set() const;
/**
set the parameter based on the string value
*/
bool set(const std::string& str_value);
protected:
/** the actual (abstract) function to write the description to a stream
that needs to be overwritten
\param os
*/
virtual void do_descr(std::ostream& os) const = 0;
private:
/** the actual (abstract) function to set the parameter that needs to be overwritten
\param str_value the parameter value as string
*/
virtual bool do_set(const std::string& str_value) = 0;
bool _M_required;
const char *_M_type;
const char *_M_descr;
};
/**
The (templated) typed parameter. There needs to be defined an
\a operator << (istream& is, T& x) for the \a do_set method to work.
*/
template <typename T, const char * const type>
class EXPORT_CORE CTParameter : public CParameter {
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
CTParameter(T& value, bool required, const char *descr);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
virtual void adjust(T& value);
T& _M_value;
};
/**
A scalar parameter that supports a bracketing range. If the user tries to set the parameter
to a value outside the range, the set method will throw an \a invalid_argument exception
*/
template <typename T, const char * const type>
class EXPORT_CORE TRangeParameter : public CTParameter<T, type> {
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param min minimum of the parameter value range
\param max maximum of the parameter value range
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
TRangeParameter(T& value, T min, T max, bool required, const char *descr);
protected:
/**
the implementation of the description-function
*/
void do_descr(std::ostream& os) const;
private:
virtual void adjust(T& value);
T _M_min;
T _M_max;
};
template <typename T>
class CDictParameter : public CParameter{
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param dict dictionary for parameter translation
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
CDictParameter(T& value, const TDictMap<T> dict, const char *descr);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
T& _M_value;
const TDictMap<T> _M_dict;
};
template <typename T>
class CSetParameter : public CParameter{
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param dict dictionary for parameter translation
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
CSetParameter(T& value, const std::set<T> valid_set, const char *descr);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
T& _M_value;
const std::set<T> _M_valid_set;
};
template <typename T>
class TParameter : public CParameter{
public:
/** Constructor
\param value reference to the parameter handled by this parameter object
\param dict dictionary for parameter translation
\param required set to \a true if the parameter has to be set by the user
\param descr a description of the parameter
*/
TParameter(T& value, bool required, const char *descr);
protected:
/**
the implementation of the description-function
*/
virtual void do_descr(std::ostream& os) const;
private:
virtual bool do_set(const std::string& str_value);
T& _M_value;
};
extern const char type_str_int[4];
extern const char type_str_float[6];
extern const char type_str_string[7];
extern const char type_str_bool[5];
/// an integer parameter (with range)
typedef TRangeParameter<int, type_str_int> CIntParameter;
/// a float parameter (with range)
typedef TRangeParameter<float, type_str_float> CFloatParameter;
/// an string parameter
typedef CTParameter<std::string,type_str_string> CStringParameter;
/// boolean parameter
typedef CTParameter<bool, type_str_bool> CBoolParameter;
template <typename T>
CDictParameter<T>::CDictParameter(T& value, const TDictMap<T> dict, const char *descr):
CParameter("dict", false, descr),
_M_value(value),
_M_dict(dict)
{
}
template <typename T>
void CDictParameter<T>::do_descr(std::ostream& os) const
{
const std::set<std::string> names = _M_dict.get_name_set();
os << '(';
std::set<std::string>::const_iterator i = names.begin();
std::set<std::string>::const_iterator e = names.end();
if ( i == e ) {
throw std::invalid_argument("Dictmap without entries");
}
os << *i;
++i;
while (i != e)
os << '|' << *i++;
os << ')';
}
template <typename T>
bool CDictParameter<T>::do_set(const std::string& str_value)
{
try {
_M_value = _M_dict.get_value(str_value.c_str());
}
catch (std::invalid_argument& x) {
cverr() << "CDictParameter<T>::do_set: " << x.what() << "\n";
return false;
}
return true;
}
template <typename T>
CSetParameter<T>::CSetParameter(T& value, const std::set<T> valid_set, const char *descr):
CParameter("set", false, descr),
_M_value(value),
_M_valid_set(valid_set)
{
if (_M_valid_set.empty())
throw std::invalid_argument("CDictParameter initialized with empty set");
}
template <typename T>
void CSetParameter<T>::do_descr(std::ostream& os) const
{
typename std::set<T>::const_iterator i = _M_valid_set.begin();
typename std::set<T>::const_iterator e = _M_valid_set.end();
assert ( i != e );
os << *i;
++i;
while (i != e)
os << '|' << *i++;
os << ')';
}
template <typename T>
bool CSetParameter<T>::do_set(const std::string& str_value)
{
std::stringstream s(str_value);
T val;
s >> val;
if (_M_valid_set.find(val) == _M_valid_set.end()) {
return false;
}
_M_value = val;
return true;
}
template <typename T>
TParameter<T>::TParameter(T& value, bool required, const char *descr):
CParameter("streamable", required, descr),
_M_value(value)
{
}
template <typename T>
void TParameter<T>::do_descr(std::ostream& os) const
{
os << _M_value;
}
template <typename T>
bool TParameter<T>::do_set(const std::string& str_value)
{
std::stringstream s(str_value);
s >> _M_value;
return !s.fail();
}
NS_MIA_END
#endif