property.h
Go to the documentation of this file.
/*
* Copyright © 2013 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef CORE_PROPERTY_H_
#define CORE_PROPERTY_H_
#include <core/signal.h>
#include <set>
namespace core
{
template<typename T>
class Property
{
public:
typedef T ValueType;
typedef std::function<ValueType()> Getter;
typedef std::function<void(const ValueType&)> Setter;
inline explicit Property(const T& t = T{})
: value{t},
getter{},
setter{}
{
}
inline Property(const Property<T>& rhs) : value{rhs.value}
{
}
inline virtual ~Property() = default;
inline Property& operator=(const T& rhs)
{
set(rhs);
return *this;
}
inline Property& operator=(const Property<T>& rhs)
{
set(rhs.value);
return *this;
}
inline operator const T&() const
{
return get();
}
inline const T* operator->() const
{
return &get();
}
friend inline bool operator==(const Property<T>& lhs, const T& rhs)
{
return lhs.get() == rhs;
}
friend inline bool operator==(const Property<T>& lhs, const Property<T>& rhs)
{
return lhs.get() == rhs.get();
}
inline virtual void set(const T& new_value)
{
if (value != new_value)
{
value = new_value;
if (setter)
setter(value);
signal_changed(value);
}
}
inline virtual const T& get() const
{
if (getter)
mutable_get() = getter();
return value;
}
inline const Signal<T>& changed() const
{
return signal_changed;
}
inline virtual bool update(const std::function<bool(T& t)>& update_functor)
{
if (update_functor(mutable_get()))
{
signal_changed(value);
return true;
}
return false;
}
inline void install(const Setter& setter)
{
this->setter = setter;
}
inline void install(const Getter& getter)
{
this->getter = getter;
}
friend inline const Property<T>& operator|(const Property<T>& lhs, Property<T>& rhs)
{
rhs.connections.emplace(
lhs.changed().connect(
std::bind(
&Property<T>::set,
std::ref(rhs),
std::placeholders::_1)));
return lhs;
}
protected:
inline virtual T& mutable_get() const
{
return value;
}
private:
mutable T value;
Getter getter;
Setter setter;
Signal<T> signal_changed;
std::set<ScopedConnection> connections;
};
}
#endif // CORE_PROPERTY_H_
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::Setter
std::function< void(const ValueType &)> Setter
Setter refers to the function type for dispatching set operations to.
Definition: property.h:53
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::update
virtual bool update(const std::function< bool(T &t)> &update_functor)
Provides in-place update facilities.
Definition: property.h:183
core::Property::operator=
Property & operator=(const T &rhs)
Assignment operator, only assigns to the contained value.
Definition: property.h:80
core::Property::operator->
const T * operator->() const
Provides access to a pointer to the contained value.
Definition: property.h:108
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
A very simple, templated class that allows for uniform declaration of get-able/set-able/observable me...
Definition: property.h:37
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::Signal< T >
core::Property::ValueType
T ValueType
ValueType refers to the type of the contained value.
Definition: property.h:43
core
Definition: connection.h:25
signal.h
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::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::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::~Property
virtual ~Property()=default
core::Property::get
virtual const T & get() const
Access the value contained within this property.
Definition: property.h:157
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::operator|
const friend Property< T > & operator|(const Property< T > &lhs, Property< T > &rhs)
Definition: property.h:212
core::Signal::connect
Connection connect(const Slot &slot) const
Connects the provided slot to this signal instance.
Definition: signal.h:86
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::Getter
std::function< ValueType()> Getter
Getter refers to the function type for dispatching get operations to.
Definition: property.h:48
core::Property::mutable_get
virtual T & mutable_get() const
Definition: property.h:224