1 // Copyright (c) 2004 Daniel Wallin and Arvid Norberg
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the
8 // Software is furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included
11 // in all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
14 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
18 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 // OR OTHER DEALINGS IN THE SOFTWARE.
24 #include <luabind/luabind.hpp>
25 #include <boost/shared_ptr.hpp>
27 struct A
: counted_type
<A
>
30 struct B
: A
, counted_type
<B
>
34 struct enum_placeholder
{};
35 typedef enum { VAL1
= 1, VAL2
= 2 } LBENUM_t
;
36 LBENUM_t
enum_by_val(LBENUM_t e
) { return e
; }
37 LBENUM_t
enum_by_const_ref(const LBENUM_t
&e
) { return e
; }
41 struct test_implicit
: counted_type
<test_implicit
>
43 char const* f(A
*) { return "f(A*)"; }
44 char const* f(B
*) { return "f(B*)"; }
47 struct char_pointer_convertable
48 : counted_type
<char_pointer_convertable
>
50 operator const char*() const { return "foo!"; }
53 void func(const char_pointer_convertable
& f
)
57 void not_convertable(boost::shared_ptr
<A
>)
69 COUNTER_GUARD(test_implicit
);
70 COUNTER_GUARD(char_pointer_convertable
);
72 void test_main(lua_State
* L
)
74 using namespace luabind
;
76 typedef char const*(test_implicit::*f1
)(A
*);
77 typedef char const*(test_implicit::*f2
)(B
*);
82 .def(constructor
<>()),
85 .def(constructor
<>()),
87 class_
<test_implicit
>("test")
89 .def("f", (f1
)&test_implicit::f
)
90 .def("f", (f2
)&test_implicit::f
),
92 class_
<char_pointer_convertable
>("char_ptr")
93 .def(constructor
<>()),
95 class_
<enum_placeholder
>("LBENUM")
101 def("enum_by_val", &enum_by_val
),
102 def("enum_by_const_ref", &enum_by_const_ref
),
105 def("no_convert", ¬_convertable
),
109 DOSTRING(L
, "a = A()");
110 DOSTRING(L
, "b = B()");
111 DOSTRING(L
, "t = test()");
113 DOSTRING(L
, "assert(t:f(a) == 'f(A*)')");
114 DOSTRING(L
, "assert(t:f(b) == 'f(B*)')");
120 DOSTRING(L
, "assert(LBENUM.VAL1 == 1)");
121 DOSTRING(L
, "assert(LBENUM.VAL2 == 2)");
122 DOSTRING(L
, "assert(enum_by_val(LBENUM.VAL1) == LBENUM.VAL1)");
123 DOSTRING(L
, "assert(enum_by_val(LBENUM.VAL2) == LBENUM.VAL2)");
124 DOSTRING(L
, "assert(enum_by_const_ref(LBENUM.VAL1) == LBENUM.VAL1)");
125 DOSTRING(L
, "assert(enum_by_const_ref(LBENUM.VAL2) == LBENUM.VAL2)");
130 ("No matching overload found, candidates:\n"
131 "void no_convert(custom ["
132 + std::string(typeid(boost::shared_ptr
<A
>).name()) + "])").c_str());
137 "No matching overload found, candidates:\n"