Skip to content

Commit 7ec6640

Browse files
authored
Merge pull request #238 from robotpy/constexpr-constructor
Add testcase with constexpr constructor and trampoline
2 parents 77fce67 + 49b46e7 commit 7ec6640

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

robotpy_build/autowrap/render_cls_rpy_include.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _render_cls_trampoline(
194194
r.write_trim(
195195
f"""
196196
template <typename PyTrampolineBase{ precomma(template_parameter_list) }, typename PyTrampolineCfg>
197-
struct PyTrampoline_{ cls.full_cpp_name_identifier } : PyTrampolineBase, virtual py::trampoline_self_life_support {{
197+
struct PyTrampoline_{ cls.full_cpp_name_identifier } : PyTrampolineBase {{
198198
using PyTrampolineBase::PyTrampolineBase;
199199
"""
200200
)

robotpy_build/autowrap/render_pybind11.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,16 @@ def cls_consts(r: RenderBuffer, cls: ClassContext):
292292
def cls_decl(r: RenderBuffer, cls: ClassContext):
293293
if cls.trampoline:
294294
tctx = cls.trampoline
295-
r.writeln(f"using {tctx.var} = {tctx.full_cpp_name};")
295+
# py::trampoline_self_life_support
296+
r.write_trim(
297+
f"""
298+
struct {tctx.var} : {tctx.full_cpp_name}, py::trampoline_self_life_support {{
299+
using RpyBase = {tctx.full_cpp_name};
300+
using RpyBase::RpyBase;
301+
}};
302+
303+
"""
304+
)
296305
r.writeln(
297306
f'static_assert(std::is_abstract<{tctx.var}>::value == false, "{cls.full_cpp_name} " RPYBUILD_BAD_TRAMPOLINE);'
298307
)

tests/cpp/rpytest/ft/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ClassWithFields,
88
ClassWithIgnored,
99
ClassWithTrampoline,
10+
ConstexprTrampoline,
1011
DocAppendClass,
1112
DocAppendEnum,
1213
DocClass,
@@ -123,6 +124,7 @@
123124
"ClassWithFields",
124125
"ClassWithIgnored",
125126
"ClassWithTrampoline",
127+
"ConstexprTrampoline",
126128
"DocAppendClass",
127129
"DocAppendEnum",
128130
"DocClass",

tests/cpp/rpytest/ft/include/trampoline.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,19 @@ struct ClassWithTrampoline {
3838
// bug: ensure this doesn't get forwarded
3939
ClassWithTrampoline(const int &name) {}
4040
};
41+
42+
43+
struct ConstexprTrampoline {
44+
constexpr ConstexprTrampoline() = default;
45+
constexpr virtual ~ConstexprTrampoline() = default;
46+
constexpr virtual int fn() const = 0;
47+
};
48+
49+
struct ChildConstexprTrampoline : ConstexprTrampoline {
50+
constexpr ChildConstexprTrampoline(int i) : something(i) {}
51+
constexpr int fn() const override {
52+
return 1;
53+
}
54+
55+
int something;
56+
};

tests/test_ft_trampoline.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from rpytest.ft import ClassWithTrampoline
1+
from rpytest.ft import ClassWithTrampoline, ConstexprTrampoline
22

33

44
def test_class_with_trampoline():
@@ -25,3 +25,7 @@ def fnWithMoveOnlyParam(self, i):
2525

2626
c = PyClassWithTrampoline()
2727
assert ClassWithTrampoline.check_moveonly(c) == 8
28+
29+
30+
def test_constexpr_trampoline():
31+
ConstexprTrampoline()

0 commit comments

Comments
 (0)