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 * \defgroup ParamFunctor_int N-D parametric functions
37 * \brief Multi-dimensional parametric functions
38 */
39
40/** class defining the signature for multi-dim parametric functions
41
42 @ingroup ParamFunctor_int
43 */
44template<class T>
46 public:
47 virtual ~ParamFunctionBase() {}
48 virtual T operator() (const T * x, const double *p) = 0;
49 virtual T operator() (T * x, double *p) = 0;
50 virtual ParamFunctionBase * Clone() const = 0;
51};
52
53
54
55/**
56 ParamFunctor Handler class is responsible for wrapping any other functor and pointer to
57 free C functions.
58 It can be created from any function implementing the correct signature
59 corresponding to the requested type
60
61 @ingroup ParamFunctor_int
62
63*/
64
65template<class ParentFunctor, class Func >
66class ParamFunctorHandler : public ParentFunctor::Impl {
67
68 typedef typename ParentFunctor::EvalType EvalType;
69 typedef typename ParentFunctor::Impl Base;
70
71public:
72
73 // constructor
74 ParamFunctorHandler(const Func & fun) : fFunc(fun) {}
75
76
78
79
80 // for 1D functions
81 inline EvalType operator() (EvalType x, double *p) {
82 return fFunc(x,p);
83 }
84// inline double operator() (double x, const double *p) const {
85// return fFunc(x,p);
86// }
87 // for multi-dimensional functions
88// inline double operator() (const double * x, const double *p) const {
89// return fFunc(x,p);
90// }
91 inline EvalType operator() (EvalType * x, double *p) {
93 }
94
95 inline EvalType operator() (const EvalType * x, const double *p) {
97 }
98
99 // clone (use same pointer)
101 return new ParamFunctorHandler(fFunc);
102 }
103
104
105private :
106
107 Func fFunc;
108
109 // structure to distinguish pointer types
110 template <typename F,typename T> struct FuncEvaluator {
111 inline static T Eval( F & f, T *x, double * p) {
112 return f(x, p);
113 }
114
115 inline static T EvalConst( F & f, const T *x, const double * p) {
116 return f((T*)x, (double*)p);
117 }
118 };
119
120 template <typename F, typename T> struct FuncEvaluator<F*, T> {
121 inline static T Eval( F * f, T *x, double * p) {
122 return (*f)(x, p);
123 }
124
125 inline static T EvalConst( F * f, const T *x, const double * p) {
126 return (*f)((T*)x, (double*)p);
127
128 }
129 };
130
131 template <typename F,typename T> struct FuncEvaluator<F* const, T> {
132 inline static T Eval( const F * f, T *x, double * p) {
133 return (*f)(x, p);
134 }
135
136 inline static T EvalConst( const F * f, const T *x, const double * p) {
137 return (*f)((T*)x, (double*)p);
138 }
139 };
140
141 // need maybe also volatile ?
142};
143
144
145#if defined(__MAKECINT__) || defined(G__DICTIONARY)
146// needed since CINT initialize it with TRootIOCtor
147//class TRootIOCtor;
148template<class ParentFunctor>
149class ParamFunctorHandler<ParentFunctor,TRootIOCtor *> : public ParentFunctor::Impl
150{
151public:
152
153 ParamFunctorHandler(TRootIOCtor *) {}
154
155 double operator() (double *, double * ) { return 0; }
156
157 double operator() (const double *, const double * ) { return 0; }
158 // clone (use same pointer)
159 ParamFunctorHandler * Clone() const {
160 return 0;
161 }
162
163};
164#endif
165
166
167/**
168 ParamFunctor Handler to Wrap pointers to member functions
169
170 @ingroup ParamFunctor_int
171*/
172template <class ParentFunctor, typename PointerToObj,
173 typename PointerToMemFn>
174class ParamMemFunHandler : public ParentFunctor::Impl
175{
176 typedef typename ParentFunctor::Impl Base;
177
178
179public:
180
181 /// constructor from a pointer to the class and a pointer to the function
182 ParamMemFunHandler(const PointerToObj& pObj, PointerToMemFn pMemFn)
183 : fObj(pObj), fMemFn(pMemFn)
184 {}
185
187
188// inline double operator() (double x, const double * p) const {
189// return ((*fObj).*fMemFn)(x,p);
190// }
191
192 inline double operator() (double x, double * p) {
193 return ((*fObj).*fMemFn)(x,p);
194 }
195
196// inline double operator() (const double * x, const double * p) const {
197// return ((*fObj).*fMemFn)(x,p);
198// }
199
200 inline double operator() (double * x, double * p) {
202 }
203
204 inline double operator() (const double * x, const double * p) {
206 }
207
208 // clone (use same pointer)
210 return new ParamMemFunHandler(fObj, fMemFn);
211 }
212
213private:
214
215 // structure to distinguish pointer types
216 template <typename PObj, typename F,typename T> struct MemFuncEvaluator {
217 inline static T Eval(PObj & pobj, F & f, T *x, double * p) {
218 return ((*pobj).*f)(x, p);
219 }
220
221 inline static T EvalConst(PObj & pobj, F & f, const T *x, const double * p) {
222 return ((*pobj).*f)((T*)x, (double*)p);
223 }
224 };
225
226
227 // // these are needed ??
228 // template <typename PObj, typename F, typename T> struct MemFuncEvaluator<PObj,F*, T> {
229 // inline static T Eval(PObj & pobj, F * f, T *x, double * p) {
230 // return ((*pobj).*f)f(x, p);
231 // }
232
233 // inline static T EvalConst(PObj & pobj, F * f, const T *x, const double * p) {
234 // return ((*pobj).*f)((T*)x, (double*)p);
235
236 // }
237 // };
238
239 // template <typename PObj, typename F,typename T> struct FuncEvaluator<PObj,F* const, T> {
240 // inline static T Eval(PObj &, const F * f, T *x, double * p) {
241 // return ((*pobj).*f)f(x, p);
242 // }
243
244 // inline static T EvalConst(PObj & pobj, const F * f, const T *x, const double * p) {
245 // return ((*pobj).*f)((T*)x, (double*)p);
246 // }
247 // };
248
249private :
250 ParamMemFunHandler(const ParamMemFunHandler&) = delete; // Not implemented
251 ParamMemFunHandler& operator=(const ParamMemFunHandler&) = delete; // Not implemented
252
253 PointerToObj fObj;
254 PointerToMemFn fMemFn;
255
256};
257
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 (implementation 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
273template<class T>
275
276
277public:
278
279 typedef T EvalType;
281
282
283 /**
284 Default constructor
285 */
286 ParamFunctorTempl () : fImpl(nullptr) {}
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) :
305 {}
306
307
308
309 // specialization used in TF1
310 typedef T (* FreeFunc ) (T * , double *);
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 */
326 if (fImpl) delete fImpl;
327 }
328
329 /**
330 Copy constructor
331 */
333 fImpl(nullptr)
334 {
335// if (rhs.fImpl.get() != 0)
336// fImpl = std::unique_ptr<Impl>( (rhs.fImpl)->Clone() );
337 if (rhs.fImpl) fImpl = rhs.fImpl->Clone();
338 }
339
340 /**
341 Assignment operator
342 */
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 = nullptr;
353 if (rhs.fImpl)
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; }
372
373
375 fImpl = f;
376 }
377
378private :
379
380
381 //std::unique_ptr<Impl> fImpl;
383
384
385};
386
387
389
390 } // end namespace Math
391
392} // end namespace ROOT
393
394
395#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)