Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-linux-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
CXXFLAGS=-ftemplate-backtrace-limit=0 cmake -DCMAKE_INSTALL_PREFIX=$HOME/install -DAPPEND_OVERRIDES_TOML=ON -DCMAKE_BUILD_TYPE=Debug ..
VERBOSE=ON cmake --build . --config Debug --target install
wget https://github.com/JuliaRegistries/General/raw/refs/heads/master/jll/L/libcxxwrap_julia_jll/Versions.toml
jllversion=$(grep '\["' Versions.toml | tail -n 1 | sed -E 's/\["([0-9]+\.[0-9]+\.[0-9]+)\+[^"]*"\]/\1/g')
jllversion=0.14.2 #$(grep '\["' Versions.toml | tail -n 1 | sed -E 's/\["([0-9]+\.[0-9]+\.[0-9]+)\+[^"]*"\]/\1/g')
cd lib
if [ ! -f libcxxwrap_julia.${jllversion}.dylib ]; then
ln -s libcxxwrap_julia.*.*.*.* libcxxwrap_julia.${jllversion}.dylib
Expand Down
8 changes: 4 additions & 4 deletions examples/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,28 @@ JLCXX_MODULE init_test_module(jlcxx::Module& mod)
JL_GC_POP();
});

mod.method("fn_clb2", [] (jl_function_t* f)
mod.method("fn_clb2", [] (jl_value_t* f)
{
std::vector<double> v{1., 2.};
auto ar = jlcxx::ArrayRef<double, 1>(v.data(), v.size());
jlcxx::JuliaFunction fnClb(f);
fnClb((jl_value_t*)ar.wrapped(), std::wstring(L"calledFromCPP"));
});

mod.method("callback_byval", [] (jl_function_t* f, int& result)
mod.method("callback_byval", [] (jl_value_t* f, int& result)
{
jlcxx::JuliaFunction juliafunc(f);
juliafunc(BoxedNumber(1), result);
});

mod.method("callback_byref", [] (jl_function_t* f, int& result)
mod.method("callback_byref", [] (jl_value_t* f, int& result)
{
jlcxx::JuliaFunction juliafunc(f);
BoxedNumber n(2);
juliafunc(n, result);
});

mod.method("callback_byptr", [] (jl_function_t* f, int& result)
mod.method("callback_byptr", [] (jl_value_t* f, int& result)
{
jlcxx::JuliaFunction juliafunc(f);
BoxedNumber n(3);
Expand Down
4 changes: 2 additions & 2 deletions examples/inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ class VirtualCppJuliaExtended : public VirtualCpp
return sum;
}

void set_callback(jl_function_t* callback)
void set_callback(jl_value_t* callback)
{
m_callback = callback;
}
private:
jl_function_t* m_callback = nullptr;
jl_value_t* m_callback = nullptr;
};

class VirtualCfunctionExtended : public VirtualCpp
Expand Down
14 changes: 14 additions & 0 deletions examples/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ enum MyEnum
EnumValB
};

enum MyEnumNew
{
EnumVal1,
EnumVal2
};

enum class EnumClass { red, green = 20, blue };
enum class BigEnumClass : uint64_t { zero = 0, verybig = 0xffffffffffffffff };

struct Foo
{
Expand Down Expand Up @@ -400,6 +407,13 @@ JLCXX_MODULE define_julia_module(jlcxx::Module& types)
types.method("check_enum_byref", [] (const EnumClass& c) { return c == EnumClass::red; });
types.set_const("StoredBlue", stored_blue);

types.add_enum<MyEnumNew>("MyEnumNew", std::vector<const char*>({"EnumVal1", "EnumVal2"}), std::vector<int>({EnumVal1, EnumVal2}));
types.method("newenum_to_int", [] (const MyEnumNew e) { return static_cast<int>(e); });
types.method("newenum_from_int", [] (int i) { return static_cast<MyEnumNew>(i); });
types.method("newenum_byref", [] (const MyEnumNew& e) { return static_cast<int>(e); });

types.add_enum<BigEnumClass>("BigEnumClass", std::vector<const char*>({"zero", "verybig"}), std::vector<uint64_t>({static_cast<uint64_t>(BigEnumClass::zero), static_cast<uint64_t>(BigEnumClass::verybig)}));

types.add_type<Foo>("Foo")
.constructor<const std::wstring&, jlcxx::ArrayRef<double,1>>()
.method("name", [](Foo& f) { return f.name; })
Expand Down
6 changes: 3 additions & 3 deletions include/jlcxx/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class JLCXX_API JuliaFunction
/// Construct using a function name and module name. Searches the current module by default. Throws if the function was not found.
JuliaFunction(const std::string& name, const std::string& module_name = "");
/// Construct directly from a pointer (throws if pointer is null)
JuliaFunction(jl_function_t* fpointer);
JuliaFunction(jl_value_t* fpointer);

/// Access to the raw pointer
jl_function_t* pointer() const
jl_value_t* pointer() const
{
return m_function;
}
Expand Down Expand Up @@ -61,7 +61,7 @@ class JLCXX_API JuliaFunction
jl_value_t** m_arg_array;
int m_i = 0;
};
jl_function_t* m_function;
jl_value_t* m_function;
};

template<typename... ArgumentsT>
Expand Down
2 changes: 1 addition & 1 deletion include/jlcxx/jlcxx_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#define JLCXX_VERSION_MAJOR 0
#define JLCXX_VERSION_MINOR 14
#define JLCXX_VERSION_PATCH 2
#define JLCXX_VERSION_PATCH 4

// From https://stackoverflow.com/questions/5459868/concatenate-int-to-string-using-c-preprocessor
#define __JLCXX_STR_HELPER(x) #x
Expand Down
38 changes: 37 additions & 1 deletion include/jlcxx/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct UpCast
};

// The CxxWrap Julia module
extern jl_module_t* g_cxxwrap_module;
extern JLCXX_API jl_module_t* g_cxxwrap_module;
extern jl_datatype_t* g_cppfunctioninfo_type;

class JLCXX_API Module;
Expand Down Expand Up @@ -690,6 +690,42 @@ class JLCXX_API Module
template<typename T, typename JLSuperT=jl_datatype_t>
void add_bits(const std::string& name, JLSuperT* super = jl_any_type);

template<typename EnumT, typename T>
void add_enum(std::string name, std::vector<const char*> labels, std::vector<T> values)
{
const std::size_t nb_items = labels.size();
if(nb_items != values.size())
{
throw std::runtime_error("Lengths of the labels and values vectors don't match for enum " + name);
}

create_if_not_exists<ArrayRef<const char*>>();
create_if_not_exists<ArrayRef<T>>();
auto labels_jl = ArrayRef<const char*>(&labels[0], nb_items);
auto values_jl = ArrayRef<T>(&values[0], nb_items);

jl_value_t* add_enum_fn = jl_get_function(g_cxxwrap_module, "add_enum");
if(add_enum_fn == nullptr)
{
throw std::runtime_error("CxxWrapCore.add_enum function not found, ensure you are using at least CxxWrap 0.17.3");
}

jl_value_t** julia_args;
JL_GC_PUSHARGS(julia_args, 4);
julia_args[0] = (jl_value_t*)m_jl_mod;
julia_args[1] = jl_cstr_to_string(name.c_str());
julia_args[2] = (jl_value_t*)labels_jl.wrapped();
julia_args[3] = (jl_value_t*)values_jl.wrapped();
jl_value_t* dt = jl_call(add_enum_fn, julia_args, 4);
JL_GC_POP();

if(!jl_is_datatype(dt))
{
throw std::runtime_error("error adding enum type " + name);
}
set_julia_type<EnumT>((jl_datatype_t*)dt);
}

/// Set a global constant value at the module level
template<typename T>
void set_const(const std::string& name, T&& value)
Expand Down
2 changes: 1 addition & 1 deletion src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ JuliaFunction::JuliaFunction(const std::string& name, const std::string& module_
}
}

JuliaFunction::JuliaFunction(jl_function_t* fpointer)
JuliaFunction::JuliaFunction(jl_value_t* fpointer)
{
if(fpointer == nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/jlcxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cxx_gc_roots_t& cxx_gc_roots()
return m_roots;
}

jl_module_t* g_cxxwrap_module = nullptr;
JLCXX_API jl_module_t* g_cxxwrap_module = nullptr;
jl_datatype_t* g_cppfunctioninfo_type = nullptr;

JLCXX_API void protect_from_gc(jl_value_t* v)
Expand Down
Loading