PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
ID.hpp
1#pragma once
2
3#include <cstdint>
4#include <limits>
5
6namespace PixelBullet
7{
8 class ID
9 {
10 public:
11 using ValueType = uint32_t;
12
13 static constexpr uint32_t IndexBits = 24;
14 static constexpr uint32_t VersionBits = 8;
15 static constexpr ValueType IndexMask = (1u << IndexBits) - 1;
16 static constexpr ValueType VersionMask = (1u << VersionBits) - 1;
17
18 constexpr ID() noexcept
19 : m_Value(Invalid().m_Value)
20 {
21 }
22 constexpr ID(uint32_t index, uint32_t version) noexcept
23 : m_Value((version << IndexBits) | (index & IndexMask))
24 {
25 }
26
27 static constexpr ID Invalid() noexcept
28 {
29 return ID(std::numeric_limits<ValueType>::max());
30 }
31
32 static constexpr ID FromRaw(ValueType raw) noexcept
33 {
34 return ID(raw);
35 }
36
37 [[nodiscard]] constexpr uint32_t Index() const noexcept
38 {
39 return m_Value & IndexMask;
40 }
41
42 [[nodiscard]] constexpr uint32_t Version() const noexcept
43 {
44 return (m_Value >> IndexBits) & VersionMask;
45 }
46
47 [[nodiscard]] constexpr ValueType Raw() const noexcept
48 {
49 return m_Value;
50 }
51
52 constexpr bool operator==(const ID& other) const noexcept = default;
53 constexpr bool operator!=(const ID& other) const noexcept = default;
54
55 constexpr explicit operator bool() const noexcept
56 {
57 return m_Value != Invalid().m_Value;
58 }
59
60 private:
61 explicit constexpr ID(ValueType raw) noexcept
62 : m_Value(raw)
63 {
64 }
65 ValueType m_Value;
66 };
67} // namespace PixelBullet
Definition ID.hpp:9