test_virtual_inheritance.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright Daniel Wallin 2009. 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. // Test the following hierarchy:
  7. //
  8. // X
  9. // / \.
  10. // Y Z
  11. // \ /
  12. // U
  13. struct X
  14. {
  15. X(int x)
  16. : value(x)
  17. {}
  18. virtual ~X()
  19. {}
  20. int f() const
  21. {
  22. return value;
  23. }
  24. int value;
  25. };
  26. struct Y : virtual X
  27. {
  28. Y(int value)
  29. : X(value)
  30. {}
  31. int g() const
  32. {
  33. return 2;
  34. }
  35. int dummy;
  36. };
  37. struct Z : virtual X
  38. {
  39. Z(int value)
  40. : X(value)
  41. {}
  42. int h() const
  43. {
  44. return 3;
  45. }
  46. int dummy;
  47. };
  48. struct U : Y, Z
  49. {
  50. U(int value)
  51. : X(value)
  52. , Y(value)
  53. , Z(value)
  54. {}
  55. int dummy;
  56. };
  57. X* upcast(U* p)
  58. {
  59. return p;
  60. }
  61. // This hiearchy tries to prove that conversion and caching works for all sub
  62. // object pointers:
  63. //
  64. //
  65. // Base Base
  66. // | |
  67. // Left Right
  68. // \ /
  69. // \ /
  70. // Derived
  71. //
  72. struct Base
  73. {
  74. Base(int value)
  75. : value(value)
  76. {}
  77. virtual ~Base()
  78. {}
  79. int value;
  80. };
  81. struct Left : Base
  82. {
  83. Left()
  84. : Base(1)
  85. {}
  86. int left() const
  87. {
  88. return value;
  89. }
  90. int dummy;
  91. };
  92. struct Right : Base
  93. {
  94. Right()
  95. : Base(2)
  96. {}
  97. int right() const
  98. {
  99. return value;
  100. }
  101. int dummy;
  102. };
  103. struct Derived : Left, Right
  104. {
  105. int f() const
  106. {
  107. return 3;
  108. }
  109. int dummy;
  110. };
  111. Base* left(Left* p)
  112. {
  113. return p;
  114. }
  115. Base* right(Right* p)
  116. {
  117. return p;
  118. }
  119. void test_main(lua_State* L)
  120. {
  121. using namespace luabind;
  122. module(L) [
  123. class_<X>("X")
  124. .def("f", &X::f),
  125. class_<Y, X>("Y")
  126. .def("g", &Y::g),
  127. class_<Z, X>("Z")
  128. .def("h", &Z::h),
  129. class_<U, bases<Y, Z> >("U")
  130. .def(constructor<int>()),
  131. def("upcast", &upcast)
  132. ];
  133. // Do everything twice to verify that caching works.
  134. DOSTRING(L,
  135. "function assert2(x)\n"
  136. " assert(x)\n"
  137. " assert(x)\n"
  138. "end\n"
  139. );
  140. DOSTRING(L,
  141. "x = U(1)\n"
  142. "assert2(x:f() == 1)\n"
  143. "assert2(x:g() == 2)\n"
  144. "assert2(x:h() == 3)\n"
  145. );
  146. DOSTRING(L,
  147. "y = upcast(x)\n"
  148. "assert2(y:f() == 1)\n"
  149. "assert2(y:g() == 2)\n"
  150. "assert2(y:h() == 3)\n"
  151. );
  152. module(L) [
  153. class_<Base>("Base"),
  154. class_<Left, Base>("Left")
  155. .def("left", &Left::left),
  156. class_<Right, Base>("Right")
  157. .def("right", &Right::right),
  158. class_<Derived, bases<Left, Right> >("Derived")
  159. .def(constructor<>())
  160. .def("f", &Derived::f),
  161. def("left", &left),
  162. def("right", &right)
  163. ];
  164. DOSTRING(L,
  165. "x = Derived()\n"
  166. "assert2(x:left() == 1)\n"
  167. "assert2(x:right() == 2)\n"
  168. "assert2(x:f() == 3)\n"
  169. );
  170. DOSTRING(L,
  171. "y = left(x)\n"
  172. "assert2(y:left() == 1)\n"
  173. "assert2(y:right() == 2)\n"
  174. "assert2(y:f() == 3)\n"
  175. );
  176. DOSTRING(L,
  177. "y = right(x)\n"
  178. "assert2(y:left() == 1)\n"
  179. "assert2(y:right() == 2)\n"
  180. "assert2(y:f() == 3)\n"
  181. );
  182. }