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
|
// Copyright Daniel Wallin 2009. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "test.hpp"
#include <luabind/luabind.hpp>
#include <boost/shared_ptr.hpp>
namespace {
struct X
{
X(int value_)
: value(value_)
{
++alive;
}
~X()
{
--alive;
}
int value;
static int alive;
};
int X::alive = 0;
struct D: X
{
D(int value_): X(value_) {}
};
struct ptr
{
ptr(X* p_)
: p(p_)
{}
ptr(ptr const& other)
: p(other.p)
{
const_cast<ptr&>(other).p = 0;
}
~ptr()
{
delete p;
}
X* p;
};
X* get_pointer(ptr const& p)
{
return p.p;
}
#ifdef LUABIND_USE_CXX11
std::unique_ptr<X> make1()
{
return std::unique_ptr<X>(new X(1));
}
#else
std::auto_ptr<X> make1()
{
return std::auto_ptr<X>(new X(1));
}
#endif
boost::shared_ptr<X> make2()
{
return boost::shared_ptr<X>(new X(2));
}
ptr make3()
{
return ptr(new X(3));
}
boost::shared_ptr<D> make_d()
{
return boost::shared_ptr<D>(new D(10));
}
void needs_x(boost::shared_ptr<X>) {}
} // namespace unnamed
void test_main(lua_State* L)
{
using namespace luabind;
module(L) [
class_<X>("X")
.def_readonly("value", &X::value),
class_<D, X>("D"),
def("make1", make1),
def("make2", make2),
def("make3", make3),
def("make_d", make_d),
def("needs_x", needs_x)
];
DOSTRING(L,
"x1 = make1()\n"
"x2 = make2()\n"
"x3 = make3()\n"
);
TEST_CHECK(X::alive == 3);
DOSTRING(L,
"assert(x1.value == 1)\n"
"assert(x2.value == 2)\n"
"assert(x3.value == 3)\n"
);
DOSTRING(L, "function get2() return x2 end");
boost::shared_ptr<X> spx = call_function<boost::shared_ptr<X> >(L, "get2");
TEST_CHECK(spx.use_count() == 2);
DOSTRING(L,
"x1 = nil\n"
"x2 = nil\n"
"x3 = nil\n"
"collectgarbage()\n"
);
TEST_CHECK(spx.use_count() == 1);
TEST_CHECK(X::alive == 1);
spx.reset();
TEST_CHECK(X::alive == 0);
DOSTRING(L,
"d = make_d()\n"
"status, err = pcall(needs_x, d)\n"
"assert(not status)\n"
"pat = '^No matching overload found, candidates:\\n'\n"
"pat = pat .. 'void needs_x%(custom %[.+%]%)$'\n"
"if not err:match(pat) then\n"
" error('expected \"' .. pat .. '\", got \"' .. err .. '\"')\n"
"end\n"
);
}
|