Basic Image AlgorithmS Library 2.8.0

ParamTypes.hh

00001 /*
00002 This file is part of the BIAS library (Basic ImageAlgorithmS).
00003 
00004 Copyright (C) 2003-2009    (see file CONTACT for details)
00005 Multimediale Systeme der Informationsverarbeitung
00006 Institut fuer Informatik
00007 Christian-Albrechts-Universitaet Kiel
00008 
00009 
00010 BIAS is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU Lesser General Public License as published by
00012 the Free Software Foundation; either version 2.1 of the License, or
00013 (at your option) any later version.
00014 
00015 BIAS is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public License
00021 along with BIAS; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 #ifndef __ParamTypes_hh__
00026 #define __ParamTypes_hh__
00027 
00028 #include <Base/Common/BIASpragmaStart.hh>
00029 
00030 #include <Base/Debug/Error.hh>
00031 #include <Base/Math/Vector.hh>
00032 #include <Base/Common/FileHandling.hh> //< for lowercase
00033 
00034 
00035 #include <string>
00036 #include <sstream>
00037 #include <map>
00038 
00039 
00040 namespace BIAS {
00041     // fwd declaratiion
00042     class BIASUtils_EXPORT ParamCallbackInterface;
00043 
00044     /** @class ParamTypeBase
00045      *  @brief This file holds the definitions of the parameter types used by BIAS::Param.
00046      *  @author mdunda 03 2004
00047      *  @ingroup g_utils
00048      */
00049     class BIASUtils_EXPORT ParamTypeBase
00050     {
00051     public:
00052         // destructor
00053         virtual ~ParamTypeBase();
00054 
00055         // constructors
00056         ParamTypeBase();
00057 
00058         virtual void SetDefault(){ /* to be overwritten in derived class */ }
00059 
00060         /**
00061         This is a common interface to let a specialized instance read
00062         its data from a string. Used from ParseCommandLine() and ReadParam()
00063         to be overwritten in derived class
00064         */
00065         virtual int SetFromString(const std::string &s);
00066 
00067         virtual std::string GetTypeName(){return "NOTYPE";};
00068         virtual std::string GetValueString(){return "NOVAL";};
00069         virtual std::string GetHint(){return "NOVAL";};
00070 
00071         // data members / entries
00072         std::string Name_;
00073         std::string Help_;
00074         char        ShortCmd_;
00075         bool        WriteToFile_; ///< is only written to parameter file if true
00076         bool        Enhanced_; ///< hide this parameter from the 'naive' user
00077         bool        Hidden_; ///< hide this parameter from all users
00078         int         Group_;
00079 
00080         /// if some one changed a parameter, we should call
00081         /// CallbackObject_->ParameterChanged(Name_);
00082         ParamCallbackInterface *CallbackObject_;
00083     };
00084 
00085 
00086     // derived parameter types following...
00087 
00088     // =========================
00089     // ===      BOOLEAN      ===
00090     // =========================
00091     class BIASUtils_EXPORT ParamTypeBool : public ParamTypeBase
00092     {
00093     public:
00094         // 'structors
00095         ParamTypeBool()
00096           : ParamTypeBase()
00097         {}
00098 
00099         virtual ~ParamTypeBool()
00100         {}
00101 
00102         // entries
00103         bool value_;
00104         bool default_;
00105 
00106         /// resets to default value
00107         inline void SetDefault(){ value_=default_; }
00108 
00109         virtual int SetFromString(const std::string &s)
00110         {
00111           // be case insensitive, don't construct expression for easier debugging
00112           if ((FileHandling::LowerCase(s) == "true" || FileHandling::LowerCase(s) == "=true"))
00113             value_ = true;
00114           else if ((FileHandling::LowerCase(s) == "on")||(FileHandling::LowerCase(s) == "=on"))
00115             value_ = true;
00116           else if ((s=="1") || (s=="=1"))
00117             value_ = true;
00118           else
00119             value_ = false;
00120 
00121           return 0; //< OK
00122         };
00123 
00124         virtual std::string GetTypeName(){return "BOOL";};
00125         virtual std::string GetValueString(){return value_?"true":"false";};
00126         virtual std::string GetHint(){return "true / false";};
00127     };
00128 
00129 
00130     // =========================
00131     // ===      INTEGER      ===
00132     // =========================
00133     class BIASUtils_EXPORT ParamTypeInt : public ParamTypeBase
00134     {
00135     public:
00136         // 'structors
00137         ParamTypeInt()
00138             : ParamTypeBase()
00139         {}
00140 
00141         virtual ~ParamTypeInt()
00142         {}
00143 
00144         // entries
00145         int value_;
00146         int default_;
00147         int min_;
00148         int max_;
00149         /// resets to default value
00150         inline void SetDefault(){ value_=default_; }
00151         virtual int SetFromString(const std::string &s){
00152             int newval = atoi(s.c_str());
00153             if ( newval < min_ ){
00154                 BIASWARN("Value for '"<<Name_<<"' clipped to "<<min_<<std::endl);
00155                 value_ = min_;
00156             }
00157             else if (newval > max_ ) {
00158                 BIASWARN("Value for '"<<Name_<<"' clipped to "<<max_<<std::endl);
00159                 value_ = max_;
00160             }
00161             else value_ = newval;
00162             return 0;
00163         };
00164         virtual std::string GetTypeName(){return "INT";};
00165 
00166         virtual std::string GetValueString()
00167         {
00168             std::ostringstream oss;oss<<value_; return oss.str();
00169         };
00170 
00171         virtual std::string GetHint()
00172         {
00173             std::ostringstream oss;oss<<"[ "<<min_<<" .. "<<max_<<" ]";
00174             return oss.str();
00175         };
00176 
00177     }; // class ParamTypeInt
00178 
00179 
00180     // =========================
00181     // ===      DOUBLE       ===
00182     // =========================
00183     class BIASUtils_EXPORT ParamTypeDouble : public ParamTypeBase
00184     {
00185     public:
00186         // 'structors
00187         ParamTypeDouble(){}
00188         virtual ~ParamTypeDouble(){}
00189         // entries
00190         double value_;
00191         double default_;
00192         double min_;
00193         double max_;
00194         /// resets to default value
00195         inline void SetDefault(){ value_=default_; }
00196         virtual int SetFromString(const std::string &s){
00197             double newval = atof(s.c_str());
00198             if ( newval < min_ ){
00199                 BIASWARN("Value for '"<<Name_<<"' clipped to "<<min_<<std::endl);
00200                 value_ = min_;
00201             }
00202             else if (newval > max_ ) {
00203                 BIASWARN("Value for '"<<Name_<<"' clipped to "<<max_<<std::endl);
00204                 value_ = max_;
00205             }
00206             else value_ = newval;
00207             return 0;
00208         };
00209         virtual std::string GetTypeName(){return "DOUBLE";};
00210         virtual std::string GetValueString(){
00211             std::ostringstream oss;oss<<value_; return oss.str();};
00212             virtual std::string GetHint(){
00213                 std::ostringstream oss;oss<<"[ "<<min_<<" .. "<<max_<<" ]";
00214                 return oss.str();};
00215     };
00216 
00217 
00218     // =========================
00219     // ===      STRING       ===
00220     // =========================
00221     class BIASUtils_EXPORT ParamTypeString : public ParamTypeBase
00222     {
00223     public:
00224         // 'structors
00225         ParamTypeString(){}
00226         virtual ~ParamTypeString(){}
00227         // entries
00228         std::string value_;
00229         std::string default_;
00230         /// resets to default value
00231         inline void SetDefault(){ value_=default_; }
00232         virtual int SetFromString(const std::string &s){
00233             value_ = s;
00234             return 0;
00235         }
00236         virtual std::string GetTypeName(){return "STRING";};
00237         virtual std::string GetValueString(){return "\""+value_+"\"";};
00238         virtual std::string GetHint(){
00239             std::ostringstream oss;oss<<" def: "<<default_;
00240             return oss.str();};
00241     };
00242 
00243 
00244     // =========================
00245     // === VECTOR OF DOUBLE  ===
00246     // =========================
00247     class BIASUtils_EXPORT ParamTypeVecDbl : public ParamTypeBase
00248     {
00249     public:
00250         // 'structors
00251         ParamTypeVecDbl(){}
00252         virtual ~ParamTypeVecDbl(){}
00253         // entries
00254         BIAS::Vector<double> value_;
00255         BIAS::Vector<double> default_;
00256         /// resets to default value
00257         inline void SetDefault(){ value_=default_; }
00258         virtual int SetFromString(const std::string &s){
00259             std::istringstream iss(s);
00260             double x;
00261             // check for size
00262             int length=-1;
00263             while (iss){
00264                 iss >> x; length++;
00265             }
00266             //std::cerr << Name_ << " has size "<<length<<std::endl;
00267             // resize vector
00268             value_.newsize(length);
00269             // now read it
00270             std::istringstream iss2(s);
00271             for(unsigned int i=0; i<value_.Size(); i++) {
00272                 iss2 >> x; value_[i] = x;
00273                 //std::cerr << x << "   ";
00274             }
00275             return 0;
00276         };
00277         virtual std::string GetTypeName(){return "VECDBL";};
00278         virtual std::string GetValueString(){
00279             std::ostringstream oss; oss<<"\"";
00280             for (int i=0;i<value_.size(); i++) {
00281                 oss<<value_[i];
00282                 if (i<value_.size()-1) oss <<" ";
00283             }
00284             oss<<"\"";
00285             return oss.str();
00286         };
00287 
00288     };
00289 
00290 
00291     // =========================
00292     // === VECTOR OF INTEGER ===
00293     // =========================
00294     class BIASUtils_EXPORT ParamTypeVecInt : public ParamTypeBase
00295     {
00296     public:
00297         // 'structors
00298         ParamTypeVecInt(){}
00299         virtual ~ParamTypeVecInt(){}
00300         // entries
00301         BIAS::Vector<int> value_;
00302         BIAS::Vector<int> default_;
00303         /// resets to default value
00304         inline void SetDefault(){ value_=default_; }
00305         virtual int SetFromString(const std::string &s){
00306             std::istringstream iss(s);
00307             int x;
00308             // check for size
00309             int length=-1;
00310             while (iss){
00311                 iss >> x; length++;
00312             }
00313             //std::cerr << Name_ << " has size "<<length<<std::endl;
00314             // resize vector
00315             value_.newsize(length);
00316             // now read it
00317             std::istringstream iss2(s);
00318             for(unsigned int i=0; i<value_.Size(); i++) {
00319                 iss2 >> x; value_[i] = x;
00320             }
00321             return 0;
00322         };
00323         virtual std::string GetTypeName(){return "VECINT";};
00324         virtual std::string GetValueString(){
00325             std::ostringstream oss; oss<<"\"";
00326             for (int i=0;i<value_.size(); i++) {
00327                 oss<<value_[i];
00328                 if (i<value_.size()-1) oss <<" ";
00329             }
00330             oss<<"\"";
00331             return oss.str();
00332         };
00333 
00334     };
00335 
00336 
00337     // =========================
00338     // ===       ENUM        ===
00339     // =========================
00340     class BIASUtils_EXPORT ParamTypeEnum : public ParamTypeBase
00341     {
00342     public:
00343         // 'structors
00344         ParamTypeEnum(){}
00345         virtual ~ParamTypeEnum(){}
00346         // entries
00347         int value_;
00348         int default_;
00349         std::map<std::string,int> Map_;
00350         /// resets to default value
00351         inline void SetDefault(){ value_=default_; }
00352         virtual int SetFromString(const std::string &s){
00353             std::map<std::string,int>::iterator it;
00354             if ( (it=Map_.find(s)) ==Map_.end() ) {
00355                 BIASERR("Identifier for '" <<Name_<< "' is not valid. Using default.")
00356                     value_ = default_;
00357             }
00358             else value_ = it->second;
00359             return 0;
00360         }
00361         virtual std::string GetTypeName(){return "ENUM";};
00362         virtual std::string GetValueString(){
00363             std::ostringstream oss;
00364             std::map<std::string,int>::iterator it;
00365             for (it=Map_.begin();it!=Map_.end(); it++)
00366                 if ( it->second == value_ ) oss<<it->first;
00367             return oss.str();};
00368 
00369             virtual std::string GetHint(){
00370                 std::ostringstream oss;
00371                 std::map<std::string,int>::iterator it;
00372                 for (it=Map_.begin();it!=Map_.end(); it++){
00373                     if (it!=Map_.begin()) oss<<", ";
00374                     oss<<it->first;
00375                 }
00376                 return oss.str();};
00377     };
00378 } // namespace BIAS
00379 
00380 
00381 #include <Base/Common/BIASpragmaEnd.hh>
00382 #endif /* __ParamTypes_hh__ */
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends