~aleteoryx/muditaos

muditaos/module-sys/SystemManager/cpu/AlgorithmFactory.cpp -rw-r--r-- 1.9 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
// 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 "AlgorithmFactory.hpp"
#include "SystemManager/cpu/algorithm/FrequencyHold.hpp"
#include <memory>

namespace sys::cpu
{

    AlgorithmFactory::PutResult AlgorithmFactory::emplace(sys::cpu::AlgoID id, std::unique_ptr<Algorithm> &&algorithm)
    {
        if (algorithms.find(id) != std::end(algorithms)) {
            algorithms[id] = std::move(algorithm);
            return PutResult::Replaced;
        }
        algorithms.emplace(id, std::move(algorithm));
        return PutResult::Added;
    }

    Algorithm *AlgorithmFactory::get(sys::cpu::AlgoID id)
    {
        if (auto el = algorithms.find(id); el != std::end(algorithms)) {
            return el->second.get();
        }
        return nullptr;
    }

    void AlgorithmFactory::remove(sys::cpu::AlgoID id)
    {
        algorithms.erase(id);
    }

    AlgorithmResult AlgorithmFactory::calculate(const std::list<sys::cpu::AlgoID> &algorithms,
                                                const cpu::AlgorithmData &data,
                                                AlgoID *used)
    {
        for (auto id : algorithms) {
            if (auto algo = get(id); algo != nullptr) {
                if (auto result = algo->calculate(data); result.change != sys::cpu::algorithm::Change::NoChange) {
                    if (used != nullptr) {
                        *used = id;
                    }
                    return result;
                }
            }
        }
        return sys::cpu::AlgorithmResult{sys::cpu::algorithm::Change::NoChange, bsp::CpuFrequencyMHz::Level_6};
    }

    void AlgorithmFactory::reset(const std::list<sys::cpu::AlgoID> &algorithms)
    {
        for (auto again : algorithms) {
            if (auto al = get(again); al != nullptr) {
                al->reset();
            }
        }
    }

} // namespace sys::cpu