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
25 static constexpr TDerived Null(void);
26
29 constexpr bool IsNull(void) const;
30
31 private:
36 constexpr explicit(false) BasicId(TValue value_ = TDerived::NullValue());
37 friend TDerived;
38};
39
47struct Id : public BasicId<Id, entt::id_type> {
48 using ValueType = entt::id_type;
49
52 constexpr Id(ValueType value_ = NullValue());
53
56 constexpr operator ValueType() const;
57
61 static constexpr ValueType NullValue(void);
62
65 static constexpr Id Null(void);
66};
67
68static_assert(sizeof(Id) == sizeof(Id::ValueType), "Id size must be equal to entt::id_type size");
69
79struct StringId : public BasicId<StringId, entt::hashed_string> {
83 using ValueType = entt::hashed_string;
84
88 constexpr StringId(ValueType v = NullValue());
89
92 static constexpr ValueType NullValue();
93
96 static constexpr StringId Null();
97};
98
101static_assert(sizeof(StringId) == sizeof(StringId::ValueType),
102 "StringId size must be equal to entt::hashed_string size");
103} // namespace Engine
104
109template <> struct entt::entt_traits<Engine::Id> : entt::entt_traits<entt::id_type> {
111};
112
114template <> struct fmt::formatter<Engine::Id> : fmt::formatter<entt::id_type> {
121 template <typename FormatContext> auto format(const Engine::Id &id, FormatContext &context) const;
122};
123
128template <> struct fmt::formatter<Engine::StringId> : fmt::formatter<std::string_view> {
136 template <typename FormatContext> auto format(const Engine::StringId &id, FormatContext &context) const;
137};
138
139#include "Id.ipp"
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
static constexpr TDerived Null(void)
Get the null value for this ID type. This should be defined by the derived class to specify what cons...
Definition Id.ipp:8
constexpr bool IsNull(void) const
Checks if the ID is null/invalid by comparing its value to the null value defined by the derived clas...
Definition Id.ipp:12
TValue value
The underlying value of the ID.
Definition Id.hpp:20
friend TDerived
Definition Id.hpp:37
A strongly-typed identifier wrapper based on entt::id_type. This structure provides a type-safe wrapp...
Definition Id.hpp:47
constexpr Id(ValueType value_=NullValue())
Constructor for Id.
Definition Id.ipp:17
static constexpr Id Null(void)
Get a null instance of Id, which has its value set to the null value defined by NullValue().
Definition Id.ipp:23
entt::id_type ValueType
Definition Id.hpp:48
static constexpr ValueType NullValue(void)
Get the null value for this ID type. This returns the null value defined by EnTT's id_type,...
Definition Id.ipp:21
A strongly-typed string identifier wrapper based on entt::hashed_string. This structure provides a ty...
Definition Id.hpp:79
static constexpr StringId Null()
Get a null instance of StringId, which has its value set to the null value defined by NullValue().
Definition Id.ipp:29
static constexpr ValueType NullValue()
Get the null value for this StringId type. This returns a default-constructed entt::hashed_string.
Definition Id.ipp:27
constexpr StringId(ValueType v=NullValue())
Constructor for StringId.
Definition Id.ipp:25
entt::hashed_string ValueType
The underlying value type for StringId, which is entt::hashed_string. This type encapsulates the stri...
Definition Id.hpp:83
Engine::Id value_type
Definition Id.hpp:110
auto format(const Engine::Id &id, FormatContext &context) const
Format an Engine::Id using the underlying entt::id_type formatter.
Definition Id.ipp:33
auto format(const Engine::StringId &id, FormatContext &context) const
Format an Engine::StringId by extracting its original string representation and formatting it as a st...
Definition Id.ipp:39