KDBindings API Documentation  1.0.95
binding.h
Go to the documentation of this file.
1 /*
2  This file is part of KDBindings.
3 
4  SPDX-FileCopyrightText: 2021 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>
17 #include <kdbindings/make_node.h>
20 
21 namespace KDBindings {
22 
33 template<typename T, typename EvaluatorT = BindingEvaluator>
34 class Binding : public PropertyUpdater<T>, public Private::Dirtyable
35 {
36  static_assert(
37  std::is_base_of<BindingEvaluator, EvaluatorT>::value,
38  "The EvaluatorT type must inherit from BindingEvaluator.");
39 
40 public:
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);
52  m_rootNode.setParent(this);
53  }
54 
56  ~Binding() override
57  {
58  m_evaluator.remove(m_bindingId);
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 
96 protected:
97  Private::Dirtyable **parentVariable() override { return nullptr; }
98  const bool *dirtyVariable() const override { return nullptr; }
99 
101  Private::Node<T> m_rootNode;
103  EvaluatorT m_evaluator;
105  std::function<void(T &&)> m_propertyUpdateFunction = [](T &&) {};
107  int m_bindingId = -1;
108 };
109 
122 template<typename T, typename EvaluatorT>
123 inline 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 
140 template<typename T, typename EvaluatorT>
141 inline std::unique_ptr<Binding<T, EvaluatorT>> makeBinding(EvaluatorT &evaluator, const Property<T> &property)
142 {
143  return std::make_unique<Binding<T, EvaluatorT>>(Private::makeNode(property), evaluator);
144 }
145 
158 template<typename T, typename EvaluatorT>
159 inline std::unique_ptr<Binding<T, EvaluatorT>> makeBinding(EvaluatorT &evaluator, Private::Node<T> &&rootNode)
160 {
161  return std::make_unique<Binding<T, EvaluatorT>>(std::move(rootNode), evaluator);
162 }
163 
180 template<typename EvaluatorT, typename Func, typename... Args, typename = std::enable_if_t<sizeof...(Args) != 0>, typename ResultType = Private::operator_node_result_t<Func, Args...>>
181 inline std::unique_ptr<Binding<ResultType, EvaluatorT>> makeBinding(EvaluatorT &evaluator, Func &&func, Args &&...args)
182 {
183  return std::make_unique<Binding<ResultType, EvaluatorT>>(Private::makeNode(std::forward<Func>(func), std::forward<Args>(args)...), evaluator);
184 }
185 
196 template<typename T>
198 {
199 public:
205  explicit Binding(Private::Node<T> &&rootNode)
206  : Binding<T, BindingEvaluator>(std::move(rootNode), ImmediateBindingEvaluator::instance())
207  {
208  }
209 
211  Binding() = delete;
212 
213  virtual ~Binding() = default;
214 
216  Binding(Binding const &other) = delete;
218  Binding &operator=(Binding const &other) = delete;
219 
221  Binding(Binding &&other) = delete;
223  Binding &operator=(Binding &&other) = delete;
224 
225  void markDirty() override
226  {
228  }
229 };
230 
242 template<typename T>
243 inline std::unique_ptr<Binding<T, ImmediateBindingEvaluator>> makeBinding(Property<T> &property)
244 {
245  return std::make_unique<Binding<T, ImmediateBindingEvaluator>>(Private::makeNode(property));
246 }
247 
260 template<typename T>
261 inline std::unique_ptr<Binding<T, ImmediateBindingEvaluator>> makeBinding(const Property<T> &property)
262 {
263  return std::make_unique<Binding<T, ImmediateBindingEvaluator>>(Private::makeNode(property));
264 }
265 
277 template<typename T>
278 inline std::unique_ptr<Binding<T, ImmediateBindingEvaluator>> makeBinding(Private::Node<T> &&rootNode)
279 {
280  return std::make_unique<Binding<T, ImmediateBindingEvaluator>>(std::move(rootNode));
281 }
282 
297 template<typename Func, typename... Args, typename = std::enable_if_t<sizeof...(Args) != 0>, typename ResultType = Private::operator_node_result_t<Func, Args...>>
298 inline std::unique_ptr<Binding<ResultType, ImmediateBindingEvaluator>> makeBinding(Func &&func, Args &&...args)
299 {
300  return std::make_unique<Binding<ResultType, ImmediateBindingEvaluator>>(Private::makeNode(std::forward<Func>(func), std::forward<Args>(args)...));
301 }
302 
327 template<typename... T>
328 inline auto makeBoundProperty(T &&...args)
329 {
330  auto binding = makeBinding(std::forward<T>(args)...);
331  return Property<decltype(binding->get())>(std::move(binding));
332 }
333 
334 } // namespace KDBindings
A BindingEvaluator provides a mechanism to control the exact time when a KDBindings::Binding is reeva...
Provides a convenience for old-school, immediate mode Bindings.
Definition: binding.h:198
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:205
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
Binding & operator=(Binding &&other)=delete
Private::Dirtyable ** parentVariable() override
Definition: binding.h:97
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
const bool * dirtyVariable() const override
Definition: binding.h:98
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
Private::Node< T > m_rootNode
Definition: binding.h:101
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
The main namespace of the KDBindings library.
Definition: binding.h:21
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
auto makeBoundProperty(T &&...args)
Helper function to create a Property with a Binding.
Definition: binding.h:328
Definition: utils.h:161

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDBindings
Reactive programming & data binding in C++
https://github.com/KDAB/KDBindings/
Generated by doxygen 1.9.1