Merge lp:~thomas-voss/properties-cpp/allow-for-dispatching-setter-and-getter-functors into lp:properties-cpp

Proposed by Thomas Voß
Status: Merged
Approved by: Jim Hodapp
Approved revision: 15
Merged at revision: 14
Proposed branch: lp:~thomas-voss/properties-cpp/allow-for-dispatching-setter-and-getter-functors
Merge into: lp:properties-cpp
Diff against target: 122 lines (+72/-1)
2 files modified
include/core/property.h (+41/-1)
tests/properties_test.cpp (+31/-0)
To merge this branch: bzr merge lp:~thomas-voss/properties-cpp/allow-for-dispatching-setter-and-getter-functors
Reviewer Review Type Date Requested Status
Jim Hodapp (community) code Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+207292@code.launchpad.net

Commit message

Allow for dispatching setting and getting to functors.

Description of the change

Allow for dispatching setting and getting to functors.

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

Add documentation and test-cases for dispatched get and set operations.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Jim Hodapp (jhodapp) wrote :

Looks good, thanks for implementing this.

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'include/core/property.h'
--- include/core/property.h 2014-01-01 06:19:38 +0000
+++ include/core/property.h 2014-02-19 20:26:03 +0000
@@ -43,10 +43,23 @@
43 typedef T ValueType;43 typedef T ValueType;
4444
45 /**45 /**
46 * @brief Getter refers to the function type for dispatching get operations to.
47 */
48 typedef std::function<ValueType()> Getter;
49
50 /**
51 * @brief Setter refers to the function type for dispatching set operations to.
52 */
53 typedef std::function<void(const ValueType&)> Setter;
54
55 /**
46 * @brief Property creates a new instance of property and initializes the contained value.56 * @brief Property creates a new instance of property and initializes the contained value.
47 * @param t The initial value, defaults to Property<T>::default_value().57 * @param t The initial value, defaults to Property<T>::default_value().
48 */58 */
49 inline explicit Property(const T& t = T{}) : value{t}59 inline explicit Property(const T& t = T{})
60 : value{t},
61 getter{},
62 setter{}
50 {63 {
51 }64 }
5265
@@ -129,6 +142,10 @@
129 if (value != new_value)142 if (value != new_value)
130 {143 {
131 value = new_value;144 value = new_value;
145
146 if (setter)
147 setter(value);
148
132 signal_changed(value);149 signal_changed(value);
133 }150 }
134 }151 }
@@ -139,6 +156,9 @@
139 */156 */
140 inline virtual const T& get() const157 inline virtual const T& get() const
141 {158 {
159 if (getter)
160 mutable_get() = getter();
161
142 return value;162 return value;
143 }163 }
144164
@@ -171,6 +191,24 @@
171 return false;191 return false;
172 }192 }
173193
194 /**
195 * @brief install takes the provided functor and installs it for dispatching all set operations.
196 * @param setter The functor to be invoked for set operations.
197 */
198 inline void install(const Setter& setter)
199 {
200 this->setter = setter;
201 }
202
203 /**
204 * @brief install takes the provided functor and installs it for dispatching all get operations.
205 * @param getter The functor to be invoked for get operations.
206 */
207 inline void install(const Getter& getter)
208 {
209 this->getter = getter;
210 }
211
174 friend inline const Property<T>& operator|(const Property<T>& lhs, Property<T>& rhs)212 friend inline const Property<T>& operator|(const Property<T>& lhs, Property<T>& rhs)
175 {213 {
176 rhs.connections.emplace(214 rhs.connections.emplace(
@@ -190,6 +228,8 @@
190228
191 private:229 private:
192 mutable T value;230 mutable T value;
231 Getter getter;
232 Setter setter;
193 Signal<T> signal_changed;233 Signal<T> signal_changed;
194 std::set<ScopedConnection> connections;234 std::set<ScopedConnection> connections;
195};235};
196236
=== modified file 'tests/properties_test.cpp'
--- tests/properties_test.cpp 2014-01-01 06:19:38 +0000
+++ tests/properties_test.cpp 2014-02-19 20:26:03 +0000
@@ -177,3 +177,34 @@
177177
178 EXPECT_EQ(42, p2.get());178 EXPECT_EQ(42, p2.get());
179}179}
180
181TEST(Property, getter_is_invoked_for_get_operations)
182{
183 bool invoked = false;
184 auto getter = [&invoked]()
185 {
186 invoked = true;
187 return 42;
188 };
189
190 core::Property<int> prop;
191 prop.install(getter);
192
193 EXPECT_EQ(42, prop.get());
194 EXPECT_TRUE(invoked);
195}
196
197TEST(Property, setter_is_invoked_for_set_operations)
198{
199 int value = 0;
200 auto setter = [&value](int new_value)
201 {
202 value = new_value;
203 };
204
205 core::Property<int> prop;
206 prop.install(setter);
207
208 prop.set(42);
209 EXPECT_EQ(42, value);
210}

Subscribers

People subscribed via source and target branches