|
| 1 | +/* |
| 2 | + This file is part of KDBindings. |
| 3 | +
|
| 4 | + SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> |
| 5 | + Author: Shivam Kunwar <shivam.kunwar@kdab.com> |
| 6 | +
|
| 7 | + SPDX-License-Identifier: MIT |
| 8 | +
|
| 9 | + Contact KDAB at <info@kdab.com> for commercial licensing options. |
| 10 | +*/ |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <functional> |
| 14 | +#include <mutex> |
| 15 | + |
| 16 | +#include <kdbindings/connection_handle.h> |
| 17 | + |
| 18 | +namespace KDBindings { |
| 19 | + |
| 20 | +/** |
| 21 | + * @brief Manages and evaluates deferred Signal connections. |
| 22 | + * |
| 23 | + * @warning Deferred connections are experimental and may be removed or changed in the future. |
| 24 | + * |
| 25 | + * The ConnectionEvaluator class is responsible for managing and evaluating connections |
| 26 | + * to Signals. It provides mechanisms to delay and control the evaluation of connections. |
| 27 | + * It therefore allows controlling when and on which thread slots connected to a Signal are executed. |
| 28 | + * |
| 29 | + * @see Signal::connectDeferred() |
| 30 | + */ |
| 31 | +class ConnectionEvaluator |
| 32 | +{ |
| 33 | + |
| 34 | +public: |
| 35 | + /** ConnectionEvaluators are default constructible */ |
| 36 | + ConnectionEvaluator() = default; |
| 37 | + |
| 38 | + /** Connectionevaluators are not copyable */ |
| 39 | + // As it is designed to manage connections, |
| 40 | + // and copying it could lead to unexpected behavior, including duplication of connections and issues |
| 41 | + // related to connection lifetimes. Therefore, it is intentionally made non-copyable. |
| 42 | + ConnectionEvaluator(const ConnectionEvaluator &) noexcept = delete; |
| 43 | + |
| 44 | + ConnectionEvaluator &operator=(const ConnectionEvaluator &) noexcept = delete; |
| 45 | + |
| 46 | + /** ConnectionEvaluators are not moveable */ |
| 47 | + // As they are captures by-reference |
| 48 | + // by the Signal, so moving them would lead to a dangling reference. |
| 49 | + ConnectionEvaluator(ConnectionEvaluator &&other) noexcept = delete; |
| 50 | + |
| 51 | + ConnectionEvaluator &operator=(ConnectionEvaluator &&other) noexcept = delete; |
| 52 | + |
| 53 | + /** |
| 54 | + * @brief Evaluate the deferred connections. |
| 55 | + * |
| 56 | + * This function is responsible for evaluating and executing deferred connections. |
| 57 | + * This function is thread safe. |
| 58 | + */ |
| 59 | + void evaluateDeferredConnections() |
| 60 | + { |
| 61 | + std::lock_guard<std::mutex> lock(m_slotInvocationMutex); |
| 62 | + |
| 63 | + for (auto &pair : m_deferredSlotInvocations) { |
| 64 | + pair.second(); |
| 65 | + } |
| 66 | + m_deferredSlotInvocations.clear(); |
| 67 | + } |
| 68 | + |
| 69 | +private: |
| 70 | + template<typename...> |
| 71 | + friend class Signal; |
| 72 | + |
| 73 | + void enqueueSlotInvocation(const ConnectionHandle &handle, const std::function<void()> &slotInvocation) |
| 74 | + { |
| 75 | + std::lock_guard<std::mutex> lock(m_slotInvocationMutex); |
| 76 | + m_deferredSlotInvocations.push_back({ handle, std::move(slotInvocation) }); |
| 77 | + } |
| 78 | + |
| 79 | + void dequeueSlotInvocation(const ConnectionHandle &handle) |
| 80 | + { |
| 81 | + std::lock_guard<std::mutex> lock(m_slotInvocationMutex); |
| 82 | + |
| 83 | + auto handleMatches = [&handle](const auto &invocationPair) { |
| 84 | + return invocationPair.first == handle; |
| 85 | + }; |
| 86 | + |
| 87 | + // Remove all invocations that match the handle |
| 88 | + m_deferredSlotInvocations.erase( |
| 89 | + std::remove_if(m_deferredSlotInvocations.begin(), m_deferredSlotInvocations.end(), handleMatches), |
| 90 | + m_deferredSlotInvocations.end()); |
| 91 | + } |
| 92 | + |
| 93 | + std::vector<std::pair<ConnectionHandle, std::function<void()>>> m_deferredSlotInvocations; |
| 94 | + std::mutex m_slotInvocationMutex; |
| 95 | +}; |
| 96 | +} // namespace KDBindings |
0 commit comments