GenAlgoOptions.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: GenAlgoOptions.h 36796 2010-11-19 17:33:27Z moneta $ 
00002 // Author: L. Moneta Nov 2010
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2010  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 #ifndef ROOT_Math_GenAlgoOptions
00012 #define ROOT_Math_GenAlgoOptions
00013 
00014 
00015 #ifndef ROOT_Math_IOptions
00016 #include "Math/IOptions.h"
00017 #endif
00018 
00019 #include <map>
00020 #include <iomanip>
00021 
00022 namespace ROOT { 
00023       namespace Math { 
00024 
00025 //_______________________________________________________________________________
00026 /** 
00027     class implementing generic options for a numerical algorithm
00028     Just store the otions in a maps of string-value pair 
00029 
00030     @ingroup NumAlgo
00031 */
00032 class GenAlgoOptions : public IOptions {
00033 
00034 public:
00035 
00036    GenAlgoOptions() /* : fExtraOptions(0) */  {}
00037 
00038    virtual ~GenAlgoOptions() {}// { if (fExtraOptions) delete fExtraOptions; }
00039 
00040    // use default copy constructor and assignment operator
00041 
00042    /** generic  methods for  retrivieng options */
00043 
00044 
00045    // methods implementing the  IOptions interface
00046 
00047    virtual IOptions * Clone() const { 
00048       return new GenAlgoOptions(*this);
00049    }
00050 
00051    // t.b.d need probably to implement in a .cxx file for CINT 
00052 
00053 
00054    virtual bool GetRealValue(const char * name, double & val) const { 
00055       const double * pval = FindValue(name, fRealOpts);
00056       if (!pval) return false; 
00057       val = *pval;
00058       return true; 
00059    }
00060 
00061    virtual bool GetIntValue(const char * name, int & val) const { 
00062       const int * pval = FindValue(name, fIntOpts);
00063       if (!pval) return false;
00064       val = *pval;
00065       return true; 
00066    }
00067 
00068    virtual bool GetNamedValue(const char * name, std::string & val) const { 
00069       const std::string * pval = FindValue(name, fNamOpts);
00070       if (!pval) return false; 
00071       val = *pval;
00072       return true;
00073    }
00074 
00075    /// method wich need to be re-implemented by the derived classes 
00076    virtual void SetRealValue(const char * name, double val)  {
00077       InsertValue(name, fRealOpts, val);
00078    }
00079 
00080    virtual void SetIntValue(const char * name , int val) {
00081       InsertValue(name, fIntOpts, val);
00082    }
00083 
00084    virtual void SetNamedValue(const char * name, const char * val) {
00085       InsertValue(name, fNamOpts, std::string(val));
00086    }
00087 
00088 
00089    /// print options 
00090    virtual void Print(std::ostream & os = std::cout ) const {
00091       Print(fNamOpts,os);
00092       Print(fIntOpts,os);
00093       Print(fRealOpts,os);
00094    } 
00095 
00096 
00097    // static methods to retrieve the default options 
00098 
00099    // find the option given a name 
00100    // return 0 if the option is not found
00101    static IOptions * FindDefault(const char * algoname);
00102 
00103    // retrieve options given the name 
00104    // if option is not found create a new GenAlgoOption for the given name 
00105    static IOptions & Default(const char * algoname);
00106 
00107    /// print all the default options
00108    static void PrintAllDefault(std::ostream & os = std::cout); 
00109 
00110 
00111 protected: 
00112 
00113    
00114 
00115 private: 
00116    
00117    template<class M> 
00118    static const typename M::mapped_type * FindValue(const std::string &  name, const M & opts) {
00119       typename M::const_iterator pos; 
00120       pos = opts.find(name); 
00121       if (pos == opts.end()) { 
00122          return 0; 
00123       }
00124       return  &((*pos).second); 
00125    }  
00126 
00127    template<class M> 
00128    static void InsertValue(const std::string &name, M & opts, const typename M::mapped_type & value) {
00129       typename M::iterator pos; 
00130       pos = opts.find(name); 
00131       if (pos != opts.end()) { 
00132          pos->second = value; 
00133       }
00134       else { 
00135          opts.insert(typename M::value_type(name, value) );
00136       }
00137    }  
00138 
00139    template<class M> 
00140    static void Print( const M & opts, std::ostream & os) {
00141       //const std::ios_base::fmtflags prevFmt = os.flags(); 
00142       for (typename M::const_iterator pos = opts.begin(); pos != opts.end(); ++pos) 
00143          os << std::setw(25) << pos->first << " : " << std::setw(15) << pos->second << std::endl;
00144    }  
00145 
00146 
00147    std::map<std::string, double>      fRealOpts;   // map of the real options 
00148    std::map<std::string, int>         fIntOpts;    // map of the integer options 
00149    std::map<std::string, std::string> fNamOpts;    // map of the named options
00150     
00151 };
00152 
00153 
00154 
00155    } // end namespace Math
00156 
00157 } // end namespace ROOT
00158 
00159 #endif

Generated on Tue Jul 5 14:25:14 2011 for ROOT_528-00b_version by  doxygen 1.5.1