Skip to content

Commit

Permalink
Don't store constants as references
Browse files Browse the repository at this point in the history
  • Loading branch information
barche committed Sep 26, 2023
1 parent c28f293 commit d007d1f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; })
Expand Down
5 changes: 3 additions & 2 deletions include/jlcxx/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d007d1f

Please sign in to comment.