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>
26 using namespace luabind
;
30 struct base
: counted_type
<base
>
34 virtual std::string
f()
40 struct base_wrap
: base
43 base_wrap(weak_ref self_
): self(self_
) {}
45 static std::string
f_static(base
* obj
)
47 return obj
->base::f();
50 virtual std::string
f()
52 return call_member
<std::string
>(self
, "f");
56 struct simple_class
: counted_type
<simple_class
>
68 const char str
[] = "foo\0bar";
69 if (a
== std::string(str
, sizeof(str
)-1))
75 const char str
[] = "foo\0bar";
76 return std::string(str
, sizeof(str
)-1);
80 int simple_class::feedback
= 0;
82 } // namespace unnamed
86 void test_lua_classes()
89 COUNTER_GUARD(simple_class
);
95 class_
<base
, base_wrap
>("base")
97 .def("f", &base_wrap::f_static
)
101 "function base:fun()\n"
106 "assert(test == 4)\n");
109 "class 'derived' (base)\n"
110 " function derived:__init() super() end\n"
111 " function derived:f()\n"
112 " return 'derived:f() : ' .. base.f(self)\n"
115 "function make_derived()\n"
116 " return derived()\n"
121 BOOST_CHECK_NO_THROW(
122 ptr
= call_function
<base
*>(L
, "make_derived")
125 BOOST_CHECK_NO_THROW(
126 ptr
= call_function
<base
*>(L
, "derived")
129 BOOST_CHECK_NO_THROW(
130 BOOST_CHECK(ptr
->f() == "derived:f() : base:f()")
133 typedef void(simple_class::*f_overload1
)();
134 typedef void(simple_class::*f_overload2
)(int, int);
135 typedef void(simple_class::*f_overload3
)(std::string
);
139 class_
<simple_class
>("simple")
140 .def(constructor
<>())
141 .def("f", (f_overload1
)&simple_class::f
)
142 .def("f", (f_overload2
)&simple_class::f
)
143 .def("f", (f_overload3
)&simple_class::f
)
144 .def("g", &simple_class::g
)
148 "class 'simple_derived' (simple)\n"
149 " function simple_derived:__init() super() end\n"
150 "a = simple_derived()\n"
152 BOOST_CHECK(simple_class::feedback
== 1);
154 DOSTRING(L
, "a:f('foo\\0bar')");
155 BOOST_CHECK(simple_class::feedback
== 2);
157 simple_class::feedback
= 0;
159 DOSTRING_EXPECTED(L
, "a:f('incorrect', 'parameters')",
160 "no overload of 'simple:f' matched the arguments "
161 "(string, string)\ncandidates are:\n"
162 "simple:f()\nsimple:f(number, number)\nsimple:f(string)\n");
164 DOSTRING(L
, "if a:g() == \"foo\\0bar\" then a:f() end");
165 BOOST_CHECK(simple_class::feedback
== 1);