#include #include #include #include #include #include #include #include #include #include namespace kapi::devices { namespace { auto constinit instance = std::optional{}; } auto device_registry::init() -> void { if (instance) { system::panic("[OS:DEV] Device registry has already been initialized"); } instance = device_registry{}; } auto device_registry::get() -> device_registry & { if (!instance) { system::panic("[OS:DEV] Device registry has not been initialized"); } return *instance; } auto device_registry::add(kstd::shared_ptr device) -> bool { if (!device) { return false; } kstd::println("[OS:DEV] Registering device {}", device->name()); auto found = m_devices.find(device->name()); if (found != m_devices.end()) { if (!found->second.expired()) { return false; } found->second = device; return true; } return m_devices.emplace(device->name(), device).second; } auto device_registry::remove(device &) -> bool { kstd::println("[OS:DEV] TODO: implement device deregistration"); return false; } auto device_registry::find(std::string_view name) const -> kstd::shared_ptr { auto found = m_devices.find(kstd::string{name}); if (found == m_devices.end()) { return nullptr; } return found->second.lock(); } auto device_registry::all() const -> kstd::vector> { auto result = kstd::vector>{}; for (auto const & [name, weak_device] : m_devices) { if (auto device = weak_device.lock()) { result.push_back(std::move(device)); } } return result; } } // namespace kapi::devices namespace kapi::test_support::devices { auto deinit_device_registry() -> void { kapi::devices::instance.reset(); } } // namespace kapi::test_support::devices