You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.3 KiB

8 years ago
8 years ago
8 years ago
  1. /*
  2. tests/pybind11_tests.cpp -- pybind example plugin
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. /*
  10. For testing purposes, we define a static global variable here in a function that each individual
  11. test .cpp calls with its initialization lambda. It's convenient here because we can just not
  12. compile some test files to disable/ignore some of the test code.
  13. It is NOT recommended as a way to use pybind11 in practice, however: the initialization order will
  14. be essentially random, which is okay for our test scripts (there are no dependencies between the
  15. individual pybind11 test .cpp files), but most likely not what you want when using pybind11
  16. productively.
  17. Instead, see the "How can I reduce the build time?" question in the "Frequently asked questions"
  18. section of the documentation for good practice on splitting binding code over multiple files.
  19. */
  20. std::list<std::function<void(py::module &)>> &initializers() {
  21. static std::list<std::function<void(py::module &)>> inits;
  22. return inits;
  23. }
  24. test_initializer::test_initializer(std::function<void(py::module &)> initializer) {
  25. initializers().push_back(std::move(initializer));
  26. }
  27. void bind_ConstructorStats(py::module &m) {
  28. py::class_<ConstructorStats>(m, "ConstructorStats")
  29. .def("alive", &ConstructorStats::alive)
  30. .def("values", &ConstructorStats::values)
  31. .def_readwrite("default_constructions", &ConstructorStats::default_constructions)
  32. .def_readwrite("copy_assignments", &ConstructorStats::copy_assignments)
  33. .def_readwrite("move_assignments", &ConstructorStats::move_assignments)
  34. .def_readwrite("copy_constructions", &ConstructorStats::copy_constructions)
  35. .def_readwrite("move_constructions", &ConstructorStats::move_constructions)
  36. .def_static("get", (ConstructorStats &(*)(py::object)) &ConstructorStats::get, py::return_value_policy::reference_internal);
  37. }
  38. PYBIND11_PLUGIN(pybind11_tests) {
  39. py::module m("pybind11_tests", "pybind testing plugin");
  40. bind_ConstructorStats(m);
  41. for (const auto &initializer : initializers())
  42. initializer(m);
  43. if (!py::hasattr(m, "have_eigen")) m.attr("have_eigen") = false;
  44. return m.ptr();
  45. }