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() noexcept = default;
45 JobHandle(JobId id, JobGenId gen, JobGenId prio) {
46 data.id = id;
47 data.gen = gen;
48 data.prio = prio;
49 }
50 explicit JobHandle(uint32_t value) {
51 val = value;
52 }
53 ~JobHandle() = default;
54
55 JobHandle(JobHandle&&) noexcept = default;
56 JobHandle(const JobHandle&) = default;
57 JobHandle& operator=(JobHandle&&) noexcept = default;
58 JobHandle& operator=(const JobHandle&) = default;
59
60 GAIA_NODISCARD constexpr bool operator==(const JobHandle& other) const noexcept {
61 return val == other.val;
62 }
63 GAIA_NODISCARD constexpr bool operator!=(const JobHandle& other) const noexcept {
64 return val != other.val;
65 }
66
67 GAIA_NODISCARD auto id() const {
68 return data.id;
69 }
70 GAIA_NODISCARD auto gen() const {
71 return data.gen;
72 }
73 GAIA_NODISCARD auto prio() const {
74 return data.prio;
75 }
76 GAIA_NODISCARD auto value() const {
77 return val;
78 }
79 };
80
81 struct JobNull_t {
82 GAIA_NODISCARD operator JobHandle() const noexcept {
83 return JobHandle(JobHandle::IdMask, JobHandle::GenMask, JobHandle::PrioMask);
84 }
85
86 GAIA_NODISCARD constexpr bool operator==([[maybe_unused]] const JobNull_t& null) const noexcept {
87 return true;
88 }
89 GAIA_NODISCARD constexpr bool operator!=([[maybe_unused]] const JobNull_t& null) const noexcept {
90 return false;
91 }
92 };
93
94 GAIA_NODISCARD inline bool operator==(const JobNull_t& null, const JobHandle& entity) noexcept {
95 return static_cast<JobHandle>(null).id() == entity.id();
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 JobHandle& entity, const JobNull_t& null) noexcept {
103 return null == entity;
104 }
105
106 GAIA_NODISCARD inline bool operator!=(const JobHandle& entity, const JobNull_t& null) noexcept {
107 return null != entity;
108 }
109
110 inline constexpr JobNull_t JobNull{};
111 } // namespace mt
112} // namespace gaia
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition jobhandle.h:13
Definition jobhandle.h:81