00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef eoParam_h
00028 #define eoParam_h
00029
00030 #include <cmath>
00031 #include <iterator>
00032 #include <stdexcept>
00033 #include <sstream>
00034 #include <string>
00035 #include <vector>
00036 #include <eoScalarFitness.h>
00037
00038
00042 class eoParam
00043 {
00044 public:
00045
00047 eoParam ()
00048 : repLongName(""), repDefault(""), repDescription(""),
00049 repShortHand(0), repRequired(false)
00050 {}
00051
00060 eoParam (std::string _longName, std::string _default,
00061 std::string _description, char _shortName = 0, bool _required = false)
00062 : repLongName(_longName), repDefault(_default),
00063 repDescription(_description ),
00064 repShortHand(_shortName), repRequired( _required)
00065 {}
00066
00070 virtual ~eoParam () {};
00071
00075 virtual std::string getValue () const = 0;
00076
00080 virtual void setValue(const std::string& _value) = 0 ;
00081
00085 char shortName() const { return repShortHand; };
00086
00090 const std::string& longName() const { return repLongName; };
00091
00095 const std::string& description() const { return repDescription; };
00096
00100 const std::string& defValue() const { return repDefault; };
00101
00105 void defValue( const std::string& str ) { repDefault = str; };
00106
00110 void setLongName(std::string _longName) { repLongName = _longName;}
00111
00115 bool required() const { return repRequired; };
00116
00117 private:
00118 std::string repLongName;
00119 std::string repDefault;
00120 std::string repDescription;
00121 char repShortHand;
00122 bool repRequired;
00123 };
00124
00125
00126
00135 template <class ValueType>
00136 class eoValueParam : public eoParam
00137 {
00138 public :
00139
00141 eoValueParam(void) : eoParam() {}
00142
00151 eoValueParam(ValueType _defaultValue,
00152 std::string _longName,
00153 std::string _description = "No description",
00154 char _shortHand = 0,
00155 bool _required = false)
00156 : eoParam(_longName, "", _description, _shortHand, _required),
00157 repValue(_defaultValue)
00158 {
00159 eoParam::defValue(getValue());
00160 }
00161
00166 ValueType& value()
00167 { return repValue; }
00168
00175 const ValueType& value() const
00176 { return repValue; }
00177
00178
00179 std::string getValue(void) const
00180 {
00181 std::ostringstream os;
00182 os << repValue;
00183 return os.str();
00184 }
00185
00186
00200 void setValue(const std::string& _value)
00201 {
00202 std::istringstream is(_value);
00203 is >> repValue;
00204 }
00205
00206 protected:
00207
00208 ValueType repValue;
00209 };
00210
00211
00212
00213
00214 template <>
00215 inline std::string eoValueParam<std::string>::getValue() const
00216 {
00217 return repValue;
00218 }
00219
00220
00221 template <>
00222 inline void eoValueParam<bool>::setValue(const std::string& _value)
00223 {
00224 if (_value.empty())
00225 {
00226 repValue = true;
00227 return;
00228 }
00229 std::istringstream is(_value);
00230 is >> repValue;
00231 }
00232
00233
00235 template <>
00236 inline std::string eoValueParam<std::pair<double, double> >::getValue(void) const
00237 {
00238
00239 std::ostringstream os;
00240 os << repValue.first << ' ' << repValue.second;
00241 return os.str();
00242 }
00243
00245 template <>
00246 inline void eoValueParam<std::pair<double, double> >::setValue(const std::string& _value)
00247 {
00248 std::istringstream is(_value);
00249 is >> repValue.first;
00250 is >> repValue.second;
00251 }
00252
00253
00256 template <>
00257 inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(void) const
00258 {
00259 std::ostringstream os;
00260 os << repValue.size() << ' ';
00261 for (unsigned i = 0; i < repValue.size(); ++i)
00262 {
00263 os << repValue[i].size() << ' ';
00264 std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
00265 }
00266 return os.str();
00267 }
00268
00270 template <>
00271 inline void eoValueParam<std::vector<std::vector<double> > >::setValue(const std::string& _value)
00272 {
00273 std::istringstream is(_value);
00274 unsigned i,j,sz;
00275 is >> sz;
00276 repValue.resize(sz);
00277
00278 for (i = 0; i < repValue.size(); ++i)
00279 {
00280 unsigned sz2;
00281 is >> sz2;
00282 repValue[i].resize(sz2);
00283 for (j = 0; j < sz2; ++j)
00284 {
00285 is >> repValue[i][j];
00286 }
00287 }
00288 }
00289
00290
00293 template <>
00294 inline std::string eoValueParam<std::vector<double> >::getValue(void) const
00295 {
00296 std::ostringstream os;
00297 os << repValue.size() << ' ';
00298 std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " "));
00299 return os.str();
00300 }
00301
00303 template <>
00304 inline void eoValueParam<std::vector<double> >::setValue(const std::string& _value)
00305 {
00306 static const std::string delimiter(",;");
00307 std::istringstream is(_value);
00308 unsigned sz;
00309 is >> sz;
00310 repValue.resize(sz);
00311 for(unsigned i=0; i<repValue.size(); ++i) {
00312 char c;
00313 do {
00314 is >> c;
00315 } while((std::string::npos != delimiter.find(c)) && (! is.eof()));
00316 is >> repValue[i];
00317 }
00318 }
00319
00320
00323 template <>
00324 inline std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(void) const
00325 {
00326 std::ostringstream os;
00327 os << repValue.size() << ' ';
00328 std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<eoMinimizingFitness>(os, " "));
00329 return os.str();
00330 }
00331
00333
00334 template <>
00335 inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(const std::string& _value)
00336 {
00337 std::istringstream is(_value);
00338 unsigned sz;
00339 is >> sz;
00340 repValue.resize(sz);
00341 std::copy(std::istream_iterator<eoMinimizingFitness>(is), std::istream_iterator<eoMinimizingFitness>(), repValue.begin());
00342 }
00343
00344
00346 template <>
00347 inline std::string eoValueParam<std::vector<void*> >::getValue(void) const
00348 {
00349 throw std::runtime_error("I cannot getValue for a std::vector<EOT*>");
00350 return std::string("");
00351 }
00352
00353 template <>
00354 inline void eoValueParam<std::vector<void*> >::setValue(const std::string&)
00355 {
00356 throw std::runtime_error("I cannot setValue for a std::vector<EOT*>");
00357 return;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00393 class eoParamParamType : public std::pair<std::string,std::vector<std::string> >
00394 {
00395 public:
00396 eoParamParamType(std::string _value)
00397 {
00398 readFrom(_value);
00399 }
00400
00401 std::ostream & printOn(std::ostream & _os) const
00402 {
00403 _os << first;
00404 unsigned narg = second.size();
00405 if (!narg)
00406 return _os;
00407
00408
00409 _os << "(";
00410 if (narg == 1)
00411 {
00412 _os << second[0] << ")" ;
00413 return _os;
00414 }
00415
00416 for (unsigned i=0; i<narg-1; i++)
00417 _os << second[i] << "," ;
00418 _os << second[narg-1] << ")";
00419 return _os;
00420 }
00421
00422 std::istream & readFrom(std::istream & _is)
00423 {
00424 std::string value;
00425 _is >> value;
00426 readFrom(value);
00427 return _is;
00428 }
00429
00430 void readFrom(std::string & _value)
00431 {
00432 second.resize(0);
00433 size_t pos = _value.find('(');
00434 if (pos >= _value.size())
00435 {
00436 first = _value;
00437 return;
00438 }
00439
00440 std::string t = _value.substr(pos+1);
00441 _value.resize(pos);
00442 first = _value;
00443
00444
00445 std::string delim(" (),");
00446 while ( (pos=t.find_first_not_of(delim)) < t.size())
00447 {
00448 size_t posEnd = t.find_first_of(delim, pos);
00449 std::string u = t.substr(pos,posEnd);
00450
00451 second.push_back(u);
00452 t = t.substr(posEnd+1);
00453 }
00454 }
00455 };
00456
00457
00458 std::ostream & operator<<(std::ostream & _os, const eoParamParamType & _rate);
00459 std::istream & operator>>(std::istream & _is, eoParamParamType & _rate);
00460
00461 #endif