Skyward boardcore
Loading...
Searching...
No Matches
DependencyManager.cpp
Go to the documentation of this file.
1/* Copyright (c) 2024 Skyward Experimental Rocketry
2 * Author: Davide Mor
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "DependencyManager.h"
24
25#include <cxxabi.h>
26#include <fmt/format.h>
27
28#include <atomic>
29
30using namespace Boardcore;
31
33{
34 static std::atomic<int32_t> NEXT_ID{0};
35
36 int32_t next_id = NEXT_ID;
37 while (next_id <= 256)
38 if (NEXT_ID.compare_exchange_weak(next_id, next_id + 1))
39 return next_id;
40
41 return -1;
42}
43
44std::string demangleName(const char* name)
45{
46 char* demangled = abi::__cxa_demangle(name, nullptr, nullptr, nullptr);
47 std::string demangled2{demangled};
48 std::free(demangled);
49
50 return demangled2;
51}
52
53void DependencyManager::graphviz(std::ostream& os)
54{
55 os << "digraph {" << std::endl;
56
57 // First print out all of the nodes
58 for (auto& module : modules)
59 os << fmt::format(" \"{}\"", module.second.name) << std::endl;
60
61 // Then print out the arcs
62 for (auto& module : modules)
63 {
64 for (auto& dep : module.second.deps)
65 {
66 os << fmt::format(" \"{}\" -> \"{}\"", module.second.name,
67 modules[dep].name)
68 << std::endl;
69 }
70 }
71
72 os << "}" << std::endl;
73}
74
76{
77 load_success = true;
78
79 for (auto& module : modules)
80 {
81 LOG_DEBUG(logger, "Configuring [{}]...", module.second.name);
82 DependencyInjector injector{*this, module.second};
83 module.second.injectable->inject(injector);
84 }
85
86 if (load_success)
87 LOG_INFO(logger, "Configuring successful!");
88 else
89 LOG_ERR(logger, "Failed to inject modules!");
90
91 return load_success;
92}
93
94bool DependencyManager::insertImpl(int32_t id, void* raw,
95 Injectable* injectable, const char* name)
96{
97 // Check that the ID is valid
98 if (id < 0)
99 return false;
100
101 return modules
102 .insert({id, ModuleInfo{demangleName(name), raw, injectable, {}}})
103 .second;
104}
105
106void* DependencyManager::getImpl(int32_t id)
107{
108 auto iter = modules.find(id);
109 if (iter == modules.end())
110 return nullptr;
111 else
112 return iter->second.raw;
113}
114
115void* DependencyInjector::getImpl(int32_t id)
116{
117 void* ptr = manager.getImpl(id);
118 if (ptr == nullptr)
119 {
120 // Get failed, signal inject failure and log it
121 manager.load_success = false;
122 LOG_ERR(logger, "[{}] requires a modules which doesn't exist",
123 info.name);
124 return nullptr;
125 }
126 else
127 {
128 // Get successful, add it to the dependencies
129 info.deps.push_back(id);
130 return ptr;
131 }
132}
std::string demangleName(const char *name)
#define LOG_INFO(logger,...)
#define LOG_ERR(logger,...)
#define LOG_DEBUG(logger,...)
Proxy class used to obtain dependencies.
bool inject()
Inject all dependencies into all inserted .
void graphviz(std::ostream &os)
Generate a gaphviz compatible output showing dependencies. Needs to be called after inject.
Interface for an injectable dependency.
This file includes all the types the logdecoder script will decode.
int32_t getNextDependencyId()
Returns the next available id.