Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable shared_ptr to work without ContainerTraits::construct #86

Merged
merged 3 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions Source/LuaBridge/detail/TypeTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ using is_base_of_template = decltype(is_base_of_template_impl<C>(std::declval<T*

template <class T, template <class...> class C>
static inline constexpr bool is_base_of_template_v = is_base_of_template<T, C>::value;

template <class... Args>
constexpr bool dependent_false = false;

} // namespace detail

//=================================================================================================
Expand Down Expand Up @@ -68,18 +72,25 @@ struct ContainerTraits
/**
* @brief Register shared_ptr support as container.
*
* @tparam T Class that is hold by the shared_ptr, must inherit from std::enable_shared_from_this.
* @tparam T Class that is hold by the shared_ptr, must inherit from std::enable_shared_from_this to support Stack::get to reconstruct it from lua.
*/
template <class T>
struct ContainerTraits<std::shared_ptr<T>>
{
static_assert(detail::is_base_of_template_v<T, std::enable_shared_from_this>);

using Type = T;

static std::shared_ptr<T> construct(T* t)
template <class U = T>
static std::shared_ptr<U> construct(U* t)
{
return std::static_pointer_cast<T>(t->shared_from_this());
if constexpr (detail::is_base_of_template_v<U, std::enable_shared_from_this>)
{
return std::static_pointer_cast<U>(t->shared_from_this());
}
else
{
static_assert(detail::dependent_false<U>,
"Failed reconstructing the reference count of the object instance, class must inherit from std::enable_shared_from_this");
}
}

static T* get(const std::shared_ptr<T>& c)
Expand Down
12 changes: 8 additions & 4 deletions Source/LuaBridge/detail/Userdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@ struct StackOpSelector<T*, true>

static ReturnType get(lua_State* L, int index) { return Userdata::get<T>(L, index, false); }

static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<T>(L, index); }
template <class U = T>
static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<U>(L, index); }
};

// pointer to const
Expand All @@ -999,7 +1000,8 @@ struct StackOpSelector<const T*, true>

static ReturnType get(lua_State* L, int index) { return Userdata::get<T>(L, index, true); }

static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<T>(L, index); }
template <class U = T>
static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<U>(L, index); }
};

// l-value reference
Expand All @@ -1013,7 +1015,8 @@ struct StackOpSelector<T&, true>

static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); }

static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<T>(L, index); }
template <class U = T>
static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<U>(L, index); }
};

// l-value reference to const
Expand All @@ -1027,7 +1030,8 @@ struct StackOpSelector<const T&, true>

static ReturnType get(lua_State* L, int index) { return Helper::get(L, index); }

static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<T>(L, index); }
template <class U = T>
static bool isInstance(lua_State* L, int index) { return Userdata::isInstance<U>(L, index); }
};

} // namespace detail
Expand Down
37 changes: 37 additions & 0 deletions Tests/Source/Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,43 @@ TEST_F(LuaBridgeTest, StdSharedPtrAsProperty)
EXPECT_EQ(42, result<int>());
}

TEST_F(LuaBridgeTest, SharedPtrNoEnableSharedFromThis)
{
using TestC = TestClass<int>;

luabridge::getGlobalNamespace(L)
.beginNamespace("ns")
.beginClass<TestC>("TestClass")
.addFunction("getValue", &TestC::getValue)
.addFunction("getValueConst", &TestC::getValueConst)
.endClass()
.endNamespace();

{
std::shared_ptr<TestC> a = std::make_shared<TestC>(42);
auto result = luabridge::setGlobal(L, a, "a");
ASSERT_TRUE(result);
}

runLua("result = a");
ASSERT_TRUE(result().isInstance<TestC*>());
ASSERT_TRUE(result().isInstance<const TestC*>());
ASSERT_TRUE(result().isInstance<TestC&>());
ASSERT_TRUE(result().isInstance<const TestC&>());

auto x1 = result<TestC*>();
EXPECT_EQ(42, x1->getValue());

auto x2 = result<const TestC*>();
EXPECT_EQ(42, x2->getValueConst());

auto& x3 = result<TestC&>();
EXPECT_EQ(42, x3.getValue());

auto x4 = result<const TestC&>();
EXPECT_EQ(42, x4.getValueConst());
}

#if LUABRIDGE_HAS_EXCEPTIONS
namespace {
template <class... Args>
Expand Down