Skip to content

Commit

Permalink
Added support for Val{T}
Browse files Browse the repository at this point in the history
  • Loading branch information
Keluaa authored and barche committed Apr 19, 2023
1 parent 1ec1784 commit ff88912
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ JLCXX_MODULE init_test_module(jlcxx::Module& mod)
mod.method("test_double2_pointer", [] () { return static_cast<double**>(nullptr); });
mod.method("test_double3_pointer", [] () { return static_cast<double***>(nullptr); });

// Val
mod.method("test_val", [](jlcxx::Val<int, 1>) { return 1; });
mod.method("test_val", [](jlcxx::Val<int, 2>) { return jlcxx::Val<int, 2>::jl_value(); });
mod.method("test_val", [](jlcxx::Val<short, 3>) { return 3; });
mod.method("test_val", [](jlcxx::Val<int, 4>) { return jlcxx::Val<int, 4>(); });
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<cst_sym_1>) { return (jl_value_t*) jl_symbol("A"); });
mod.method("test_val", [](jlcxx::ValSym<cst_sym_2>) { return jlcxx::ValSym<cst_sym_2>::jl_value(); });
mod.method("test_val", [](jlcxx::ValSym<cst_sym_3>) { return jlcxx::ValSym<cst_sym_3>(); });

// wstring
mod.method("test_wstring_to_julia", [] () { return std::wstring(L"šČô_φ_привет_일보"); });
mod.method("test_wstring_to_cpp", [] (const std::wstring& ws) { return ws == L"šČô_φ_привет_일보"; });
Expand Down
74 changes: 74 additions & 0 deletions include/jlcxx/type_conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,80 @@ struct ConvertToJulia<SingletonType<T>, NoMappingTrait>
}
};

/// Helper for Val{V} types
template<typename T, T v>
struct Val
{
static constexpr T value = v;

static jl_value_t* jl_value()
{
if constexpr (std::is_same_v<T, const std::string_view&>) {
return (jl_value_t*) jl_symbol(v.data());
} else {
return ::jlcxx::box<T>(v);
}
}
};

template<const std::string_view& str>
using ValSym = Val<const std::string_view&, str>;

#define JLCXX_VAL_SYM static constexpr const std::string_view

template<typename T, T v>
struct static_type_mapping<Val<T, v>>
{
using type = jl_datatype_t*;
};

template<typename T, T v>
struct julia_type_factory<Val<T, v>>
{
static inline jl_datatype_t* julia_type()
{
return apply_type(::jlcxx::julia_type("Val", jl_base_module), (jl_datatype_t*) ::jlcxx::box<T>(v));
}
};

template<const std::string_view& str>
struct julia_type_factory<Val<const std::string_view&, str>>
{
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<typename T, T v>
struct ConvertToCpp<Val<T, v>, NoMappingTrait>
{
Val<T, v> operator()(jl_datatype_t*) const
{
return Val<T, v>();
}
};

template<typename T, T v>
struct ConvertToJulia<Val<T, v>, NoMappingTrait>
{
jl_datatype_t* operator()(Val<T, v>) const
{
static jl_datatype_t* type = apply_type(::jlcxx::julia_type("Val", jl_base_module), (jl_datatype_t*) ::jlcxx::box<T>(v));
return type;
}
};

template<const std::string_view& str>
struct ConvertToJulia<Val<const std::string_view&, str>, NoMappingTrait>
{
jl_datatype_t* operator()(Val<const std::string_view&, str>) 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<typename NumberT>
struct StrictlyTypedNumber
Expand Down

0 comments on commit ff88912

Please sign in to comment.