1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// Copyright (c) 2004 Daniel Wallin and Arvid Norberg
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
// ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
// OR OTHER DEALINGS IN THE SOFTWARE.
#include "test.hpp"
#include <luabind/luabind.hpp>
#include <luabind/version.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
namespace luabind {
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
template<class T>
T* get_pointer(boost::shared_ptr<T> const& p) { return p.get(); }
#endif
}
struct base : counted_type<base>
{
base(): n(4) {}
virtual ~base() {}
void f(int)
{
}
int n;
};
// this is here to make sure the pointer offsetting works
struct first_base : counted_type<first_base>
{
virtual ~first_base() {}
virtual void a() {}
int padding;
};
struct derived : first_base, base
{
derived(): n2(7) {}
void f() {}
int n2;
};
COUNTER_GUARD(first_base);
COUNTER_GUARD(base);
int feedback = 0;
void tester(base* t)
{
if (t->n == 4) feedback = 1;
}
void tester_(derived* t)
{
if (t->n2 == 7) feedback = 2;
}
void tester2(boost::shared_ptr<base> t)
{
if (t->n == 4) feedback = 3;
}
void tester3(boost::shared_ptr<const base> t)
{
if (t->n == 4) feedback = 4;
}
void tester4(const boost::shared_ptr<const base>& t)
{
if (t->n == 4) feedback = 5;
}
void tester5(const boost::shared_ptr<const base>* t)
{
if ((*t)->n == 4) feedback = 6;
}
void tester6(const boost::shared_ptr<base>* t)
{
if ((*t)->n == 4) feedback = 7;
}
void tester7(boost::shared_ptr<base>* t)
{
if ((*t)->n == 4) feedback = 8;
}
boost::shared_ptr<base> tester9()
{
feedback = 9;
return boost::shared_ptr<base>(new base());
}
void tester10(boost::shared_ptr<base> const& r)
{
if (r->n == 4) feedback = 10;
}
void tester11(boost::shared_ptr<const base> const& r)
{
if (r->n == 4) feedback = 11;
}
void tester12(boost::shared_ptr<derived> const& r)
{
if (r->n2 == 7) feedback = 12;
}
derived tester13()
{
feedback = 13;
derived d;
d.n2 = 13;
return d;
}
void test_main(lua_State* L)
{
boost::shared_ptr<base> base_ptr(new base());
using namespace luabind;
module(L)
[
def("tester", &tester),
def("tester", &tester_),
def("tester2", &tester2),
def("tester3", &tester3),
def("tester4", &tester4),
def("tester5", &tester5),
def("tester6", &tester6),
def("tester7", &tester7),
def("tester9", &tester9),
def("tester10", &tester10),
def("tester11", &tester11),
def("tester12", &tester12),
def("tester13", &tester13),
class_<base, boost::shared_ptr<base> >("base")
.def(constructor<>())
.def("f", &base::f),
class_<derived, base, boost::shared_ptr<base> >("derived")
.def(constructor<>())
.def("f", &derived::f)
];
object g = globals(L);
g["ptr"] = base_ptr;
DOSTRING(L, "tester(ptr)");
TEST_CHECK(feedback == 1);
DOSTRING(L,
"a = base()\n"
"b = derived()\n");
#if LUABIND_VERSION != 900
DOSTRING(L, "tester(b)");
TEST_CHECK(feedback == 2);
#endif
DOSTRING(L, "tester(a)");
TEST_CHECK(feedback == 1);
DOSTRING(L, "tester2(b)");
TEST_CHECK(feedback == 3);
feedback = 0;
DOSTRING(L, "tester10(b)");
TEST_CHECK(feedback == 10);
/* this test is messed up, shared_ptr<derived> isn't even registered
DOSTRING_EXPECTED(
L
, "tester12(b)"
, "no match for function call 'tester12' with the parameters (derived)\n"
"candidates are:\n"
"tester12(const custom&)\n");
*/
#if LUABIND_VERSION != 900
object nil = globals(L)["non_existing_variable_is_nil"];
TEST_CHECK(object_cast<boost::shared_ptr<base> >(nil).get() == 0);
TEST_CHECK(object_cast<boost::shared_ptr<const base> >(nil).get() == 0);
#endif
DOSTRING(L, "tester13()");
TEST_CHECK(feedback == 13);
}
|