12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include "pybind11_tests.h"
- #include "constructor_stats.h"
- #include <functional>
- #include <list>
- std::list<std::function<void(py::module_ &)>> &initializers() {
- static std::list<std::function<void(py::module_ &)>> inits;
- return inits;
- }
- test_initializer::test_initializer(Initializer init) {
- initializers().emplace_back(init);
- }
- test_initializer::test_initializer(const char *submodule_name, Initializer init) {
- initializers().emplace_back([=](py::module_ &parent) {
- auto m = parent.def_submodule(submodule_name);
- init(m);
- });
- }
- void bind_ConstructorStats(py::module_ &m) {
- py::class_<ConstructorStats>(m, "ConstructorStats")
- .def("alive", &ConstructorStats::alive)
- .def("values", &ConstructorStats::values)
- .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
- .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
- .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
- .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
- .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
- .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal)
-
-
- .def_static("detail_reg_inst", []() {
- ConstructorStats::gc();
- return py::detail::get_internals().registered_instances.size();
- })
- ;
- }
- PYBIND11_MODULE(pybind11_tests, m) {
- m.doc() = "pybind11 test module";
- bind_ConstructorStats(m);
- #if !defined(NDEBUG)
- m.attr("debug_enabled") = true;
- #else
- m.attr("debug_enabled") = false;
- #endif
- py::class_<UserType>(m, "UserType", "A `py::class_` type for testing")
- .def(py::init<>())
- .def(py::init<int>())
- .def("get_value", &UserType::value, "Get value using a method")
- .def("set_value", &UserType::set, "Set value using a method")
- .def_property("value", &UserType::value, &UserType::set, "Get/set value using a property")
- .def("__repr__", [](const UserType& u) { return "UserType({})"_s.format(u.value()); });
- py::class_<IncType, UserType>(m, "IncType")
- .def(py::init<>())
- .def(py::init<int>())
- .def("__repr__", [](const IncType& u) { return "IncType({})"_s.format(u.value()); });
- for (const auto &initializer : initializers())
- initializer(m);
- }
|