From f8a2a54734c0184d42c66f3d825c5cefa4df828d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 29 Dec 2023 16:24:54 -0800 Subject: [PATCH] auto wrapper --- src/gen/gsl_types.jl | 2 +- src/manual_wrappers.jl | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/gen/gsl_types.jl b/src/gen/gsl_types.jl index 90622df..cd60c59 100644 --- a/src/gen/gsl_types.jl +++ b/src/gen/gsl_types.jl @@ -876,7 +876,7 @@ defined in `gsl_math.h`. """ mutable struct gsl_function function_::Ptr{Cvoid} - params::Ptr{Cvoid} + params end @doc md""" diff --git a/src/manual_wrappers.jl b/src/manual_wrappers.jl index c73e789..fa9311b 100644 --- a/src/manual_wrappers.jl +++ b/src/manual_wrappers.jl @@ -30,6 +30,25 @@ end ## Root finding + +# wrapper function +function gsl_function_helper(x::Cdouble, @nospecialize(fn::Any))::Cdouble + try + return Cdouble(fn(x)) + catch + return NaN + end +end + +function Base.cconvert(::Type{Ref{gsl_function}}, fn) + fptr = @cfunction(gsl_function_helper, Cdouble, (Cdouble, Any)) + convert(Ref{gsl_function}, gsl_function(fptr, fn)) +end + +Base.cconvert(::Type{Ref{gsl_function}}, gslf::gsl_function) = + convert(Ref{gsl_function}, gslf) + + # Macros for easier creation of gsl_function and gsl_function_fdf structs export @gsl_function, @gsl_function_fdf