Skip to content

Commit

Permalink
Fix static inheritance problem (#157)
Browse files Browse the repository at this point in the history
Issue Regression with 0.12.3 in the support of static inheritance #156
  • Loading branch information
barche authored Jun 1, 2024
1 parent 72851eb commit 1e26ca8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
14 changes: 14 additions & 0 deletions examples/inheritance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ std::string take_ref(A& a)
return a.message();
}

// Static inheritance test (issue #156)
struct StaticBase {
};

struct StaticDerived: public StaticBase {
};

// Example based on https://discourse.julialang.org/t/simplest-way-to-wrap-virtual-c-class/4977
namespace virtualsolver
{
Expand Down Expand Up @@ -100,6 +107,10 @@ namespace jlcxx

template<> struct SuperType<virtualsolver::E> { typedef virtualsolver::Base type; };
template<> struct SuperType<virtualsolver::F> { typedef virtualsolver::Base type; };

template<> struct IsMirroredType<StaticBase> : std::false_type { };
template<> struct IsMirroredType<StaticDerived> : std::false_type { };
template<> struct SuperType<StaticDerived> { typedef StaticBase type; };
}

JLCXX_MODULE define_types_module(jlcxx::Module& types)
Expand All @@ -121,6 +132,9 @@ JLCXX_MODULE define_types_module(jlcxx::Module& types)
types.method("dynamic_message_c", [](const A& c) { return dynamic_cast<const C*>(&c)->data; });

types.method("take_ref", take_ref);

types.add_type<StaticBase>("StaticBase");
types.add_type<StaticDerived>("StaticDerived", jlcxx::julia_base_type<StaticBase>());
}

JLCXX_MODULE define_vsolver_module(jlcxx::Module& vsolver_mod)
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 @@ -15,7 +15,7 @@

#define JLCXX_VERSION_MAJOR 0
#define JLCXX_VERSION_MINOR 12
#define JLCXX_VERSION_PATCH 3
#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
12 changes: 11 additions & 1 deletion include/jlcxx/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,17 @@ struct DownCast
{
static inline void apply(Module& mod)
{
mod.method("cxxdowncast", [](SingletonType<DerivedT>, SuperT* base) { return dynamic_cast<DerivedT*>(base); });
mod.method("cxxdowncast", [](SingletonType<DerivedT>, SuperT* base)
{
if constexpr (std::is_polymorphic<DerivedT>::value)
{
return dynamic_cast<DerivedT*>(base);
}
else
{
return static_cast<DerivedT*>(base);
}
});
using newsuper_t = supertype<SuperT>;
if constexpr (!std::is_same<newsuper_t,SuperT>::value)
{
Expand Down

0 comments on commit 1e26ca8

Please sign in to comment.