test_back_reference.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2005 Daniel Wallin
  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. #include <boost/shared_ptr.hpp>
  22. using namespace luabind;
  23. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  24. namespace luabind
  25. {
  26. using boost::get_pointer;
  27. }
  28. #endif
  29. struct base0
  30. {
  31. virtual ~base0() {}
  32. };
  33. struct base_wrap0 : base0, wrap_base
  34. {};
  35. struct base1
  36. {
  37. virtual ~base1() {}
  38. };
  39. struct base_wrap1 : base1, wrap_base
  40. {};
  41. base0* filter0(base0* p)
  42. {
  43. return p;
  44. }
  45. boost::shared_ptr<base1> filter1(boost::shared_ptr<base1> const& p)
  46. {
  47. return p;
  48. }
  49. void test_main(lua_State* L)
  50. {
  51. module(L)
  52. [
  53. class_<base0, base_wrap0>("base0")
  54. .def(constructor<>()),
  55. def("filter0", &filter0),
  56. class_<base1, base_wrap1, boost::shared_ptr<base1> >("base1")
  57. .def(constructor<>()),
  58. def("filter1", &filter1)
  59. ];
  60. DOSTRING(L,
  61. "class 'derived0' (base0)\n"
  62. " function derived0:__init()\n"
  63. " base0.__init(self)\n"
  64. " end\n"
  65. "class 'derived1' (base1)\n"
  66. " function derived1:__init()\n"
  67. " base1.__init(self)\n"
  68. " end\n"
  69. );
  70. DOSTRING(L,
  71. "x = derived0()\n"
  72. "y = filter0(x)\n"
  73. "assert(x == y)\n"
  74. );
  75. DOSTRING(L,
  76. "x = derived1()\n"
  77. "y = filter1(x)\n"
  78. "assert(x == y)\n"
  79. );
  80. }