Skip to content

Commit

Permalink
Refactor property2 to use new binders.
Browse files Browse the repository at this point in the history
  • Loading branch information
billyquith committed Dec 2, 2020
1 parent 9bdbbf3 commit 0b6f3b5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
67 changes: 52 additions & 15 deletions include/ponder/detail/propertyfactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,28 @@ class ValueBinder
Binding m_bound;
};

// Bind to internal reference.
template <class C, typename PropTraits>
class ValueBinder2 : public ValueBinder<C, PropTraits>
{
typedef ValueBinder<C, PropTraits> Base;
public:
template <typename S>
ValueBinder2(const Base::Binding& g, S s) : Base(g), m_set(s) {}

bool setter(ClassType& c, SetType v) const {
return m_set(c, v), true;
}

bool setter(ClassType& c, Value const& value) const {
return setter(c, value.to<SetType>());
}

protected:
std::function<void(Base::ClassType&, Base::AccessType)> m_set;
};


// Bind to internal reference getter.
template <class C, typename PropTraits>
class InternalRefBinder
{
Expand Down Expand Up @@ -111,6 +132,22 @@ class InternalRefBinder
Binding m_bound;
};

// Internal reference getter & setter.
template <class C, typename PropTraits>
class InternalRefBinder2 : public InternalRefBinder<C, PropTraits>
{
typedef InternalRefBinder<C, PropTraits> Base;
public:
template <typename S>
InternalRefBinder2(const Base::Binding& g, S s) : Base(g), m_set(s) {}

bool setter(ClassType& c, AccessType v) const { return m_set(c, v), true; }
bool setter(ClassType& c, Value const& value) const { return setter(c, value.to<AccessType>()); }

protected:
std::function<void(ClassType&, AccessType)> m_set;
};

/*
* Access traits for an exposed type T.
* - I.e. how we use an instance to access the bound property data using the correct interface.
Expand All @@ -126,6 +163,9 @@ struct AccessTraits
template <class C>
using ValueBinder = ValueBinder<C, PT>;

template <class C>
using ValueBinder2 = ValueBinder2<C, PT>;

template <typename A>
using Impl = SimplePropertyImpl<A>;
};
Expand All @@ -142,6 +182,9 @@ struct AccessTraits<PT,
template <class C>
using ValueBinder = ValueBinder<C, PT>;

template <class C>
using ValueBinder2 = ValueBinder2<C, PT>;

template <typename A>
using Impl = EnumPropertyImpl<A>;
};
Expand Down Expand Up @@ -199,6 +242,11 @@ struct AccessTraits<PT,
std::is_pointer<typename PT::ExposedType>::value, InternalRefBinder<C, PT>, ValueBinder<C, PT>
>::type;

template <class C>
using ValueBinder2 = typename std::conditional<
std::is_pointer<typename PT::ExposedType>::value, InternalRefBinder2<C, PT>, ValueBinder2<C, PT>
>::type;

template <typename A>
using Impl = UserPropertyImpl<A>;
};
Expand Down Expand Up @@ -246,24 +294,13 @@ class GetSet2

typedef AccessTraits<PropTraits> Access;

struct InterfaceType
{
template <typename F1, typename F2>
InterfaceType(F1 get, F2 set) : getter(get), m_setter(set) {}

std::function<typename PropTraits::DispatchType> getter;
std::function<void(ClassType&, DataType)> m_setter;
typedef typename Access::template ValueBinder2<ClassType> InterfaceType;

Value getValue(ClassType& c) const { return UserObject::makeCopy(getter(c)); }

// setter returns writable status, so wrap it
bool setter(ClassType& c, DataType v) const {return m_setter(c,v), true;}
bool setter(ClassType& c, Value const& v) const { return m_setter(c, v.to<DataType>()), true; }
} m_interface;
InterfaceType m_interface;

template <typename F1, typename F2>
GetSet2(F1 getter, F2 setter)
: m_interface(getter, setter)
: m_interface(typename InterfaceType::Binding(getter), setter)
{}
};

Expand Down
4 changes: 3 additions & 1 deletion test/ponder/property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ namespace PropertyTest

const MyType* getCT() const {return &mt;}
MyType* getT() { return &mt; }
void setT(const MyType * t) { mt = *t; }
};

#define FUNCTION_ACCESSORS(T,N) \
Expand Down Expand Up @@ -134,8 +135,9 @@ namespace PropertyTest
PROPERTY(float,f)
PROPERTY(std::string,s)
PROPERTY(MyEnum,e)
.property("getConstT", &MyClass::getCT)
.property("getCT", &MyClass::getCT)
.property("getT", &MyClass::getT)
.property("myType", &MyClass::getCT, &MyClass::setT)
;

// ***** std::function *****
Expand Down

0 comments on commit 0b6f3b5

Please sign in to comment.