test_private_destructors.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright Daniel Wallin 2008. Use, modification and distribution is
  2. // subject to the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #include "test.hpp"
  5. #include <luabind/luabind.hpp>
  6. struct X
  7. {
  8. private:
  9. ~X() {}
  10. };
  11. int ptr_count = 0;
  12. template <class T>
  13. struct ptr
  14. {
  15. ptr()
  16. : p(0)
  17. {
  18. ptr_count++;
  19. }
  20. ptr(T* p)
  21. : p(p)
  22. {
  23. ptr_count++;
  24. }
  25. ptr(ptr const& other)
  26. : p(other.p)
  27. {
  28. ptr_count++;
  29. }
  30. template <class U>
  31. ptr(ptr<U> const& other)
  32. : p(other.p)
  33. {
  34. ptr_count++;
  35. }
  36. ~ptr()
  37. {
  38. ptr_count--;
  39. }
  40. T* p;
  41. };
  42. template <class T>
  43. T* get_pointer(ptr<T> const& x)
  44. {
  45. return x.p;
  46. }
  47. template <class T>
  48. ptr<T const>* get_const_holder(ptr<T>*)
  49. {
  50. return 0;
  51. }
  52. void f1(X const&)
  53. {}
  54. void f2(X&)
  55. {}
  56. void f3(X const*)
  57. {}
  58. void f4(X*)
  59. {}
  60. void g1(ptr<X> p)
  61. {
  62. TEST_CHECK(ptr_count == (p.p ? 2 : 3));
  63. }
  64. void g2(ptr<X> const& p)
  65. {
  66. TEST_CHECK(ptr_count == (p.p ? 1 : 2));
  67. }
  68. void g3(ptr<X>*)
  69. {
  70. TEST_CHECK(ptr_count == 1);
  71. }
  72. void g4(ptr<X> const*)
  73. {
  74. TEST_CHECK(ptr_count == 1);
  75. }
  76. ptr<X> get()
  77. {
  78. return ptr<X>(new X);
  79. }
  80. void test_main(lua_State* L)
  81. {
  82. using namespace luabind;
  83. module(L) [
  84. class_<X, ptr<X> >("X"),
  85. def("get", &get),
  86. def("f1", &f1),
  87. def("f2", &f2),
  88. def("f3", &f3),
  89. def("f4", &f4),
  90. def("g1", &g1),
  91. def("g2", &g2),
  92. def("g3", &g3),
  93. def("g4", &g4)
  94. ];
  95. DOSTRING(L, "x = get()\n");
  96. TEST_CHECK(ptr_count == 1);
  97. DOSTRING(L, "f1(x)\n");
  98. TEST_CHECK(ptr_count == 1);
  99. DOSTRING(L, "f2(x)\n");
  100. TEST_CHECK(ptr_count == 1);
  101. DOSTRING(L, "f3(x)\n");
  102. TEST_CHECK(ptr_count == 1);
  103. DOSTRING(L, "f4(x)\n");
  104. TEST_CHECK(ptr_count == 1);
  105. DOSTRING(L, "g1(x)\n");
  106. TEST_CHECK(ptr_count == 1);
  107. DOSTRING(L, "g2(x)\n");
  108. TEST_CHECK(ptr_count == 1);
  109. DOSTRING(L, "g3(x)\n");
  110. TEST_CHECK(ptr_count == 1);
  111. DOSTRING(L, "g4(x)\n");
  112. TEST_CHECK(ptr_count == 1);
  113. DOSTRING(L,
  114. "x = nil\n"
  115. );
  116. lua_gc(L, LUA_GCCOLLECT, 0);
  117. TEST_CHECK(ptr_count == 0);
  118. }