-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.mpp
More file actions
155 lines (138 loc) · 5.22 KB
/
Stack.mpp
File metadata and controls
155 lines (138 loc) · 5.22 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
export module CppUtils.Container.Stack;
import std;
import CppUtils.Type;
import CppUtils.Logger;
export namespace CppUtils::Container
{
class Stack final
{
public:
using Logger = Logger<"CppUtils">;
template<std::size_t I, class... Args>
[[nodiscard]] static inline consteval auto getOffset() noexcept -> std::size_t
{
if constexpr (I + 1 < sizeof...(Args))
return sizeof(Type::NthType<I + 1, Args...>) + getOffset<I + 1, Args...>();
else
return 0;
}
[[nodiscard]] inline constexpr auto size() const noexcept -> std::size_t
{
return std::size(m_data);
}
[[nodiscard]] inline constexpr auto empty() const noexcept -> bool
{
return std::empty(m_data);
}
inline constexpr auto set(Type::TriviallyCopyable auto value, std::size_t offset = 0) -> std::expected<void, std::string_view>
{
using namespace std::literals;
if (offset + sizeof(decltype(value)) > std::size(m_data)) [[unlikely]]
return std::unexpected{"Stack underflow"sv};
auto* destination = std::data(m_data) + std::size(m_data) - offset - sizeof(decltype(value));
std::memcpy(destination, std::addressof(value), sizeof(decltype(value)));
return {};
}
inline constexpr auto push(Type::TriviallyCopyable auto value) -> void
{
m_data.resize(std::size(m_data) + sizeof(decltype(value)), std::byte{0});
auto _ = set(value);
}
template<Type::TriviallyCopyable T>
[[nodiscard]] inline constexpr auto get(std::size_t offset = 0) const -> std::expected<T, std::string_view>
{
using namespace std::literals;
if (offset + sizeof(T) > std::size(m_data)) [[unlikely]]
return std::unexpected{"Stack underflow"sv};
auto value = T{};
std::memcpy(std::addressof(value), std::data(m_data) + std::size(m_data) - offset - sizeof(T), sizeof(T));
return value;
}
inline constexpr auto drop(std::size_t nbBytes) -> std::expected<void, std::string_view>
{
using namespace std::literals;
if (nbBytes > std::size(m_data)) [[unlikely]]
return std::unexpected{"Stack underflow"sv};
m_data.resize(std::size(m_data) - nbBytes);
return {};
}
template<Type::TriviallyCopyable T>
[[nodiscard]] inline constexpr auto pop() -> std::expected<T, std::string_view>
{
auto value = get<T>();
if (auto result = drop(sizeof(T)); not result) [[unlikely]]
return std::unexpected{result.error()};
return value;
}
inline constexpr auto call(auto&& function, std::size_t functionOffset = 0) -> std::expected<void, std::string_view>
{
using FunctionType = std::decay_t<decltype(function)>;
using FunctionInformations = Type::CallableTrait<FunctionType>;
using ReturnType = FunctionInformations::ReturnType;
using ArgumentsTypes = FunctionInformations::ArgumentsTypes;
constexpr auto nbArguments = std::tuple_size_v<ArgumentsTypes>;
constexpr auto argumentsOffset = []<class ArgumentsTypes, std::size_t... I>(std::index_sequence<I...>) consteval {
return (0uz + ... + sizeof(std::tuple_element_t<I, ArgumentsTypes>));
}.template operator()<ArgumentsTypes>(std::make_index_sequence<nbArguments>{});
auto arguments = [&]<std::size_t... I>(std::index_sequence<I...>) -> std::expected<ArgumentsTypes, std::string_view> {
auto offset = argumentsOffset + functionOffset;
auto success = std::expected<void, std::string_view>{};
auto arguments = ArgumentsTypes{[&]() {
using ArgumentType = std::tuple_element_t<I, ArgumentsTypes>;
offset -= sizeof(ArgumentType);
auto value = get<ArgumentType>(offset);
if (not value) [[unlikely]]
{
success = std::unexpected{value.error()};
return ArgumentType{};
}
return *value;
}()...};
if (not success)
return std::unexpected{success.error()};
return arguments;
}(std::make_index_sequence<nbArguments>{});
if (not arguments)
return std::unexpected{arguments.error()};
if constexpr (std::is_void_v<ReturnType>)
std::apply(std::forward<decltype(function)>(function), std::move(*arguments));
else
return set(std::apply(std::forward<decltype(function)>(function), std::move(*arguments)), argumentsOffset + functionOffset);
return {};
}
inline auto dump() const noexcept -> void
{
Logger::print<"debug">("Memory dump:");
auto line = std::string{};
for (auto i = 0uz; i < std::size(m_data);)
{
line += std::format("{:#04x} ", static_cast<unsigned char>(m_data[i]));
if (++i % sizeof(std::size_t) == 0)
{
auto int1 = 0;
auto int2 = 0;
auto sizeT = 0uz;
std::memcpy(std::addressof(int1), std::data(m_data) + i - sizeof(int) * 2, sizeof(int));
std::memcpy(std::addressof(int2), std::data(m_data) + i - sizeof(int), sizeof(int));
std::memcpy(std::addressof(sizeT), std::data(m_data) + i - sizeof(std::size_t), sizeof(std::size_t));
Logger::print<"debug">("{} ({} | {} -> {})", line, int1, int2, sizeT);
line.clear();
}
}
if (not std::empty(line))
{
if (std::size(m_data) % sizeof(int) == 0)
{
auto value = 0;
std::memcpy(std::addressof(value), std::data(m_data) + std::size(m_data) - sizeof(int), sizeof(int));
Logger::print<"debug">("{:<40} ({})", line, value);
}
else
Logger::print<"debug">("{}", line);
}
Logger::print<>("");
}
private:
std::vector<std::byte> m_data;
};
}