ROOT  6.06/09
Reference Guide
GenAlgoOptions.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Nov 2010
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2010 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 #ifndef ROOT_Math_GenAlgoOptions
12 #define ROOT_Math_GenAlgoOptions
13 
14 
15 #ifndef ROOT_Math_IOptions
16 #include "Math/IOptions.h"
17 #endif
18 
19 #include <map>
20 #include <iomanip>
21 
22 namespace ROOT {
23  namespace Math {
24 
25 //_______________________________________________________________________________
26 /**
27  class implementing generic options for a numerical algorithm
28  Just store the options in a map of string-value pairs
29 
30  @ingroup NumAlgo
31 */
32 class GenAlgoOptions : public IOptions {
33 
34 public:
35 
36  GenAlgoOptions() /* : fExtraOptions(0) */ {}
37 
38  virtual ~GenAlgoOptions() {}// { if (fExtraOptions) delete fExtraOptions; }
39 
40  // use default copy constructor and assignment operator
41 
42  /** generic methods for retrivieng options */
43 
44 
45  // methods implementing the IOptions interface
46 
47  virtual IOptions * Clone() const {
48  return new GenAlgoOptions(*this);
49  }
50 
51  // t.b.d need probably to implement in a .cxx file for CINT
52 
53 
54  virtual bool GetRealValue(const char * name, double & val) const {
55  const double * pval = FindValue(name, fRealOpts);
56  if (!pval) return false;
57  val = *pval;
58  return true;
59  }
60 
61  virtual bool GetIntValue(const char * name, int & val) const {
62  const int * pval = FindValue(name, fIntOpts);
63  if (!pval) return false;
64  val = *pval;
65  return true;
66  }
67 
68  virtual bool GetNamedValue(const char * name, std::string & val) const {
69  const std::string * pval = FindValue(name, fNamOpts);
70  if (!pval) return false;
71  val = *pval;
72  return true;
73  }
74 
75  /// method wich need to be re-implemented by the derived classes
76  virtual void SetRealValue(const char * name, double val) {
77  InsertValue(name, fRealOpts, val);
78  }
79 
80  virtual void SetIntValue(const char * name , int val) {
81  InsertValue(name, fIntOpts, val);
82  }
83 
84  virtual void SetNamedValue(const char * name, const char * val) {
85  InsertValue(name, fNamOpts, std::string(val));
86  }
87 
88 
89  /// print options
90  virtual void Print(std::ostream & os = std::cout ) const {
91  Print(fNamOpts,os);
92  Print(fIntOpts,os);
93  Print(fRealOpts,os);
94  }
95 
96 
97  // static methods to retrieve the default options
98 
99  // find the option given a name
100  // return 0 if the option is not found
101  static IOptions * FindDefault(const char * algoname);
102 
103  // retrieve options given the name
104  // if option is not found create a new GenAlgoOption for the given name
105  static IOptions & Default(const char * algoname);
106 
107  /// print all the default options
108  static void PrintAllDefault(std::ostream & os = std::cout);
109 
110 
111 protected:
112 
113 
114 
115 private:
116 
117  template<class M>
118  static const typename M::mapped_type * FindValue(const std::string & name, const M & opts) {
119  typename M::const_iterator pos;
120  pos = opts.find(name);
121  if (pos == opts.end()) {
122  return 0;
123  }
124  return &((*pos).second);
125  }
126 
127  template<class M>
128  static void InsertValue(const std::string &name, M & opts, const typename M::mapped_type & value) {
129  typename M::iterator pos;
130  pos = opts.find(name);
131  if (pos != opts.end()) {
132  pos->second = value;
133  }
134  else {
135  opts.insert(typename M::value_type(name, value) );
136  }
137  }
138 
139  template<class M>
140  static void Print( const M & opts, std::ostream & os) {
141  //const std::ios_base::fmtflags prevFmt = os.flags();
142  for (typename M::const_iterator pos = opts.begin(); pos != opts.end(); ++pos)
143  os << std::setw(25) << pos->first << " : " << std::setw(15) << pos->second << std::endl;
144  }
145 
146 
147  std::map<std::string, double> fRealOpts; // map of the real options
148  std::map<std::string, int> fIntOpts; // map of the integer options
149  std::map<std::string, std::string> fNamOpts; // map of the named options
150 
151 };
152 
153 
154 
155  } // end namespace Math
156 
157 } // end namespace ROOT
158 
159 #endif
virtual bool GetNamedValue(const char *name, std::string &val) const
std::map< std::string, std::string > fNamOpts
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
virtual bool GetRealValue(const char *name, double &val) const
virtual void Print(std::ostream &os=std::cout) const
print options
static const M::mapped_type * FindValue(const std::string &name, const M &opts)
virtual void SetNamedValue(const char *name, const char *val)
static void PrintAllDefault(std::ostream &os=std::cout)
print all the default options
virtual void SetRealValue(const char *name, double val)
method wich need to be re-implemented by the derived classes
std::map< std::string, double > fRealOpts
class implementing generic options for a numerical algorithm Just store the options in a map of strin...
static void Print(const M &opts, std::ostream &os)
static void InsertValue(const std::string &name, M &opts, const typename M::mapped_type &value)
virtual IOptions * Clone() const
generic methods for retrivieng options
Namespace for new Math classes and functions.
#define name(a, b)
Definition: linkTestLib0.cpp:5
static IOptions & Default(const char *algoname)
Generic interface for defining configuration options of a numerical algorithm.
Definition: IOptions.h:32
virtual bool GetIntValue(const char *name, int &val) const
static IOptions * FindDefault(const char *algoname)
std::map< std::string, int > fIntOpts
float value
Definition: math.cpp:443
virtual void SetIntValue(const char *name, int val)