LeviLamina
Loading...
Searching...
No Matches
typeid_t.h
1#pragma once
2
3#include "mc/_HeaderOutputPredefine.h"
4
5#include "ll/api/reflection/TypeName.h"
6#include "ll/api/utils/HashUtils.h"
7
9
10namespace Bedrock {
11template <typename Category>
12class typeid_t;
13} // namespace Bedrock
14
15namespace ll {
16
18public:
19 void* ptr{};
20
21 template <typename Category>
22 [[nodiscard]] Bedrock::typeid_t<Category>& get() const noexcept {
23 return *static_cast<Bedrock::typeid_t<Category>*>(ptr);
24 }
25};
26
27static_assert(sizeof(type_id_ref) == sizeof(void*));
28static_assert(std::is_trivially_copyable_v<type_id_ref>);
29static_assert(std::is_aggregate_v<type_id_ref>);
30
31} // namespace ll
32
33namespace Bedrock {
34
35template <typename Category>
36class typeid_t {
37public:
38 ushort value{};
39 [[nodiscard]] constexpr typeid_t(typeid_t const& other) : value(other.value) {}
40 constexpr typeid_t& operator=(typeid_t const& other) {
41 value = other.value;
42 return *this;
43 }
44
45 [[nodiscard]] constexpr typeid_t(ushort value) : value(value) {}
46 [[nodiscard]] constexpr typeid_t() = default;
47
48 constexpr bool operator==(typeid_t const& other) const { return value == other.value; }
49
50 constexpr bool operator<(typeid_t const& other) const { return value < other.value; }
51
52 static std::atomic_ushort& _getCounter() {
53
54 // MCAPI // error C2201
55 static std::atomic_ushort storage;
56
57 return storage;
58 }
59};
60
61template <>
62LLAPI std::atomic_ushort& typeid_t<CommandRegistry>::_getCounter();
63
64// if dll reload, typeid can't keep, so we need a static implementation
65LLAPI ushort crtypidImpl(size_t type);
66
67template <typename Category, typename Type>
68ll::type_id_ref typeid_storage_impl() {
69 static_assert(std::is_same_v<Category, CommandRegistry>);
70 constexpr size_t hash = ll::hash_utils::doHash(ll::reflection::type_raw_name_v<Type>);
71 static typeid_t<Category> id{crtypidImpl(hash)};
72 return ll::type_id_ref{&id};
73}
74
75template <typename Category, typename Type>
76typeid_t<Category> type_id() {
77 if constexpr (std::is_same_v<Category, CommandRegistry>) {
78 return typeid_storage_impl<Category, Type>().template get<Category>();
79 } else {
80 static typeid_t<Category> id{++typeid_t<Category>::_getCounter()};
81 return id;
82 }
83} // namespace Bedrock
84
85}; // namespace Bedrock
Definition typeid_t.h:36
Definition CommandRegistry.h:51
Definition typeid_t.h:17