Merge lp:~thomas-voss/properties-cpp/make-scoped-connection-movable into lp:properties-cpp

Proposed by Thomas Voß
Status: Merged
Approved by: Timo Jyrinki
Approved revision: 13
Merged at revision: 11
Proposed branch: lp:~thomas-voss/properties-cpp/make-scoped-connection-movable
Merge into: lp:properties-cpp
Diff against target: 166 lines (+61/-5)
3 files modified
include/core/connection.h (+31/-3)
include/core/property.h (+19/-2)
tests/properties_test.cpp (+11/-0)
To merge this branch: bzr merge lp:~thomas-voss/properties-cpp/make-scoped-connection-movable
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Ubuntu Phablet Team Pending
Review via email: mp+201454@code.launchpad.net

Commit message

Refactor to rely on default move-operator for core::Connection.
Enable property chaining.
Make sure that an abandoned connection safely accesses its private member.
Make ScopedConnection a movable type.

Description of the change

Refactor to rely on default move-operator for core::Connection.
Enable property chaining.
Make sure that an abandoned connection safely accesses its private member.
Make ScopedConnection a movable type.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'include/core/connection.h'
--- include/core/connection.h 2013-11-28 07:35:14 +0000
+++ include/core/connection.h 2014-01-13 16:44:20 +0000
@@ -24,6 +24,7 @@
2424
25namespace core25namespace core
26{26{
27class ScopedConnection;
27/**28/**
28 * @brief The Connection class models a signal-slot connection.29 * @brief The Connection class models a signal-slot connection.
29 */30 */
@@ -38,6 +39,9 @@
38 */39 */
39 inline bool is_connected() const40 inline bool is_connected() const
40 {41 {
42 if (!d)
43 return false;
44
41 return (d->disconnector ? true : false);45 return (d->disconnector ? true : false);
42 }46 }
4347
@@ -46,7 +50,8 @@
46 */50 */
47 inline void disconnect()51 inline void disconnect()
48 {52 {
49 d->disconnect();53 if (d)
54 d->disconnect();
50 }55 }
5156
52 /**57 /**
@@ -55,11 +60,13 @@
55 */60 */
56 inline void dispatch_via(const Dispatcher& dispatcher)61 inline void dispatch_via(const Dispatcher& dispatcher)
57 {62 {
58 if (d->dispatcher_installer)63 if (d && d->dispatcher_installer)
59 d->dispatcher_installer(dispatcher);64 d->dispatcher_installer(dispatcher);
60 }65 }
6166
62private:67private:
68 friend class ScopedConnection;
69
63 typedef std::function<void()> Disconnector;70 typedef std::function<void()> Disconnector;
64 typedef std::function<void(const Dispatcher&)> DispatcherInstaller;71 typedef std::function<void(const Dispatcher&)> DispatcherInstaller;
6572
@@ -71,9 +78,15 @@
71 {78 {
72 }79 }
7380
81 inline bool operator<(const Connection& rhs) const
82 {
83 return d < rhs.d;
84 }
85
74 inline void reset()86 inline void reset()
75 {87 {
76 d->reset();88 if (d)
89 d->reset();
77 }90 }
7891
79 struct Private92 struct Private
@@ -136,6 +149,10 @@
136 {149 {
137 }150 }
138151
152 inline ScopedConnection(ScopedConnection&& rhs) : connection(std::move(rhs.connection))
153 {
154 }
155
139 ScopedConnection(const ScopedConnection&) = delete;156 ScopedConnection(const ScopedConnection&) = delete;
140157
141 /**158 /**
@@ -151,9 +168,20 @@
151 }168 }
152 }169 }
153170
171 inline ScopedConnection& operator=(ScopedConnection&& rhs)
172 {
173 connection = std::move(rhs.connection);
174 return *this;
175 }
176
154 ScopedConnection& operator=(const ScopedConnection&) = delete;177 ScopedConnection& operator=(const ScopedConnection&) = delete;
155 bool operator==(const ScopedConnection&) = delete;178 bool operator==(const ScopedConnection&) = delete;
156179
180 inline bool operator<(const ScopedConnection& rhs) const
181 {
182 return connection < rhs.connection;
183 }
184
157private:185private:
158 Connection connection;186 Connection connection;
159};187};
160188
=== modified file 'include/core/property.h'
--- include/core/property.h 2013-11-28 07:35:14 +0000
+++ include/core/property.h 2014-01-13 16:44:20 +0000
@@ -20,12 +20,17 @@
2020
21#include <core/signal.h>21#include <core/signal.h>
2222
23#include <iostream>23#include <set>
2424
25namespace core25namespace core
26{26{
27/**27/**
28 * @brief A very simple, templated class that allows for uniform declaration of get-able/set-able/observable members.28 * @brief A very simple, templated class that allows for uniform declaration of get-able/set-able/observable members.
29 *
30 * A note on thread-safety: The property class itself does not give any thread-safety guarantees.
31 * That is, consumers must not assume that concurrent get() and set() operations are synchronized by
32 * this class.
33 *
29 * @tparam The type of the value contained within the property.34 * @tparam The type of the value contained within the property.
30 */35 */
31template<typename T>36template<typename T>
@@ -166,8 +171,19 @@
166 return false;171 return false;
167 }172 }
168173
174 friend inline const Property<T>& operator|(const Property<T>& lhs, Property<T>& rhs)
175 {
176 rhs.connections.emplace(
177 lhs.changed().connect(
178 std::bind(
179 &Property<T>::set,
180 std::ref(rhs),
181 std::placeholders::_1)));
182 return lhs;
183 }
184
169 protected:185 protected:
170 inline T& mutable_get() const186 inline virtual T& mutable_get() const
171 {187 {
172 return value;188 return value;
173 }189 }
@@ -175,6 +191,7 @@
175 private:191 private:
176 mutable T value;192 mutable T value;
177 Signal<T> signal_changed;193 Signal<T> signal_changed;
194 std::set<ScopedConnection> connections;
178};195};
179}196}
180197
181198
=== modified file 'tests/properties_test.cpp'
--- tests/properties_test.cpp 2013-11-28 07:35:14 +0000
+++ tests/properties_test.cpp 2014-01-13 16:44:20 +0000
@@ -166,3 +166,14 @@
166166
167 EXPECT_EQ(22, position);167 EXPECT_EQ(22, position);
168}168}
169
170TEST(Property, chaining_properties_works)
171{
172 core::Property<int> p1, p2;
173
174 p1 | p2;
175
176 p1.set(42);
177
178 EXPECT_EQ(42, p2.get());
179}

Subscribers

People subscribed via source and target branches