Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Id.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <entt/entt.hpp>
5#include <fmt/format.h>
6
7namespace Engine {
8
18template <typename TDerived, typename TValue> struct BasicId {
20 TValue value;
21
28 constexpr explicit(false) BasicId(TValue v = TDerived::NullValue()) : value{v} {}
29
34 static constexpr TDerived Null() { return TDerived::Null(); }
35
39 constexpr bool IsNull() const { return value == TDerived::NullValue(); }
40};
41
49struct Id : public BasicId<Id, entt::id_type> {
50 using ValueType = entt::id_type;
51
56 constexpr Id(ValueType v = NullValue()) : BasicId<Id, ValueType>{v} {}
57
60 constexpr operator ValueType() const { return value; }
61
66 static constexpr ValueType NullValue() { return entt::null; }
67
71 static constexpr Id Null() { return Id{NullValue()}; }
72};
73
77static_assert(sizeof(Id) == sizeof(Id::ValueType), "Id size must be equal to entt::id_type size");
78
88struct StringId : public BasicId<StringId, entt::hashed_string> {
92 using ValueType = entt::hashed_string;
93
99
103 static constexpr ValueType NullValue() { return entt::hashed_string{}; }
104
108 static constexpr StringId Null() { return StringId{NullValue()}; }
109};
110
113static_assert(sizeof(StringId) == sizeof(StringId::ValueType),
114 "StringId size must be equal to entt::hashed_string size");
115} // namespace Engine
116
121template <> struct entt::entt_traits<Engine::Id> : entt::entt_traits<entt::id_type> {
123};
124
126template <> struct fmt::formatter<Engine::Id> : fmt::formatter<entt::id_type> {
134 template <typename FormatContext> auto format(const Engine::Id &id, FormatContext &ctx) const
135 {
136 return fmt::formatter<entt::id_type>::format(id.value, ctx);
137 }
138};
139
144template <> struct fmt::formatter<Engine::StringId> : fmt::formatter<std::string_view> {
153 template <typename FormatContext> auto format(const Engine::StringId &id, FormatContext &ctx) const
154 {
155 return fmt::formatter<std::string_view>::format(std::string_view(id.value.data(), id.value.size()), ctx);
156 }
157};
Definition Core.hpp:17
An Id class for creating strongly-typed ID wrappers. This template provides a common interface for ID...
Definition Id.hpp:18
TValue value
The underlying value of the ID.
Definition Id.hpp:20
A strongly-typed identifier wrapper based on entt::id_type. This structure provides a type-safe wrapp...
Definition Id.hpp:49
static constexpr ValueType NullValue()
Get the null value for this ID type. This returns the null value defined by EnTT's id_type,...
Definition Id.hpp:66
constexpr Id(ValueType v=NullValue())
Constructor for Id.
Definition Id.hpp:56
static constexpr Id Null()
Get a null instance of Id, which has its value set to the null value defined by NullValue().
Definition Id.hpp:71
entt::id_type ValueType
Definition Id.hpp:50
Static assertion to ensure that the size of Id matches the size of entt::id_type, guaranteeing that t...
Definition Id.hpp:88
static constexpr ValueType NullValue()
Get the null value for this StringId type. This returns a default-constructed entt::hashed_string.
Definition Id.hpp:103
static constexpr StringId Null()
Get a null instance of StringId, which has its value set to the null value defined by NullValue().
Definition Id.hpp:108
constexpr StringId(ValueType v=NullValue())
Constructor for StringId.
Definition Id.hpp:98
entt::hashed_string ValueType
The underlying value type for StringId, which is entt::hashed_string. This type encapsulates the stri...
Definition Id.hpp:92
Engine::Id value_type
Definition Id.hpp:122
auto format(const Engine::Id &id, FormatContext &ctx) const
Format an Engine::Id using the underlying entt::id_type formatter.
Definition Id.hpp:134
auto format(const Engine::StringId &id, FormatContext &ctx) const
Format an Engine::StringId by extracting its original string representation and formatting it as a st...
Definition Id.hpp:153