Skip to content

Commit 4d84c12

Browse files
committed
Container/Size & Container/Vec2 & Container/Vec3
1 parent bc76ed0 commit 4d84c12

12 files changed

Lines changed: 292 additions & 115 deletions

File tree

modules/Container/Container.mpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export import CppUtils.Container.NetworkPtr;
88
export import CppUtils.Container.Pair;
99
export import CppUtils.Container.SafeShared;
1010
export import CppUtils.Container.Sequence;
11-
export import CppUtils.Container.Size2d;
12-
export import CppUtils.Container.Size3d;
11+
export import CppUtils.Container.Size;
1312
export import CppUtils.Container.Stack;
1413
export import CppUtils.Container.Tree;
1514
export import CppUtils.Container.TypedStack;
15+
export import CppUtils.Container.Vec2;
16+
export import CppUtils.Container.Vec3;
1617
export import CppUtils.Container.Vector;

modules/Container/Size.mpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export module CppUtils.Container.Size;
2+
3+
import std;
4+
import CppUtils.Container.Vec2;
5+
import CppUtils.Container.Vec3;
6+
7+
export namespace CppUtils::Container
8+
{
9+
using Size2 = Vec2<std::size_t>;
10+
using Size3 = Vec3<std::size_t>;
11+
}

modules/Container/Size2d.mpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

modules/Container/Size3d.mpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

modules/Container/Vec2.mpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
module;
2+
3+
#include <cassert>
4+
5+
export module CppUtils.Container.Vec2;
6+
7+
import std;
8+
9+
export namespace CppUtils::Container
10+
{
11+
template<class T>
12+
requires (std::integral<T> or std::floating_point<T>)
13+
struct Vec2 final
14+
{
15+
inline constexpr Vec2() = default;
16+
17+
template<std::convertible_to<T>... Args>
18+
requires (sizeof...(Args) == 2)
19+
inline constexpr Vec2(Args&&... args):
20+
m_values{static_cast<T>(args)...}
21+
{}
22+
23+
[[nodiscard]] inline constexpr auto operator[](this auto&& self, std::size_t axis) noexcept -> decltype(auto)
24+
{
25+
assert(axis < 2);
26+
return self.m_values[axis];
27+
}
28+
29+
[[nodiscard]] inline constexpr auto x(this auto&& self) noexcept -> decltype(auto) { return self[0]; }
30+
[[nodiscard]] inline constexpr auto y(this auto&& self) noexcept -> decltype(auto) { return self[1]; }
31+
[[nodiscard]] inline constexpr auto width(this auto&& self) noexcept -> decltype(auto) { return self[0]; }
32+
[[nodiscard]] inline constexpr auto height(this auto&& self) noexcept -> decltype(auto) { return self[1]; }
33+
34+
inline constexpr auto apply(this auto&& self, auto&& function) noexcept -> decltype(auto)
35+
{
36+
for (const auto axis : {0uz, 1uz})
37+
function(axis);
38+
return self;
39+
}
40+
41+
inline constexpr auto operator+=(const Vec2<T>& rhs) noexcept -> decltype(auto)
42+
{
43+
return apply([this, rhs](std::size_t axis) { m_values[axis] += rhs[axis]; });
44+
}
45+
46+
inline constexpr auto operator-=(const Vec2<T>& rhs) noexcept -> decltype(auto)
47+
{
48+
return apply([this, rhs](std::size_t axis) { m_values[axis] -= rhs[axis]; });
49+
}
50+
51+
inline constexpr auto operator*=(T value) noexcept -> decltype(auto)
52+
{
53+
return apply([this, value](std::size_t axis) { m_values[axis] *= value; });
54+
}
55+
56+
inline constexpr auto operator/=(T value) noexcept -> decltype(auto)
57+
{
58+
return apply([this, value](std::size_t axis) { m_values[axis] /= value; });
59+
}
60+
61+
std::array<T, 2> m_values;
62+
};
63+
64+
template<class T>
65+
Vec2(T, T) -> Vec2<T>;
66+
67+
template<class T>
68+
[[nodiscard]] inline constexpr auto operator+(const Vec2<T>& lhs, const Vec2<T>& rhs) noexcept -> Vec2<T>
69+
{
70+
return auto{lhs} += rhs;
71+
}
72+
73+
template<class T>
74+
[[nodiscard]] inline constexpr auto operator-(const Vec2<T>& lhs, const Vec2<T>& rhs) noexcept -> Vec2<T>
75+
{
76+
return auto{lhs} -= rhs;
77+
}
78+
79+
template<class T>
80+
[[nodiscard]] inline constexpr auto operator*(const Vec2<T>& lhs, T value) noexcept -> Vec2<T>
81+
{
82+
return auto{lhs} *= value;
83+
}
84+
85+
template<class T>
86+
[[nodiscard]] inline constexpr auto operator*(T value, const Vec2<T>& rhs) noexcept -> Vec2<T>
87+
{
88+
return auto{rhs} += value;
89+
}
90+
91+
template<class T>
92+
[[nodiscard]] inline constexpr auto operator/(const Vec2<T>& lhs, T value) noexcept -> Vec2<T>
93+
{
94+
return auto{lhs} /= value;
95+
}
96+
97+
}

modules/Container/Vec3.mpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
module;
2+
3+
#include <cassert>
4+
5+
export module CppUtils.Container.Vec3;
6+
7+
import std;
8+
9+
export namespace CppUtils::Container
10+
{
11+
template<class T>
12+
requires (std::integral<T> or std::floating_point<T>)
13+
struct Vec3 final
14+
{
15+
inline constexpr Vec3() = default;
16+
17+
template<std::convertible_to<T>... Args>
18+
requires (sizeof...(Args) == 3)
19+
inline constexpr Vec3(Args&&... args):
20+
m_values{static_cast<T>(args)...}
21+
{}
22+
23+
[[nodiscard]] inline constexpr auto operator[](this auto&& self, std::size_t axis) noexcept -> decltype(auto)
24+
{
25+
assert(axis < 3);
26+
return self.m_values[axis];
27+
}
28+
29+
[[nodiscard]] inline constexpr auto x(this auto&& self) noexcept -> decltype(auto) { return self[0]; }
30+
[[nodiscard]] inline constexpr auto y(this auto&& self) noexcept -> decltype(auto) { return self[1]; }
31+
[[nodiscard]] inline constexpr auto z(this auto&& self) noexcept -> decltype(auto) { return self[2]; }
32+
[[nodiscard]] inline constexpr auto width(this auto&& self) noexcept -> decltype(auto) { return self[0]; }
33+
[[nodiscard]] inline constexpr auto height(this auto&& self) noexcept -> decltype(auto) { return self[1]; }
34+
[[nodiscard]] inline constexpr auto depth(this auto&& self) noexcept -> decltype(auto) { return self[2]; }
35+
36+
inline constexpr auto apply(this auto&& self, auto&& function) noexcept -> decltype(auto)
37+
{
38+
for (const auto axis : {0uz, 1uz, 2uz})
39+
function(axis);
40+
return self;
41+
}
42+
43+
inline constexpr auto operator+=(const Vec3<T>& rhs) noexcept -> decltype(auto)
44+
{
45+
return apply([this, rhs](std::size_t axis) { m_values[axis] += rhs[axis]; });
46+
}
47+
48+
inline constexpr auto operator-=(const Vec3<T>& rhs) noexcept -> decltype(auto)
49+
{
50+
return apply([this, rhs](std::size_t axis) { m_values[axis] -= rhs[axis]; });
51+
}
52+
53+
inline constexpr auto operator*=(T value) noexcept -> decltype(auto)
54+
{
55+
return apply([this, value](std::size_t axis) { m_values[axis] *= value; });
56+
}
57+
58+
inline constexpr auto operator/=(T value) noexcept -> decltype(auto)
59+
{
60+
return apply([this, value](std::size_t axis) { m_values[axis] /= value; });
61+
}
62+
63+
std::array<T, 3> m_values;
64+
};
65+
66+
template<class T>
67+
Vec3(T, T, T) -> Vec3<T>;
68+
69+
template<class T>
70+
[[nodiscard]] inline constexpr auto operator+(const Vec3<T>& lhs, const Vec3<T>& rhs) noexcept -> Vec3<T>
71+
{
72+
return auto{lhs} += rhs;
73+
}
74+
75+
template<class T>
76+
[[nodiscard]] inline constexpr auto operator-(const Vec3<T>& lhs, const Vec3<T>& rhs) noexcept -> Vec3<T>
77+
{
78+
return auto{lhs} -= rhs;
79+
}
80+
81+
template<class T>
82+
[[nodiscard]] inline constexpr auto operator*(const Vec3<T>& lhs, T value) noexcept -> Vec3<T>
83+
{
84+
return auto{lhs} *= value;
85+
}
86+
87+
template<class T>
88+
[[nodiscard]] inline constexpr auto operator*(T value, const Vec3<T>& rhs) noexcept -> Vec3<T>
89+
{
90+
return auto{rhs} *= value;
91+
}
92+
93+
template<class T>
94+
[[nodiscard]] inline constexpr auto operator/(const Vec3<T>& lhs, T value) noexcept -> Vec3<T>
95+
{
96+
return auto{lhs} /= value;
97+
}
98+
}

modules/Log/Logger.mpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export namespace CppUtils
5656
{
5757
using namespace std::literals;
5858
auto line = ""s;
59-
auto terminalWidth = Terminal::getTerminalSize().x;
59+
auto terminalWidth = Terminal::getTerminalSize().width();
6060
for (auto i = 0uz; i < terminalWidth; ++i)
6161
line += "─";
6262
auto [textModifier, message] = format<logType>("");
@@ -69,7 +69,7 @@ export namespace CppUtils
6969
{
7070
using namespace std::literals;
7171
auto line = ""s;
72-
auto terminalWidth = Terminal::getTerminalSize().x;
72+
auto terminalWidth = Terminal::getTerminalSize().width();
7373
for (auto i = 0uz; i < terminalWidth; ++i)
7474
line += "─";
7575
auto [textModifier, message] = format<logType>("");
@@ -128,7 +128,7 @@ export namespace CppUtils
128128
{
129129
return {
130130
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Yellow},
131-
std::format("{:^{}}\n{} ⚠\n", "🛆 WARNING 🛆", Terminal::getTerminalSize().x, message)};
131+
std::format("{:^{}}\n{} ⚠\n", "🛆 WARNING 🛆", Terminal::getTerminalSize().width(), message)};
132132
}
133133

134134
template<>
@@ -137,7 +137,7 @@ export namespace CppUtils
137137
{
138138
return {
139139
Terminal::TextModifier{stdout, Terminal::TextColor::TextColorEnum::Red},
140-
std::format("{:^{}}\n{} ✕\n", "🛇 ERROR 🛇", Terminal::getTerminalSize().x, message)};
140+
std::format("{:^{}}\n{} ✕\n", "🛇 ERROR 🛇", Terminal::getTerminalSize().width(), message)};
141141
}
142142

143143
inline auto logException(const std::exception& exception, std::size_t depth = 0) -> void

0 commit comments

Comments
 (0)