Skip to content

Commit

Permalink
Another fix for multiple inheritance (issue #62)
Browse files Browse the repository at this point in the history
Supply actual registry type to `object_registry::find_object()` on unwrapping
in order to cast the result object.
  • Loading branch information
pmed committed Sep 7, 2017
1 parent bd31f89 commit d7b4482
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
10 changes: 7 additions & 3 deletions test/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,19 +244,23 @@ void test_multiple_inheritance()
v8::Isolate* isolate = context.isolate();
v8::HandleScope scope(isolate);

v8pp::class_<B, use_shared_ptr> B_class(isolate);
B_class
.set("xB", &B::x)
.set("zB", &B::z)
.set("g", &B::g);

v8pp::class_<C, use_shared_ptr> C_class(isolate);
C_class
.template inherit<B>()
.template ctor<>()
.set("xA", &A::x)
.set("xB", &B::x)
.set("xC", &C::x)

.set("zA", &A::z)
.set("zB", &B::z)
.set("zC", &C::z)

.set("f", &A::f)
.set("g", &B::g)
.set("h", &C::h)

.set("rF", v8pp::property(&C::f))
Expand Down
43 changes: 14 additions & 29 deletions v8pp/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,14 @@ inline std::shared_ptr<void> const_pointer_cast(std::shared_ptr<void const> cons
return std::const_pointer_cast<void>(ptr);
}

template<typename T>
T* static_pointer_cast(void* ptr)
template<typename T, typename U>
T* static_pointer_cast(U* ptr)
{
return static_cast<T*>(ptr);
}

template<typename T>
T const* static_pointer_cast(void const* ptr)
{
return static_cast<T const*>(ptr);
}

template<typename T>
std::shared_ptr<T> static_pointer_cast(std::shared_ptr<void> const& ptr)
{
return std::static_pointer_cast<T>(ptr);
}

template<typename T>
std::shared_ptr<T const> static_pointer_cast(std::shared_ptr<void const> const& ptr)
{
return std::static_pointer_cast<T const>(ptr);
}
using std::static_pointer_cast;
using std::const_pointer_cast;

struct class_info
{
Expand Down Expand Up @@ -248,7 +233,7 @@ class object_registry final : public class_info
objects_.clear();
}

pointer_type find_object(object_id id) const
pointer_type find_object(object_id id, type_info const& type) const
{
auto it = objects_.find(key(id, std::integral_constant<bool, use_shared_ptr>{}));
if (it != objects_.end())
Expand All @@ -262,9 +247,9 @@ class object_registry final : public class_info
return nullptr;
}

v8::Local<v8::Object> find_v8_object(object_id id) const
v8::Local<v8::Object> find_v8_object(pointer_type const& ptr) const
{
auto it = objects_.find(key(id, std::integral_constant<bool, use_shared_ptr>{}));
auto it = objects_.find(ptr);
if (it != objects_.end())
{
return to_local(isolate_, it->second);
Expand All @@ -273,7 +258,7 @@ class object_registry final : public class_info
v8::Local<v8::Object> result;
for (auto const info : derivatives_)
{
result = info->find_v8_object(id);
result = info->find_v8_object(ptr);
if (!result.IsEmpty()) break;
}
return result;
Expand Down Expand Up @@ -341,7 +326,7 @@ class object_registry final : public class_info
obj->GetAlignedPointerFromInternalField(1));
if (registry)
{
pointer_type ptr = registry->find_object(id);
pointer_type ptr = registry->find_object(id, type);
if (ptr)
{
return ptr;
Expand Down Expand Up @@ -557,10 +542,10 @@ class class_
using const_pointer_type = typename object_registry::const_pointer_type;

public:
using object_pointer_type = decltype(
detail::static_pointer_cast<T>(std::declval<pointer_type>()));
using object_const_pointer_type = decltype(
detail::static_pointer_cast<T>(std::declval<const_pointer_type>()));
using object_pointer_type = typename std::conditional<use_shared_ptr,
std::shared_ptr<T>, T*>::type;
using object_const_pointer_type = typename std::conditional<use_shared_ptr,
std::shared_ptr<T const>, T const*>::type;

template<typename ...Args>
struct factory_create
Expand Down Expand Up @@ -770,7 +755,7 @@ class class_
{
using namespace detail;
return classes::find<use_shared_ptr>(isolate, type_id<T>())
.find_v8_object(pointer_id(const_pointer_cast(obj)));
.find_v8_object(const_pointer_cast(obj));
}

/// Destroy wrapped C++ object
Expand Down

0 comments on commit d7b4482

Please sign in to comment.