1 // Copyright (c) 2003 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/adopt_policy.hpp>
29 using namespace luabind
;
31 int test_object_param(const object
& table
)
34 object current_object
;
35 current_object
= table
;
37 if (table
.type() == LUA_TTABLE
)
39 int sum
= object_cast
<int>(table
["oh"]);;
40 for (object::array_iterator i
= table
.abegin(); i
!= table
.aend(); ++i
)
42 sum
+= object_cast
<int>(*i
);
46 for (object::iterator j
= table
.begin(); j
!= table
.end(); ++j
)
48 sum2
+= object_cast
<int>(*j
);
52 for (object::raw_iterator j
= table
.raw_begin(); j
!= table
.raw_end(); ++j
)
54 sum3
+= object_cast
<int>(*j
);
65 if (table
.type() != LUA_TNIL
)
81 struct test_param
: counted_type
<test_param
>
87 int test_match(const luabind::object
& o
)
97 } // anonymous namespace
101 COUNTER_GUARD(test_param
);
105 using namespace luabind
;
109 def("test_object_param", &test_object_param
),
110 def("test_fun", &test_fun
),
111 def("test_match", (int(*)(const luabind::object
&))&test_match
),
112 def("test_match", (int(*)(int))&test_match
),
114 class_
<test_param
>("test_param")
115 .def(constructor
<>())
116 .def_readwrite("obj", &test_param::obj
)
117 .def_readonly("obj2", &test_param::obj2
)
122 "assert(test_object_param(t) == 1)");
124 DOSTRING(L
, "assert(test_object_param(nil) == 2)");
125 DOSTRING(L
, "t = { ['oh'] = 4, 3, 5, 7, 13 }");
126 DOSTRING(L
, "assert(test_object_param(t) == 0)");
127 DOSTRING(L
, "assert(t.sum == 4 + 3 + 5 + 7 + 13)");
128 DOSTRING(L
, "assert(t.sum2 == 4 + 3 + 5 + 7 + 13)");
129 DOSTRING(L
, "assert(t.sum3 == 4 + 3 + 5 + 7 + 13)");
130 DOSTRING(L
, "assert(t.blurp == 5)");
132 object g
= get_globals(L
);
133 object fun
= g
["test_fun"];
135 BOOST_CHECK(object_cast
<int>(ret
) == 42);
137 DOSTRING(L
, "function test_param_policies(x, y) end");
138 object test_param_policies
= g
["test_param_policies"];
139 int a
= test_param_policies
.type();
140 BOOST_CHECK(a
== LUA_TFUNCTION
);
142 // call the function and tell lua to adopt the pointer passed as first argument
143 test_param_policies(5, new test_param())[adopt(_2
)];
145 DOSTRING(L
, "assert(test_match(7) == 1)");
146 DOSTRING(L
, "assert(test_match('oo') == 0)");
151 "assert(t.obj == 'foo')\n"
152 "assert(t.obj2 == nil)");
155 "function test_object_policies(a) glob = a\n"
158 object test_object_policies
= g
["test_object_policies"];
159 object ret_val
= test_object_policies("teststring")[detail::null_type()];
160 BOOST_CHECK(object_cast
<int>(ret_val
) == 6);
161 BOOST_CHECK(object_cast
<std::string
>(g
["glob"]) == "teststring");
162 BOOST_CHECK(object_cast
<std::string
>(g
.at("glob")) == "teststring");
163 BOOST_CHECK(object_cast
<std::string
>(g
.raw_at("glob")) == "teststring");