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
13#ifndef ROOT_Math_ParamFunctor
14#define ROOT_Math_ParamFunctor
15
16#include "RtypesCore.h"
17
18#include <functional>
19#include <type_traits>
20#include <utility>
21
22namespace ROOT {
23
24namespace Math {
25
26/**
27 Param Functor class for Multidimensional functions.
28 It is used to wrap in a very simple and convenient way
29 any other C++ callable object (implementation double operator( const double *, const double * ) )
30 or a member function with the correct signature,
31 like Foo::EvalPar(const double *, const double *)
32
33 @ingroup ParamFunc
34
35 */
36
37template <class T>
39
40public:
41 using EvalType = T;
42
43 /// The signature every wrapped callable is normalized to.
44 using Signature = T(const T *, const double *);
45
46 ParamFunctorTempl() = default;
47
48 /// Construct from a pointer to a class object and a pointer to one of its member
49 /// functions, like `Foo::EvalPar(const double *x, const double *p)`.
50 template <class Obj, typename MemFn>
52 : fFunc{[p, memFn](const T *x, const double *par) {
53 return (p->*memFn)(const_cast<T *>(x), const_cast<double *>(par));
54 }}
55 {
56 }
57
58 /// Construct from any callable object, or from a pointer to one.
59 ///
60 /// The callable is normalized to the `T (const T *, const double *)` signature: anything
61 /// `std::function` accepts as-is is handed to it directly, a pointer to a callable object is
62 /// called through without taking ownership of it, and callables that insist on non-const
63 /// pointers (the classic `T (T *x, double *p)` signature) get their arguments cast for them.
64 ///
65 /// The two exclusions keep this greedy forwarding reference away from overloads that a
66 /// non-const lvalue would otherwise bind to it by exact match: the implicit copy constructor,
67 /// which would end up wrapping a functor in itself, and the `std::function` conversion below,
68 /// which is implicit for the Python interface and would become ill-formed via this `explicit` one.
69 template <typename Func, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Func>, ParamFunctorTempl> &&
70 !std::is_same_v<std::decay_t<Func>, std::function<Signature>>>>
71 explicit ParamFunctorTempl(Func &&f)
72 {
73 using F = std::decay_t<Func>;
74 if constexpr (std::is_constructible_v<std::function<Signature>, Func>) {
75 fFunc = std::function<Signature>{std::forward<Func>(f)};
76 } else if constexpr (std::is_pointer_v<F> && std::is_class_v<std::remove_pointer_t<F>>) {
77 fFunc = [f](const T *x, const double *p) { return (*f)(const_cast<T *>(x), const_cast<double *>(p)); };
78 } else {
79 fFunc = [f = std::forward<Func>(f)](const T *x, const double *p) mutable {
80 return f(const_cast<T *>(x), const_cast<double *>(p));
81 };
82 }
83 }
84
85 /// Implicit conversion, relied on by the Python interface when passing a callable to TF1.
86 ParamFunctorTempl(std::function<Signature> f) : fFunc{std::move(f)} {}
87
88 T operator()(const T *x, const double *p) const { return fFunc(x, p); }
89
90 bool Empty() const { return !fFunc; }
91
92private:
93 std::function<Signature> fFunc;
94};
95
97
98} // end namespace Math
99
100} // end namespace ROOT
101
102#endif /* ROOT_Math_ParamFunctor */
#define f(i)
Definition RSha256.hxx:104
Basic types used by ROOT and required by TInterpreter.
winID h TVirtualViewer3D TVirtualGLPainter p
Param Functor class for Multidimensional functions.
ParamFunctorTempl(Obj *p, MemFn memFn)
Construct from a pointer to a class object and a pointer to one of its member functions,...
T operator()(const T *x, const double *p) const
T(const T *, const double *) Signature
The signature every wrapped callable is normalized to.
std::function< Signature > fFunc
ParamFunctorTempl(Func &&f)
Construct from any callable object, or from a pointer to one.
ParamFunctorTempl(std::function< Signature > f)
Implicit conversion, relied on by the Python interface when passing a callable to TF1.
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.