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
32namespace ROOT {
33
34namespace Math {
35
36
37/** class defining the signature for multi-dim parametric functions
38
39 @ingroup ParamFunctor_int
40 */
41template<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
63template<class ParentFunctor, class Func >
64class ParamFunctorHandler : public ParentFunctor::Impl {
65
66 typedef typename ParentFunctor::EvalType EvalType;
67 typedef typename ParentFunctor::Impl Base;
68
69public:
70
71 // constructor
72 ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
73
74
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
103private :
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;
146template<class ParentFunctor>
147class ParamFunctorHandler<ParentFunctor,TRootIOCtor *> : public ParentFunctor::Impl
148{
149public:
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*/
170template <class ParentFunctor, typename PointerToObj,
171 typename PointerToMemFn>
172class ParamMemFunHandler : public ParentFunctor::Impl
173{
174 typedef typename ParentFunctor::Impl Base;
175
176
177public:
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
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
211private:
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
247private :
248 ParamMemFunHandler(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
272template<class T>
274
275
276public:
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 */
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
374 fImpl = f;
375 }
376
377private :
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 */
#define f(i)
Definition: RSha256.hxx:104
double Double_t
Definition: RtypesCore.h:59
TRObject operator()(const T1 &t1) const
class defining the signature for multi-dim parametric functions
Definition: ParamFunctor.h:42
virtual T operator()(const T *x, const double *p)=0
virtual ParamFunctionBase * Clone() const =0
ParamFunctor Handler class is responsible for wrapping any other functor and pointer to free C functi...
Definition: ParamFunctor.h:64
EvalType operator()(EvalType x, double *p)
Definition: ParamFunctor.h:79
ParentFunctor::EvalType EvalType
Definition: ParamFunctor.h:66
ParentFunctor::Impl Base
Definition: ParamFunctor.h:67
ParamFunctorHandler(const Func &fun)
Definition: ParamFunctor.h:72
ParamFunctorHandler * Clone() const
Definition: ParamFunctor.h:98
Param Functor class for Multidimensional functions.
Definition: ParamFunctor.h:273
ParamFunctorTempl(const ParamFunctorTempl &rhs)
Copy constructor.
Definition: ParamFunctor.h:331
ParamFunctorTempl(const Func &f)
construct from another generic Functor of multi-dimension
Definition: ParamFunctor.h:302
ParamFunctorTempl()
Default constructor.
Definition: ParamFunctor.h:285
ParamFunctorTempl(const PtrObj &p, MemFn memFn)
construct from a pointer to member function (multi-dim type)
Definition: ParamFunctor.h:292
ParamFunctorTempl(const std::function< T(const T *f, const Double_t *param)> &func)
Definition: ParamFunctor.h:316
T operator()(T *x, double *p)
Definition: ParamFunctor.h:361
virtual ~ParamFunctorTempl()
Destructor (no operations)
Definition: ParamFunctor.h:324
T(* FreeFunc)(T *, double *)
Definition: ParamFunctor.h:309
ParamFunctionBase< T > Impl
Definition: ParamFunctor.h:279
ParamFunctorTempl & operator=(const ParamFunctorTempl &rhs)
Assignment operator.
Definition: ParamFunctor.h:342
ParamFunctor Handler to Wrap pointers to member functions.
Definition: ParamFunctor.h:173
ParamMemFunHandler * Clone() const
Definition: ParamFunctor.h:207
ParentFunctor::Impl Base
Definition: ParamFunctor.h:174
double operator()(double x, double *p)
Definition: ParamFunctor.h:190
ParamMemFunHandler & operator=(const ParamMemFunHandler &)
ParamMemFunHandler(const ParamMemFunHandler &)
ParamMemFunHandler(const PointerToObj &pObj, PointerToMemFn pMemFn)
constructor from a pointer to the class and a pointer to the function
Definition: ParamFunctor.h:180
Double_t x[n]
Definition: legend1.C:17
#define F(x, y, z)
Namespace for new Math classes and functions.
double T(double x)
Definition: ChebyshevPol.h:34
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:150
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
static T EvalConst(F *f, const T *x, const double *p)
Definition: ParamFunctor.h:123
static T EvalConst(const F *f, const T *x, const double *p)
Definition: ParamFunctor.h:134
static T EvalConst(F &f, const T *x, const double *p)
Definition: ParamFunctor.h:113
static T Eval(F &f, T *x, double *p)
Definition: ParamFunctor.h:109
static T EvalConst(PObj &pobj, F &f, const T *x, const double *p)
Definition: ParamFunctor.h:219
static T Eval(PObj &pobj, F &f, T *x, double *p)
Definition: ParamFunctor.h:215