Gaia-ECS v0.9.3
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 {
9 using JobInternalType = uint32_t;
10 using JobId = JobInternalType;
11 using JobGenId = JobInternalType;
12
13 struct GAIA_API JobHandle final {
14 static constexpr JobInternalType IdBits = 20;
15 static constexpr JobInternalType GenBits = 11;
16 static constexpr JobInternalType PrioBits = 1;
17 static constexpr JobInternalType AllBits = IdBits + GenBits + PrioBits;
18 static constexpr JobInternalType IdMask = (uint32_t)(uint64_t(1) << IdBits) - 1;
19 static constexpr JobInternalType GenMask = (uint32_t)(uint64_t(1) << GenBits) - 1;
20 static constexpr JobInternalType PrioMask = (uint32_t)(uint64_t(1) << PrioBits) - 1;
21
22 using JobSizeType = std::conditional_t<(AllBits > 32), uint64_t, uint32_t>;
23
24 static_assert(AllBits <= 64, "Job IdBits and GenBits must fit inside 64 bits");
25 static_assert(IdBits <= 31, "Job IdBits must be at most 31 bits long");
26 static_assert(GenBits > 10, "Job GenBits must be at least 10 bits long");
27
28 private:
29 struct JobData {
31 JobInternalType id : IdBits;
33 JobInternalType gen : GenBits;
35 JobInternalType prio : PrioBits;
36 };
37
38 union {
39 JobData data;
40 JobSizeType val;
41 };
42
43 public:
44 JobHandle() {
45 data.id = JobHandle::IdMask;
46 data.gen = JobHandle::GenMask;
47 data.prio = JobHandle::PrioMask;
48 }
49 JobHandle(JobId id, JobGenId gen, JobGenId prio) {
50 data.id = id;
51 data.gen = gen;
52 data.prio = prio;
53 }
54 explicit JobHandle(uint32_t value) {
55 val = value;
56 }
57 ~JobHandle() = default;
58
59 JobHandle(JobHandle&&) noexcept = default;
60 JobHandle(const JobHandle&) = default;
61 JobHandle& operator=(JobHandle&&) noexcept = default;
62 JobHandle& operator=(const JobHandle&) = default;
63
64 GAIA_NODISCARD constexpr bool operator==(const JobHandle& other) const noexcept {
65 return val == other.val;
66 }
67 GAIA_NODISCARD constexpr bool operator!=(const JobHandle& other) const noexcept {
68 return val != other.val;
69 }
70
71 GAIA_NODISCARD auto id() const {
72 return data.id;
73 }
74 GAIA_NODISCARD auto gen() const {
75 return data.gen;
76 }
77 GAIA_NODISCARD auto prio() const {
78 return data.prio;
79 }
80 GAIA_NODISCARD auto value() const {
81 return val;
82 }
83 };
84
85 struct JobNull_t {
86 GAIA_NODISCARD operator JobHandle() const noexcept {
87 return JobHandle(JobHandle::IdMask, JobHandle::GenMask, JobHandle::PrioMask);
88 }
89
90 GAIA_NODISCARD constexpr bool operator==([[maybe_unused]] const JobNull_t& null) const noexcept {
91 return true;
92 }
93 GAIA_NODISCARD constexpr bool operator!=([[maybe_unused]] const JobNull_t& null) const noexcept {
94 return false;
95 }
96 };
97
98 GAIA_NODISCARD inline bool operator==(const JobNull_t& null, const JobHandle& entity) noexcept {
99 return static_cast<JobHandle>(null).id() == entity.id();
100 }
101
102 GAIA_NODISCARD inline bool operator!=(const JobNull_t& null, const JobHandle& entity) noexcept {
103 return static_cast<JobHandle>(null).id() != entity.id();
104 }
105
106 GAIA_NODISCARD inline bool operator==(const JobHandle& entity, const JobNull_t& null) noexcept {
107 return null == entity;
108 }
109
110 GAIA_NODISCARD inline bool operator!=(const JobHandle& entity, const JobNull_t& null) noexcept {
111 return null != entity;
112 }
113
114 inline constexpr JobNull_t JobNull{};
115 } // namespace mt
116} // namespace gaia
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition jobhandle.h:13
Definition jobhandle.h:85