Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't store constants as references #134

Merged
merged 1 commit into from
Nov 16, 2023
Merged
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
Don't store constants as references
barche committed Nov 16, 2023
commit abad04b1af55fb2a45e3ab672f361bc72626f30b
4 changes: 4 additions & 0 deletions examples/types.cpp
Original file line number Diff line number Diff line change
@@ -367,6 +367,10 @@ JLCXX_MODULE define_julia_module(jlcxx::Module& types)
types.set_const("EnumClassBlue", EnumClass::blue);
types.method("check_red", [] (const EnumClass c) { return c == EnumClass::red; });

static const EnumClass stored_blue = EnumClass::blue;
types.method("check_enum_byref", [] (const EnumClass& c) { return c == EnumClass::red; });
types.set_const("StoredBlue", stored_blue);

types.add_type<Foo>("Foo")
.constructor<const std::wstring&, jlcxx::ArrayRef<double,1>>()
.method("name", [](Foo& f) { return f.name; })
5 changes: 3 additions & 2 deletions include/jlcxx/module.hpp
Original file line number Diff line number Diff line change
@@ -629,12 +629,13 @@ class JLCXX_API Module
template<typename T>
void set_const(const std::string& name, T&& value)
{
static_assert(IsMirroredType<typename std::remove_reference<T>::type>::value, "set_const can only be applied to IsMirrored types (e.g. numbers or standard layout structs)");
using plain_type = remove_const_ref<T>;
static_assert(IsMirroredType<plain_type>::value, "set_const can only be applied to IsMirrored types (e.g. numbers or standard layout structs)");
if(get_constant(name) != nullptr)
{
throw std::runtime_error("Duplicate registration of constant " + name);
}
set_constant(name, box<T>(value));
set_constant(name, box<plain_type>(value));
}

std::string name() const