7
7
namespace pybind11_tests {
8
8
namespace test_class_sh_with_alias {
9
9
10
+ template <int SerNo> // Using int as a trick to easily generate a series of types.
10
11
struct Abase {
11
12
int val = 0 ;
12
13
virtual ~Abase () = default ;
@@ -21,41 +22,65 @@ struct Abase {
21
22
Abase &operator =(Abase &&) = default ;
22
23
};
23
24
24
- struct AbaseAlias : Abase {
25
- using Abase::Abase;
25
+ template <int SerNo>
26
+ struct AbaseAlias : Abase<SerNo> {
27
+ using Abase<SerNo>::Abase;
26
28
27
29
int Add (int other_val) const override {
28
- PYBIND11_OVERRIDE_PURE (int , /* Return type */
29
- Abase, /* Parent class */
30
- Add, /* Name of function in C++ (must match Python name) */
30
+ PYBIND11_OVERRIDE_PURE (int , /* Return type */
31
+ Abase<SerNo> , /* Parent class */
32
+ Add, /* Name of function in C++ (must match Python name) */
31
33
other_val);
32
34
}
33
35
};
34
36
35
- int AddInCppRawPtr (const Abase *obj, int other_val) { return obj->Add (other_val) * 10 + 7 ; }
37
+ template <>
38
+ struct AbaseAlias <1 > : Abase<1 >, py::detail::virtual_overrider_self_life_support {
39
+ using Abase<1 >::Abase;
36
40
37
- int AddInCppSharedPtr (std::shared_ptr<Abase> obj, int other_val) {
41
+ int Add (int other_val) const override {
42
+ PYBIND11_OVERRIDE_PURE (int , /* Return type */
43
+ Abase<1 >, /* Parent class */
44
+ Add, /* Name of function in C++ (must match Python name) */
45
+ other_val);
46
+ }
47
+ };
48
+
49
+ template <int SerNo>
50
+ int AddInCppRawPtr (const Abase<SerNo> *obj, int other_val) {
51
+ return obj->Add (other_val) * 10 + 7 ;
52
+ }
53
+
54
+ template <int SerNo>
55
+ int AddInCppSharedPtr (std::shared_ptr<Abase<SerNo>> obj, int other_val) {
38
56
return obj->Add (other_val) * 100 + 11 ;
39
57
}
40
58
41
- int AddInCppUniquePtr (std::unique_ptr<Abase> obj, int other_val) {
59
+ template <int SerNo>
60
+ int AddInCppUniquePtr (std::unique_ptr<Abase<SerNo>> obj, int other_val) {
42
61
return obj->Add (other_val) * 100 + 13 ;
43
62
}
44
63
64
+ template <int SerNo>
65
+ void wrap (py::module_ m, const char *py_class_name) {
66
+ py::classh<Abase<SerNo>, AbaseAlias<SerNo>>(m, py_class_name)
67
+ .def (py::init<int >(), py::arg (" val" ))
68
+ .def (" Get" , &Abase<SerNo>::Get)
69
+ .def (" Add" , &Abase<SerNo>::Add, py::arg (" other_val" ));
70
+
71
+ m.def (" AddInCppRawPtr" , AddInCppRawPtr<SerNo>, py::arg (" obj" ), py::arg (" other_val" ));
72
+ m.def (" AddInCppSharedPtr" , AddInCppSharedPtr<SerNo>, py::arg (" obj" ), py::arg (" other_val" ));
73
+ m.def (" AddInCppUniquePtr" , AddInCppUniquePtr<SerNo>, py::arg (" obj" ), py::arg (" other_val" ));
74
+ }
75
+
45
76
} // namespace test_class_sh_with_alias
46
77
} // namespace pybind11_tests
47
78
48
- PYBIND11_SMART_HOLDER_TYPE_CASTERS (pybind11_tests::test_class_sh_with_alias::Abase)
79
+ PYBIND11_SMART_HOLDER_TYPE_CASTERS (pybind11_tests::test_class_sh_with_alias::Abase<0 >)
80
+ PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::test_class_sh_with_alias::Abase<1 >)
49
81
50
82
TEST_SUBMODULE(class_sh_with_alias, m) {
51
83
using namespace pybind11_tests ::test_class_sh_with_alias;
52
-
53
- py::classh<Abase, AbaseAlias>(m, " Abase" )
54
- .def (py::init<int >(), py::arg (" val" ))
55
- .def (" Get" , &Abase::Get)
56
- .def (" Add" , &Abase::Add, py::arg (" other_val" ));
57
-
58
- m.def (" AddInCppRawPtr" , AddInCppRawPtr, py::arg (" obj" ), py::arg (" other_val" ));
59
- m.def (" AddInCppSharedPtr" , AddInCppSharedPtr, py::arg (" obj" ), py::arg (" other_val" ));
60
- m.def (" AddInCppUniquePtr" , AddInCppUniquePtr, py::arg (" obj" ), py::arg (" other_val" ));
84
+ wrap<0 >(m, " Abase0" );
85
+ wrap<1 >(m, " Abase1" );
61
86
}
0 commit comments