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)
36 struct base
: counted_type
<base
>
61 void test_value_converter(const std::string str
)
63 TEST_CHECK(str
== "converted string");
66 void test_pointer_converter(const char* const str
)
68 TEST_CHECK(std::strcmp(str
, "converted string") == 0);
75 void take_by_value(copy_me m
)
79 int function_should_never_be_called(lua_State
* L
)
81 lua_pushnumber(L
, 10);
85 namespace luabind
{ namespace converters
87 yes_t
is_user_defined(by_value
<int>);
89 int convert_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
91 return static_cast<int>(lua_tonumber(L
, index
));
94 int match_lua_to_cpp(lua_State
* L
, by_value
<int>, int index
)
96 if (lua_isnumber(L
, index
)) return 0; else return -1;
99 void convert_cpp_to_lua(lua_State
* L
, const int& v
)
101 lua_pushnumber(L
, v
);
106 void test_main(lua_State
* L
)
108 using namespace luabind
;
110 lua_pushstring(L
, "f");
111 lua_pushcclosure(L
, &function_should_never_be_called
, 0);
112 lua_settable(L
, LUA_GLOBALSINDEX
);
114 DOSTRING(L
, "assert(f() == 10)");
118 class_
<copy_me
>("copy_me")
119 .def(constructor
<>()),
125 def("by_value", &take_by_value
),
127 def("f", (int(*)(int)) &f
),
128 def("f", (int(*)(int, int)) &f
),
129 def("create", &create_base
, adopt(return_value
))
130 // def("set_functor", &set_functor)
132 #if !(BOOST_MSVC < 1300)
134 def("test_value_converter", &test_value_converter
),
135 def("test_pointer_converter", &test_pointer_converter
)
142 "assert(e:f() == 5)");
144 DOSTRING(L
, "assert(f(7) == 8)");
146 DOSTRING(L
, "assert(f(3, 9) == 12)");
148 // DOSTRING(L, "set_functor(function(x) return x * 10 end)");
150 // TEST_CHECK(functor_test(20) == 200);
152 // DOSTRING(L, "set_functor(nil)");
154 DOSTRING(L
, "function lua_create() return create() end");
155 base
* ptr
= call_function
<base
*>(L
, "lua_create") [ adopt(result
) ];
158 #if !(BOOST_MSVC < 1300)
159 DOSTRING(L
, "test_value_converter('converted string')");
160 DOSTRING(L
, "test_pointer_converter('converted string')");
163 DOSTRING_EXPECTED(L
, "f('incorrect', 'parameters')",
164 "no match for function call 'f' with the parameters (string, string)\n"
167 "f(number, number)\n");
170 // "function functor_test(a) glob = a\n"
171 // " return 'foobar'\n"
173 // functor<std::string> functor_test = object_cast<functor<std::string> >(globals(L)["functor_test"]);
175 // TEST_CHECK(functor_test(6)[detail::null_type()] == "foobar");
176 // TEST_CHECK(object_cast<int>(globals(L)["glob"]) == 6);
178 // functor<std::string> functor_test2 = object_cast<functor<std::string> >(globals(L)["functor_test"]);
180 // TEST_CHECK(functor_test == functor_test2);