SimpleKernel 1.17.0
Loading...
Searching...
No Matches
resource_id.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <etl/hash.h>
8
9#include <cstdint>
10#include <functional>
11
15enum class ResourceType : uint8_t {
17 kNone = 0x00,
19 kMutex = 0x01,
21 kSemaphore = 0x02,
23 kCondVar = 0x03,
25 kChildExit = 0x04,
27 kIoComplete = 0x05,
29 kFutex = 0x06,
31 kSignal = 0x07,
33 kTimer = 0x08,
35 kInterrupt = 0x09,
36 // 可以继续扩展...
38};
39
45[[nodiscard]] constexpr auto GetResourceTypeName(ResourceType type) -> const
46 char* {
47 switch (type) {
49 return "None";
51 return "Mutex";
53 return "Semaphore";
55 return "CondVar";
57 return "ChildExit";
59 return "IoComplete";
61 return "Futex";
63 return "Signal";
65 return "Timer";
66 default:
67 return "Unknown";
68 }
69}
70
78 public:
83 [[nodiscard]] constexpr auto GetType() const -> ResourceType {
84 return static_cast<ResourceType>((value_ >> kTypeShift) & 0xFF);
85 }
86
91 [[nodiscard]] constexpr auto GetData() const -> uint64_t {
92 return value_ & kDataMask;
93 }
94
99 [[nodiscard]] constexpr auto GetTypeName() const -> const char* {
101 }
102
107 constexpr explicit operator bool() const {
108 return GetType() != ResourceType::kNone;
109 }
110
115 constexpr operator uint64_t() const { return value_; }
116
122 constexpr auto operator==(const ResourceId& other) const -> bool {
123 return value_ == other.value_;
124 }
125
131 constexpr auto operator!=(const ResourceId& other) const -> bool {
132 return value_ != other.value_;
133 }
134
137
143 constexpr ResourceId(ResourceType type, uint64_t data)
144 : value_((static_cast<uint64_t>(type) << kTypeShift) |
145 (data & kDataMask)) {}
146
147 ResourceId() = default;
148 ResourceId(const ResourceId&) = default;
149 ResourceId(ResourceId&&) = default;
150 auto operator=(const ResourceId&) -> ResourceId& = default;
151 auto operator=(ResourceId&&) -> ResourceId& = default;
152 ~ResourceId() = default;
154
155 private:
156 static constexpr uint8_t kTypeShift = 56;
157 static constexpr uint64_t kTypeMask = 0xFF00000000000000ULL;
158 static constexpr uint64_t kDataMask = 0x00FFFFFFFFFFFFFFULL;
159
161 uint64_t value_{0};
162};
163
164// std::hash 特化,使 ResourceId 可以作为 unordered_map 的键
165namespace std {
166template <>
167struct hash<ResourceId> {
168 constexpr auto operator()(const ResourceId& id) const noexcept -> size_t {
169 return hash<uint64_t>{}(static_cast<uint64_t>(id));
170 }
171};
172} // namespace std
173
174// etl::hash 特化,使 ResourceId 可以作为 etl::unordered_map 的键
175
176namespace etl {
177template <>
178struct hash<ResourceId> {
179 auto operator()(const ResourceId& id) const -> size_t {
180 return etl::hash<uint64_t>{}(static_cast<uint64_t>(id));
181 }
182};
183} // namespace etl
资源 ID
uint64_t value_
内部存储的原始值
ResourceId(ResourceId &&)=default
~ResourceId()=default
auto operator=(const ResourceId &) -> ResourceId &=default
static constexpr uint64_t kDataMask
constexpr auto GetTypeName() const -> const char *
获取类型名称(用于调试)
constexpr auto GetType() const -> ResourceType
获取资源类型
constexpr auto operator!=(const ResourceId &other) const -> bool
不等比较操作符
constexpr auto GetData() const -> uint64_t
获取资源数据
static constexpr uint64_t kTypeMask
ResourceId(const ResourceId &)=default
auto operator=(ResourceId &&) -> ResourceId &=default
static constexpr uint8_t kTypeShift
ResourceId()=default
constexpr auto operator==(const ResourceId &other) const -> bool
相等比较操作符
constexpr ResourceId(ResourceType type, uint64_t data)
构造资源 ID
constexpr auto GetResourceTypeName(ResourceType type) -> const char *
获取资源类型的字符串表示(用于调试)
ResourceType
资源类型枚举
@ kFutex
Futex (快速用户空间互斥锁)
@ kNone
无效资源
@ kInterrupt
中断(用于中断线程化)
@ kSignal
信号
@ kCondVar
条件变量
@ kChildExit
等待子进程退出
@ kSemaphore
信号量
@ kMutex
互斥锁
@ kTimer
定时器
@ kIoComplete
IO 完成
auto operator()(const ResourceId &id) const -> size_t
constexpr auto operator()(const ResourceId &id) const noexcept -> size_t