Contur 2
Educational OS kernel simulator
Loading...
Searching...
No Matches
device_manager.h
Go to the documentation of this file.
1
7
8#pragma once
9
10#include <functional>
11#include <memory>
12#include <optional>
13#include <vector>
14
15#include "contur/core/error.h"
16#include "contur/core/types.h"
17
18#include "contur/io/i_device.h"
19
20namespace contur {
21
27 {
28 public:
31
34
35 // Non-copyable, movable
36 DeviceManager(const DeviceManager &) = delete;
40
42 DeviceManager &operator=(DeviceManager &&) noexcept;
43
47 [[nodiscard]] Result<void> registerDevice(std::unique_ptr<IDevice> device);
48
51 [[nodiscard]] Result<void> unregisterDevice(DeviceId id);
52
54 [[nodiscard]] std::optional<std::reference_wrapper<IDevice>> getDevice(DeviceId id) noexcept;
55
57 [[nodiscard]] std::optional<std::reference_wrapper<const IDevice>> getDevice(DeviceId id) const noexcept;
58
61 [[nodiscard]] Result<void> write(DeviceId id, RegisterValue value);
62
65 [[nodiscard]] Result<RegisterValue> read(DeviceId id);
66
68 [[nodiscard]] std::size_t deviceCount() const noexcept;
69
71 [[nodiscard]] bool hasDevice(DeviceId id) const noexcept;
72
73 private:
74 struct Impl;
75 std::unique_ptr<Impl> impl_;
76 };
77
78} // namespace contur
std::size_t deviceCount() const noexcept
Returns the number of registered devices.
DeviceManager(const DeviceManager &)=delete
Result< RegisterValue > read(DeviceId id)
Reads a value from the device with the given ID.
~DeviceManager()
Destroys registry and owned devices.
Result< void > write(DeviceId id, RegisterValue value)
Writes a value to the device with the given ID.
std::optional< std::reference_wrapper< IDevice > > getDevice(DeviceId id) noexcept
Returns a non-owning reference to the device with the given ID, if present.
std::unique_ptr< Impl > impl_
DeviceManager()
Constructs an empty device registry.
DeviceManager & operator=(const DeviceManager &)=delete
Result< void > registerDevice(std::unique_ptr< IDevice > device)
Registers a device. Takes ownership.
DeviceManager(DeviceManager &&) noexcept
Move-constructs registry state.
Result< void > unregisterDevice(DeviceId id)
Removes and destroys a device by ID.
bool hasDevice(DeviceId id) const noexcept
Returns true if a device with the given ID is registered.
Abstract interface for a simulated I/O device.
Definition i_device.h:22
A result type that holds either a success value of type T or an ErrorCode.
Definition error.h:104
Error codes and Result<T> type for fallible operations.
IDevice — abstract interface for I/O devices.
Definition block.h:15
std::int32_t RegisterValue
Value stored in a CPU register.
Definition types.h:21
std::uint16_t DeviceId
Unique identifier for an I/O device.
Definition types.h:24
Common type aliases, sentinel constants, and forward declarations used throughout the Contur 2 kernel...