Skip to content

Commit

Permalink
fixed not handling raw ptrs as properties.
Browse files Browse the repository at this point in the history
The problem was a specialization for raw ptr's, which should be used only for
for the property policy bind_as_ptr.
This special treatment was now donw directly, instead of using the propery_accessor specialization

Fixes #52
  • Loading branch information
acki-m committed May 7, 2017
1 parent 74144d6 commit 034ebcc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
13 changes: 3 additions & 10 deletions src/rttr/detail/misc/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,10 @@ make_unique(Args&&...) = delete;
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

template< typename T >
RTTR_INLINE const T& as_const(T& obj) { return const_cast<T&>(obj); }

template< typename T >
RTTR_INLINE const T& as_const(const T& obj) { return obj; }

template<typename T>
RTTR_INLINE const T as_const(T&& obj)
template <class T>
RTTR_CONSTEXPR RTTR_INLINE add_const_t<T>& as_const(T& value) RTTR_NOEXCEPT
{
static_assert(!std::is_const<T>::value, "The given obj is already const, moving a const RValue will result in a copy!");
return std::forward<T>(obj);
return value;
}

/////////////////////////////////////////////////////////////////////////////////////////
Expand Down
10 changes: 0 additions & 10 deletions src/rttr/detail/property/property_accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ struct property_accessor<T[N]>
}
};

template<typename T>
struct property_accessor<T*>
{
static bool set_value(T* prop, argument& arg)
{
*prop = *arg.get_value<T*>();
return true;
}
};

template<typename T, std::size_t N>
struct property_accessor<T(*)[N]>
{
Expand Down
5 changes: 3 additions & 2 deletions src/rttr/detail/property/property_wrapper_member_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class property_wrapper<member_object_ptr, A(C::*), void, Acc_Level, return_as_co
variant get_value(instance& object) const
{
if (C* ptr = object.try_convert<C>())
return variant((ptr->*m_acc));
return variant(ptr->*m_acc);
else
return variant();
}
Expand Down Expand Up @@ -158,7 +158,8 @@ class property_wrapper<member_object_ptr, A(C::*), void, Acc_Level, return_as_pt
C* ptr = object.try_convert<C>();
if (ptr && arg.is_type<A*>())
{
return property_accessor<A*>::set_value(&(ptr->*m_acc), arg);
(ptr->*m_acc) = *arg.get_value<A*>();
return true;
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/rttr/detail/property/property_wrapper_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ class property_wrapper<object_ptr, C*, void, Acc_Level, return_as_ptr, set_as_pt
{
if (arg.is_type<C*>())
{
return property_accessor<C*>::set_value(m_accessor, arg);
*m_accessor = *arg.get_value<C*>();
return true;
}
else
{
Expand Down Expand Up @@ -198,7 +199,7 @@ class property_wrapper<object_ptr, C*, void, Acc_Level, return_as_ptr, read_only

variant get_value(instance& object) const
{
return (variant(const_cast<const C*>(m_accessor)));
return (variant(m_accessor));
}

private:
Expand Down
41 changes: 41 additions & 0 deletions src/unit_tests/property/property_member_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ struct property_member_obj_test
std::vector<int> _p4 = std::vector<int>(50, 12);
variant _p7 = std::string("hello");
const variant _p8 = 23;
int* _p9 = nullptr;
int* _p10 = &_p1;



Expand Down Expand Up @@ -98,6 +100,8 @@ RTTR_REGISTRATION
)
.property("p7", &property_member_obj_test::_p7)
.property_readonly("p8", &property_member_obj_test::_p8)
.property("p9", &property_member_obj_test::_p9)
.property_readonly("p10", &property_member_obj_test::_p10)
;
}

Expand Down Expand Up @@ -346,3 +350,40 @@ TEST_CASE("property - variant as property", "[property]")
}

/////////////////////////////////////////////////////////////////////////////////////////

TEST_CASE("property - raw pointer as property", "[property]")
{
SECTION("Writable")
{
property_member_obj_test obj;
type prop_type = type::get(obj);

property prop = prop_type.get_property("p9");
REQUIRE(prop.is_valid() == true);

variant var = prop.get_value(obj);
CHECK(obj._p9 == nullptr);

int new_value = 23;
prop.set_value(obj, &new_value);

CHECK(obj._p9 == &new_value);
}

SECTION("Read Only")
{
property_member_obj_test obj;
type prop_type = type::get(obj);

property prop = prop_type.get_property("p10");
REQUIRE(prop.is_valid() == true);

variant var = prop.get_value(obj);
CHECK(obj._p10 == &obj._p1);

CHECK(var.get_type() == type::get<int*>());
CHECK(obj._p10 == var.get_value<int*>());
}
}

/////////////////////////////////////////////////////////////////////////////////////////

0 comments on commit 034ebcc

Please sign in to comment.