eoParam.h

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 //-----------------------------------------------------------------------------
00004 // eoParam.h
00005 // (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
00006 /*
00007   This library is free software; you can redistribute it and/or
00008   modify it under the terms of the GNU Lesser General Public
00009   License as published by the Free Software Foundation; either
00010   version 2 of the License, or (at your option) any later version.
00011 
00012   This library is distributed in the hope that it will be useful,
00013   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   Lesser General Public License for more details.
00016 
00017   You should have received a copy of the GNU Lesser General Public
00018   License along with this library; if not, write to the Free Software
00019   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021   Contact: todos@geneura.ugr.es, http://geneura.ugr.es
00022   Marc.Schoenauer@polytechnique.fr
00023   mkeijzer@dhi.dk
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 
00187     void setValue(const std::string& _value)
00188         {
00189             std::istringstream is(_value);
00190             is >> repValue;
00191         }
00192 
00193 protected:
00194 
00195     ValueType repValue;
00196 };
00197 
00198 /*
00199   Specialization for std::string
00200 */
00201 template <>
00202 inline std::string eoValueParam<std::string>::getValue() const
00203 {
00204     return repValue;
00205 }
00206 
00207 
00208 template <>
00209 inline void eoValueParam<bool>::setValue(const std::string& _value)
00210 {
00211     if (_value.empty())
00212     {
00213         repValue = true;
00214         return;
00215     }
00216     std::istringstream is(_value);
00217     is >> repValue;
00218 }
00219 
00220 
00222 template <>
00223 inline std::string eoValueParam<std::pair<double, double> >::getValue(void) const
00224 {
00225     // use own buffer as MSVC's buffer leaks!
00226     std::ostringstream os;
00227     os << repValue.first << ' ' << repValue.second;
00228     return os.str();
00229 }
00230 
00232 template <>
00233 inline void eoValueParam<std::pair<double, double> >::setValue(const std::string& _value)
00234 {
00235     std::istringstream is(_value);
00236     is >> repValue.first;
00237     is >> repValue.second;
00238 }
00239 
00240 // The std::vector<std::vector<double> >
00243 template <>
00244 inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(void) const
00245 {
00246     std::ostringstream os;
00247     os << repValue.size() << ' ';
00248     for (unsigned i = 0; i < repValue.size(); ++i)
00249     {
00250         os << repValue[i].size() << ' ';
00251         std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
00252     }
00253     return os.str();
00254 }
00255 
00257 template <>
00258 inline void eoValueParam<std::vector<std::vector<double> > >::setValue(const std::string& _value)
00259 {
00260     std::istringstream is(_value);
00261     unsigned i,j,sz;
00262     is >> sz;
00263     repValue.resize(sz);
00264 
00265     for (i = 0; i < repValue.size(); ++i)
00266     {
00267         unsigned sz2;
00268         is >> sz2;
00269         repValue[i].resize(sz2);
00270         for (j = 0; j < sz2; ++j)
00271         {
00272             is >> repValue[i][j];
00273         }
00274     }
00275 }
00276 
00277 // The std::vector<double>
00280 template <>
00281 inline std::string eoValueParam<std::vector<double> >::getValue(void) const
00282 {
00283     std::ostringstream os;
00284     os << repValue.size() << ' ';
00285     std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " "));
00286     return os.str();
00287 }
00288 
00290 template <>
00291 inline void eoValueParam<std::vector<double> >::setValue(const std::string& _value)
00292 {
00293     std::istringstream is(_value);
00294     unsigned sz;
00295     is >> sz;
00296     repValue.resize(sz);
00297     std::copy(std::istream_iterator<double>(is), std::istream_iterator<double>(), repValue.begin());
00298 }
00299 
00300 // The std::vector<eoMinimizingFitness>
00303 template <>
00304 inline std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(void) const
00305 {
00306     std::ostringstream os;
00307     os << repValue.size() << ' ';
00308     std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<eoMinimizingFitness>(os, " "));
00309     return os.str();
00310 }
00311 
00313 // NOTE: g++ doesn support it either!!!
00314 template <>
00315 inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(const std::string& _value)
00316 {
00317     std::istringstream is(_value);
00318     unsigned sz;
00319     is >> sz;
00320     repValue.resize(sz);
00321     std::copy(std::istream_iterator<eoMinimizingFitness>(is), std::istream_iterator<eoMinimizingFitness>(), repValue.begin());
00322 }
00323 
00324 // The std::vector<const EOT*>
00326 template <>
00327 inline std::string eoValueParam<std::vector<void*> >::getValue(void) const
00328 {
00329     throw std::runtime_error("I cannot getValue for a std::vector<EOT*>");
00330     return std::string("");
00331 }
00332 
00333 template <>
00334 inline void eoValueParam<std::vector<void*> >::setValue(const std::string&)
00335 {
00336     throw std::runtime_error("I cannot setValue for a std::vector<EOT*>");
00337     return;
00338 }
00339 
00340 /*template <class ContainerType>
00341   class eoContainerParam : public eoParam
00342   {
00343   public :
00344   eoContainerParam (ContainerType& value, std::string _shortName, std::string _longName,
00345   std::string _default,
00346   std::string _description,
00347   bool _required,
00348   bool _change )
00349   : value(_value), eoParam(_shortName, _longName, _description, _default, _required, _change)
00350   {}
00351 
00352 
00353   //  void setValue(const std::string & _value)
00354   // {
00355   //     std::istd::stringstream is(_value);
00356   //    copy(std::istream_iterator<Container::value_type>(is), std::istream_iterator<Container::value_type>(), back_inserter(value));
00357   // }
00358 
00359   private :
00360   ContainerType& value;
00361   };*/
00362 
00373 class eoParamParamType : public std::pair<std::string,std::vector<std::string> >
00374 {
00375 public:
00376     eoParamParamType(std::string _value)
00377         {
00378             readFrom(_value);
00379         }
00380 
00381     std::ostream & printOn(std::ostream & _os) const
00382         {
00383             _os << first;
00384             unsigned narg = second.size();
00385             if (!narg)
00386                 return _os;
00387 
00388             // Here, we do have args
00389             _os << "(";
00390             if (narg == 1)         // 1 arg only
00391             {
00392                 _os << second[0] << ")" ;
00393                 return _os;
00394             }
00395             // and here more than 1 arg
00396             for (unsigned i=0; i<narg-1; i++)
00397                 _os << second[i] << "," ;
00398             _os << second[narg-1] << ")";
00399             return _os;
00400         }
00401 
00402     std::istream & readFrom(std::istream & _is)
00403         {
00404             std::string value;
00405             _is >> value;
00406             readFrom(value);
00407             return _is;
00408         }
00409 
00410     void readFrom(std::string &  _value)
00411         {
00412             second.resize(0);              // just in case
00413             size_t pos = _value.find('(');
00414             if (pos >= _value.size())      // no arguments
00415             {
00416                 first = _value;
00417                 return;
00418             }
00419             // so here we do have arguments
00420             std::string t = _value.substr(pos+1);// the arguments
00421             _value.resize(pos);
00422             first = _value;    // done for the keyword (NOTE: may be empty std::string!)
00423 
00424             // now all arguments
00425             std::string delim(" (),");
00426             while ( (pos=t.find_first_not_of(delim)) < t.size())
00427             {
00428                 size_t posEnd = t.find_first_of(delim, pos);
00429                 std::string u = t.substr(pos,posEnd);//(t, pos);
00430                 /*u.resize(posEnd - pos);*/
00431                 second.push_back(u);
00432                 t = t.substr(posEnd+1);
00433             }
00434         }
00435 };
00436 
00437 // at the moment, the following are defined in eoParser.cpp
00438 std::ostream & operator<<(std::ostream & _os, const eoParamParamType & _rate);
00439 std::istream & operator>>(std::istream & _is,  eoParamParamType & _rate);
00440 
00441 #endif

Generated on Thu Oct 19 05:06:36 2006 for EO by  doxygen 1.3.9.1