-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypedStack.mpp
More file actions
272 lines (236 loc) · 11 KB
/
TypedStack.mpp
File metadata and controls
272 lines (236 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
export module CppUtils.Container.TypedStack;
import std;
import CppUtils.Logger;
import CppUtils.String.Utility;
import CppUtils.Type;
export namespace CppUtils::Container
{
template<Type::TriviallyCopyable... SupportedTypes>
class TypedStack final
{
public:
using Logger = Logger<"CppUtils">;
using Types = std::tuple<SupportedTypes...>;
static inline constexpr auto typesSize = std::array<std::size_t, sizeof...(SupportedTypes)>{sizeof(SupportedTypes)...};
template<Type::TriviallyCopyable... Args>
requires (Type::IsOneOf<Args, SupportedTypes...> and ...)
inline constexpr explicit TypedStack(Args... values)
{
(push(std::forward<Args>(values)), ...);
}
[[nodiscard]] inline constexpr auto empty() const noexcept -> bool
{
return std::empty(m_types);
}
[[nodiscard]] inline constexpr auto size() const noexcept -> std::size_t
{
return std::size(m_types);
}
[[nodiscard]] inline constexpr auto getByteSize() const noexcept -> std::size_t
{
return std::size(m_data);
}
[[nodiscard]] inline constexpr auto getType(std::size_t position) const -> std::size_t
{
if (position >= size()) [[unlikely]]
throw std::out_of_range{std::format("TypedStack::getType({}) : Out of range", position)};
return m_types[position];
}
template<Type::TriviallyCopyable T>
requires Type::IsOneOf<T, SupportedTypes...>
[[nodiscard]] inline constexpr auto get(std::size_t position) const -> T
{
if (position >= size()) [[unlikely]]
throw std::out_of_range{std::format("TypedStack::get({}) : Out of range", position)};
if (auto storedType = m_types[position]; Type::getPosition<T, SupportedTypes...>() != storedType) [[unlikely]]
throw std::logic_error{std::format("TypedStack::get({}) : The type at the specified position does not match the stored type (Type: {})", position, storedType)};
auto offset = getTypeOffset(position);
auto buffer = std::array<std::byte, sizeof(T)>{};
for (auto i = 0uz; i < sizeof(T); ++i)
buffer[i] = m_data[offset + i];
return std::bit_cast<T>(buffer);
}
template<Type::TriviallyCopyable T>
requires Type::IsOneOf<T, SupportedTypes...>
[[nodiscard]] inline constexpr auto top() const -> decltype(auto)
{
if (empty()) [[unlikely]]
throw std::out_of_range{std::format("TypedStack::top() : Stack underflow")};
return get<T>(size() - 1);
}
inline constexpr auto top(auto&& visitor) const -> void
{
if (empty()) [[unlikely]]
throw std::out_of_range{std::format("TypedStack::top() : Stack underflow")};
visit(size() - 1, std::forward<decltype(visitor)>(visitor));
}
template<Type::TriviallyCopyable T>
requires Type::IsOneOf<T, SupportedTypes...>
inline constexpr auto set(std::size_t position, T newValue) -> void
{
if (position >= size()) [[unlikely]]
throw std::out_of_range{std::format("TypedStack::set({}, T newValue) : Out of range", position)};
if (auto storedType = m_types[position]; Type::getPosition<T, SupportedTypes...>() != storedType) [[unlikely]]
throw std::logic_error{std::format("TypedStack::set(std::size_t position, T newValue) : The type at the specified position does not match the stored type (Type: {})", storedType)};
auto offset = getTypeOffset(position);
auto buffer = std::bit_cast<std::array<std::byte, sizeof(T)>>(newValue);
for (auto i = 0uz; i < sizeof(T); ++i)
m_data[offset + i] = buffer[i];
}
template<Type::TriviallyCopyable T>
requires Type::IsOneOf<T, SupportedTypes...>
inline constexpr auto set(T newValue) -> void
{
set(size() - 1, std::move(newValue));
}
template<Type::TriviallyCopyable T>
requires Type::IsOneOf<T, SupportedTypes...>
inline constexpr auto push(T value = T{}) -> void
{
m_types.push_back(Type::getPosition<T, SupportedTypes...>());
m_data.resize(getByteSize() + sizeof(T), std::byte{0});
set(size() - 1, std::move(value));
}
inline constexpr auto pushType(std::size_t type) -> void
{
if (type >= sizeof...(SupportedTypes))
throw std::out_of_range{std::format("TypedStack::pushType({}) : Out of range", type)};
static constexpr auto pushbyType = std::array{
+[](TypedStack<SupportedTypes...>& stack) static -> void {
stack.template push<SupportedTypes>();
}...};
pushbyType[type](*this);
}
inline constexpr auto drop() -> void
{
static constexpr auto dropFunctions = std::array{
+[](const TypedStack<SupportedTypes...>& stack, std::vector<std::byte>& data) static -> void {
data.resize(stack.getByteSize() - sizeof(SupportedTypes));
}...};
if (empty()) [[unlikely]]
throw std::underflow_error{"TypedStack::drop() : Container already empty"};
dropFunctions[m_types[size() - 1]](*this, m_data);
m_types.pop_back();
}
template<Type::TriviallyCopyable T>
requires Type::IsOneOf<T, SupportedTypes...>
[[nodiscard]] inline constexpr auto pop() -> decltype(auto)
{
if (empty()) [[unlikely]]
throw std::underflow_error{"TypedStack::pop() : Container already empty"};
if (auto specifiedType = Type::getPosition<T, SupportedTypes...>(), storedType = m_types.back();
specifiedType != storedType) [[unlikely]]
throw std::logic_error{std::format("TypedStack::pop() : The type at the specified position ({}) does not match the stored type ({})", specifiedType, storedType)};
auto value = get<T>(size() - 1);
drop();
return value;
}
inline constexpr auto pop(auto&& visitor) -> void
{
top([this, visitor = std::forward<decltype(visitor)>(visitor)](auto&& value) mutable -> void {
drop();
visitor(std::forward<decltype(value)>(value));
});
}
private:
template<class SourceType, class SupportedType>
static inline constexpr auto copyToDestinationImpl() -> decltype(auto)
{
return [](TypedStack<SupportedTypes...>& stack, std::size_t sourcePosition, std::size_t destinationPosition) static -> void {
if constexpr (not std::is_convertible_v<SourceType, SupportedType>)
throw std::logic_error{std::format("TypedStack::copy : The source type ({}) is not convertible to the destination type ({})", sourcePosition, destinationPosition)};
else
stack.set(destinationPosition, static_cast<SupportedType>(stack.template get<SourceType>(sourcePosition)));
};
}
public:
inline constexpr auto copy(std::size_t sourcePosition, std::size_t destinationPosition) -> void
{
static constexpr auto copyToDestination = []<class SourceType>() consteval -> auto {
return std::array{+copyToDestinationImpl<SourceType, SupportedTypes>()...};
};
static constexpr auto copyFromSource = std::array<std::array<void (*)(TypedStack<SupportedTypes...>&, std::size_t, std::size_t), sizeof...(SupportedTypes)>, sizeof...(SupportedTypes)>{
copyToDestination.template operator()<SupportedTypes>()...};
if (sourcePosition >= size())
throw std::out_of_range{std::format("TypedStack::copy({}, {}) : Source out of range", sourcePosition, destinationPosition)};
if (destinationPosition >= size())
throw std::out_of_range{std::format("TypedStack::copy({}, {}) : Destination out of range", sourcePosition, destinationPosition)};
copyFromSource[m_types[sourcePosition]][m_types[destinationPosition]](*this, sourcePosition, destinationPosition);
}
inline constexpr auto visit(std::size_t position, auto&& visitor) const -> void
{
static constexpr auto visitors = std::array{
+[](const TypedStack<SupportedTypes...>& stack, std::size_t position, decltype(visitor)&& visitor) static -> void { // Todo: Retirer le && quand ça compilera avec MSVC (bug MSVC)
visitor(stack.template get<SupportedTypes>(position));
}...};
if (position >= size())
throw std::out_of_range{std::format("TypedStack::visit({}, auto&& visitor) : Out of range", position)};
visitors[m_types[position]](*this, position, std::forward<decltype(visitor)>(visitor));
}
inline constexpr auto print(std::size_t position) const -> void
{
visit(position, [position, type = getType(position)](auto&& value) -> void {
Logger::print<"debug">("Position: {}; Type: {}; Size: {} bytes; Value: {}", position, type, sizeof(decltype(value)), String::formatValue(value));
});
};
inline constexpr auto print() const -> void
{
Logger::emit<"separator">();
Logger::print<"debug">("Stack size: {} elements; {} bytes", size(), getByteSize());
for (auto i = size(); i > 0;)
print(--i);
};
template<class ReturnType, class... Args>
inline constexpr auto call(ReturnType (*function)(Args...)) -> void
{
auto call = [this, function]<std::size_t... I> /* Not supported on MSVC [[nodiscard]] */ (/* Not supported on MSVC [[maybe_unused]] */ std::index_sequence<I...> indexSequence) -> decltype(auto) {
static_cast<void>(indexSequence);
return std::invoke(function, get<std::remove_cvref_t<Type::NthType<I, Args...>>>(size() - sizeof...(Args) + I)...);
};
if constexpr (std::is_void_v<ReturnType>)
call(std::index_sequence_for<Args...>{});
else
set(size() - 1 - sizeof...(Args), call(std::index_sequence_for<Args...>{}));
}
template<class ReturnType, class Object, class... Args>
inline constexpr auto call(ReturnType (Object::*function)(Args...)) -> void
{
auto* objectPointer = get<Object*>(size() - 1 - sizeof...(Args));
auto call = [this, function, objectPointer]<std::size_t... I> /* Not supported on MSVC [[nodiscard]] */ (/* Not supported on MSVC [[maybe_unused]] */ std::index_sequence<I...> indexSequence) -> decltype(auto) {
static_cast<void>(indexSequence);
return std::invoke(function, objectPointer, get<std::remove_cvref_t<Type::NthType<I, Args...>>>(size() - sizeof...(Args) + I)...);
};
if constexpr (std::is_void_v<ReturnType>)
call(std::index_sequence_for<Args...>{});
else
set(size() - 2 - sizeof...(Args), call(std::index_sequence_for<Args...>{}));
}
template<class ReturnType, class Object, class... Args>
inline constexpr auto call(ReturnType (Object::*function)(Args...) const) -> void
{
visit(size() - 1 - sizeof...(Args), [this, function](auto&& objectPointer) -> void {
using ObjectPointer = std::remove_cvref_t<decltype(objectPointer)>;
auto call = [this, function, objectPointer]<std::size_t... I> /* Not supported on MSVC [[nodiscard]] */ (/* Not supported on MSVC [[maybe_unused]] */ std::index_sequence<I...> indexSequence) -> decltype(auto) {
static_cast<void>(indexSequence);
return std::invoke(function, objectPointer, get<std::remove_cvref_t<Type::NthType<I, Args...>>>(size() - sizeof...(Args) + I)...);
};
if constexpr (not std::same_as<ObjectPointer, const Object*> and not std::same_as<ObjectPointer, Object*>)
throw std::logic_error{"The type is not an object pointer"};
else if constexpr (std::is_void_v<ReturnType>)
call(std::index_sequence_for<Args...>{});
else
set(size() - 2 - sizeof...(Args), call(std::index_sequence_for<Args...>{}));
});
}
private:
[[nodiscard]] inline constexpr auto getTypeOffset(std::size_t position) const -> std::size_t
{
auto offset = 0uz;
for (auto i = 0uz; i < position; ++i)
offset += typesSize[m_types[i]];
return offset;
}
std::vector<std::byte> m_data = {};
std::vector<std::size_t> m_types = {};
};
}