Skip to content

kdbindings/property.h

Namespaces

Name
KDBindings
The main namespace of the KDBindings library.

Classes

Name
struct KDBindings::equal_to
An instance of the KDBindings::equal_to struct is used to decide whether two values of type T are equal in the context of data binding.
class KDBindings::Property
A property represents a value that can be part of or the result of data binding.
struct KDBindings::ReadOnlyProperty

Source code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
  This file is part of KDBindings.

  SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Sean Harmer <sean.harmer@kdab.com>

  SPDX-License-Identifier: MIT

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#pragma once

#include <kdbindings/property_updater.h>
#include <kdbindings/signal.h>

#include <iostream>
#include <memory>
#include <type_traits>

namespace KDBindings {

namespace Private {

template<typename X, typename Y, typename = void>
struct are_equality_comparable : std::false_type {
};

template<typename X, typename Y>
struct are_equality_comparable<X, Y,
                               std::enable_if_t<
                                       std::is_same<
                                               std::decay_t<
                                                       decltype(std::equal_to<>{}(std::declval<X>(), std::declval<Y>()))>,
                                               bool>::value>> : std::true_type {
};

template<typename X, typename Y>
constexpr bool are_equality_comparable_v = are_equality_comparable<X, Y>::value;

} // namespace Private

struct ReadOnlyProperty : std::runtime_error {
    ReadOnlyProperty() = delete;

    using std::runtime_error::runtime_error;
};

template<typename T>
struct equal_to {
    auto operator()(const T &x, const T &y) const noexcept
            -> std::enable_if_t<Private::are_equality_comparable_v<T, T>, bool>
    {
        return std::equal_to<>{}(x, y);
    }

    template<typename X, typename Y>
    auto operator()(const X &, const Y &) const noexcept
            -> std::enable_if_t<!Private::are_equality_comparable_v<X, Y>, bool>
    {
        return false;
    }
};

// This forwrad declaration is required so that
// Property can declare PropertyNode as a friend
// class.
namespace Private {
    template<typename PropertyType>
    class PropertyNode;
}

template<typename T>
class Property
{
public:
    typedef T valuetype;

    Property() = default;

    ~Property()
    {
        m_destroyed.emit();
    }

    explicit Property(T value) noexcept(std::is_nothrow_move_constructible<T>::value)
        : m_value{ std::move(value) }
    {
    }

    Property(Property<T> const &other) = delete;
    Property &operator=(Property<T> const &other) = delete;

    Property(Property<T> &&other) noexcept(std::is_nothrow_move_constructible<T>::value)
        : m_value(std::move(other.m_value))
        , m_valueAboutToChange(std::move(other.m_valueAboutToChange))
        , m_valueChanged(std::move(other.m_valueChanged))
        , m_destroyed(std::move(other.m_destroyed))
        , m_updater(std::move(other.m_updater))
    {
        // We do not move the m_moved signal yet so that objects interested in the moved-into
        // property can recreate any connections they need.

        // If we have an updater, let it know how to update our internal value
        if (m_updater) {
            using namespace std::placeholders;
            m_updater->setUpdateFunction(
                    std::bind(&Property<T>::setHelper, this, _1));
        }

        // Emit the moved signals for the moved from and moved to properties
        m_moved.emit(*this);
        other.m_moved.emit(*this);
        m_moved = std::move(other.m_moved);
    }

    Property &operator=(Property<T> &&other) noexcept(std::is_nothrow_move_assignable<T>::value)
    {
        // We do not move the m_moved signal yet so that objects interested in the moved-into
        // property can recreate any connections they need.
        m_value = std::move(other.m_value);
        m_valueAboutToChange = std::move(other.m_valueAboutToChange);
        m_valueChanged = std::move(other.m_valueChanged);
        m_destroyed = std::move(other.m_destroyed);
        m_updater = std::move(other.m_updater);

        // If we have an updater, let it know how to update our internal value
        if (m_updater) {
            using namespace std::placeholders;
            m_updater->setUpdateFunction(
                    std::bind(&Property<T>::setHelper, this, _1));
        }

        // Emit the moved signals for the moved from and moved to properties
        m_moved.emit(*this);
        other.m_moved.emit(*this);
        m_moved = std::move(other.m_moved);

        return *this;
    }

    template<typename UpdaterT>
    explicit Property(std::unique_ptr<UpdaterT> &&updater)
    {
        *this = std::move(updater);
    }

    template<typename UpdaterT>
    Property &operator=(std::unique_ptr<UpdaterT> &&updater)
    {
        m_updater = std::move(updater);

        // Let the updater know how to update our internal value
        using namespace std::placeholders;
        m_updater->setUpdateFunction(
                std::bind(&Property<T>::setHelper, this, _1));

        // Now synchronise our value with whatever the updator has right now.
        setHelper(m_updater->get());

        return *this;
    }

    void reset()
    {
        m_updater.reset();
    }

    Signal<const T &, const T &> &valueAboutToChange() const { return m_valueAboutToChange; }

    Signal<const T &> &valueChanged() const { return m_valueChanged; }

    Signal<> &destroyed() const { return m_destroyed; }

    void set(T value)
    {
        if (m_updater) {
            throw ReadOnlyProperty{
                "Cannot set value on a read-only property. This property likely holds the result of a binding expression."
            };
        }
        setHelper(std::move(value));
    }

    T const &get() const
    {
        return m_value;
    }

    Property<T> &operator=(T const &rhs)
    {
        set(std::move(rhs));
        return *this;
    }

    T const &operator()() const
    {
        return Property<T>::get();
    }

private:
    void setHelper(T value)
    {
        if (equal_to<T>{}(value, m_value))
            return;

        m_valueAboutToChange.emit(m_value, value);
        m_value = std::move(value);
        m_valueChanged.emit(m_value);
    }

    T m_value;
    // the signals in a property are mutable, as a property
    // being "const" should mean that it's value or binding does
    // not change, not that nobody can listen to it anymore.
    mutable Signal<const T &, const T &> m_valueAboutToChange;
    mutable Signal<const T &> m_valueChanged; // By const ref so we can emit the signal for move-only types of T e.g. std::unique_ptr<int>

    // The PropertyNode needs to be a friend class of the Property, as it needs
    // access to the m_moved Signal.
    // The decision to make this Signal private was made after the suggestion by
    // @jm4R who reported issues with the move constructors noexcept guarantee.
    // (https://github.com/KDAB/KDBindings/issues/24)
    // Ideally we would like to figure out a way to remove the moved signal entirely
    // at some point. However currently it is still needed for Property bindings to
    // keep track of moved Properties.
    template<typename PropertyType>
    friend class Private::PropertyNode;
    Signal<Property<T> &> m_moved;

    mutable Signal<> m_destroyed;
    std::unique_ptr<PropertyUpdater<T>> m_updater;
};

template<typename T>
std::ostream &operator<<(std::ostream &stream, Property<T> const &property)
{
    stream << property.get();
    return stream;
}

template<typename T>
std::istream &operator>>(std::istream &stream, Property<T> &prop)
{
    T temp;
    stream >> temp;
    prop.set(std::move(temp));
    return stream;
}

namespace Private {

template<typename T>
struct is_property_helper : std::false_type {
};

template<typename T>
struct is_property_helper<Property<T>> : std::true_type {
};

template<typename T>
struct is_property : is_property_helper<std::decay_t<T>> {
};

} // namespace Private

} // namespace KDBindings

Updated on 2024-03-19 at 00:03:31 +0000