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