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

Proposed by Thomas Voß on 2014-02-19
Status: Merged
Approved by: Jim Hodapp on 2014-02-20
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 2014-02-19 Approve on 2014-02-20
PS Jenkins bot continuous-integration Approve on 2014-02-19
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.
15. By Thomas Voß on 2014-02-19

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

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
1=== modified file 'include/core/property.h'
2--- include/core/property.h 2014-01-01 06:19:38 +0000
3+++ include/core/property.h 2014-02-19 20:26:03 +0000
4@@ -43,10 +43,23 @@
5 typedef T ValueType;
6
7 /**
8+ * @brief Getter refers to the function type for dispatching get operations to.
9+ */
10+ typedef std::function<ValueType()> Getter;
11+
12+ /**
13+ * @brief Setter refers to the function type for dispatching set operations to.
14+ */
15+ typedef std::function<void(const ValueType&)> Setter;
16+
17+ /**
18 * @brief Property creates a new instance of property and initializes the contained value.
19 * @param t The initial value, defaults to Property<T>::default_value().
20 */
21- inline explicit Property(const T& t = T{}) : value{t}
22+ inline explicit Property(const T& t = T{})
23+ : value{t},
24+ getter{},
25+ setter{}
26 {
27 }
28
29@@ -129,6 +142,10 @@
30 if (value != new_value)
31 {
32 value = new_value;
33+
34+ if (setter)
35+ setter(value);
36+
37 signal_changed(value);
38 }
39 }
40@@ -139,6 +156,9 @@
41 */
42 inline virtual const T& get() const
43 {
44+ if (getter)
45+ mutable_get() = getter();
46+
47 return value;
48 }
49
50@@ -171,6 +191,24 @@
51 return false;
52 }
53
54+ /**
55+ * @brief install takes the provided functor and installs it for dispatching all set operations.
56+ * @param setter The functor to be invoked for set operations.
57+ */
58+ inline void install(const Setter& setter)
59+ {
60+ this->setter = setter;
61+ }
62+
63+ /**
64+ * @brief install takes the provided functor and installs it for dispatching all get operations.
65+ * @param getter The functor to be invoked for get operations.
66+ */
67+ inline void install(const Getter& getter)
68+ {
69+ this->getter = getter;
70+ }
71+
72 friend inline const Property<T>& operator|(const Property<T>& lhs, Property<T>& rhs)
73 {
74 rhs.connections.emplace(
75@@ -190,6 +228,8 @@
76
77 private:
78 mutable T value;
79+ Getter getter;
80+ Setter setter;
81 Signal<T> signal_changed;
82 std::set<ScopedConnection> connections;
83 };
84
85=== modified file 'tests/properties_test.cpp'
86--- tests/properties_test.cpp 2014-01-01 06:19:38 +0000
87+++ tests/properties_test.cpp 2014-02-19 20:26:03 +0000
88@@ -177,3 +177,34 @@
89
90 EXPECT_EQ(42, p2.get());
91 }
92+
93+TEST(Property, getter_is_invoked_for_get_operations)
94+{
95+ bool invoked = false;
96+ auto getter = [&invoked]()
97+ {
98+ invoked = true;
99+ return 42;
100+ };
101+
102+ core::Property<int> prop;
103+ prop.install(getter);
104+
105+ EXPECT_EQ(42, prop.get());
106+ EXPECT_TRUE(invoked);
107+}
108+
109+TEST(Property, setter_is_invoked_for_set_operations)
110+{
111+ int value = 0;
112+ auto setter = [&value](int new_value)
113+ {
114+ value = new_value;
115+ };
116+
117+ core::Property<int> prop;
118+ prop.install(setter);
119+
120+ prop.set(42);
121+ EXPECT_EQ(42, value);
122+}

Subscribers

People subscribed via source and target branches