Guest User

Untitled

a guest
Mar 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <list>
  3. #include <memory>
  4.  
  5. #include <pybind11/pybind11.h>
  6. #include <pybind11/stl.h>
  7.  
  8. struct B;
  9.  
  10. struct A {
  11. A() {};
  12. ~A() {
  13. printf("~A()\n");
  14. if (!b_list.empty()) {
  15. printf("b_list not empty!\n");
  16. }
  17. };
  18.  
  19. void register_b(B *b) {
  20. b_list.push_back(b);
  21. };
  22. void unregister_b(B *b) {
  23. auto removed = std::remove(b_list.begin(), b_list.end(), b);
  24. b_list.erase(removed, b_list.end());
  25. };
  26.  
  27. std::list<B *> b_list;
  28. };
  29.  
  30. struct B {
  31. B(A& a) : m_a(a) {
  32. m_a.register_b(this);
  33. };
  34. ~B() {
  35. printf("~B()\n");
  36. m_a.unregister_b(this);
  37. };
  38.  
  39. A& m_a;
  40. };
  41.  
  42. namespace py = pybind11;
  43. PYBIND11_MODULE(example, m) {
  44. py::class_<B>(m, "B");
  45. py::class_<A>(m, "A")
  46. .def(py::init<>())
  47. .def("get_b_list", [](A& _this) -> std::vector<B> {
  48. std::vector<B> b_vec;
  49. b_vec.push_back({_this});
  50. b_vec.push_back({_this});
  51. return b_vec;
  52. #if 1 // Switch between implementation described in the issue
  53. });
  54. #else
  55. }, py::return_value_policy::move, py::keep_alive<0, 1>());
  56. #endif
  57. }
Add Comment
Please, Sign In to add comment