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 <luabind/functor.hpp>
26 #include <luabind/adopt_policy.hpp>
28 luabind::functor
<int> functor_test
;
30 void set_functor(luabind::functor
<int> f
)
35 struct base
: counted_type
<base
>
60 void test_value_converter(const std::string str
)
62 TEST_CHECK(str
== "converted string");
65 void test_pointer_converter(const char* const str
)
67 TEST_CHECK(std::strcmp(str
, "converted string") == 0);
74 void take_by_value(copy_me m
)
78 int function_should_never_be_called(lua_State
* L
)
80 lua_pushnumber(L
, 10);
84 namespace luabind
{ namespace converters
86 yes_t
is_user_defined(by_value
<int>);
88 int convert_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
90 return static_cast<int>(lua_tonumber(L
, index
));
93 int match_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
95 if (lua_isnumber(L
, index
)) return 0; else return -1;
98 void convert_cpp_to_lua(lua_State
* L
, const int& v
)
100 lua_pushnumber(L
, v
);
105 void test_main(lua_State
* L
)
107 using namespace luabind
;
109 lua_pushstring(L
, "f");
110 lua_pushcclosure(L
, &function_should_never_be_called
, 0);
111 lua_settable(L
, LUA_GLOBALSINDEX
);
113 DOSTRING(L
, "assert(f() == 10)");
117 class_
<copy_me
>("copy_me")
118 .def(constructor
<>()),
124 def("by_value", &take_by_value
),
126 def("f", (int(*)(int)) &f
),
127 def("f", (int(*)(int, int)) &f
),
128 def("create", &create_base
, adopt(return_value
)),
129 def("set_functor", &set_functor
)
131 #if !(BOOST_MSVC < 1300)
133 def("test_value_converter", &test_value_converter
),
134 def("test_pointer_converter", &test_pointer_converter
)
141 "assert(e:f() == 5)");
143 DOSTRING(L
, "assert(f(7) == 8)");
145 DOSTRING(L
, "assert(f(3, 9) == 12)");
147 DOSTRING(L
, "set_functor(function(x) return x * 10 end)");
149 TEST_CHECK(functor_test(20) == 200);
151 DOSTRING(L
, "set_functor(nil)");
153 DOSTRING(L
, "function lua_create() return create() end");
154 base
* ptr
= call_function
<base
*>(L
, "lua_create") [ adopt(result
) ];
157 #if !(BOOST_MSVC < 1300)
158 DOSTRING(L
, "test_value_converter('converted string')");
159 DOSTRING(L
, "test_pointer_converter('converted string')");
162 DOSTRING_EXPECTED(L
, "f('incorrect', 'parameters')",
163 "no match for function call 'f' with the parameters (string, string)\n"
166 "f(number, number)\n");
169 "function functor_test(a) glob = a\n"
172 functor
<std::string
> functor_test
= object_cast
<functor
<std::string
> >(get_globals(L
)["functor_test"]);
174 TEST_CHECK(functor_test(6)[detail::null_type()] == "foobar");
175 TEST_CHECK(object_cast
<int>(get_globals(L
)["glob"]) == 6);
177 functor
<std::string
> functor_test2
= object_cast
<functor
<std::string
> >(get_globals(L
)["functor_test"]);
179 TEST_CHECK(functor_test
== functor_test2
);
181 // this must be reset before the lua state is destructed!
182 functor_test
.reset();