From ff8891221146f894faed2e882267d729c1f9cf8d Mon Sep 17 00:00:00 2001 From: Keluaa <34173752+Keluaa@users.noreply.github.com> Date: Fri, 7 Apr 2023 16:00:00 +0200 Subject: [PATCH] Added support for Val{T} --- examples/functions.cpp | 12 +++++ include/jlcxx/type_conversion.hpp | 74 +++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/examples/functions.cpp b/examples/functions.cpp index 8f976cc..55bb2f8 100644 --- a/examples/functions.cpp +++ b/examples/functions.cpp @@ -307,6 +307,18 @@ JLCXX_MODULE init_test_module(jlcxx::Module& mod) mod.method("test_double2_pointer", [] () { return static_cast(nullptr); }); mod.method("test_double3_pointer", [] () { return static_cast(nullptr); }); + // Val + mod.method("test_val", [](jlcxx::Val) { return 1; }); + mod.method("test_val", [](jlcxx::Val) { return jlcxx::Val::jl_value(); }); + mod.method("test_val", [](jlcxx::Val) { return 3; }); + mod.method("test_val", [](jlcxx::Val) { return jlcxx::Val(); }); + JLCXX_VAL_SYM cst_sym_1 = "A"; + JLCXX_VAL_SYM cst_sym_2 = "B"; + JLCXX_VAL_SYM cst_sym_3 = "C"; + mod.method("test_val", [](jlcxx::ValSym) { return (jl_value_t*) jl_symbol("A"); }); + mod.method("test_val", [](jlcxx::ValSym) { return jlcxx::ValSym::jl_value(); }); + mod.method("test_val", [](jlcxx::ValSym) { return jlcxx::ValSym(); }); + // wstring mod.method("test_wstring_to_julia", [] () { return std::wstring(L"šČô_φ_привет_일보"); }); mod.method("test_wstring_to_cpp", [] (const std::wstring& ws) { return ws == L"šČô_φ_привет_일보"; }); diff --git a/include/jlcxx/type_conversion.hpp b/include/jlcxx/type_conversion.hpp index e866f16..ddc9f82 100644 --- a/include/jlcxx/type_conversion.hpp +++ b/include/jlcxx/type_conversion.hpp @@ -1038,6 +1038,80 @@ struct ConvertToJulia, NoMappingTrait> } }; +/// Helper for Val{V} types +template +struct Val +{ + static constexpr T value = v; + + static jl_value_t* jl_value() + { + if constexpr (std::is_same_v) { + return (jl_value_t*) jl_symbol(v.data()); + } else { + return ::jlcxx::box(v); + } + } +}; + +template +using ValSym = Val; + +#define JLCXX_VAL_SYM static constexpr const std::string_view + +template +struct static_type_mapping> +{ + using type = jl_datatype_t*; +}; + +template +struct julia_type_factory> +{ + static inline jl_datatype_t* julia_type() + { + return apply_type(::jlcxx::julia_type("Val", jl_base_module), (jl_datatype_t*) ::jlcxx::box(v)); + } +}; + +template +struct julia_type_factory> +{ + static inline jl_datatype_t* julia_type() + { + return apply_type(::jlcxx::julia_type("Val", jl_base_module), (jl_datatype_t*) jl_symbol(str.data())); + } +}; + +template +struct ConvertToCpp, NoMappingTrait> +{ + Val operator()(jl_datatype_t*) const + { + return Val(); + } +}; + +template +struct ConvertToJulia, NoMappingTrait> +{ + jl_datatype_t* operator()(Val) const + { + static jl_datatype_t* type = apply_type(::jlcxx::julia_type("Val", jl_base_module), (jl_datatype_t*) ::jlcxx::box(v)); + return type; + } +}; + +template +struct ConvertToJulia, NoMappingTrait> +{ + jl_datatype_t* operator()(Val) const + { + static jl_datatype_t* type = apply_type(::jlcxx::julia_type("Val", jl_base_module), (jl_datatype_t*) jl_symbol(str.data())); + return type; + } +}; + /// Helper to encapsulate a strictly typed number type. Numbers typed like this will not be involved in the convenience-overloads that allow passing e.g. an Int to a Float64 argument template struct StrictlyTypedNumber