diff --git a/src/kdbindings/make_node.h b/src/kdbindings/make_node.h index 24e63d7..0f93d99 100644 --- a/src/kdbindings/make_node.h +++ b/src/kdbindings/make_node.h @@ -71,13 +71,13 @@ inline Node> makeNode(T &&value) template inline Node makeNode(Property &property) { - return Node(std::make_unique>(property)); + return Node(std::make_unique>(property)); } template inline Node makeNode(const Property &property) { - return Node(std::make_unique>(property)); + return Node(std::make_unique>(property)); } template diff --git a/src/kdbindings/node.h b/src/kdbindings/node.h index ef0c3c8..c4eb361 100644 --- a/src/kdbindings/node.h +++ b/src/kdbindings/node.h @@ -148,22 +148,20 @@ class ConstantNode : public NodeInterface T m_value; }; -template +template class PropertyNode : public NodeInterface { public: - using PropertyReference = std::conditional_t &, Property &>; - - explicit PropertyNode(PropertyReference property) + explicit PropertyNode(const Property &property) : m_parent(nullptr), m_dirty(false) { setProperty(property); } // PropertyNodes cannot be moved - PropertyNode(PropertyNode &&) = delete; + PropertyNode(PropertyNode &&) = delete; - PropertyNode(const PropertyNode &other) + PropertyNode(const PropertyNode &other) : Dirtyable(other.isDirty()) { setProperty(*other.m_property); @@ -172,10 +170,8 @@ class PropertyNode : public NodeInterface 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 @@ -188,8 +184,7 @@ class PropertyNode : public NodeInterface return m_property->get(); } - // This must currently take a const reference, as the "moved" signal emits a const& - void propertyMoved(Property &property) + void propertyMoved(const Property &property) { if (&property != m_property) { m_property = &property; @@ -210,21 +205,17 @@ class PropertyNode : public NodeInterface const bool *dirtyVariable() const override { return &m_dirty; } private: - void setProperty(PropertyReference property) + void setProperty(const Property &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 &newProp) { this->propertyMoved(newProp); }); - m_destroyedHandle = m_property->destroyed().connect([this]() { this->propertyDestroyed(); }); - } + m_movedHandle = m_property->m_moved.connect([this](const Property &newProp) { this->propertyMoved(newProp); }); + m_destroyedHandle = m_property->destroyed().connect([this]() { this->propertyDestroyed(); }); } - std::conditional_t *, Property *> m_property; + const Property *m_property; ConnectionHandle m_movedHandle; ConnectionHandle m_valueChangedHandle; ConnectionHandle m_destroyedHandle; diff --git a/src/kdbindings/property.h b/src/kdbindings/property.h index 7c0ba54..122eaed 100644 --- a/src/kdbindings/property.h +++ b/src/kdbindings/property.h @@ -108,8 +108,8 @@ struct equal_to { // Property can declare PropertyNode as a friend // class. namespace Private { - template - class PropertyNode; +template +class PropertyNode; } /** @@ -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 + template friend class Private::PropertyNode; - Signal &> m_moved; + mutable Signal &> m_moved; mutable Signal<> m_destroyed; std::unique_ptr> m_updater; diff --git a/tests/binding/tst_binding.cpp b/tests/binding/tst_binding.cpp index 5649fe8..9130941 100644 --- a/tests/binding/tst_binding.cpp +++ b/tests/binding/tst_binding.cpp @@ -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);