~aleteoryx/muditaos

ref: 34ee674f371c0588f1c47bb0ce6fdda517e2ace0 muditaos/module-services/service-appmgr/model/ApplicationStack.cpp -rw-r--r-- 4.0 KiB
34ee674f — Mateusz Piesta [MOS-939] Apply target update scripts 2 years 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ApplicationStack.hpp"

#include <algorithm>

namespace app::manager
{
    bool operator==(const ApplicationStack::Item &lhs, const ApplicationStack::Item &rhs) noexcept
    {
        return lhs.appName == rhs.appName;
    }

    bool operator!=(const ApplicationStack::Item &lhs, const ApplicationStack::Item &rhs) noexcept
    {
        return !operator==(lhs, rhs);
    }

    bool operator<(const ApplicationStack::Item &lhs, const ApplicationStack::Item &rhs) noexcept
    {
        return lhs.appName < rhs.appName;
    }

    bool operator>(const ApplicationStack::Item &lhs, const ApplicationStack::Item &rhs) noexcept
    {
        return lhs.appName > rhs.appName;
    }

    void ApplicationStack::push(Item &&item)
    {
        stack.push_front(std::move(item));
    }

    void ApplicationStack::pop()
    {
        if (!isEmpty()) {
            stack.pop_front();
        }
    }

    void ApplicationStack::eraseFirstOf(const ApplicationName &appName) noexcept
    {
        const auto targetApp =
            std::find_if(stack.begin(), stack.end(), [&appName](const auto &item) { return item.appName == appName; });
        if (targetApp != stack.end()) {
            stack.erase(targetApp);
        }
    }

    void ApplicationStack::clear()
    {
        stack.clear();
    }

    auto ApplicationStack::unique() const noexcept -> std::vector<ApplicationName>
    {
        auto stackCopy = stack;
        std::sort(stackCopy.begin(), stackCopy.end());
        const auto last = std::unique(stackCopy.begin(), stackCopy.end());
        stackCopy.erase(last, stackCopy.end());

        std::vector<ApplicationName> uniqueApps{};
        uniqueApps.reserve(stackCopy.size());
        std::transform(stackCopy.begin(), stackCopy.end(), std::back_inserter(uniqueApps), [](const auto &item) {
            return item.appName;
        });
        return uniqueApps;
    }

    bool ApplicationStack::isEmpty() const noexcept
    {
        return stack.empty();
    }

    auto ApplicationStack::size() const noexcept -> Container::size_type
    {
        return stack.size();
    }

    auto ApplicationStack::front() noexcept -> Item &
    {
        return stack.front();
    }

    auto ApplicationStack::front() const noexcept -> const Item &
    {
        return stack.front();
    }

    auto ApplicationStack::find(const ApplicationName &appName) const -> std::vector<Item>
    {
        std::vector<Item> found;
        std::copy_if(stack.begin(), stack.end(), std::back_inserter(found), [&appName](const auto &item) {
            return item.appName == appName;
        });
        return found;
    }

    auto ApplicationStack::contains(const std::string &appName) const noexcept -> bool
    {
        const auto it =
            std::find_if(stack.begin(), stack.end(), [&appName](const auto &item) { return item.appName == appName; });
        return it != stack.end();
    }

    auto ApplicationStack::getIndexOf(const std::string &appName) const noexcept -> int
    {
        const auto targetApp =
            std::find_if(stack.begin(), stack.end(), [&appName](const auto &item) { return item.appName == appName; });
        if (targetApp == stack.end()) {
            return -1;
        }
        return std::distance(stack.begin(), targetApp);
    }

    auto ApplicationStack::begin() -> Container::iterator
    {
        return stack.begin();
    }

    auto ApplicationStack::begin() const -> Container::const_iterator
    {
        return stack.begin();
    }

    auto ApplicationStack::end() -> Container::iterator
    {
        return stack.end();
    }

    auto ApplicationStack::end() const -> Container::const_iterator
    {
        return stack.end();
    }

    auto ApplicationStack::operator[](int index) -> Item &
    {
        return stack[index];
    }

    auto ApplicationStack::operator[](int index) const -> const Item &
    {
        return stack[index];
    }
} // namespace app::manager