test_construction.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #include "test.hpp"
  20. #include <luabind/luabind.hpp>
  21. struct A : counted_type<A>
  22. {
  23. int test;
  24. A(int a) { test = a; }
  25. A(const A&) { test = 1; }
  26. A() { test = 2; }
  27. ~A() {}
  28. };
  29. void f(A*, const A&) {}
  30. struct B: A, counted_type<B>
  31. {
  32. int test2;
  33. B(int a):A(a) { test2 = a; }
  34. B() { test2 = 3; }
  35. ~B() {}
  36. };
  37. struct C : counted_type<C> {};
  38. struct base1 : counted_type<base1>
  39. {
  40. virtual void doSomething() = 0;
  41. virtual ~base1() {}
  42. };
  43. struct deriv_1 : base1, counted_type<deriv_1>
  44. {
  45. void doSomething() {}
  46. };
  47. struct deriv_2 : deriv_1, counted_type<deriv_2>
  48. {
  49. void doMore() {}
  50. };
  51. COUNTER_GUARD(A);
  52. COUNTER_GUARD(B);
  53. COUNTER_GUARD(C);
  54. COUNTER_GUARD(base1);
  55. COUNTER_GUARD(deriv_1);
  56. COUNTER_GUARD(deriv_2);
  57. void test_main(lua_State* L)
  58. {
  59. using namespace luabind;
  60. module(L)
  61. [
  62. class_<A>("A")
  63. .def("f", &f)
  64. .def_readonly("test", &A::test)
  65. .def(constructor<int>())
  66. .def(constructor<const A&>())
  67. .def(constructor<>()),
  68. class_<B, A>("B")
  69. .def(constructor<int>())
  70. .def(constructor<>())
  71. .def_readonly("test2", &B::test2),
  72. class_<C>("C")
  73. ];
  74. DOSTRING_EXPECTED(L, "a = C()", "attempt to call a nil value");
  75. DOSTRING(L,
  76. "a = A(4)\n"
  77. "assert(a.test == 4)");
  78. DOSTRING(L,
  79. "a2 = A(a)\n"
  80. "assert(a2.test == 1)");
  81. DOSTRING(L,
  82. "b = B(6)\n"
  83. "assert(b.test2 == 6)\n");
  84. DOSTRING(L, "assert(b.test == 6)");
  85. DOSTRING(L,
  86. "b2 = B()\n"
  87. "assert(b2.test2 == 3)\n");
  88. DOSTRING(L, "assert(b2.test == 2)");
  89. }