New Super Mario Bros. U Headers
Loading...
Searching...
No Matches
ActorUniqueID.h
Go to the documentation of this file.
1#pragma once
2
3#include <basis/seadAssert.h>
4#include <basis/seadTypes.h>
5
6/**
7 * @brief Safe identifier handle used as an alternative to raw actor pointers.
8 * @details Combines a physical @c ActorPtrCache array index with a global generation
9 * counter. This ensures that recycled array slots are never misidentified
10 * as the original actor.
11 */
13{
14public:
15 /// Represents an invalid or uninitialized ID.
16 static const u32 cInvalidID = 0;
17 /// The maximum allowed array index (10 bits; 1023).
18 static const u32 cArrayIndexMax = 0x3ff;
19 /// The maximum allowed global create index (22 bits; 4,194,303)
20 static const u32 cCreateIndexMax = 0x3fffff;
21
22public:
23 /**
24 * @brief Constructs an invalid ActorUniqueID.
25 */
27 {
28 setValue(cInvalidID);
29 }
30
31 /**
32 * @brief Constructs an ActorUniqueID from a raw 32-bit packed value containing both the array index and create index.
33 * @param id The raw 32-bit representation of the desired ID to create.
34 */
36 {
37 setValue(id);
38 }
39
40 /**
41 * @brief Constructs an ActorUniqueID by packing a physical index and a global generation index.
42 * @param i_array_index The 10-bit physical index in the ActorPtrCache array.
43 * @param i_create_index The 22-bit global generation counter.
44 */
45 ActorUniqueID(u32 i_array_index, u32 i_create_index)
46 {
47 setValue(i_array_index, i_create_index);
48 }
49
50 /**
51 * @brief Overwrites the ID with a raw 32-bit packed value containing both the array index and create index.
52 * @param id The raw 32-bit representation of the desired ID to set.
53 */
54 void setValue(u32 id)
55 {
56 mValue = id;
57 }
58
59 /**
60 * @brief Overwrites the ID by packing a physical index and a global generation index.
61 * @param i_array_index The 10-bit physical index in the ActorPtrCache array. Must be <= @c cArrayIndexMax.
62 * @param i_create_index The 22-bit global generation counter. Must be <= @c cCreateIndexMax.
63 */
64 void setValue(u32 i_array_index, u32 i_create_index)
65 {
66 SEAD_ASSERT(i_array_index <= cArrayIndexMax);
67 SEAD_ASSERT(i_create_index <= cCreateIndexMax);
68 mValue = i_array_index << /* log2(cCreateIndexMax + 1) */ 22 | i_create_index;
69 }
70
71 /**
72 * @brief The raw 32-bit packed value of this ID.
73 */
74 u32 getValue() const
75 {
76 return mValue;
77 }
78
79 /**
80 * @brief Extracts the physical array index where this actor is stored.
81 * @return The 10-bit array index (top bits of the packed ID).
82 */
84 {
85 return mValue >> /* log2(cCreateIndexMax + 1) */ 22;
86 }
87
88 /**
89 * @brief Extracts the global generation counter assigned to this actor at creation.
90 * @return The 22-bit create index (bottom bits of the packed ID).
91 */
93 {
94 return mValue & cCreateIndexMax;
95 }
96
97 /**
98 * @brief Whether this ID has been assigned a valid value (not equal to @c cInvalidID).
99 */
100 bool isValid() const
101 {
102 return mValue != cInvalidID;
103 }
104
105 /**
106 * @brief Clears the ID, setting it back to @c cInvalidID.
107 */
109 {
110 mValue = cInvalidID;
111 }
112
113 /**
114 * @brief Compares two ActorUniqueIDs for equality.
115 */
116 friend bool operator==(const ActorUniqueID& lhs, const ActorUniqueID& rhs)
117 {
118 return lhs.mValue == rhs.mValue;
119 }
120
121 /**
122 * @brief Compares two ActorUniqueIDs for inequality.
123 */
124 friend bool operator!=(const ActorUniqueID& lhs, const ActorUniqueID& rhs)
125 {
126 return lhs.mValue != rhs.mValue;
127 }
128
129private:
130 /// The raw 32-bit packed value containing both the array index and create index.
132};
133static_assert(sizeof(ActorUniqueID) == 4);
Safe identifier handle used as an alternative to raw actor pointers.
Definition ActorUniqueID.h:13
static const u32 cInvalidID
Represents an invalid or uninitialized ID.
Definition ActorUniqueID.h:16
ActorUniqueID()
Constructs an invalid ActorUniqueID.
Definition ActorUniqueID.h:26
static const u32 cCreateIndexMax
The maximum allowed global create index (22 bits; 4,194,303)
Definition ActorUniqueID.h:20
u32 getValue() const
The raw 32-bit packed value of this ID.
Definition ActorUniqueID.h:74
bool isValid() const
Whether this ID has been assigned a valid value (not equal to cInvalidID).
Definition ActorUniqueID.h:100
u32 getCreateIndex() const
Extracts the global generation counter assigned to this actor at creation.
Definition ActorUniqueID.h:92
u32 getArrayIndex() const
Extracts the physical array index where this actor is stored.
Definition ActorUniqueID.h:83
ActorUniqueID(u32 i_array_index, u32 i_create_index)
Constructs an ActorUniqueID by packing a physical index and a global generation index.
Definition ActorUniqueID.h:45
friend bool operator!=(const ActorUniqueID &lhs, const ActorUniqueID &rhs)
Compares two ActorUniqueIDs for inequality.
Definition ActorUniqueID.h:124
friend bool operator==(const ActorUniqueID &lhs, const ActorUniqueID &rhs)
Compares two ActorUniqueIDs for equality.
Definition ActorUniqueID.h:116
void invalidate()
Clears the ID, setting it back to cInvalidID.
Definition ActorUniqueID.h:108
void setValue(u32 id)
Overwrites the ID with a raw 32-bit packed value containing both the array index and create index.
Definition ActorUniqueID.h:54
void setValue(u32 i_array_index, u32 i_create_index)
Overwrites the ID by packing a physical index and a global generation index.
Definition ActorUniqueID.h:64
ActorUniqueID(u32 id)
Constructs an ActorUniqueID from a raw 32-bit packed value containing both the array index and create...
Definition ActorUniqueID.h:35
static const u32 cArrayIndexMax
The maximum allowed array index (10 bits; 1023).
Definition ActorUniqueID.h:18
u32 mValue
The raw 32-bit packed value containing both the array index and create index.
Definition ActorUniqueID.h:131