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 static std::atomic_ushort& _getCounter() {
51
52 // MCAPI // error C2201
53 static std::atomic_ushort storage;
54
55 return storage;
56 }
57};
58
59template <>
60LLAPI std::atomic_ushort& typeid_t<CommandRegistry>::_getCounter();
61
62// if dll reload, typeid can't keep, so we need a static implementation
63LLAPI ushort crtypidImpl(size_t type);
64
65template <typename Category, typename Type>
66ll::type_id_ref typeid_storage_impl() {
67 static_assert(std::is_same_v<Category, CommandRegistry>);
68 constexpr size_t hash = ll::hash_utils::doHash(ll::reflection::type_raw_name_v<Type>);
69 static typeid_t<Category> id{crtypidImpl(hash)};
70 return ll::type_id_ref{&id};
71}
72
73template <typename Category, typename Type>
74typeid_t<Category> type_id() {
75 if constexpr (std::is_same_v<Category, CommandRegistry>) {
76 return typeid_storage_impl<Category, Type>().template get<Category>();
77 } else {
78 static typeid_t<Category> id{++typeid_t<Category>::_getCounter()};
79 return id;
80 }
81} // namespace Bedrock
82
83}; // namespace Bedrock
Definition typeid_t.h:36
Definition CommandRegistry.h:51
Definition typeid_t.h:17