PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
entity_id.h
1#pragma once
2
3#include "pixelbullet/serialization/node.h"
4
5#include <cstdint>
6#include <limits>
7
8namespace pixelbullet
9{
11{
12public:
13 using ValueType = uint32_t;
14
15 static constexpr uint32_t index_bits = 24;
16 static constexpr uint32_t version_bits = 8;
17 static constexpr ValueType IndexMask = (1u << index_bits) - 1;
18 static constexpr ValueType VersionMask = (1u << version_bits) - 1;
19
20 constexpr EntityId() noexcept
21 : value_(Invalid().value_)
22 {
23 }
24 constexpr EntityId(uint32_t index, uint32_t version) noexcept
25 : value_((version << index_bits) | (index & IndexMask))
26 {
27 }
28
29 static constexpr EntityId Invalid() noexcept
30 {
31 return EntityId(std::numeric_limits<ValueType>::max());
32 }
33
34 static constexpr EntityId FromRaw(ValueType raw) noexcept
35 {
36 return EntityId(raw);
37 }
38
39 [[nodiscard]] constexpr uint32_t Index() const noexcept
40 {
41 return value_ & IndexMask;
42 }
43
44 [[nodiscard]] constexpr uint32_t Version() const noexcept
45 {
46 return (value_ >> index_bits) & VersionMask;
47 }
48
49 [[nodiscard]] constexpr ValueType Raw() const noexcept
50 {
51 return value_;
52 }
53
54 constexpr bool operator==(const EntityId& other) const noexcept = default;
55 constexpr bool operator!=(const EntityId& other) const noexcept = default;
56
57 constexpr explicit operator bool() const noexcept
58 {
59 return value_ != Invalid().value_;
60 }
61
62private:
63 explicit constexpr EntityId(ValueType raw) noexcept
64 : value_(raw)
65 {
66 }
67 ValueType value_;
68};
69
70inline Node& operator<<(Node& node, const EntityId& entityId)
71{
72 return node << entityId.Raw();
73}
74
75inline const Node& operator>>(const Node& node, EntityId& entityId)
76{
77 uint32_t raw = static_cast<uint32_t>(std::stoul(node.GetValue()));
78 entityId = EntityId::FromRaw(raw);
79 return node;
80}
81} // namespace pixelbullet
Definition entity_id.h:11
Represents a hierarchical node capable of storing various data types and supporting YAML serializatio...
Definition node.h:45