properties-cpp: /home/builder/build/src/properties-cpp-0.0.4/include/core/property.h Source File

properties-cpp 0.0.3 A very simple convenience library for handling properties and signals in C++11.

property.h
Go to the documentation of this file.
1/*
2 * Copyright © 2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 */
18#ifndef CORE_PROPERTY_H_
19#define CORE_PROPERTY_H_
20
21#include <core/signal.h>
22
23#include <set>
24
25namespace core
26{
36template<typename T>
37 class Property
38{
39 public:
43 typedef T ValueType;
44
48 typedef std::function<ValueType()> Getter;
49
53 typedef std::function<void(const ValueType&)> Setter;
54
59 inline explicit Property(const T& t = T{})
60 : value{t},
61 getter{},
62 setter{}
63 {
64 }
65
70 inline Property(const Property<T>& rhs) : value{rhs.value}
71 {
72 }
73
74 inline virtual ~Property() = default;
75
80 inline Property& operator=(const T& rhs)
81 {
82 set(rhs);
83 return *this;
84 }
85
90 inline Property& operator=(const Property<T>& rhs)
91 {
92 set(rhs.value);
93 return *this;
94 }
95
100 inline operator const T&() const
101 {
102 return get();
103 }
104
108 inline const T* operator->() const
109 {
110 return &get();
111 }
112
119 friend inline bool operator==(const Property<T>& lhs, const T& rhs)
120 {
121 return lhs.get() == rhs;
122 }
123
130 friend inline bool operator==(const Property<T>& lhs, const Property<T>& rhs)
131 {
132 return lhs.get() == rhs.get();
133 }
134
140 inline virtual void set(const T& new_value)
141 {
142 if (value != new_value)
143 {
144 value = new_value;
145
146 if (setter)
147 setter(value);
148
149 signal_changed(value);
150 }
151 }
152
157 inline virtual const T& get() const
158 {
159 if (getter)
160 mutable_get() = getter();
161
162 return value;
163 }
164
169 inline const Signal<T>& changed() const
170 {
171 return signal_changed;
172 }
173
183 inline virtual bool update(const std::function<bool(T& t)>& update_functor)
184 {
185 if (update_functor(mutable_get()))
186 {
187 signal_changed(value);
188 return true;
189 }
190
191 return false;
192 }
193
198 inline void install(const Setter& setter)
199 {
200 this->setter = setter;
201 }
202
207 inline void install(const Getter& getter)
208 {
209 this->getter = getter;
210 }
211
212 friend inline const Property<T>& operator|(const Property<T>& lhs, Property<T>& rhs)
213 {
214 rhs.connections.emplace(
215 lhs.changed().connect(
216 std::bind(
217 &Property<T>::set,
218 std::ref(rhs),
219 std::placeholders::_1)));
220 return lhs;
221 }
222
223 protected:
224 inline virtual T& mutable_get() const
225 {
226 return value;
227 }
228
229 private:
230 mutable T value;
231 Getter getter;
232 Setter setter;
233 Signal<T> signal_changed;
234 std::set<ScopedConnection> connections;
235};
236}
237
238#endif // CORE_PROPERTY_H_
core::Property::Property
Property(const Property< T > &rhs)
Copy c'tor, only copies the contained value, not the changed signal and its connections.
Definition property.h:70
core::Property::operator=
Property & operator=(const Property< T > &rhs)
Assignment operator, only assigns to the contained value, not the changed signal and its connections.
Definition property.h:90
core::Property::operator|
friend const Property< T > & operator|(const Property< T > &lhs, Property< T > &rhs)
Definition property.h:212
core::Property::install
void install(const Getter &getter)
install takes the provided functor and installs it for dispatching all get operations.
Definition property.h:207
core::Property::operator=
Property & operator=(const T &rhs)
Assignment operator, only assigns to the contained value.
Definition property.h:80
core::Property::mutable_get
virtual T & mutable_get() const
Definition property.h:224
core::Property::~Property
virtual ~Property()=default
core::Property::install
void install(const Setter &setter)
install takes the provided functor and installs it for dispatching all set operations.
Definition property.h:198
core::Property::set
virtual void set(const T &new_value)
Set the contained value to the provided value. Notify observers of the change.
Definition property.h:140
core::Property::Property
Property(const T &t=T{})
Property creates a new instance of property and initializes the contained value.
Definition property.h:59
core::Property::operator==
friend bool operator==(const Property< T > &lhs, const Property< T > &rhs)
operator == checks if the value of two properties are equal.
Definition property.h:130
core::Property::Setter
std::function< void(const ValueType &)> Setter
Setter refers to the function type for dispatching set operations to.
Definition property.h:53
core::Property::changed
const Signal< T > & changed() const
Access to the changed signal, allows observers to subscribe to change notifications.
Definition property.h:169
core::Property::operator->
const T * operator->() const
Provides access to a pointer to the contained value.
Definition property.h:108
core::Property::get
virtual const T & get() const
Access the value contained within this property.
Definition property.h:157
core::Property::ValueType
T ValueType
ValueType refers to the type of the contained value.
Definition property.h:43
core::Property::operator==
friend bool operator==(const Property< T > &lhs, const T &rhs)
operator == checks if the value of a property and a raw value are equal.
Definition property.h:119
core::Property::Getter
std::function< ValueType()> Getter
Getter refers to the function type for dispatching get operations to.
Definition property.h:48
core::Property::update
virtual bool update(const std::function< bool(T &t)> &update_functor)
Provides in-place update facilities.
Definition property.h:183
core::Signal
A signal class that observers can subscribe to.
Definition signal.h:37
core::Signal::connect
Connection connect(const Slot &slot) const
Connects the provided slot to this signal instance.
Definition signal.h:86
core
Definition connection.h:26
signal.h