Skip to content

Commit

Permalink
Fix regression introduced in previous commit.
Browse files Browse the repository at this point in the history
A upcast to class which has a base class deriving from
enable_shared_from_this and is neither the template parameter of
enable_shared_from_this nor the pointee type of a shared_ptr stored
in Lua would fail with a compile time error. The added test should make
the issue clear.
  • Loading branch information
Oberon00 committed Aug 5, 2013
1 parent dd1cb88 commit 014c3a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion luabind/shared_ptr_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct shared_ptr_converter
// shared_from_this() available.
ptr_t shared_from_raw(rawptr_t raw, lua_State*, int, mpl::true_)
{
return raw->shared_from_this();
return ptr_t(raw->shared_from_this(), raw);
}

public:
Expand Down
22 changes: 17 additions & 5 deletions test/test_shared_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
namespace {

struct B: boost::enable_shared_from_this<B> {};
struct D: B {};
struct I: B {};
struct D: I {};

struct X
{
Expand All @@ -38,6 +39,11 @@ void needs_b(boost::shared_ptr<B> const& p)
TEST_CHECK(p.use_count() == 2);
}

void needs_i(boost::shared_ptr<I> const& p)
{
TEST_CHECK(p.use_count() == 2);
}

#ifndef LUABIND_NO_STD_SHARED_PTR

struct B2: std::enable_shared_from_this<B2> {};
Expand All @@ -64,9 +70,11 @@ void test_main(lua_State* L)

class_<B, boost::shared_ptr<B> >("B")
.def(constructor<>()),
class_<D, B, boost::shared_ptr<D> >("D")
class_<I, B, boost::shared_ptr<I> >("I"),
class_<D, I, boost::shared_ptr<D> >("D")
.def(constructor<>()),
def("needs_b", &needs_b)
def("needs_b", &needs_b),
def("needs_i", &needs_i)

#ifndef LUABIND_NO_STD_SHARED_PTR
,
Expand Down Expand Up @@ -106,14 +114,18 @@ void test_main(lua_State* L)

DOSTRING(L,
"d = D()\n"
"needs_b(d)\n" // test that automatic downcasting works
"needs_b(d)\n" // test that automatic upcasting works
);

// upcast to class which is neither the template parameter of
// enable_shared_from_this nor a direct match to the holder type:
DOSTRING(L, "needs_i(d)");

#ifndef LUABIND_NO_STD_SHARED_PTR
// And the same once again with std::shared_ptr:
DOSTRING(L,
"d2 = D2()\n"
"needs_b2(d2)\n" // test that automatic downcasting works
"needs_b2(d2)\n" // test that automatic upcasting works
);
#endif
}

0 comments on commit 014c3a0

Please sign in to comment.