test_implicit_cast.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <boost/shared_ptr.hpp>
  22. struct A : counted_type<A>
  23. { virtual ~A() {} };
  24. struct B : A, counted_type<B>
  25. {};
  26. struct enum_placeholder {};
  27. typedef enum { VAL1 = 1, VAL2 = 2 } LBENUM_t;
  28. LBENUM_t enum_by_val(LBENUM_t e) { return e; }
  29. LBENUM_t enum_by_const_ref(const LBENUM_t &e) { return e; }
  30. struct test_implicit : counted_type<test_implicit>
  31. {
  32. char const* f(A*) { return "f(A*)"; }
  33. char const* f(B*) { return "f(B*)"; }
  34. };
  35. struct char_pointer_convertable
  36. : counted_type<char_pointer_convertable>
  37. {
  38. operator const char*() const { return "foo!"; }
  39. };
  40. void func(const char_pointer_convertable& f)
  41. {
  42. }
  43. void not_convertable(boost::shared_ptr<A>)
  44. {
  45. TEST_CHECK(false);
  46. }
  47. int f(int& a)
  48. {
  49. return a;
  50. }
  51. COUNTER_GUARD(A);
  52. COUNTER_GUARD(B);
  53. COUNTER_GUARD(test_implicit);
  54. COUNTER_GUARD(char_pointer_convertable);
  55. void test_main(lua_State* L)
  56. {
  57. using namespace luabind;
  58. typedef char const*(test_implicit::*f1)(A*);
  59. typedef char const*(test_implicit::*f2)(B*);
  60. module(L)
  61. [
  62. class_<A>("A")
  63. .def(constructor<>()),
  64. class_<B, A>("B")
  65. .def(constructor<>()),
  66. class_<test_implicit>("test")
  67. .def(constructor<>())
  68. .def("f", (f1)&test_implicit::f)
  69. .def("f", (f2)&test_implicit::f),
  70. class_<char_pointer_convertable>("char_ptr")
  71. .def(constructor<>()),
  72. class_<enum_placeholder>("LBENUM")
  73. .enum_("constants")
  74. [
  75. value("VAL1", VAL1),
  76. value("VAL2", VAL2)
  77. ],
  78. def("enum_by_val", &enum_by_val),
  79. def("enum_by_const_ref", &enum_by_const_ref),
  80. def("func", &func),
  81. def("no_convert", &not_convertable),
  82. def("f", &f)
  83. ];
  84. DOSTRING(L, "a = A()");
  85. DOSTRING(L, "b = B()");
  86. DOSTRING(L, "t = test()");
  87. DOSTRING(L, "assert(t:f(a) == 'f(A*)')");
  88. DOSTRING(L, "assert(t:f(b) == 'f(B*)')");
  89. DOSTRING(L,
  90. "a = char_ptr()\n"
  91. "func(a)");
  92. DOSTRING(L, "assert(LBENUM.VAL1 == 1)");
  93. DOSTRING(L, "assert(LBENUM.VAL2 == 2)");
  94. DOSTRING(L, "assert(enum_by_val(LBENUM.VAL1) == LBENUM.VAL1)");
  95. DOSTRING(L, "assert(enum_by_val(LBENUM.VAL2) == LBENUM.VAL2)");
  96. DOSTRING(L, "assert(enum_by_const_ref(LBENUM.VAL1) == LBENUM.VAL1)");
  97. DOSTRING(L, "assert(enum_by_const_ref(LBENUM.VAL2) == LBENUM.VAL2)");
  98. DOSTRING_EXPECTED(L,
  99. "a = A()\n"
  100. "no_convert(a)",
  101. ("No matching overload found, candidates:\n"
  102. "void no_convert(custom ["
  103. + std::string(typeid(boost::shared_ptr<A>).name()) + "])").c_str());
  104. DOSTRING_EXPECTED(L,
  105. "a = nil\n"
  106. "f(a)",
  107. "No matching overload found, candidates:\n"
  108. "int f(int&)");
  109. }