Gaia-ECS v1.0.0
A simple and powerful entity component system
Loading...
Searching...
No Matches
jobhandle.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstdint>
5#include <type_traits>
6
7namespace gaia {
8 namespace mt {
10 using JobInternalType = uint32_t;
12 using JobId = JobInternalType;
14 using JobGenId = JobInternalType;
15
17 struct GAIA_API JobHandle final {
19 static constexpr JobInternalType IdBits = 20;
21 static constexpr JobInternalType GenBits = 11;
23 static constexpr JobInternalType PrioBits = 1;
25 static constexpr JobInternalType AllBits = IdBits + GenBits + PrioBits;
27 static constexpr JobInternalType IdMask = (uint32_t)(uint64_t(1) << IdBits) - 1;
29 static constexpr JobInternalType GenMask = (uint32_t)(uint64_t(1) << GenBits) - 1;
31 static constexpr JobInternalType PrioMask = (uint32_t)(uint64_t(1) << PrioBits) - 1;
32
34 using JobSizeType = std::conditional_t<(AllBits > 32), uint64_t, uint32_t>;
35
36 static_assert(AllBits <= 64, "Job IdBits and GenBits must fit inside 64 bits");
37 static_assert(IdBits <= 31, "Job IdBits must be at most 31 bits long");
38 static_assert(GenBits > 10, "Job GenBits must be at least 10 bits long");
39
40 private:
41 struct JobData {
43 JobInternalType id : IdBits;
45 JobInternalType gen : GenBits;
47 JobInternalType prio : PrioBits;
48 };
49
50 union {
51 JobData data;
52 JobSizeType val;
53 };
54
55 public:
58 data.id = JobHandle::IdMask;
59 data.gen = JobHandle::GenMask;
60 data.prio = JobHandle::PrioMask;
61 }
66 JobHandle(JobId id, JobGenId gen, JobGenId prio) {
67 data.id = id;
68 data.gen = gen;
69 data.prio = prio;
70 }
73 explicit JobHandle(uint32_t value) {
74 val = value;
75 }
76 ~JobHandle() = default;
77
78 JobHandle(JobHandle&&) noexcept = default;
79 JobHandle(const JobHandle&) = default;
80 JobHandle& operator=(JobHandle&&) noexcept = default;
81 JobHandle& operator=(const JobHandle&) = default;
82
86 GAIA_NODISCARD constexpr bool operator==(const JobHandle& other) const noexcept {
87 return val == other.val;
88 }
92 GAIA_NODISCARD constexpr bool operator!=(const JobHandle& other) const noexcept {
93 return val != other.val;
94 }
95
98 GAIA_NODISCARD auto id() const {
99 return data.id;
100 }
103 GAIA_NODISCARD auto gen() const {
104 return data.gen;
105 }
108 GAIA_NODISCARD auto prio() const {
109 return data.prio;
110 }
113 GAIA_NODISCARD auto value() const {
114 return val;
115 }
116 };
117
119 struct JobNull_t {
122 GAIA_NODISCARD operator JobHandle() const noexcept {
124 }
125
126 GAIA_NODISCARD constexpr bool operator==([[maybe_unused]] const JobNull_t& null) const noexcept {
127 return true;
128 }
129 GAIA_NODISCARD constexpr bool operator!=([[maybe_unused]] const JobNull_t& null) const noexcept {
130 return false;
131 }
132 };
133
134 GAIA_NODISCARD inline bool operator==(const JobNull_t& null, const JobHandle& entity) noexcept {
135 return static_cast<JobHandle>(null).id() == entity.id();
136 }
137
138 GAIA_NODISCARD inline bool operator!=(const JobNull_t& null, const JobHandle& entity) noexcept {
139 return static_cast<JobHandle>(null).id() != entity.id();
140 }
141
142 GAIA_NODISCARD inline bool operator==(const JobHandle& entity, const JobNull_t& null) noexcept {
143 return null == entity;
144 }
145
146 GAIA_NODISCARD inline bool operator!=(const JobHandle& entity, const JobNull_t& null) noexcept {
147 return null != entity;
148 }
149
151 inline constexpr JobNull_t JobNull{};
152 } // namespace mt
153} // namespace gaia
Packed identifier for a job-pool slot, generation, and priority.
Definition jobhandle.h:17
JobHandle(uint32_t value)
Creates a handle from its packed representation.
Definition jobhandle.h:73
GAIA_NODISCARD auto id() const
Returns the job-pool slot identifier.
Definition jobhandle.h:98
GAIA_NODISCARD auto prio() const
Returns the encoded priority bit.
Definition jobhandle.h:108
static constexpr JobInternalType PrioMask
Mask selecting the unshifted priority.
Definition jobhandle.h:31
GAIA_NODISCARD auto gen() const
Returns the slot generation.
Definition jobhandle.h:103
static constexpr JobInternalType IdMask
Mask selecting the slot identifier.
Definition jobhandle.h:27
JobHandle(JobId id, JobGenId gen, JobGenId prio)
Creates a handle from its component fields.
Definition jobhandle.h:66
static constexpr JobInternalType GenMask
Mask selecting the unshifted generation.
Definition jobhandle.h:29
JobHandle()
Creates the null job handle.
Definition jobhandle.h:57
GAIA_NODISCARD constexpr bool operator!=(const JobHandle &other) const noexcept
Compares packed handle values.
Definition jobhandle.h:92
GAIA_NODISCARD auto value() const
Returns the packed handle representation.
Definition jobhandle.h:113
std::conditional_t<(AllBits > 32), uint64_t, uint32_t > JobSizeType
Integer type large enough to hold all packed fields.
Definition jobhandle.h:34
Sentinel type representing an invalid job handle.
Definition jobhandle.h:119