KDBindings API Documentation  1.0.95
binding_evaluator.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 <functional>
15 #include <map>
16 #include <memory>
17 
18 namespace KDBindings {
19 
42 {
43  // We use pimpl here so that we can pass evaluators around by value (copies)
44  // yet each copy refers to the same set of data
45  struct Private {
46  // TODO: Use std::vector here?
47  std::map<int, std::function<void()>> m_bindingEvalFunctions;
48  int m_currentId;
49  };
50 
51 public:
53  BindingEvaluator() = default;
54 
62  BindingEvaluator(const BindingEvaluator &) noexcept = default;
63 
71  BindingEvaluator &operator=(const BindingEvaluator &) noexcept = default;
72 
76  BindingEvaluator(BindingEvaluator &&other) noexcept = delete;
77 
81  BindingEvaluator &operator=(BindingEvaluator &&other) noexcept = delete;
82 
89  void evaluateAll() const
90  {
91  // a std::map's ordering is deterministic, so the bindings are evaluated
92  // in the order they were inserted, ensuring correct transitive dependency
93  // evaluation.
94  for (auto &[id, func] : m_d->m_bindingEvalFunctions)
95  func();
96  }
97 
98 private:
99  template<typename BindingType>
100  int insert(BindingType *binding)
101  {
102  m_d->m_bindingEvalFunctions.insert({ ++(m_d->m_currentId),
103  [=]() { binding->evaluate(); } });
104  return m_d->m_currentId;
105  }
106 
107  void remove(int id)
108  {
109  m_d->m_bindingEvalFunctions.erase(id);
110  }
111 
112  std::shared_ptr<Private> m_d{ std::make_shared<Private>() };
113 
114  template<typename T, typename UpdaterT>
115  friend class Binding;
116 };
117 
130 {
131 public:
133  {
134  static ImmediateBindingEvaluator evaluator;
135  return evaluator;
136  }
137 };
138 
139 } // namespace KDBindings
140 
A BindingEvaluator provides a mechanism to control the exact time when a KDBindings::Binding is reeva...
BindingEvaluator(BindingEvaluator &&other) noexcept=delete
BindingEvaluator & operator=(const BindingEvaluator &) noexcept=default
BindingEvaluator & operator=(BindingEvaluator &&other) noexcept=delete
BindingEvaluator(const BindingEvaluator &) noexcept=default
A combination of a root Node with an evaluator.
Definition: binding.h:35
static ImmediateBindingEvaluator instance()
The main namespace of the KDBindings library.
Definition: binding.h:21

© 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