Skip to content

Commit

Permalink
remove the special template parameter for constness
Browse files Browse the repository at this point in the history
  • Loading branch information
phyBrackets committed Jun 30, 2024
1 parent fcf68bf commit a74f98c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/kdbindings/make_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ inline Node<std::decay_t<T>> makeNode(T &&value)
template<typename T>
inline Node<T> makeNode(Property<T> &property)
{
return Node<T>(std::make_unique<PropertyNode<T, false>>(property));
return Node<T>(std::make_unique<PropertyNode<T>>(property));
}

template<typename T>
inline Node<T> makeNode(const Property<T> &property)
{
return Node<T>(std::make_unique<PropertyNode<T, true>>(property));
return Node<T>(std::make_unique<PropertyNode<T>>(property));
}

template<typename T>
Expand Down
33 changes: 12 additions & 21 deletions src/kdbindings/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,20 @@ class ConstantNode : public NodeInterface<T>
T m_value;
};

template<typename PropertyType, bool IsConst>
template<typename PropertyType>
class PropertyNode : public NodeInterface<PropertyType>
{
public:
using PropertyReference = std::conditional_t<IsConst, const Property<PropertyType> &, Property<PropertyType> &>;

explicit PropertyNode(PropertyReference property)
explicit PropertyNode(const Property<PropertyType> &property)
: m_parent(nullptr), m_dirty(false)
{
setProperty(property);
}

// PropertyNodes cannot be moved
PropertyNode(PropertyNode<PropertyType, IsConst> &&) = delete;
PropertyNode(PropertyNode<PropertyType> &&) = delete;

PropertyNode(const PropertyNode<PropertyType, IsConst> &other)
PropertyNode(const PropertyNode<PropertyType> &other)
: Dirtyable(other.isDirty())
{
setProperty(*other.m_property);
Expand All @@ -172,10 +170,8 @@ class PropertyNode : public NodeInterface<PropertyType>
virtual ~PropertyNode()
{
m_valueChangedHandle.disconnect();
if constexpr (!IsConst) {
m_movedHandle.disconnect();
m_destroyedHandle.disconnect();
}
m_movedHandle.disconnect();
m_destroyedHandle.disconnect();
}

const PropertyType &evaluate() const override
Expand All @@ -188,8 +184,7 @@ class PropertyNode : public NodeInterface<PropertyType>
return m_property->get();
}

// This must currently take a const reference, as the "moved" signal emits a const&
void propertyMoved(Property<PropertyType> &property)
void propertyMoved(const Property<PropertyType> &property)
{
if (&property != m_property) {
m_property = &property;
Expand All @@ -210,21 +205,17 @@ class PropertyNode : public NodeInterface<PropertyType>
const bool *dirtyVariable() const override { return &m_dirty; }

private:
void setProperty(PropertyReference property)
void setProperty(const Property<PropertyType> &property)
{
m_property = &property;

// Connect to the valueChanged signal in all cases
// Connect to all signals, even for const properties
m_valueChangedHandle = m_property->valueChanged().connect([this]() { this->markDirty(); });

// Only connect move and destruction signals if property is non-const
if constexpr (!IsConst) {
m_movedHandle = m_property->m_moved.connect([this](Property<PropertyType> &newProp) { this->propertyMoved(newProp); });
m_destroyedHandle = m_property->destroyed().connect([this]() { this->propertyDestroyed(); });
}
m_movedHandle = m_property->m_moved.connect([this](const Property<PropertyType> &newProp) { this->propertyMoved(newProp); });
m_destroyedHandle = m_property->destroyed().connect([this]() { this->propertyDestroyed(); });
}

std::conditional_t<IsConst, const Property<PropertyType> *, Property<PropertyType> *> m_property;
const Property<PropertyType> *m_property;
ConnectionHandle m_movedHandle;
ConnectionHandle m_valueChangedHandle;
ConnectionHandle m_destroyedHandle;
Expand Down
8 changes: 4 additions & 4 deletions src/kdbindings/property.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ struct equal_to {
// Property can declare PropertyNode as a friend
// class.
namespace Private {
template<typename PropertyType, bool IsConst>
class PropertyNode;
template<typename PropertyType>
class PropertyNode;
}

/**
Expand Down Expand Up @@ -384,9 +384,9 @@ class Property
// Ideally we would like to figure out a way to remove the moved signal entirely
// at some point. However currently it is still needed for Property bindings to
// keep track of moved Properties.
template<typename PropertyType, bool IsConst>
template<typename PropertyType>
friend class Private::PropertyNode;
Signal<Property<T> &> m_moved;
mutable Signal<Property<T> &> m_moved;

mutable Signal<> m_destroyed;
std::unique_ptr<PropertyUpdater<T>> m_updater;
Expand Down
2 changes: 1 addition & 1 deletion tests/binding/tst_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ TEST_CASE("Binding with const Property")
REQUIRE(bound.get() == 5);

bool called = false;
bound.valueChanged().connect([&called]() { called = true; });
(void)bound.valueChanged().connect([&called]() { called = true; });

source = 10; // Change the original non-const property
REQUIRE(called);
Expand Down

0 comments on commit a74f98c

Please sign in to comment.