14 #ifndef ROOT_Math_ParamFunctor
15 #define ROOT_Math_ParamFunctor
43 class ParamFunctionBase {
45 virtual ~ParamFunctionBase() {}
46 virtual T operator() (
const T * x,
const double *p) = 0;
47 virtual T operator() (T * x,
double *p) = 0;
48 virtual ParamFunctionBase * Clone()
const = 0;
64 template<
class ParentFunctor,
class Func >
65 class ParamFunctorHandler :
public ParentFunctor::Impl {
67 typedef typename ParentFunctor::EvalType EvalType;
68 typedef typename ParentFunctor::Impl Base;
73 ParamFunctorHandler(
const Func & fun) : fFunc(fun) {}
76 virtual ~ParamFunctorHandler() {}
80 inline EvalType operator() (EvalType x,
double *p) {
90 inline EvalType operator() (EvalType * x,
double *p) {
91 return FuncEvaluator<Func, EvalType>::Eval(fFunc,x,p);
94 inline EvalType operator() (
const EvalType * x,
const double *p) {
95 return FuncEvaluator<Func, EvalType>::EvalConst(fFunc,x,p);
99 ParamFunctorHandler * Clone()
const {
100 return new ParamFunctorHandler(fFunc);
109 template <
typename F,
typename T>
struct FuncEvaluator {
110 inline static T Eval( F & f, T *x,
double * p) {
114 inline static T EvalConst( F & f,
const T *x,
const double * p) {
115 return f((T*)x, (
double*)p);
119 template <
typename F,
typename T>
struct FuncEvaluator<F*, T> {
120 inline static T Eval( F * f, T *x,
double * p) {
124 inline static T EvalConst( F * f,
const T *x,
const double * p) {
125 return (*f)((T*)x, (
double*)p);
130 template <
typename F,
typename T>
struct FuncEvaluator<F*
const, T> {
131 inline static T Eval(
const F * f, T *x,
double * p) {
135 inline static T EvalConst(
const F * f,
const T *x,
const double * p) {
136 return (*f)((T*)x, (
double*)p);
144 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
147 template<
class ParentFunctor>
148 class ParamFunctorHandler<ParentFunctor,TRootIOCtor *> :
public ParentFunctor::Impl
152 ParamFunctorHandler(TRootIOCtor *) {}
154 double operator() (
double *,
double * ) {
return 0; }
156 double operator() (
const double *,
const double * ) {
return 0; }
158 ParamFunctorHandler * Clone()
const {
171 template <
class ParentFunctor,
typename PointerToObj,
172 typename PointerToMemFn>
173 class ParamMemFunHandler :
public ParentFunctor::Impl
175 typedef typename ParentFunctor::Impl Base;
181 ParamMemFunHandler(
const PointerToObj& pObj, PointerToMemFn pMemFn)
182 : fObj(pObj), fMemFn(pMemFn)
185 virtual ~ParamMemFunHandler() {}
191 inline double operator() (
double x,
double * p) {
192 return ((*fObj).*fMemFn)(x,p);
199 inline double operator() (
double * x,
double * p) {
200 return MemFuncEvaluator<PointerToObj,PointerToMemFn, double>::Eval(fObj,fMemFn,x,p);
203 inline double operator() (
const double * x,
const double * p) {
204 return MemFuncEvaluator<PointerToObj,PointerToMemFn, double>::EvalConst(fObj,fMemFn,x,p);
208 ParamMemFunHandler * Clone()
const {
209 return new ParamMemFunHandler(fObj, fMemFn);
215 template <
typename PObj,
typename F,
typename T>
struct MemFuncEvaluator {
216 inline static T Eval(PObj & pobj, F & f, T *x,
double * p) {
217 return ((*pobj).*f)(x, p);
220 inline static T EvalConst(PObj & pobj, F & f,
const T *x,
const double * p) {
221 return ((*pobj).*f)((T*)x, (
double*)p);
249 ParamMemFunHandler(
const ParamMemFunHandler&);
250 ParamMemFunHandler& operator=(
const ParamMemFunHandler&);
253 PointerToMemFn fMemFn;
274 class ParamFunctorTempl {
280 typedef ParamFunctionBase<T> Impl;
286 ParamFunctorTempl () : fImpl(0) {}
292 template <
class PtrObj,
typename MemFn>
293 ParamFunctorTempl(
const PtrObj& p, MemFn memFn)
294 : fImpl(new ParamMemFunHandler<ParamFunctorTempl<T>, PtrObj, MemFn>(p, memFn))
302 template <
typename Func>
303 explicit ParamFunctorTempl(
const Func & f) :
304 fImpl(new ParamFunctorHandler<ParamFunctorTempl<T>,Func>(f) )
310 typedef T (* FreeFunc ) (T * ,
double *);
311 ParamFunctorTempl(FreeFunc f) :
312 fImpl(new ParamFunctorHandler<ParamFunctorTempl<T>,FreeFunc>(f) )
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))
325 virtual ~ParamFunctorTempl () {
326 if (fImpl)
delete fImpl;
332 ParamFunctorTempl(
const ParamFunctorTempl & rhs) :
337 if (rhs.fImpl != 0) fImpl = rhs.fImpl->Clone();
343 ParamFunctorTempl & operator = (
const ParamFunctorTempl & rhs) {
351 if (fImpl)
delete fImpl;
354 fImpl = rhs.fImpl->Clone();
359 void * GetImpl() {
return (
void *) fImpl; }
362 T operator() ( T * x,
double * p) {
363 return (*fImpl)(x,p);
366 T operator() (
const T * x,
const double * p) {
367 return (*fImpl)(x,p);
371 bool Empty()
const {
return fImpl == 0; }
374 void SetFunction(Impl * f) {
388 using ParamFunctor = ParamFunctorTempl<double>;