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
|
// Copyright Daniel Wallin 2008. 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>
struct X
{
private:
~X() {}
};
int ptr_count = 0;
template <class T>
struct ptr
{
ptr()
: p(0)
{
ptr_count++;
}
ptr(T* p)
: p(p)
{
ptr_count++;
}
ptr(ptr const& other)
: p(other.p)
{
ptr_count++;
}
template <class U>
ptr(ptr<U> const& other)
: p(other.p)
{
ptr_count++;
}
~ptr()
{
ptr_count--;
}
T* p;
};
template <class T>
T* get_pointer(ptr<T> const& x)
{
return x.p;
}
template <class T>
ptr<T const>* get_const_holder(ptr<T>*)
{
return 0;
}
void f1(X const&)
{}
void f2(X&)
{}
void f3(X const*)
{}
void f4(X*)
{}
void g1(ptr<X> p)
{
TEST_CHECK(ptr_count == (p.p ? 2 : 3));
}
void g2(ptr<X> const& p)
{
TEST_CHECK(ptr_count == (p.p ? 1 : 2));
}
void g3(ptr<X>*)
{
TEST_CHECK(ptr_count == 1);
}
void g4(ptr<X> const*)
{
TEST_CHECK(ptr_count == 1);
}
ptr<X> get()
{
return ptr<X>(new X);
}
void test_main(lua_State* L)
{
using namespace luabind;
module(L) [
class_<X, ptr<X> >("X"),
def("get", &get),
def("f1", &f1),
def("f2", &f2),
def("f3", &f3),
def("f4", &f4),
def("g1", &g1),
def("g2", &g2),
def("g3", &g3),
def("g4", &g4)
];
DOSTRING(L, "x = get()\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "f1(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "f2(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "f3(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "f4(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "g1(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "g2(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "g3(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L, "g4(x)\n");
TEST_CHECK(ptr_count == 1);
DOSTRING(L,
"x = nil\n"
);
lua_gc(L, LUA_GCCOLLECT, 0);
TEST_CHECK(ptr_count == 0);
}
|