Symphony Of Empires
entity.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // entity.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <concepts>
28 #include <limits>
29 #include "eng3d/string.hpp"
30 
31 template<typename T>
32 struct EntityId {
33  using Type = T;
34  constexpr static T invalid_id = 0;
35  T id = invalid_id;
36 
37  constexpr EntityId<T>() noexcept = default;
38  constexpr EntityId<T>(T _id) noexcept : id{ _id } { }
39  constexpr EntityId<T>(size_t _id) noexcept : id{ static_cast<T>(_id) } { }
40  constexpr EntityId<T>(EntityId<T>&&) noexcept = default;
41  constexpr EntityId<T>(const EntityId<T>&) noexcept = default;
42  constexpr EntityId<T>& operator=(const EntityId<T>&) noexcept = default;
43  constexpr ~EntityId() noexcept = default;
44 
45  constexpr operator size_t() const noexcept {
46  return static_cast<size_t>(id);
47  }
48 
49  constexpr auto operator==(const EntityId<T>& o) const noexcept {
50  return id == o.id;
51  }
52 
53  constexpr auto operator<=>(const EntityId<T>& o) const noexcept = default;
54 
55  EntityId<T>& operator++() noexcept {
56  this->id++;
57  return *this;
58  }
59 
60  EntityId<T>& operator--() noexcept {
61  this->id--;
62  return *this;
63  }
64 
65  EntityId<T> operator++(int) noexcept {
66  this->id++;
67  return *this;
68  }
69 
70  EntityId<T> operator--(int) noexcept {
71  this->id--;
72  return *this;
73  }
74 };
75 
76 template<typename T>
77 struct std::hash<EntityId<T>> {
78  std::size_t operator()(const EntityId<T>& id) const noexcept {
79  return std::hash(id.id);
80  }
81 };
82 
83 template<typename T>
84 struct std::equal_to<EntityId<T>> {
85  constexpr bool operator()(const EntityId<T>& a, const EntityId<T>& b) const {
86  return a == b;
87  }
88 };
89 
93 template<typename T>
94 struct Entity {
95  using Id = T;
96 
97  constexpr Entity() noexcept = default;
98  constexpr Entity(Entity&&) noexcept = default;
99  constexpr Entity(const Entity&) noexcept = default;
100  constexpr Entity& operator=(const Entity&) noexcept = default;
101  constexpr ~Entity() noexcept = default;
102 
107 
110  constexpr static Id invalid() {
111  return Id{ static_cast<T>(-1) };
112  }
113 
118  constexpr static bool is_invalid(Id id) {
119  return id == invalid();
120  }
121 
126  constexpr static bool is_valid(Id id) {
127  return id != invalid();
128  }
129 
133  constexpr bool is_invalid() const {
134  return is_invalid(cached_id);
135  }
136 
140  constexpr bool is_valid() const {
141  return !is_invalid();
142  }
143 
144  constexpr operator Id() const noexcept {
145  return cached_id;
146  }
147 
148  constexpr operator size_t() const noexcept {
149  return static_cast<size_t>(cached_id);
150  }
151 
152  constexpr Id get_id() const {
153  return cached_id;
154  }
155 };
156 
159 template<typename T>
160 struct RefnameEntity : Entity<T> {
162 };
A reference to a string on the global string pool.
Definition: string.hpp:39
An entity which can only be referenced by an (presumably) unique Id this is the base class for the ot...
Definition: entity.hpp:94
constexpr bool is_invalid() const
Checks if the current id is invalid.
Definition: entity.hpp:133
constexpr bool is_valid() const
Checks if the current id is valid.
Definition: entity.hpp:140
constexpr static bool is_valid(Id id)
Checks if the id is valid.
Definition: entity.hpp:126
T Id
Definition: entity.hpp:95
constexpr Id get_id() const
Definition: entity.hpp:152
constexpr static bool is_invalid(Id id)
Checks if the id is not valid.
Definition: entity.hpp:118
Id cached_id
Id used to speed up Id lookups on any context.
Definition: entity.hpp:106
constexpr Entity() noexcept=default
constexpr static Id invalid()
Returns an invalid id.
Definition: entity.hpp:110
constexpr auto operator<=>(const EntityId< T > &o) const noexcept=default
T Type
Definition: entity.hpp:33
EntityId< T > operator++(int) noexcept
Definition: entity.hpp:65
constexpr static T invalid_id
Definition: entity.hpp:34
EntityId< T > & operator++() noexcept
Definition: entity.hpp:55
constexpr auto operator==(const EntityId< T > &o) const noexcept
Definition: entity.hpp:49
EntityId< T > operator--(int) noexcept
Definition: entity.hpp:70
EntityId< T > & operator--() noexcept
Definition: entity.hpp:60
An entity which can be referenced via a ref_name and also via id.
Definition: entity.hpp:160
Eng3D::StringRef ref_name
Definition: entity.hpp:161
constexpr bool operator()(const EntityId< T > &a, const EntityId< T > &b) const
Definition: entity.hpp:85
std::size_t operator()(const EntityId< T > &id) const noexcept
Definition: entity.hpp:78