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