From 1e26ca8b8c669a7b3b2e2367439c6e0f840ed451 Mon Sep 17 00:00:00 2001 From: Bart Janssens Date: Sat, 1 Jun 2024 14:10:36 +0200 Subject: [PATCH] Fix static inheritance problem (#157) Issue Regression with 0.12.3 in the support of static inheritance #156 --- examples/inheritance.cpp | 14 ++++++++++++++ include/jlcxx/jlcxx_config.hpp | 2 +- include/jlcxx/module.hpp | 12 +++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/examples/inheritance.cpp b/examples/inheritance.cpp index 56e1f51..db5b832 100644 --- a/examples/inheritance.cpp +++ b/examples/inheritance.cpp @@ -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 { @@ -100,6 +107,10 @@ namespace jlcxx template<> struct SuperType { typedef virtualsolver::Base type; }; template<> struct SuperType { typedef virtualsolver::Base type; }; + + template<> struct IsMirroredType : std::false_type { }; + template<> struct IsMirroredType : std::false_type { }; + template<> struct SuperType { typedef StaticBase type; }; } JLCXX_MODULE define_types_module(jlcxx::Module& types) @@ -121,6 +132,9 @@ JLCXX_MODULE define_types_module(jlcxx::Module& types) types.method("dynamic_message_c", [](const A& c) { return dynamic_cast(&c)->data; }); types.method("take_ref", take_ref); + + types.add_type("StaticBase"); + types.add_type("StaticDerived", jlcxx::julia_base_type()); } JLCXX_MODULE define_vsolver_module(jlcxx::Module& vsolver_mod) diff --git a/include/jlcxx/jlcxx_config.hpp b/include/jlcxx/jlcxx_config.hpp index 4b68ea9..5922cbf 100644 --- a/include/jlcxx/jlcxx_config.hpp +++ b/include/jlcxx/jlcxx_config.hpp @@ -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 diff --git a/include/jlcxx/module.hpp b/include/jlcxx/module.hpp index 0726542..3a35f78 100644 --- a/include/jlcxx/module.hpp +++ b/include/jlcxx/module.hpp @@ -1035,7 +1035,17 @@ struct DownCast { static inline void apply(Module& mod) { - mod.method("cxxdowncast", [](SingletonType, SuperT* base) { return dynamic_cast(base); }); + mod.method("cxxdowncast", [](SingletonType, SuperT* base) + { + if constexpr (std::is_polymorphic::value) + { + return dynamic_cast(base); + } + else + { + return static_cast(base); + } + }); using newsuper_t = supertype; if constexpr (!std::is_same::value) {