a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
gslpp_function_adapter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 HEPfit Collaboration
3  *
4  *
5  * For the licensing terms see doc/COPYING.
6  */
7 
8 #ifndef GSLPP_FUNCTION_ADAPTER_H
9 #define GSLPP_FUNCTION_ADAPTER_H
10 
11 #include <gsl/gsl_math.h>
12 #include <assert.h>
13 
14 template<class F>
15 static double gslFunctionAdapter( double x, void* p)
16 {
17  // Here I do recover the "right" pointer, safer to use static_cast
18  // than reinterpret_cast.
19  F* function = static_cast<F*>( p );
20  return (*function)( x );
21 }
22 
23 template<class F>
24 gsl_function convertToGslFunction( const F& f )
25 {
26  gsl_function gslFunction;
27 
28  const void* p = &f;
29  assert (p != 0);
30 
31  gslFunction.function = &gslFunctionAdapter<F>;
32  // Just to eliminate the const.
33  gslFunction.params = const_cast<void*>( p );
34 
35  return gslFunction;
36 }
37 
38 #endif /* GSLPP_FUNCTION_ADAPTER_H */
39 
convertToGslFunction
gsl_function convertToGslFunction(const F &f)
Definition: gslpp_function_adapter.h:24