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