Loading...
Searching...
No Matches
Identifier.hpp
1#pragma once
2#include <score/tools/std/Optional.hpp>
3
4#include <QPointer>
5#if __cplusplus > 202302L
6#include <QObject>
7#endif
8#include <type_traits>
9
10namespace score
11{
12template <typename T, bool Order>
13class EntityMap;
14}
15template <typename Element, typename Model, bool Order>
16class IdContainer;
17template <typename T>
57template <typename tag, typename impl>
59{
60 friend tag;
61 friend class score::EntityMap<tag, false>;
62 friend class score::EntityMap<tag, true>;
63 friend class IdentifiedObject<tag>;
64
65 // TODO Try to only have Map as a template type here
66 template <typename Element, typename Model, bool Order>
67 friend class IdContainer;
68
69public:
70 using value_type = impl;
71 explicit id_base_t() noexcept { }
72
73 id_base_t(const id_base_t& other) noexcept
74 : m_id{other.m_id}
75 {
76 }
77
78 id_base_t(id_base_t&& other) noexcept
79 : m_id{std::move(other.m_id)}
80 {
81 }
82 template <typename other>
83 requires(std::is_base_of_v<tag, other>)
84 id_base_t(const id_base_t<other, impl>& oid) noexcept
85 : m_id{oid.val()}
86 {
87 }
88
89 template <typename other>
90 requires(std::is_base_of_v<tag, other>)
91 id_base_t(id_base_t&& oid) noexcept
92 : m_id{oid.val()}
93 {
94 }
95
96 id_base_t& operator=(const id_base_t& other) noexcept
97 {
98 m_id = other.m_id;
99 m_ptr.clear();
100 return *this;
101 }
102
103 id_base_t& operator=(id_base_t&& other) noexcept
104 {
105 m_id = other.m_id;
106 m_ptr.clear();
107 return *this;
108 }
109
110 // TODO check if when an id is returned by value,
111 // the pointer gets copied correctly
112 explicit id_base_t(value_type val) noexcept
113 : m_id{std::move(val)}
114 {
115 }
116
117 explicit id_base_t(tag& element) noexcept
118 : m_ptr{&element}
119 , m_id{element.id()}
120 {
121 }
122
123 id_base_t& operator=(tag& element) noexcept
124 {
125 m_ptr = &element;
126 m_id = element.id();
127
128 return *this;
129 }
130
131 friend bool operator==(const id_base_t& lhs, const id_base_t& rhs) noexcept
132 {
133 return lhs.m_id == rhs.m_id;
134 }
135
136 friend bool operator!=(const id_base_t& lhs, const id_base_t& rhs) noexcept
137 {
138 return lhs.m_id != rhs.m_id;
139 }
140
141 friend bool operator<(const id_base_t& lhs, const id_base_t& rhs) noexcept
142 {
143 return lhs.val() < rhs.val();
144 }
145
146 explicit operator value_type() const noexcept { return m_id; }
147
148 const value_type& val() const noexcept { return m_id; }
149
150 void setVal(value_type val) noexcept { m_id = val; }
151
152private:
153 mutable QPointer<QObject> m_ptr;
154 value_type m_id{};
155};
156
160template <typename tag>
162
166template <typename tag>
167using OptionalId = std::optional<Id<tag>>;
168
169namespace std
170{
171template <typename tag>
172struct hash<Id<tag>>
173{
174 std::size_t operator()(const Id<tag>& id) const
175 {
176 return std::hash<int32_t>{}(id.val());
177 }
178};
179}
180template <typename T>
181uint qHash(const Id<T>& id, uint seed)
182{
183 return qHash(*id.val(), seed);
184}
A map to access child objects through their id.
Definition IdentifiedObjectMap.hpp:16
The IdentifiedObject class.
Definition IdentifiedObject.hpp:19
The id_base_t class.
Definition Identifier.hpp:59
The EntityMap class.
Definition EntityMap.hpp:36
Base toolkit upon which the software is built.
Definition Application.cpp:97
STL namespace.