diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-03-25 23:28:26 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-03-25 23:28:26 +0100 |
commit | e1229e05aba7554363b2aa9874bd383b5923ee8b (patch) | |
tree | adc8a8bc65942cc0c8e2cc69faa60fee29dbea8a | |
parent | e3675d4d75c708f35f8041f493fde2fbfbea55b2 (diff) |
Basic console API & API registry implementation
-rw-r--r-- | deimos/core/BUILD | 2 | ||||
-rw-r--r-- | deimos/core/api_registry.cpp | 18 | ||||
-rw-r--r-- | deimos/core/api_registry.h | 7 | ||||
-rw-r--r-- | deimos/core/base.h | 10 | ||||
-rw-r--r-- | deimos/core/id_name.h | 5 |
5 files changed, 39 insertions, 3 deletions
diff --git a/deimos/core/BUILD b/deimos/core/BUILD index cb76f90..2f0dc2d 100644 --- a/deimos/core/BUILD +++ b/deimos/core/BUILD @@ -6,10 +6,12 @@ cc_library( "base.h",
"hash.h",
"id_name.h",
+ "os.h",
],
srcs = [
"allocator.cpp",
"api_registry.cpp",
+ "os_win32.cpp",
],
visibility = ["//:__subpackages__"],
)
diff --git a/deimos/core/api_registry.cpp b/deimos/core/api_registry.cpp index 873d72d..1489484 100644 --- a/deimos/core/api_registry.cpp +++ b/deimos/core/api_registry.cpp @@ -7,6 +7,7 @@ namespace deimos {
AllocatorApi* BootstrapAllocatorApi();
+void RegisterOsApi(ApiRegistry*);
struct ApiEntry
{
@@ -22,6 +23,7 @@ struct ApiEntry class ApiRegistryImpl: public ApiRegistry
{
Allocator* m_allocator;
+ const ApiEntry* m_head{};
public:
explicit ApiRegistryImpl(Allocator* allocator) :
@@ -30,9 +32,17 @@ public: void Set(const IdName& name, void* impl) final
{
- (void)name;
- (void)impl;
- (void)m_allocator;
+ auto* entry = m_allocator->New<ApiEntry>(name, impl);
+ entry->next = std::exchange(m_head, entry);
+ }
+
+ void* Get(const IdName& name) final
+ {
+ for (const ApiEntry* it = m_head; it != nullptr; it = it->next)
+ {
+ if (it->name == name) { return it->impl; }
+ }
+ return nullptr;
}
};
@@ -45,6 +55,8 @@ ApiRegistry* InitializeGlobalApiRegistry() api_registry->Set(g_allocator_api);
+ RegisterOsApi(api_registry);
+
return api_registry;
}
diff --git a/deimos/core/api_registry.h b/deimos/core/api_registry.h index 2624935..f2f850a 100644 --- a/deimos/core/api_registry.h +++ b/deimos/core/api_registry.h @@ -15,9 +15,16 @@ public: virtual ~ApiRegistry() = default;
+ virtual void* Get(const IdName&) = 0;
virtual void Set(const IdName&, void* impl) = 0;
template<typename Api>
+ Api* Get()
+ {
+ return (Api*)Get(Api::kApiName);
+ }
+
+ template<typename Api>
void Set(Api* impl)
{
Set(Api::kApiName, impl);
diff --git a/deimos/core/base.h b/deimos/core/base.h index ea14610..0669f07 100644 --- a/deimos/core/base.h +++ b/deimos/core/base.h @@ -46,6 +46,8 @@ struct uint128 {
uint64 high;
uint64 low;
+
+ constexpr bool operator==(const uint128& other) const = default;
};
struct SourceLocation
@@ -96,5 +98,13 @@ constexpr T&& forward(deimos::RemoveReference<T>&& t) noexcept // NOLINT return static_cast<T&&>(t);
}
+template<typename T, typename U = T>
+constexpr T exchange(T& obj, U&& new_value)
+{
+ T old_value = std::move(obj);
+ obj = std::forward<U>(new_value);
+ return old_value;
+}
+
} // namespace std
diff --git a/deimos/core/id_name.h b/deimos/core/id_name.h index 0fb793b..47e7708 100644 --- a/deimos/core/id_name.h +++ b/deimos/core/id_name.h @@ -15,6 +15,11 @@ struct IdName hash{MurmurHash3_x64_128(name_)},
name{name_}
{}
+
+ constexpr bool operator==(const IdName& other) const
+ {
+ return hash == other.hash;
+ }
};
} // namespace deimos
|