// @(#)root/mathcore:$Id$ // Author: L. Moneta Nov 2010 /********************************************************************** * * * Copyright (c) 2010 LCG ROOT Math Team, CERN/PH-SFT * * * * * **********************************************************************/ #ifndef ROOT_Math_GenAlgoOptions #define ROOT_Math_GenAlgoOptions #include "Math/IOptions.h" #include #include #include #include namespace ROOT { namespace Math { //_______________________________________________________________________________ /** class implementing generic options for a numerical algorithm Just store the options in a map of string-value pairs @ingroup NumAlgo */ class GenAlgoOptions : public IOptions { public: GenAlgoOptions() /* : fExtraOptions(0) */ {} ~GenAlgoOptions() override {}// { if (fExtraOptions) delete fExtraOptions; } // use default copy constructor and assignment operator /** generic methods for retrieving options */ // methods implementing the IOptions interface IOptions * Clone() const override { return new GenAlgoOptions(*this); } // t.b.d need probably to implement in a .cxx file for CINT bool GetRealValue(const char * name, double & val) const override { const double * pval = FindValue(name, fRealOpts); if (!pval) return false; val = *pval; return true; } bool GetIntValue(const char * name, int & val) const override { const int * pval = FindValue(name, fIntOpts); if (!pval) return false; val = *pval; return true; } bool GetNamedValue(const char * name, std::string & val) const override { const std::string * pval = FindValue(name, fNamOpts); if (!pval) return false; val = *pval; return true; } /// Method that needs to be re-implemented by the derived classes. void SetRealValue(const char * name, double val) override { InsertValue(name, fRealOpts, val); } void SetIntValue(const char * name , int val) override { InsertValue(name, fIntOpts, val); } void SetNamedValue(const char * name, const char * val) override { InsertValue(name, fNamOpts, std::string(val)); } std::vector GetAllNamedKeys() { std::vector names; names.reserve(fNamOpts.size()); // start by named options for (auto const & e : fNamOpts) names.push_back(e.first); return names; } std::vector GetAllRealKeys() { std::vector names; names.reserve(fRealOpts.size()); // start by named options for (auto const & e : fRealOpts) names.push_back(e.first); return names; } std::vector GetAllIntKeys() { std::vector names; names.reserve(fIntOpts.size()); // start by named options for (auto const & e : fIntOpts) names.push_back(e.first); return names; } /// print options void Print(std::ostream & os = std::cout ) const override { Print(fNamOpts,os); Print(fIntOpts,os); Print(fRealOpts,os); } // static methods to retrieve the default options // find the option given a name // return 0 if the option is not found static IOptions * FindDefault(const char * algoname); // retrieve options given the name // if option is not found create a new GenAlgoOption for the given name static IOptions & Default(const char * algoname); /// print all the default options static void PrintAllDefault(std::ostream & os = std::cout); protected: private: template static const typename M::mapped_type * FindValue(const std::string & name, const M & opts) { typename M::const_iterator pos; pos = opts.find(name); if (pos == opts.end()) { return nullptr; } return &((*pos).second); } template static void InsertValue(const std::string &name, M & opts, const typename M::mapped_type & value) { typename M::iterator pos; pos = opts.find(name); if (pos != opts.end()) { pos->second = value; } else { opts.insert(typename M::value_type(name, value) ); } } template static void Print( const M & opts, std::ostream & os) { //const std::ios_base::fmtflags prevFmt = os.flags(); for (typename M::const_iterator pos = opts.begin(); pos != opts.end(); ++pos) os << std::setw(25) << pos->first << " : " << std::setw(15) << pos->second << std::endl; } std::map fIntOpts; ///< map of the integer options std::map fRealOpts; ///< map of the real options std::map fNamOpts; ///< map of the named options }; } // end namespace Math } // end namespace ROOT #endif