Logo ROOT   6.13/01
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 "Rtypes.h"
29 #include <functional>
30 #include <vector>
31 #include <iostream>
32 
33 namespace ROOT {
34 
35 namespace Math {
36 
37 
38 /** class defining the signature for multi-dim parametric functions
39 
40  @ingroup ParamFunctor_int
41  */
42 template<class T>
44  public:
45  virtual ~ParamFunctionBase() {}
46  virtual T operator() (const T * x, const double *p) = 0;
47  virtual T operator() (T * x, double *p) = 0;
48  virtual ParamFunctionBase * Clone() const = 0;
49 };
50 
51 
52 
53 /**
54  ParamFunctor Handler class is responsible for wrapping any other functor and pointer to
55  free C functions.
56  It can be created from any function implementing the correct signature
57  corresponding to the requested type
58 
59  @ingroup ParamFunctor_int
60 
61 */
62 #ifndef __CINT__
63 
64 template<class ParentFunctor, class Func >
65 class ParamFunctorHandler : public ParentFunctor::Impl {
66 
67  typedef typename ParentFunctor::EvalType EvalType;
68  typedef typename ParentFunctor::Impl Base;
69 
70 public:
71 
72  // constructor
73  ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
74 
75 
76  virtual ~ParamFunctorHandler() {}
77 
78 
79  // for 1D functions
80  inline EvalType operator() (EvalType x, double *p) {
81  return fFunc(x,p);
82  }
83 // inline double operator() (double x, const double *p) const {
84 // return fFunc(x,p);
85 // }
86  // for multi-dimensional functions
87 // inline double operator() (const double * x, const double *p) const {
88 // return fFunc(x,p);
89 // }
90  inline EvalType operator() (EvalType * x, double *p) {
91  return FuncEvaluator<Func, EvalType>::Eval(fFunc,x,p);
92  }
93 
94  inline EvalType operator() (const EvalType * x, const double *p) {
96  }
97 
98  // clone (use same pointer)
100  return new ParamFunctorHandler(fFunc);
101  }
102 
103 
104 private :
105 
106  Func fFunc;
107 
108  // structure to distinguish pointer types
109  template <typename F,typename T> struct FuncEvaluator {
110  inline static T Eval( F & f, T *x, double * p) {
111  return f(x, p);
112  }
113 
114  inline static T EvalConst( F & f, const T *x, const double * p) {
115  return f((T*)x, (double*)p);
116  }
117  };
118 
119  template <typename F, typename T> struct FuncEvaluator<F*, T> {
120  inline static T Eval( F * f, T *x, double * p) {
121  return (*f)(x, p);
122  }
123 
124  inline static T EvalConst( F * f, const T *x, const double * p) {
125  return (*f)((T*)x, (double*)p);
126 
127  }
128  };
129 
130  template <typename F,typename T> struct FuncEvaluator<F* const, T> {
131  inline static T Eval( const F * f, T *x, double * p) {
132  return (*f)(x, p);
133  }
134 
135  inline static T EvalConst( const F * f, const T *x, const double * p) {
136  return (*f)((T*)x, (double*)p);
137  }
138  };
139 
140  // need maybe also volatile ?
141 };
142 
143 
144 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
145 // needed since CINT initialize it with TRootIOCtor
146 //class TRootIOCtor;
147 template<class ParentFunctor>
148 class ParamFunctorHandler<ParentFunctor,TRootIOCtor *> : public ParentFunctor::Impl
149 {
150 public:
151 
152  ParamFunctorHandler(TRootIOCtor *) {}
153 
154  double operator() (double *, double * ) { return 0; }
155 
156  double operator() (const double *, const double * ) { return 0; }
157  // clone (use same pointer)
158  ParamFunctorHandler * Clone() const {
159  return 0;
160  }
161 
162 };
163 #endif
164 
165 
166 /**
167  ParamFunctor Handler to Wrap pointers to member functions
168 
169  @ingroup ParamFunctor_int
170 */
171 template <class ParentFunctor, typename PointerToObj,
172  typename PointerToMemFn>
173 class ParamMemFunHandler : public ParentFunctor::Impl
174 {
175  typedef typename ParentFunctor::Impl Base;
176 
177 
178 public:
179 
180  /// constructor from a pointer to the class and a pointer to the function
181  ParamMemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn)
182  : fObj(pObj), fMemFn(pMemFn)
183  {}
184 
185  virtual ~ParamMemFunHandler() {}
186 
187 // inline double operator() (double x, const double * p) const {
188 // return ((*fObj).*fMemFn)(x,p);
189 // }
190 
191  inline double operator() (double x, double * p) {
192  return ((*fObj).*fMemFn)(x,p);
193  }
194 
195 // inline double operator() (const double * x, const double * p) const {
196 // return ((*fObj).*fMemFn)(x,p);
197 // }
198 
199  inline double operator() (double * x, double * p) {
201  }
202 
203  inline double operator() (const double * x, const double * p) {
205  }
206 
207  // clone (use same pointer)
209  return new ParamMemFunHandler(fObj, fMemFn);
210  }
211 
212 private:
213 
214  // structure to distinguish pointer types
215  template <typename PObj, typename F,typename T> struct MemFuncEvaluator {
216  inline static T Eval(PObj & pobj, F & f, T *x, double * p) {
217  return ((*pobj).*f)(x, p);
218  }
219 
220  inline static T EvalConst(PObj & pobj, F & f, const T *x, const double * p) {
221  return ((*pobj).*f)((T*)x, (double*)p);
222  }
223  };
224 
225 
226  // // these are needed ??
227  // template <typename PObj, typename F, typename T> struct MemFuncEvaluator<PObj,F*, T> {
228  // inline static T Eval(PObj & pobj, F * f, T *x, double * p) {
229  // return ((*pobj).*f)f(x, p);
230  // }
231 
232  // inline static T EvalConst(PObj & pobj, F * f, const T *x, const double * p) {
233  // return ((*pobj).*f)((T*)x, (double*)p);
234 
235  // }
236  // };
237 
238  // template <typename PObj, typename F,typename T> struct FuncEvaluator<PObj,F* const, T> {
239  // inline static T Eval(PObj &, const F * f, T *x, double * p) {
240  // return ((*pobj).*f)f(x, p);
241  // }
242 
243  // inline static T EvalConst(PObj & pobj, const F * f, const T *x, const double * p) {
244  // return ((*pobj).*f)((T*)x, (double*)p);
245  // }
246  // };
247 
248 private :
249  ParamMemFunHandler(const ParamMemFunHandler&); // Not implemented
250  ParamMemFunHandler& operator=(const ParamMemFunHandler&); // Not implemented
251 
252  PointerToObj fObj;
253  PointerToMemFn fMemFn;
254 
255 };
256 
257 #endif
258 
259 
260 
261 /**
262  Param Functor class for Multidimensional functions.
263  It is used to wrap in a very simple and convenient way
264  any other C++ callable object (implemention double operator( const double *, const double * ) )
265  or a member function with the correct signature,
266  like Foo::EvalPar(const double *, const double *)
267 
268  @ingroup ParamFunc
269 
270  */
271 
272 
273 template<class T>
275 
276 
277 public:
278 
279  typedef T EvalType;
281 
282 
283  /**
284  Default constructor
285  */
286  ParamFunctorTempl () : fImpl(0) {}
287 
288 
289  /**
290  construct from a pointer to member function (multi-dim type)
291  */
292  template <class PtrObj, typename MemFn>
293  ParamFunctorTempl(const PtrObj& p, MemFn memFn)
294  : fImpl(new ParamMemFunHandler<ParamFunctorTempl<T>, PtrObj, MemFn>(p, memFn))
295  {}
296 
297 
298 
299  /**
300  construct from another generic Functor of multi-dimension
301  */
302  template <typename Func>
303  explicit ParamFunctorTempl( const Func & f) :
304  fImpl(new ParamFunctorHandler<ParamFunctorTempl<T>,Func>(f) )
305  {}
306 
307 
308 
309  // specialization used in TF1
310  typedef T (* FreeFunc ) (T * , double *);
311  ParamFunctorTempl(FreeFunc f) :
312  fImpl(new ParamFunctorHandler<ParamFunctorTempl<T>,FreeFunc>(f) )
313  {
314  }
315 
316  // specialization used in TF1
317  ParamFunctorTempl(const std::function<T(const T *f, const Double_t *param)> &func) :
318  fImpl(new ParamFunctorHandler<ParamFunctorTempl<T>, const std::function<T(const T *f, const Double_t *param)>>(func))
319  {
320  }
321 
322  /**
323  Destructor (no operations)
324  */
325  virtual ~ParamFunctorTempl () {
326  if (fImpl) delete fImpl;
327  }
328 
329  /**
330  Copy constructor
331  */
333  fImpl(0)
334  {
335 // if (rhs.fImpl.get() != 0)
336 // fImpl = std::unique_ptr<Impl>( (rhs.fImpl)->Clone() );
337  if (rhs.fImpl != 0) fImpl = rhs.fImpl->Clone();
338  }
339 
340  /**
341  Assignment operator
342  */
343  ParamFunctorTempl & operator = (const ParamFunctorTempl & rhs) {
344 // ParamFunctor copy(rhs);
345  // swap unique_ptr by hand
346 // Impl * p = fImpl.release();
347 // fImpl.reset(copy.fImpl.release());
348 // copy.fImpl.reset(p);
349 
350  if(this != &rhs) {
351  if (fImpl) delete fImpl;
352  fImpl = 0;
353  if (rhs.fImpl != 0)
354  fImpl = rhs.fImpl->Clone();
355  }
356  return *this;
357  }
358 
359  void * GetImpl() { return (void *) fImpl; }
360 
361 
362  T operator() ( T * x, double * p) {
363  return (*fImpl)(x,p);
364  }
365 
366  T operator() (const T * x, const double * p) {
367  return (*fImpl)(x,p);
368  }
369 
370 
371  bool Empty() const { return fImpl == 0; }
372 
373 
374  void SetFunction(Impl * f) {
375  fImpl = f;
376  }
377 
378 private :
379 
380 
381  //std::unique_ptr<Impl> fImpl;
382  Impl * fImpl;
383 
384 
385 };
386 
387 
389 
390  } // end namespace Math
391 
392 } // end namespace ROOT
393 
394 
395 #endif /* ROOT_Math_ParamFunctor */
ParentFunctor::EvalType EvalType
Definition: ParamFunctor.h:67
Param Functor class for Multidimensional functions.
Definition: ParamFunctor.h:274
ParamFunctorTempl(const std::function< T(const T *f, const Double_t *param)> &func)
Definition: ParamFunctor.h:317
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
static T Eval(F &f, T *x, double *p)
Definition: ParamFunctor.h:110
double T(double x)
Definition: ChebyshevPol.h:34
virtual ~ParamFunctorTempl()
Destructor (no operations)
Definition: ParamFunctor.h:325
STL namespace.
ParamFunctor Handler class is responsible for wrapping any other functor and pointer to free C functi...
Definition: ParamFunctor.h:65
static T EvalConst(F *f, const T *x, const double *p)
Definition: ParamFunctor.h:124
static T Eval(PObj &pobj, F &f, T *x, double *p)
Definition: ParamFunctor.h:216
ParentFunctor::Impl Base
Definition: ParamFunctor.h:175
ParamFunctorTempl(const ParamFunctorTempl &rhs)
Copy constructor.
Definition: ParamFunctor.h:332
ParamMemFunHandler(const PointerToObj &pObj, PointerToMemFn pMemFn)
constructor from a pointer to the class and a pointer to the function
Definition: ParamFunctor.h:181
virtual ParamFunctionBase * Clone() const =0
ParamFunctorTempl()
Default constructor.
Definition: ParamFunctor.h:286
ParamFunctorHandler * Clone() const
Definition: ParamFunctor.h:99
virtual T operator()(const T *x, const double *p)=0
ParentFunctor::Impl Base
Definition: ParamFunctor.h:68
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
ParamFunctorTempl(const Func &f)
construct from another generic Functor of multi-dimension
Definition: ParamFunctor.h:303
static T EvalConst(PObj &pobj, F &f, const T *x, const double *p)
Definition: ParamFunctor.h:220
ParamFunctorTempl(const PtrObj &p, MemFn memFn)
construct from a pointer to member function (multi-dim type)
Definition: ParamFunctor.h:293
ParamFunctor Handler to Wrap pointers to member functions.
Definition: ParamFunctor.h:173
class defining the signature for multi-dim parametric functions
Definition: ParamFunctor.h:43
ParamMemFunHandler * Clone() const
Definition: ParamFunctor.h:208
Namespace for new Math classes and functions.
double Eval(double x, const double *c)
Definition: ChebyshevPol.h:43
static T EvalConst(F &f, const T *x, const double *p)
Definition: ParamFunctor.h:114
ParamFunctorHandler(const Func &fun)
Definition: ParamFunctor.h:73
ParamFunctionBase< T > Impl
Definition: ParamFunctor.h:280
static T EvalConst(const F *f, const T *x, const double *p)
Definition: ParamFunctor.h:135