added weak_ref
[luabind.git] / test / test_free_functions.cpp
blob90c25f6ecbae69856ca081ddd69fe8b430073828
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.
23 #include "test.hpp"
25 #include <luabind/luabind.hpp>
26 #include <luabind/functor.hpp>
27 #include <luabind/adopt_policy.hpp>
29 namespace {
31 luabind::functor<int> functor_test;
33 void set_functor(luabind::functor<int> f)
35 functor_test = f;
38 struct base: counted_type<base>
40 int f()
42 return 5;
46 int f(int x)
48 return x + 1;
51 int f(int x, int y)
53 return x + y;
56 base* create_base()
58 return new base();
61 void test_value_converter(const std::string str)
63 BOOST_TEST(str == "converted string");
66 void test_pointer_converter(const char* const str)
68 BOOST_TEST(std::strcmp(str, "converted string") == 0);
71 struct copy_me
75 void take_by_value(copy_me m)
79 int function_should_never_be_called(lua_State* L)
81 lua_pushnumber(L, 10);
82 return 1;
85 } // anonymous namespace
87 namespace luabind { namespace converters
89 yes_t is_user_defined(by_value<int>);
91 int convert_lua_to_cpp(lua_State* L, by_value<int>, int index)
93 return static_cast<int>(lua_tonumber(L, index));
96 int match_lua_to_cpp(lua_State* L, by_value<int>, int index)
98 if (lua_isnumber(L, index)) return 0; else return -1;
101 void convert_cpp_to_lua(lua_State* L, const int& v)
103 lua_pushnumber(L, v);
108 void test_free_functions()
110 COUNTER_GUARD(base);
112 lua_state L;
114 using namespace luabind;
116 lua_pushstring(L, "f");
117 lua_pushcclosure(L, &function_should_never_be_called, 0);
118 lua_settable(L, LUA_GLOBALSINDEX);
120 DOSTRING(L, "assert(f() == 10)");
122 module(L)
124 class_<copy_me>("copy_me")
125 .def(constructor<>()),
127 class_<base>("base")
128 .def("f", &base::f),
131 def("by_value", &take_by_value),
133 def("f", (int(*)(int)) &f),
134 def("f", (int(*)(int, int)) &f),
135 def("create", &create_base, adopt(return_value)),
136 def("set_functor", &set_functor)
138 #if !(BOOST_MSVC < 1300)
140 def("test_value_converter", &test_value_converter),
141 def("test_pointer_converter", &test_pointer_converter)
142 #endif
146 DOSTRING(L,
147 "e = create()\n"
148 "assert(e:f() == 5)");
150 DOSTRING(L, "assert(f(7) == 8)");
152 DOSTRING(L, "assert(f(3, 9) == 12)");
154 DOSTRING(L, "set_functor(function(x) return x * 10 end)");
156 BOOST_CHECK(functor_test(20) == 200);
158 DOSTRING(L, "set_functor(nil)");
160 DOSTRING(L, "function lua_create() return create() end");
161 base* ptr = call_function<base*>(L, "lua_create") [ adopt(result) ];
162 delete ptr;
164 #if !(BOOST_MSVC < 1300)
165 DOSTRING(L, "test_value_converter('converted string')");
166 DOSTRING(L, "test_pointer_converter('converted string')");
167 #endif
169 DOSTRING_EXPECTED(L, "f('incorrect', 'parameters')",
170 "no match for function call 'f' with the parameters (string, string)\n"
171 "candidates are:\n"
172 "f(number)\n"
173 "f(number, number)\n");
175 DOSTRING(L,
176 "function functor_test(a) glob = a\n"
177 " return 'foobar'\n"
178 "end");
179 functor<std::string> functor_test = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
181 BOOST_CHECK(functor_test(6)[detail::null_type()] == "foobar");
182 BOOST_CHECK(object_cast<int>(get_globals(L)["glob"]) == 6);
184 functor<std::string> functor_test2 = object_cast<functor<std::string> >(get_globals(L)["functor_test"]);
186 BOOST_CHECK(functor_test == functor_test2);