~aleteoryx/muditaos

ref: 0e8b4c848e3f87f3bbb1f64ef7460cf56017b87f muditaos/module-services/service-gui/ContextPool.hpp -rw-r--r-- 2.2 KiB
0e8b4c84 — Lefucjusz [BH-2108] Fix misaligned charging symbol 3 months 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include "SynchronizationMechanism.hpp"

#include <gui/Common.hpp>
#include <gui/core/Context.hpp>

#include <mutex.hpp>

#include <chrono>
#include <functional>
#include <memory>
#include <utility>
#include <vector>

namespace service::gui
{
    class ContextPool
    {
      public:
        ContextPool(
            ::gui::Size screenSize,
            std::size_t capacity,
            std::unique_ptr<SynchronizationMechanism> &&synchronization = getFreeRtosSynchronizationMechanism());

        /**
         * Peeks the context under specific id
         * @param id    Context id
         * @return Context data
         */
        [[nodiscard]] auto peekContext(int id) noexcept -> ::gui::Context *;
        /**
         * Locks the first available context
         * @return A context and its id
         */
        [[nodiscard]] auto borrowContext() -> std::pair<int, ::gui::Context *>;
        /**
         * Locks the context
         * @param id    Context id
         * @return Context data
         */
        [[nodiscard]] auto borrowContext(int id) -> ::gui::Context *;
        /**
         * Checks whether any context is currently locked
         * @return True if any context is locked, false otherwise
         */
        [[nodiscard]] auto isAnyContextLocked() const -> bool;
        /**
         * Makes a context (specified by its id) available again.
         * @param id    Context id
         */
        void returnContext(int id);

      private:
        using ContextIdContainer = std::vector<int>;

        [[nodiscard]] auto isAvailable() const noexcept -> bool;

        void freeContext(int contextId);
        void lockContext(int contextId);
        static void removeContextId(std::vector<int> &sequence, int contextId);
        static void addContextId(std::vector<int> &sequence, int contextId);

        std::vector<std::unique_ptr<::gui::Context>> contexts;
        ContextIdContainer lockedContextIds;
        ContextIdContainer freeContextIds;

        mutable cpp_freertos::MutexStandard mutex;
        std::unique_ptr<SynchronizationMechanism> synchronization;
    };
} // namespace service::gui