test_attributes.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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
  22. {
  23. int get() const
  24. {
  25. return 5;
  26. }
  27. };
  28. struct B : A
  29. {
  30. };
  31. struct C
  32. {
  33. int a;
  34. };
  35. struct property_test : counted_type<property_test>
  36. {
  37. property_test(): o(6), c_ref(&c) {}
  38. ~property_test() { c.a = 0; }
  39. std::string str_;
  40. int a_;
  41. float o;
  42. signed char b;
  43. C c;
  44. C* c_ref;
  45. void set(int a) { a_ = a; }
  46. int get() const { return a_; }
  47. void setA(A* a) {}
  48. A* getA() const { return 0; }
  49. void set_str(const char* str)
  50. { str_ = str; }
  51. const char* get_str() const
  52. { return str_.c_str(); }
  53. };
  54. COUNTER_GUARD(property_test);
  55. void free_setter(property_test& p, int a)
  56. { p.set(a); }
  57. int free_getter(const property_test& p)
  58. { return p.get(); }
  59. struct borrowed_attribute : counted_type<borrowed_attribute>
  60. {
  61. };
  62. struct attribute_holder : counted_type<attribute_holder>
  63. {
  64. attribute_holder(borrowed_attribute* b)
  65. : borrowed(b)
  66. {}
  67. borrowed_attribute* borrowed;
  68. };
  69. void test_main(lua_State* L)
  70. {
  71. using namespace luabind;
  72. module(L)
  73. [
  74. class_<property_test>("property")
  75. .def(luabind::constructor<>())
  76. .def("get", &property_test::get)
  77. .property("a", &property_test::get, &property_test::set)
  78. .property("str", &property_test::get_str, &property_test::set_str)
  79. .property("A", &property_test::getA, &property_test::setA)
  80. .def_readonly("o", &property_test::o)
  81. .property("free", &free_getter, &free_setter)
  82. .def_readwrite("b", &property_test::b)
  83. .def_readwrite("c", &property_test::c)
  84. .def_readwrite("c_ref", &property_test::c_ref),
  85. class_<A>("A")
  86. .def(constructor<>())
  87. .property("a", &A::get),
  88. class_<B, A>("B")
  89. .def(constructor<>())
  90. .property("x", &A::get),
  91. class_<C>("C")
  92. .def(constructor<>())
  93. .def_readwrite("a", &C::a)
  94. ];
  95. module(L) [
  96. class_<borrowed_attribute>("borrowed_attribute")
  97. .def(constructor<>()),
  98. class_<attribute_holder>("attribute_holder")
  99. .def(constructor<borrowed_attribute*>())
  100. .def_readwrite(
  101. "borrowed", &attribute_holder::borrowed, no_dependency)
  102. ];
  103. DOSTRING(L, "test = property()\n");
  104. DOSTRING(L,
  105. "test.a = 5\n"
  106. "assert(test.a == 5)");
  107. DOSTRING(L, "assert(test.o == 6)");
  108. DOSTRING(L,
  109. "test.new_member = 'foo'\n"
  110. "assert(test.new_member == 'foo')");
  111. DOSTRING(L,
  112. "property.new_member2 = 'bar'\n"
  113. "assert(property.new_member2 == 'bar')");
  114. DOSTRING(L,
  115. "b = property()\n"
  116. "assert(b.new_member2 == 'bar')");
  117. DOSTRING(L,
  118. "test.str = 'foobar'\n"
  119. "assert(test.str == 'foobar')\n");
  120. DOSTRING(L,
  121. "test.free = 6\n"
  122. "assert(test.free == 6)\n");
  123. DOSTRING(L,
  124. "test.b = 3\n"
  125. "assert(test.b == 3)\n");
  126. DOSTRING(L, "test.c.a = 1\n"
  127. "assert(test.c.a == 1)\n"
  128. "assert(test.c_ref.a == 1)");
  129. DOSTRING(L, "t1 = property()\n"
  130. "c = t1.c\n"
  131. "c2 = t1.c_ref\n"
  132. "c.a = 1\n"
  133. "t1 = nil\n"
  134. "collectgarbage()\n"
  135. "collectgarbage()\n"
  136. "assert(c.a == 1)\n"
  137. "assert(c2.a == 1)");
  138. DOSTRING(L,
  139. "a = B()\n");
  140. DOSTRING(L,
  141. "assert(a.a == 5)\n");
  142. DOSTRING(L,
  143. "assert(a.x == 5)\n");
  144. DOSTRING(L,
  145. "borrowed = borrowed_attribute()\n"
  146. "holder = attribute_holder(borrowed)\n"
  147. );
  148. TEST_CHECK(borrowed_attribute::count == 1);
  149. TEST_CHECK(attribute_holder::count == 1);
  150. DOSTRING(L,
  151. "holder.borrowed = borrowed\n"
  152. "borrowed2 = holder.borrowed\n"
  153. "holder = nil\n"
  154. "collectgarbage()\n"
  155. );
  156. TEST_CHECK(borrowed_attribute::count == 1);
  157. TEST_CHECK(attribute_holder::count == 0);
  158. }