Logo ROOT   6.10/09
Reference Guide
ParamFunctor.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Mon Nov 13 15:58:13 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for Functor classes.
12 // design is inspired by the Loki Functor
13 
14 #ifndef ROOT_Math_ParamFunctor
15 #define ROOT_Math_ParamFunctor
16 
17 // #ifndef ROOT_Math_IFunction
18 // #include "Math/IFunction.h"
19 // #endif
20 
21 // #ifndef Root_Math_StaticCheck
22 // #include "Math/StaticCheck.h"
23 // #endif
24 
25 //#ifndef __CINT__
26 //#include <memory>
27 
28 #include <vector>
29 #include <iostream>
30 
31 namespace ROOT {
32 
33 namespace Math {
34 
35 
36 /** class defining the signature for multi-dim parametric functions
37 
38  @ingroup ParamFunctor_int
39  */
40 
42  public:
43  virtual ~ParamFunctionBase() {}
44 // virtual double operator() (const double * x, const double *p) const = 0;
45  virtual double operator() (double * x, double *p) = 0;
46  virtual ParamFunctionBase * Clone() const = 0;
47 };
48 
49 
50 
51 /**
52  ParamFunctor Handler class is responsible for wrapping any other functor and pointer to
53  free C functions.
54  It can be created from any function implementing the correct signature
55  corresponding to the requested type
56 
57  @ingroup ParamFunctor_int
58 
59 */
60 #ifndef __CINT__
61 
62 template<class ParentFunctor, class Func >
63 class ParamFunctorHandler : public ParentFunctor::Impl {
64 
65  typedef typename ParentFunctor::Impl Base;
66 
67 public:
68 
69  // constructor
70  ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
71 
72 
73  virtual ~ParamFunctorHandler() {}
74 
75 
76  // for 1D functions
77  inline double operator() (double x, double *p) {
78  return fFunc(x,p);
79  }
80 // inline double operator() (double x, const double *p) const {
81 // return fFunc(x,p);
82 // }
83  // for multi-dimensional functions
84 // inline double operator() (const double * x, const double *p) const {
85 // return fFunc(x,p);
86 // }
87  inline double operator() (double * x, double *p) {
88  return FuncEvaluator<Func>::Eval(fFunc,x,p);
89  }
90 
91  // clone (use same pointer)
93  return new ParamFunctorHandler(fFunc);
94  }
95 
96 
97 private :
98 
100 
101  // structure to distinguish pointer types
102  template <typename F> struct FuncEvaluator {
103  inline static double Eval( F & f, double *x, double * p) {
104  return f(x,p);
105  }
106  };
107  template <typename F> struct FuncEvaluator<F*> {
108  inline static double Eval( F * f, double *x, double * p) {
109  return (*f)(x,p);
110  }
111  };
112  template <typename F> struct FuncEvaluator<F* const> {
113  inline static double Eval( const F * f, double *x, double * p) {
114  return (*f)(x,p);
115  }
116  };
117  // need maybe also volatile ?
118 };
119 
120 
121 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
122 // needed since CINT initialize it with TRootIOCtor
123 //class TRootIOCtor;
124 template<class ParentFunctor>
125 class ParamFunctorHandler<ParentFunctor,TRootIOCtor *> : public ParentFunctor::Impl
126 {
127 public:
128 
129  ParamFunctorHandler(TRootIOCtor *) {}
130 
131  double operator() (double *, double * ) { return 0; }
132  // clone (use same pointer)
133  ParamFunctorHandler * Clone() const {
134  return 0;
135  }
136 
137 };
138 #endif
139 
140 
141 /**
142  ParamFunctor Handler to Wrap pointers to member functions
143 
144  @ingroup ParamFunctor_int
145 */
146 template <class ParentFunctor, typename PointerToObj,
147  typename PointerToMemFn>
148 class ParamMemFunHandler : public ParentFunctor::Impl
149 {
150  typedef typename ParentFunctor::Impl Base;
151 
152 
153 public:
154 
155  /// constructor from a pointer to the class and a pointer to the function
156  ParamMemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn)
157  : fObj(pObj), fMemFn(pMemFn)
158  {}
159 
160  virtual ~ParamMemFunHandler() {}
161 
162 // inline double operator() (double x, const double * p) const {
163 // return ((*fObj).*fMemFn)(x,p);
164 // }
165 
166  inline double operator() (double x, double * p) {
167  return ((*fObj).*fMemFn)(x,p);
168  }
169 
170 // inline double operator() (const double * x, const double * p) const {
171 // return ((*fObj).*fMemFn)(x,p);
172 // }
173 
174  inline double operator() (double * x, double * p) {
175  return ((*fObj).*fMemFn)(x,p);
176  }
177 
178  // clone (use same pointer)
180  return new ParamMemFunHandler(fObj, fMemFn);
181  }
182 
183 
184 private :
185  ParamMemFunHandler(const ParamMemFunHandler&); // Not implemented
186  ParamMemFunHandler& operator=(const ParamMemFunHandler&); // Not implemented
187 
188  PointerToObj fObj;
189  PointerToMemFn fMemFn;
190 
191 };
192 
193 #endif
194 
195 
196 
197 /**
198  Param Functor class for Multidimensional functions.
199  It is used to wrap in a very simple and convenient way
200  any other C++ callable object (implemention double operator( const double *, const double * ) )
201  or a member function with the correct signature,
202  like Foo::EvalPar(const double *, const double *)
203 
204  @ingroup ParamFunc
205 
206  */
207 
208 
210 
211 
212 public:
213 
215 
216 
217  /**
218  Default constructor
219  */
220  ParamFunctor () : fImpl(0) {}
221 
222 
223  /**
224  construct from a pointer to member function (multi-dim type)
225  */
226  template <class PtrObj, typename MemFn>
227  ParamFunctor(const PtrObj& p, MemFn memFn)
228  : fImpl(new ParamMemFunHandler<ParamFunctor, PtrObj, MemFn>(p, memFn))
229  {}
230 
231 
232 
233  /**
234  construct from another generic Functor of multi-dimension
235  */
236  template <typename Func>
237  explicit ParamFunctor( const Func & f) :
238  fImpl(new ParamFunctorHandler<ParamFunctor,Func>(f) )
239  {}
240 
241 
242 
243  // specialization used in TF1
244  typedef double (* FreeFunc ) (double * , double *);
245  ParamFunctor(FreeFunc f) :
246  fImpl(new ParamFunctorHandler<ParamFunctor,FreeFunc>(f) )
247  {
248  }
249 
250 
251  /**
252  Destructor (no operations)
253  */
254  virtual ~ParamFunctor () {
255  if (fImpl) delete fImpl;
256  }
257 
258  /**
259  Copy constructor
260  */
261  ParamFunctor(const ParamFunctor & rhs) :
262  fImpl(0)
263  {
264 // if (rhs.fImpl.get() != 0)
265 // fImpl = std::unique_ptr<Impl>( (rhs.fImpl)->Clone() );
266  if (rhs.fImpl != 0) fImpl = rhs.fImpl->Clone();
267  }
268 
269  /**
270  Assignment operator
271  */
273 // ParamFunctor copy(rhs);
274  // swap unique_ptr by hand
275 // Impl * p = fImpl.release();
276 // fImpl.reset(copy.fImpl.release());
277 // copy.fImpl.reset(p);
278 
279  if(this != &rhs) {
280  if (fImpl) delete fImpl;
281  fImpl = 0;
282  if (rhs.fImpl != 0)
283  fImpl = rhs.fImpl->Clone();
284  }
285  return *this;
286  }
287 
288  void * GetImpl() { return (void *) fImpl; }
289 
290 
291  double operator() (double * x, double * p) {
292  return (*fImpl)(x,p);
293  }
294 
295 
296 
297  bool Empty() const { return fImpl == 0; }
298 
299 
300  void SetFunction(Impl * f) {
301  fImpl = f;
302  }
303 
304 private :
305 
306 
307  //std::unique_ptr<Impl> fImpl;
308  Impl * fImpl;
309 
310 
311 };
312 
313 
314 
315  } // end namespace Math
316 
317 } // end namespace ROOT
318 
319 
320 #endif /* ROOT_Math_ParamFunctor */
ParamFunctor(const ParamFunctor &rhs)
Copy constructor.
Definition: ParamFunctor.h:261
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
ParamFunctor Handler class is responsible for wrapping any other functor and pointer to free C functi...
Definition: ParamFunctor.h:63
static double Eval(F &f, double *x, double *p)
Definition: ParamFunctor.h:103
Double_t x[n]
Definition: legend1.C:17
virtual double operator()(double *x, double *p)=0
static double Eval(const F *f, double *x, double *p)
Definition: ParamFunctor.h:113
ParentFunctor::Impl Base
Definition: ParamFunctor.h:150
ParamFunctor(const Func &f)
construct from another generic Functor of multi-dimension
Definition: ParamFunctor.h:237
virtual ParamFunctionBase * Clone() const =0
ParamMemFunHandler(const PointerToObj &pObj, PointerToMemFn pMemFn)
constructor from a pointer to the class and a pointer to the function
Definition: ParamFunctor.h:156
#define F(x, y, z)
ParamFunctor()
Default constructor.
Definition: ParamFunctor.h:220
ParamFunctorHandler * Clone() const
Definition: ParamFunctor.h:92
ParamFunctor(const PtrObj &p, MemFn memFn)
construct from a pointer to member function (multi-dim type)
Definition: ParamFunctor.h:227
ParamFunctionBase Impl
Definition: ParamFunctor.h:214
ParentFunctor::Impl Base
Definition: ParamFunctor.h:65
double f(double x)
virtual ~ParamFunctor()
Destructor (no operations)
Definition: ParamFunctor.h:254
ParamFunctor Handler to Wrap pointers to member functions.
Definition: ParamFunctor.h:148
class defining the signature for multi-dim parametric functions
Definition: ParamFunctor.h:41
ParamMemFunHandler * Clone() const
Definition: ParamFunctor.h:179
Namespace for new Math classes and functions.
Binding & operator=(OUT(*fun)(void))
static double Eval(F *f, double *x, double *p)
Definition: ParamFunctor.h:108
Param Functor class for Multidimensional functions.
Definition: ParamFunctor.h:209
void SetFunction(Impl *f)
Definition: ParamFunctor.h:300
ParamFunctorHandler(const Func &fun)
Definition: ParamFunctor.h:70