KDDockWidgets API Documentation 2.0
Loading...
Searching...
No Matches
binding.h
Go to the documentation of this file.
1/*
2 This file is part of KDBindings.
3
4 SPDX-FileCopyrightText: 2021-2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Sean Harmer <sean.harmer@kdab.com>
6
7 SPDX-License-Identifier: MIT
8
9 Contact KDAB at <info@kdab.com> for commercial licensing options.
10*/
11
12#pragma once
13
14#include <kdbindings/node.h>
20
21namespace KDBindings {
22
33template<typename T, typename EvaluatorT = BindingEvaluator>
35{
36 static_assert(
37 std::is_base_of<BindingEvaluator, EvaluatorT>::value,
38 "The EvaluatorT type must inherit from BindingEvaluator.");
39
40public:
47 explicit Binding(Private::Node<T> &&rootNode, EvaluatorT const &evaluator)
48 : m_rootNode{ std::move(rootNode) }
49 , m_evaluator{ evaluator }
50 {
51 m_bindingId = m_evaluator.insert(this);
53 }
54
56 ~Binding() override
57 {
59 }
60
62 Binding() = delete;
63
65 Binding(Binding const &other) = delete;
67 Binding &operator=(Binding const &other) = delete;
68
69 // Move construction would invalidate the this pointer passed to the evaluator
70 // in the constructor
72 Binding(Binding &&other) = delete;
74 Binding &operator=(Binding &&other) = delete;
75
79 void setUpdateFunction(std::function<void(T &&)> const &updateFunction) override
80 {
81 m_propertyUpdateFunction = updateFunction;
82 }
83
85 T get() const override { return m_rootNode.evaluate(); }
86
88 void evaluate()
89 {
90 T value = m_rootNode.evaluate();
91
92 // Use this to update any associated property via the PropertyUpdater's update function
93 m_propertyUpdateFunction(std::move(value));
94 }
95
96protected:
97 Private::Dirtyable **parentVariable() override { return nullptr; }
98 const bool *dirtyVariable() const override { return nullptr; }
99
103 EvaluatorT m_evaluator;
105 std::function<void(T &&)> m_propertyUpdateFunction = [](T &&) {};
107 int m_bindingId = -1;
108};
109
122template<typename T, typename EvaluatorT>
123inline std::unique_ptr<Binding<T, EvaluatorT>> makeBinding(EvaluatorT &evaluator, Property<T> &property)
124{
125 return std::make_unique<Binding<T, EvaluatorT>>(Private::makeNode(property), evaluator);
126}
127
140template<typename T, typename EvaluatorT>
141inline std::unique_ptr<Binding<T, EvaluatorT>> makeBinding(EvaluatorT &evaluator, Private::Node<T> &&rootNode)
142{
143 return std::make_unique<Binding<T, EvaluatorT>>(std::move(rootNode), evaluator);
144}
145
162template<typename EvaluatorT, typename Func, typename... Args, typename = std::enable_if_t<sizeof...(Args) != 0>, typename ResultType = Private::operator_node_result_t<Func, Args...>>
163inline std::unique_ptr<Binding<ResultType, EvaluatorT>> makeBinding(EvaluatorT &evaluator, Func &&func, Args &&...args)
164{
165 return std::make_unique<Binding<ResultType, EvaluatorT>>(Private::makeNode(std::forward<Func>(func), std::forward<Args>(args)...), evaluator);
166}
167
178template<typename T>
180{
181public:
187 explicit Binding(Private::Node<T> &&rootNode)
188 : Binding<T, BindingEvaluator>(std::move(rootNode), ImmediateBindingEvaluator::instance())
189 {
190 }
191
193 Binding() = delete;
194
195 virtual ~Binding() = default;
196
198 Binding(Binding const &other) = delete;
200 Binding &operator=(Binding const &other) = delete;
201
203 Binding(Binding &&other) = delete;
205 Binding &operator=(Binding &&other) = delete;
206
207 void markDirty() override
208 {
210 }
211};
212
224template<typename T>
225inline std::unique_ptr<Binding<T, ImmediateBindingEvaluator>> makeBinding(Property<T> &property)
226{
227 return std::make_unique<Binding<T, ImmediateBindingEvaluator>>(Private::makeNode(property));
228}
229
241template<typename T>
242inline std::unique_ptr<Binding<T, ImmediateBindingEvaluator>> makeBinding(Private::Node<T> &&rootNode)
243{
244 return std::make_unique<Binding<T, ImmediateBindingEvaluator>>(std::move(rootNode));
245}
246
261template<typename Func, typename... Args, typename = std::enable_if_t<sizeof...(Args) != 0>, typename ResultType = Private::operator_node_result_t<Func, Args...>>
262inline std::unique_ptr<Binding<ResultType, ImmediateBindingEvaluator>> makeBinding(Func &&func, Args &&...args)
263{
264 return std::make_unique<Binding<ResultType, ImmediateBindingEvaluator>>(Private::makeNode(std::forward<Func>(func), std::forward<Args>(args)...));
265}
266
291template<typename... T>
292inline auto makeBoundProperty(T &&...args)
293{
294 auto binding = makeBinding(std::forward<T>(args)...);
295 return Property<decltype(binding->get())>(std::move(binding));
296}
297
298} // namespace KDBindings
A BindingEvaluator provides a mechanism to control the exact time when a KDBindings::Binding is reeva...
Binding & operator=(Binding &&other)=delete
Binding & operator=(Binding const &other)=delete
Binding(Private::Node< T > &&rootNode)
Construct a new Binding with an immediate mode evaluator.
Definition binding.h:187
A combination of a root Node with an evaluator.
Definition binding.h:35
void setUpdateFunction(std::function< void(T &&)> const &updateFunction) override
Definition binding.h:79
std::function< void(T &&)> m_propertyUpdateFunction
Definition binding.h:105
Binding(Binding &&other)=delete
~Binding() override
Definition binding.h:56
T get() const override
Definition binding.h:85
Private::Dirtyable ** parentVariable() override
Definition binding.h:97
Binding & operator=(Binding &&other)=delete
Binding(Private::Node< T > &&rootNode, EvaluatorT const &evaluator)
Construct a new Binding with a specific evaluator.
Definition binding.h:47
EvaluatorT m_evaluator
Definition binding.h:103
Binding(Binding const &other)=delete
Binding & operator=(Binding const &other)=delete
const bool * dirtyVariable() const override
Definition binding.h:98
Private::Node< T > m_rootNode
Definition binding.h:101
void setParent(Dirtyable *newParent)
Definition node.h:113
const ResultType & evaluate() const
Definition node.h:108
A PropertyUpdater defines the interface used to update a Property, e.g. from a binding expression.
A property represents a value that can be part of or the result of data binding.
Definition property.h:138
typename operator_node_result< Operator, Ts... >::type operator_node_result_t
Definition make_node.h:57
Node< std::decay_t< T > > makeNode(T &&value)
Definition make_node.h:61
The main namespace of the KDBindings library.
Definition binding.h:21
auto makeBoundProperty(T &&...args)
Helper function to create a Property with a Binding.
Definition binding.h:292
std::unique_ptr< Binding< T, EvaluatorT > > makeBinding(EvaluatorT &evaluator, Property< T > &property)
Helper function to create a Binding from a Property.
Definition binding.h:123
Definition utils.h:161

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDDockWidgets
Advanced Dock Widget Framework for Qt
https://www.kdab.com/development-resources/qt-tools/kddockwidgets/
Generated by doxygen 1.9.8