Skip to content

Commit

Permalink
Merge pull request #238 from robotpy/constexpr-constructor
Browse files Browse the repository at this point in the history
Add testcase with constexpr constructor and trampoline
  • Loading branch information
virtuald authored Nov 20, 2024
2 parents 77fce67 + 49b46e7 commit 7ec6640
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion robotpy_build/autowrap/render_cls_rpy_include.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _render_cls_trampoline(
r.write_trim(
f"""
template <typename PyTrampolineBase{ precomma(template_parameter_list) }, typename PyTrampolineCfg>
struct PyTrampoline_{ cls.full_cpp_name_identifier } : PyTrampolineBase, virtual py::trampoline_self_life_support {{
struct PyTrampoline_{ cls.full_cpp_name_identifier } : PyTrampolineBase {{
using PyTrampolineBase::PyTrampolineBase;
"""
)
Expand Down
11 changes: 10 additions & 1 deletion robotpy_build/autowrap/render_pybind11.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,16 @@ def cls_consts(r: RenderBuffer, cls: ClassContext):
def cls_decl(r: RenderBuffer, cls: ClassContext):
if cls.trampoline:
tctx = cls.trampoline
r.writeln(f"using {tctx.var} = {tctx.full_cpp_name};")
# py::trampoline_self_life_support
r.write_trim(
f"""
struct {tctx.var} : {tctx.full_cpp_name}, py::trampoline_self_life_support {{
using RpyBase = {tctx.full_cpp_name};
using RpyBase::RpyBase;
}};
"""
)
r.writeln(
f'static_assert(std::is_abstract<{tctx.var}>::value == false, "{cls.full_cpp_name} " RPYBUILD_BAD_TRAMPOLINE);'
)
Expand Down
2 changes: 2 additions & 0 deletions tests/cpp/rpytest/ft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ClassWithFields,
ClassWithIgnored,
ClassWithTrampoline,
ConstexprTrampoline,
DocAppendClass,
DocAppendEnum,
DocClass,
Expand Down Expand Up @@ -123,6 +124,7 @@
"ClassWithFields",
"ClassWithIgnored",
"ClassWithTrampoline",
"ConstexprTrampoline",
"DocAppendClass",
"DocAppendEnum",
"DocClass",
Expand Down
16 changes: 16 additions & 0 deletions tests/cpp/rpytest/ft/include/trampoline.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ struct ClassWithTrampoline {
// bug: ensure this doesn't get forwarded
ClassWithTrampoline(const int &name) {}
};


struct ConstexprTrampoline {
constexpr ConstexprTrampoline() = default;
constexpr virtual ~ConstexprTrampoline() = default;
constexpr virtual int fn() const = 0;
};

struct ChildConstexprTrampoline : ConstexprTrampoline {
constexpr ChildConstexprTrampoline(int i) : something(i) {}
constexpr int fn() const override {
return 1;
}

int something;
};
6 changes: 5 additions & 1 deletion tests/test_ft_trampoline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rpytest.ft import ClassWithTrampoline
from rpytest.ft import ClassWithTrampoline, ConstexprTrampoline


def test_class_with_trampoline():
Expand All @@ -25,3 +25,7 @@ def fnWithMoveOnlyParam(self, i):

c = PyClassWithTrampoline()
assert ClassWithTrampoline.check_moveonly(c) == 8


def test_constexpr_trampoline():
ConstexprTrampoline()

0 comments on commit 7ec6640

Please sign in to comment.