Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 1 | /* |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 2 | tests/test_class.cpp -- test py::class_ definitions and basic functionality |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 3 | |
| 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
| 5 | |
| 6 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #include "pybind11_tests.h" |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 11 | #include "constructor_stats.h" |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 12 | #include "local_bindings.h" |
Jason Rhinelander | adbc811 | 2018-01-11 13:22:13 -0400 | [diff] [blame] | 13 | #include <pybind11/stl.h> |
| 14 | |
Wenzel Jakob | e2eca4f | 2018-11-09 20:14:53 +0100 | [diff] [blame] | 15 | #if defined(_MSC_VER) |
| 16 | # pragma warning(disable: 4324) // warning C4324: structure was padded due to alignment specifier |
| 17 | #endif |
| 18 | |
Jason Rhinelander | adbc811 | 2018-01-11 13:22:13 -0400 | [diff] [blame] | 19 | // test_brace_initialization |
| 20 | struct NoBraceInitialization { |
| 21 | NoBraceInitialization(std::vector<int> v) : vec{std::move(v)} {} |
| 22 | template <typename T> |
| 23 | NoBraceInitialization(std::initializer_list<T> l) : vec(l) {} |
| 24 | |
| 25 | std::vector<int> vec; |
| 26 | }; |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 27 | |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 28 | TEST_SUBMODULE(class_, m) { |
| 29 | // test_instance |
| 30 | struct NoConstructor { |
Francesco Biscani | ba33b2f | 2017-11-20 14:19:53 +0100 | [diff] [blame] | 31 | NoConstructor() = default; |
| 32 | NoConstructor(const NoConstructor &) = default; |
| 33 | NoConstructor(NoConstructor &&) = default; |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 34 | static NoConstructor *new_instance() { |
| 35 | auto *ptr = new NoConstructor(); |
| 36 | print_created(ptr, "via new_instance"); |
| 37 | return ptr; |
| 38 | } |
| 39 | ~NoConstructor() { print_destroyed(this); } |
| 40 | }; |
| 41 | |
| 42 | py::class_<NoConstructor>(m, "NoConstructor") |
| 43 | .def_static("new_instance", &NoConstructor::new_instance, "Return an instance"); |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 44 | |
| 45 | // test_inheritance |
| 46 | class Pet { |
| 47 | public: |
| 48 | Pet(const std::string &name, const std::string &species) |
| 49 | : m_name(name), m_species(species) {} |
| 50 | std::string name() const { return m_name; } |
| 51 | std::string species() const { return m_species; } |
| 52 | private: |
| 53 | std::string m_name; |
| 54 | std::string m_species; |
| 55 | }; |
| 56 | |
| 57 | class Dog : public Pet { |
| 58 | public: |
| 59 | Dog(const std::string &name) : Pet(name, "dog") {} |
| 60 | std::string bark() const { return "Woof!"; } |
| 61 | }; |
| 62 | |
| 63 | class Rabbit : public Pet { |
| 64 | public: |
| 65 | Rabbit(const std::string &name) : Pet(name, "parrot") {} |
| 66 | }; |
| 67 | |
| 68 | class Hamster : public Pet { |
| 69 | public: |
| 70 | Hamster(const std::string &name) : Pet(name, "rodent") {} |
| 71 | }; |
| 72 | |
| 73 | class Chimera : public Pet { |
| 74 | Chimera() : Pet("Kimmy", "chimera") {} |
| 75 | }; |
| 76 | |
| 77 | py::class_<Pet> pet_class(m, "Pet"); |
| 78 | pet_class |
| 79 | .def(py::init<std::string, std::string>()) |
| 80 | .def("name", &Pet::name) |
| 81 | .def("species", &Pet::species); |
| 82 | |
| 83 | /* One way of declaring a subclass relationship: reference parent's class_ object */ |
| 84 | py::class_<Dog>(m, "Dog", pet_class) |
| 85 | .def(py::init<std::string>()); |
| 86 | |
| 87 | /* Another way of declaring a subclass relationship: reference parent's C++ type */ |
| 88 | py::class_<Rabbit, Pet>(m, "Rabbit") |
| 89 | .def(py::init<std::string>()); |
| 90 | |
| 91 | /* And another: list parent in class template arguments */ |
| 92 | py::class_<Hamster, Pet>(m, "Hamster") |
| 93 | .def(py::init<std::string>()); |
| 94 | |
| 95 | /* Constructors are not inherited by default */ |
| 96 | py::class_<Chimera, Pet>(m, "Chimera"); |
| 97 | |
| 98 | m.def("pet_name_species", [](const Pet &pet) { return pet.name() + " is a " + pet.species(); }); |
| 99 | m.def("dog_bark", [](const Dog &dog) { return dog.bark(); }); |
| 100 | |
| 101 | // test_automatic_upcasting |
Francesco Biscani | ba33b2f | 2017-11-20 14:19:53 +0100 | [diff] [blame] | 102 | struct BaseClass { |
| 103 | BaseClass() = default; |
| 104 | BaseClass(const BaseClass &) = default; |
| 105 | BaseClass(BaseClass &&) = default; |
Henry Schreiner | b491b46 | 2020-09-10 23:15:22 -0400 | [diff] [blame] | 106 | virtual ~BaseClass() = default; |
Francesco Biscani | ba33b2f | 2017-11-20 14:19:53 +0100 | [diff] [blame] | 107 | }; |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 108 | struct DerivedClass1 : BaseClass { }; |
| 109 | struct DerivedClass2 : BaseClass { }; |
| 110 | |
| 111 | py::class_<BaseClass>(m, "BaseClass").def(py::init<>()); |
| 112 | py::class_<DerivedClass1>(m, "DerivedClass1").def(py::init<>()); |
| 113 | py::class_<DerivedClass2>(m, "DerivedClass2").def(py::init<>()); |
| 114 | |
| 115 | m.def("return_class_1", []() -> BaseClass* { return new DerivedClass1(); }); |
| 116 | m.def("return_class_2", []() -> BaseClass* { return new DerivedClass2(); }); |
| 117 | m.def("return_class_n", [](int n) -> BaseClass* { |
| 118 | if (n == 1) return new DerivedClass1(); |
| 119 | if (n == 2) return new DerivedClass2(); |
| 120 | return new BaseClass(); |
| 121 | }); |
| 122 | m.def("return_none", []() -> BaseClass* { return nullptr; }); |
| 123 | |
| 124 | // test_isinstance |
| 125 | m.def("check_instances", [](py::list l) { |
| 126 | return py::make_tuple( |
| 127 | py::isinstance<py::tuple>(l[0]), |
| 128 | py::isinstance<py::dict>(l[1]), |
| 129 | py::isinstance<Pet>(l[2]), |
| 130 | py::isinstance<Pet>(l[3]), |
| 131 | py::isinstance<Dog>(l[4]), |
| 132 | py::isinstance<Rabbit>(l[5]), |
| 133 | py::isinstance<UnregisteredType>(l[6]) |
| 134 | ); |
| 135 | }); |
| 136 | |
Henry Schreiner | f12ec00 | 2020-09-14 18:06:26 -0400 | [diff] [blame] | 137 | struct Invalid {}; |
| 138 | |
| 139 | // test_type |
| 140 | m.def("check_type", [](int category) { |
| 141 | // Currently not supported (via a fail at compile time) |
| 142 | // See https://github.com/pybind/pybind11/issues/2486 |
| 143 | // if (category == 2) |
| 144 | // return py::type::of<int>(); |
| 145 | if (category == 1) |
| 146 | return py::type::of<DerivedClass1>(); |
| 147 | else |
| 148 | return py::type::of<Invalid>(); |
| 149 | }); |
| 150 | |
| 151 | m.def("get_type_of", [](py::object ob) { |
| 152 | return py::type::of(ob); |
| 153 | }); |
| 154 | |
Henry Fredrick Schreiner | 11f756f | 2020-09-16 22:02:09 -0400 | [diff] [blame] | 155 | m.def("get_type_classic", [](py::handle h) { |
| 156 | return h.get_type(); |
| 157 | }); |
| 158 | |
Henry Schreiner | f12ec00 | 2020-09-14 18:06:26 -0400 | [diff] [blame] | 159 | m.def("as_type", [](py::object ob) { |
| 160 | auto tp = py::type(ob); |
| 161 | if (py::isinstance<py::type>(ob)) |
| 162 | return tp; |
| 163 | else |
| 164 | throw std::runtime_error("Invalid type"); |
| 165 | }); |
| 166 | |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 167 | // test_mismatched_holder |
| 168 | struct MismatchBase1 { }; |
| 169 | struct MismatchDerived1 : MismatchBase1 { }; |
| 170 | |
| 171 | struct MismatchBase2 { }; |
| 172 | struct MismatchDerived2 : MismatchBase2 { }; |
| 173 | |
| 174 | m.def("mismatched_holder_1", []() { |
Henry Schreiner | 6bcd220 | 2020-10-03 13:38:03 -0400 | [diff] [blame^] | 175 | auto mod = py::module_::import("__main__"); |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 176 | py::class_<MismatchBase1, std::shared_ptr<MismatchBase1>>(mod, "MismatchBase1"); |
| 177 | py::class_<MismatchDerived1, MismatchBase1>(mod, "MismatchDerived1"); |
| 178 | }); |
| 179 | m.def("mismatched_holder_2", []() { |
Henry Schreiner | 6bcd220 | 2020-10-03 13:38:03 -0400 | [diff] [blame^] | 180 | auto mod = py::module_::import("__main__"); |
Dean Moldovan | 0bc272b | 2017-06-22 23:42:11 +0200 | [diff] [blame] | 181 | py::class_<MismatchBase2>(mod, "MismatchBase2"); |
| 182 | py::class_<MismatchDerived2, std::shared_ptr<MismatchDerived2>, |
| 183 | MismatchBase2>(mod, "MismatchDerived2"); |
| 184 | }); |
| 185 | |
| 186 | // test_override_static |
| 187 | // #511: problem with inheritance + overwritten def_static |
| 188 | struct MyBase { |
| 189 | static std::unique_ptr<MyBase> make() { |
| 190 | return std::unique_ptr<MyBase>(new MyBase()); |
| 191 | } |
| 192 | }; |
| 193 | |
| 194 | struct MyDerived : MyBase { |
| 195 | static std::unique_ptr<MyDerived> make() { |
| 196 | return std::unique_ptr<MyDerived>(new MyDerived()); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | py::class_<MyBase>(m, "MyBase") |
| 201 | .def_static("make", &MyBase::make); |
| 202 | |
| 203 | py::class_<MyDerived, MyBase>(m, "MyDerived") |
| 204 | .def_static("make", &MyDerived::make) |
| 205 | .def_static("make2", &MyDerived::make); |
Dean Moldovan | af2dda3 | 2017-06-26 20:34:06 +0200 | [diff] [blame] | 206 | |
| 207 | // test_implicit_conversion_life_support |
| 208 | struct ConvertibleFromUserType { |
| 209 | int i; |
| 210 | |
| 211 | ConvertibleFromUserType(UserType u) : i(u.value()) { } |
| 212 | }; |
| 213 | |
| 214 | py::class_<ConvertibleFromUserType>(m, "AcceptsUserType") |
| 215 | .def(py::init<UserType>()); |
| 216 | py::implicitly_convertible<UserType, ConvertibleFromUserType>(); |
| 217 | |
| 218 | m.def("implicitly_convert_argument", [](const ConvertibleFromUserType &r) { return r.i; }); |
| 219 | m.def("implicitly_convert_variable", [](py::object o) { |
| 220 | // `o` is `UserType` and `r` is a reference to a temporary created by implicit |
| 221 | // conversion. This is valid when called inside a bound function because the temp |
| 222 | // object is attached to the same life support system as the arguments. |
| 223 | const auto &r = o.cast<const ConvertibleFromUserType &>(); |
| 224 | return r.i; |
| 225 | }); |
| 226 | m.add_object("implicitly_convert_variable_fail", [&] { |
| 227 | auto f = [](PyObject *, PyObject *args) -> PyObject * { |
| 228 | auto o = py::reinterpret_borrow<py::tuple>(args)[0]; |
| 229 | try { // It should fail here because there is no life support. |
| 230 | o.cast<const ConvertibleFromUserType &>(); |
| 231 | } catch (const py::cast_error &e) { |
| 232 | return py::str(e.what()).release().ptr(); |
| 233 | } |
| 234 | return py::str().release().ptr(); |
| 235 | }; |
| 236 | |
| 237 | auto def = new PyMethodDef{"f", f, METH_VARARGS, nullptr}; |
| 238 | return py::reinterpret_steal<py::object>(PyCFunction_NewEx(def, nullptr, m.ptr())); |
| 239 | }()); |
Jason Rhinelander | a03408c | 2017-07-23 00:32:58 -0400 | [diff] [blame] | 240 | |
| 241 | // test_operator_new_delete |
| 242 | struct HasOpNewDel { |
| 243 | std::uint64_t i; |
| 244 | static void *operator new(size_t s) { py::print("A new", s); return ::operator new(s); } |
| 245 | static void *operator new(size_t s, void *ptr) { py::print("A placement-new", s); return ptr; } |
| 246 | static void operator delete(void *p) { py::print("A delete"); return ::operator delete(p); } |
| 247 | }; |
| 248 | struct HasOpNewDelSize { |
| 249 | std::uint32_t i; |
| 250 | static void *operator new(size_t s) { py::print("B new", s); return ::operator new(s); } |
| 251 | static void *operator new(size_t s, void *ptr) { py::print("B placement-new", s); return ptr; } |
| 252 | static void operator delete(void *p, size_t s) { py::print("B delete", s); return ::operator delete(p); } |
| 253 | }; |
| 254 | struct AliasedHasOpNewDelSize { |
| 255 | std::uint64_t i; |
| 256 | static void *operator new(size_t s) { py::print("C new", s); return ::operator new(s); } |
| 257 | static void *operator new(size_t s, void *ptr) { py::print("C placement-new", s); return ptr; } |
| 258 | static void operator delete(void *p, size_t s) { py::print("C delete", s); return ::operator delete(p); } |
| 259 | virtual ~AliasedHasOpNewDelSize() = default; |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 260 | AliasedHasOpNewDelSize() = default; |
| 261 | AliasedHasOpNewDelSize(const AliasedHasOpNewDelSize&) = delete; |
Jason Rhinelander | a03408c | 2017-07-23 00:32:58 -0400 | [diff] [blame] | 262 | }; |
| 263 | struct PyAliasedHasOpNewDelSize : AliasedHasOpNewDelSize { |
| 264 | PyAliasedHasOpNewDelSize() = default; |
| 265 | PyAliasedHasOpNewDelSize(int) { } |
| 266 | std::uint64_t j; |
| 267 | }; |
| 268 | struct HasOpNewDelBoth { |
| 269 | std::uint32_t i[8]; |
| 270 | static void *operator new(size_t s) { py::print("D new", s); return ::operator new(s); } |
| 271 | static void *operator new(size_t s, void *ptr) { py::print("D placement-new", s); return ptr; } |
| 272 | static void operator delete(void *p) { py::print("D delete"); return ::operator delete(p); } |
| 273 | static void operator delete(void *p, size_t s) { py::print("D wrong delete", s); return ::operator delete(p); } |
| 274 | }; |
| 275 | py::class_<HasOpNewDel>(m, "HasOpNewDel").def(py::init<>()); |
| 276 | py::class_<HasOpNewDelSize>(m, "HasOpNewDelSize").def(py::init<>()); |
| 277 | py::class_<HasOpNewDelBoth>(m, "HasOpNewDelBoth").def(py::init<>()); |
| 278 | py::class_<AliasedHasOpNewDelSize, PyAliasedHasOpNewDelSize> aliased(m, "AliasedHasOpNewDelSize"); |
| 279 | aliased.def(py::init<>()); |
| 280 | aliased.attr("size_noalias") = py::int_(sizeof(AliasedHasOpNewDelSize)); |
| 281 | aliased.attr("size_alias") = py::int_(sizeof(PyAliasedHasOpNewDelSize)); |
Jason Rhinelander | 7437c69 | 2017-07-28 22:03:44 -0400 | [diff] [blame] | 282 | |
| 283 | // This test is actually part of test_local_bindings (test_duplicate_local), but we need a |
| 284 | // definition in a different compilation unit within the same module: |
| 285 | bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local()); |
Dean Moldovan | 234f7c3 | 2017-08-17 17:03:46 +0200 | [diff] [blame] | 286 | |
| 287 | // test_bind_protected_functions |
| 288 | class ProtectedA { |
| 289 | protected: |
| 290 | int foo() const { return value; } |
| 291 | |
| 292 | private: |
| 293 | int value = 42; |
| 294 | }; |
| 295 | |
| 296 | class PublicistA : public ProtectedA { |
| 297 | public: |
| 298 | using ProtectedA::foo; |
| 299 | }; |
| 300 | |
| 301 | py::class_<ProtectedA>(m, "ProtectedA") |
| 302 | .def(py::init<>()) |
| 303 | #if !defined(_MSC_VER) || _MSC_VER >= 1910 |
| 304 | .def("foo", &PublicistA::foo); |
| 305 | #else |
| 306 | .def("foo", static_cast<int (ProtectedA::*)() const>(&PublicistA::foo)); |
| 307 | #endif |
| 308 | |
| 309 | class ProtectedB { |
| 310 | public: |
| 311 | virtual ~ProtectedB() = default; |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 312 | ProtectedB() = default; |
| 313 | ProtectedB(const ProtectedB &) = delete; |
Dean Moldovan | 234f7c3 | 2017-08-17 17:03:46 +0200 | [diff] [blame] | 314 | |
| 315 | protected: |
| 316 | virtual int foo() const { return value; } |
| 317 | |
| 318 | private: |
| 319 | int value = 42; |
| 320 | }; |
| 321 | |
| 322 | class TrampolineB : public ProtectedB { |
| 323 | public: |
Yannick Jadoul | d65e34d | 2020-09-15 14:56:20 +0200 | [diff] [blame] | 324 | int foo() const override { PYBIND11_OVERRIDE(int, ProtectedB, foo, ); } |
Dean Moldovan | 234f7c3 | 2017-08-17 17:03:46 +0200 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | class PublicistB : public ProtectedB { |
| 328 | public: |
| 329 | using ProtectedB::foo; |
| 330 | }; |
| 331 | |
| 332 | py::class_<ProtectedB, TrampolineB>(m, "ProtectedB") |
| 333 | .def(py::init<>()) |
| 334 | #if !defined(_MSC_VER) || _MSC_VER >= 1910 |
| 335 | .def("foo", &PublicistB::foo); |
| 336 | #else |
| 337 | .def("foo", static_cast<int (ProtectedB::*)() const>(&PublicistB::foo)); |
| 338 | #endif |
Wenzel Jakob | 4336a7d | 2017-08-21 22:48:28 +0200 | [diff] [blame] | 339 | |
| 340 | // test_brace_initialization |
| 341 | struct BraceInitialization { |
| 342 | int field1; |
| 343 | std::string field2; |
| 344 | }; |
| 345 | |
| 346 | py::class_<BraceInitialization>(m, "BraceInitialization") |
| 347 | .def(py::init<int, const std::string &>()) |
| 348 | .def_readwrite("field1", &BraceInitialization::field1) |
| 349 | .def_readwrite("field2", &BraceInitialization::field2); |
Jason Rhinelander | adbc811 | 2018-01-11 13:22:13 -0400 | [diff] [blame] | 350 | // We *don't* want to construct using braces when the given constructor argument maps to a |
| 351 | // constructor, because brace initialization could go to the wrong place (in particular when |
| 352 | // there is also an `initializer_list<T>`-accept constructor): |
| 353 | py::class_<NoBraceInitialization>(m, "NoBraceInitialization") |
| 354 | .def(py::init<std::vector<int>>()) |
| 355 | .def_readonly("vec", &NoBraceInitialization::vec); |
Wenzel Jakob | 8ed5b8a | 2017-08-28 16:34:06 +0200 | [diff] [blame] | 356 | |
| 357 | // test_reentrant_implicit_conversion_failure |
| 358 | // #1035: issue with runaway reentrant implicit conversion |
| 359 | struct BogusImplicitConversion { |
Henry Schreiner | b491b46 | 2020-09-10 23:15:22 -0400 | [diff] [blame] | 360 | BogusImplicitConversion(const BogusImplicitConversion &) = default; |
Wenzel Jakob | 8ed5b8a | 2017-08-28 16:34:06 +0200 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | py::class_<BogusImplicitConversion>(m, "BogusImplicitConversion") |
| 364 | .def(py::init<const BogusImplicitConversion &>()); |
| 365 | |
| 366 | py::implicitly_convertible<int, BogusImplicitConversion>(); |
Jason Rhinelander | 7117892 | 2017-11-07 12:33:05 -0400 | [diff] [blame] | 367 | |
| 368 | // test_qualname |
| 369 | // #1166: nested class docstring doesn't show nested name |
| 370 | // Also related: tests that __qualname__ is set properly |
| 371 | struct NestBase {}; |
| 372 | struct Nested {}; |
| 373 | py::class_<NestBase> base(m, "NestBase"); |
| 374 | base.def(py::init<>()); |
| 375 | py::class_<Nested>(base, "Nested") |
| 376 | .def(py::init<>()) |
| 377 | .def("fn", [](Nested &, int, NestBase &, Nested &) {}) |
| 378 | .def("fa", [](Nested &, int, NestBase &, Nested &) {}, |
| 379 | "a"_a, "b"_a, "c"_a); |
| 380 | base.def("g", [](NestBase &, Nested &) {}); |
| 381 | base.def("h", []() { return NestBase(); }); |
oremanj | e7761e3 | 2018-09-25 14:55:18 -0700 | [diff] [blame] | 382 | |
| 383 | // test_error_after_conversion |
| 384 | // The second-pass path through dispatcher() previously didn't |
| 385 | // remember which overload was used, and would crash trying to |
| 386 | // generate a useful error message |
| 387 | |
| 388 | struct NotRegistered {}; |
| 389 | struct StringWrapper { std::string str; }; |
| 390 | m.def("test_error_after_conversions", [](int) {}); |
| 391 | m.def("test_error_after_conversions", |
| 392 | [](StringWrapper) -> NotRegistered { return {}; }); |
| 393 | py::class_<StringWrapper>(m, "StringWrapper").def(py::init<std::string>()); |
| 394 | py::implicitly_convertible<std::string, StringWrapper>(); |
Wenzel Jakob | e2eca4f | 2018-11-09 20:14:53 +0100 | [diff] [blame] | 395 | |
| 396 | #if defined(PYBIND11_CPP17) |
| 397 | struct alignas(1024) Aligned { |
| 398 | std::uintptr_t ptr() const { return (uintptr_t) this; } |
| 399 | }; |
| 400 | py::class_<Aligned>(m, "Aligned") |
| 401 | .def(py::init<>()) |
| 402 | .def("ptr", &Aligned::ptr); |
| 403 | #endif |
Dustin Spicuzza | 0dfffcf | 2020-04-05 02:34:00 -0400 | [diff] [blame] | 404 | |
| 405 | // test_final |
| 406 | struct IsFinal final {}; |
| 407 | py::class_<IsFinal>(m, "IsFinal", py::is_final()); |
| 408 | |
| 409 | // test_non_final_final |
| 410 | struct IsNonFinalFinal {}; |
| 411 | py::class_<IsNonFinalFinal>(m, "IsNonFinalFinal", py::is_final()); |
jbarlow83 | 4d90f1a | 2020-07-31 17:46:12 -0700 | [diff] [blame] | 412 | |
| 413 | struct PyPrintDestructor { |
Henry Schreiner | b491b46 | 2020-09-10 23:15:22 -0400 | [diff] [blame] | 414 | PyPrintDestructor() = default; |
jbarlow83 | 4d90f1a | 2020-07-31 17:46:12 -0700 | [diff] [blame] | 415 | ~PyPrintDestructor() { |
| 416 | py::print("Print from destructor"); |
| 417 | } |
| 418 | void throw_something() { throw std::runtime_error("error"); } |
| 419 | }; |
| 420 | py::class_<PyPrintDestructor>(m, "PyPrintDestructor") |
| 421 | .def(py::init<>()) |
| 422 | .def("throw_something", &PyPrintDestructor::throw_something); |
Riyaz Haque | 2b6b98e | 2020-10-02 10:06:04 -0700 | [diff] [blame] | 423 | |
| 424 | struct SamePointer {}; |
| 425 | static SamePointer samePointer; |
| 426 | py::class_<SamePointer, std::unique_ptr<SamePointer, py::nodelete>>(m, "SamePointer") |
| 427 | .def(py::init([]() { return &samePointer; })) |
| 428 | .def("__del__", [](SamePointer&) { py::print("__del__ called"); }); |
| 429 | |
| 430 | struct Empty {}; |
| 431 | py::class_<Empty>(m, "Empty") |
| 432 | .def(py::init<>()); |
Dean Moldovan | 83e328f | 2017-06-09 00:44:49 +0200 | [diff] [blame] | 433 | } |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 434 | |
Henry Schreiner | e428a7f | 2020-07-23 21:16:54 -0400 | [diff] [blame] | 435 | template <int N> class BreaksBase { public: |
| 436 | virtual ~BreaksBase() = default; |
| 437 | BreaksBase() = default; |
| 438 | BreaksBase(const BreaksBase&) = delete; |
| 439 | }; |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 440 | template <int N> class BreaksTramp : public BreaksBase<N> {}; |
| 441 | // These should all compile just fine: |
Henry Schreiner | b342c37 | 2020-09-10 22:57:10 -0400 | [diff] [blame] | 442 | using DoesntBreak1 = py::class_<BreaksBase<1>, std::unique_ptr<BreaksBase<1>>, BreaksTramp<1>>; |
| 443 | using DoesntBreak2 = py::class_<BreaksBase<2>, BreaksTramp<2>, std::unique_ptr<BreaksBase<2>>>; |
| 444 | using DoesntBreak3 = py::class_<BreaksBase<3>, std::unique_ptr<BreaksBase<3>>>; |
| 445 | using DoesntBreak4 = py::class_<BreaksBase<4>, BreaksTramp<4>>; |
| 446 | using DoesntBreak5 = py::class_<BreaksBase<5>>; |
| 447 | using DoesntBreak6 = py::class_<BreaksBase<6>, std::shared_ptr<BreaksBase<6>>, BreaksTramp<6>>; |
| 448 | using DoesntBreak7 = py::class_<BreaksBase<7>, BreaksTramp<7>, std::shared_ptr<BreaksBase<7>>>; |
| 449 | using DoesntBreak8 = py::class_<BreaksBase<8>, std::shared_ptr<BreaksBase<8>>>; |
Jason Rhinelander | 5fffe20 | 2016-09-06 12:17:06 -0400 | [diff] [blame] | 450 | #define CHECK_BASE(N) static_assert(std::is_same<typename DoesntBreak##N::type, BreaksBase<N>>::value, \ |
| 451 | "DoesntBreak" #N " has wrong type!") |
| 452 | CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK_BASE(6); CHECK_BASE(7); CHECK_BASE(8); |
| 453 | #define CHECK_ALIAS(N) static_assert(DoesntBreak##N::has_alias && std::is_same<typename DoesntBreak##N::type_alias, BreaksTramp<N>>::value, \ |
| 454 | "DoesntBreak" #N " has wrong type_alias!") |
| 455 | #define CHECK_NOALIAS(N) static_assert(!DoesntBreak##N::has_alias && std::is_void<typename DoesntBreak##N::type_alias>::value, \ |
| 456 | "DoesntBreak" #N " has type alias, but shouldn't!") |
| 457 | CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8); |
| 458 | #define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \ |
| 459 | "DoesntBreak" #N " has wrong holder_type!") |
| 460 | CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique); |
| 461 | CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared); |
| 462 | |
| 463 | // There's no nice way to test that these fail because they fail to compile; leave them here, |
| 464 | // though, so that they can be manually tested by uncommenting them (and seeing that compilation |
| 465 | // failures occurs). |
| 466 | |
| 467 | // We have to actually look into the type: the typedef alone isn't enough to instantiate the type: |
| 468 | #define CHECK_BROKEN(N) static_assert(std::is_same<typename Breaks##N::type, BreaksBase<-N>>::value, \ |
| 469 | "Breaks1 has wrong type!"); |
| 470 | |
| 471 | //// Two holder classes: |
| 472 | //typedef py::class_<BreaksBase<-1>, std::unique_ptr<BreaksBase<-1>>, std::unique_ptr<BreaksBase<-1>>> Breaks1; |
| 473 | //CHECK_BROKEN(1); |
| 474 | //// Two aliases: |
| 475 | //typedef py::class_<BreaksBase<-2>, BreaksTramp<-2>, BreaksTramp<-2>> Breaks2; |
| 476 | //CHECK_BROKEN(2); |
| 477 | //// Holder + 2 aliases |
| 478 | //typedef py::class_<BreaksBase<-3>, std::unique_ptr<BreaksBase<-3>>, BreaksTramp<-3>, BreaksTramp<-3>> Breaks3; |
| 479 | //CHECK_BROKEN(3); |
| 480 | //// Alias + 2 holders |
| 481 | //typedef py::class_<BreaksBase<-4>, std::unique_ptr<BreaksBase<-4>>, BreaksTramp<-4>, std::shared_ptr<BreaksBase<-4>>> Breaks4; |
| 482 | //CHECK_BROKEN(4); |
| 483 | //// Invalid option (not a subclass or holder) |
| 484 | //typedef py::class_<BreaksBase<-5>, BreaksTramp<-4>> Breaks5; |
| 485 | //CHECK_BROKEN(5); |
| 486 | //// Invalid option: multiple inheritance not supported: |
| 487 | //template <> struct BreaksBase<-8> : BreaksBase<-6>, BreaksBase<-7> {}; |
| 488 | //typedef py::class_<BreaksBase<-8>, BreaksBase<-6>, BreaksBase<-7>> Breaks8; |
| 489 | //CHECK_BROKEN(8); |