~aleteoryx/muditaos

muditaos/module-apps/apps-common/CallbackStorage.cpp -rw-r--r-- 3.3 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "CallbackStorage.hpp"

#include "MessageType.hpp"

#include <algorithm>

namespace app
{
    CallbackStorage::CallbackEntry::CallbackEntry(RequestId id,
                                                  AsyncCallbackReceiver::Ptr receiver,
                                                  std::optional<CallbackFunction> callbackFunction,
                                                  ReceiverBehavior receiverBehavior) noexcept
        : id{id}, receiver{receiver}, callbackFunction(callbackFunction), receiverBehavior(receiverBehavior)
    {}

    auto CallbackStorage::getCallback(sys::ResponseMessage *response) -> std::unique_ptr<AsyncCallback>
    {
        if (auto cbEntry = getCallbackEntryFor(response); cbEntry) {
            remove(response);
            return toCallback(response, (*cbEntry).callbackFunction);
        }
        return std::make_unique<NullCallback>();
    }

    auto CallbackStorage::getCallbackEntryFor(sys::ResponseMessage *response) const noexcept
        -> std::optional<CallbackEntry>
    {
        auto it = std::find_if(
            entries.begin(), entries.end(), [response](const auto &entry) { return entry->id == response->uniID; });

        return it != entries.end() ? std::optional<CallbackEntry>(**it) : std::nullopt;
    }

    void CallbackStorage::remove(sys::ResponseMessage *response)
    {
        const auto it = std::remove_if(
            entries.begin(), entries.end(), [response](auto &&entry) { return entry->id == response->uniID; });
        entries.erase(it, entries.end());
    }

    auto CallbackStorage::toCallback(sys::ResponseMessage *response, std::optional<CallbackFunction> callbackFunction)
        -> std::unique_ptr<AsyncCallback>
    {
        if (response->responseTo == MessageType::DBQuery) {
            if (auto queryResponse = dynamic_cast<db::QueryResponse *>(response); queryResponse != nullptr) {
                return std::make_unique<QueryCallback>(queryResponse);
            }
        }

        if (callbackFunction) {
            return std::make_unique<AsyncResponseCallback>(response, *callbackFunction);
        }

        return std::make_unique<NullCallback>();
    }

    void CallbackStorage::registerCallback(RequestId id,
                                           AsyncCallbackReceiver::Ptr receiver,
                                           std::optional<CallbackFunction> &&callback,
                                           ReceiverBehavior receiverBehavior)
    {
        entries.push_back(std::make_unique<CallbackEntry>(id, receiver, std::move(callback), receiverBehavior));
    }

    void CallbackStorage::removeAll(AsyncCallbackReceiver::Ptr receiver)
    {
        const auto it = std::remove_if(
            entries.begin(), entries.end(), [receiver](const auto &entry) { return entry->receiver == receiver; });
        entries.erase(it, entries.end());
    }

    bool CallbackStorage::checkBlockingCloseRequests() const noexcept
    {
        return std::any_of(entries.begin(), entries.end(), [](const auto &request) {
            return request->receiverBehavior == ReceiverBehavior::WaitForResponseToClose;
        });
    }
} // namespace app