Skip to content

Commit a097c0a

Browse files
authored
Merge pull request #57 from orange-cpp/feature/added_formatters
Adds std::format support for math types
2 parents 7789362 + 20ae2b4 commit a097c0a

File tree

7 files changed

+110
-18
lines changed

7 files changed

+110
-18
lines changed

include/omath/angle.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "omath/angles.hpp"
77
#include <algorithm>
88
#include <utility>
9+
#include <format>
910

1011
namespace omath
1112
{
@@ -149,3 +150,17 @@ namespace omath
149150
}
150151
};
151152
} // namespace omath
153+
template<class Type, Type min, Type max, omath::AngleFlags flags>
154+
struct std::formatter<omath::Angle<Type, min, max, flags>> // NOLINT(*-dcl58-cpp)
155+
{
156+
[[nodiscard]]
157+
static constexpr auto parse(std::format_parse_context& ctx)
158+
{
159+
return ctx.begin();
160+
}
161+
[[nodiscard]]
162+
static auto format(const omath::Angle<Type, min, max, flags>& deg, std::format_context& ctx)
163+
{
164+
return std::format_to(ctx.out(), "{}deg", deg.as_degrees());
165+
}
166+
};

include/omath/color.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,21 @@ namespace omath
167167
#endif
168168
};
169169
} // namespace omath
170+
template<>
171+
struct std::formatter<omath::Color> // NOLINT(*-dcl58-cpp)
172+
{
173+
[[nodiscard]]
174+
static constexpr auto parse(std::format_parse_context& ctx)
175+
{
176+
return ctx.begin();
177+
}
178+
[[nodiscard]]
179+
static auto format(const omath::Color& col, std::format_context& ctx)
180+
{
181+
return std::format_to(ctx.out(), "[r:{}, g:{}, b:{}, a:{}]",
182+
static_cast<int>(col.x * 255.f),
183+
static_cast<int>(col.y * 255.f),
184+
static_cast<int>(col.z * 255.f),
185+
static_cast<int>(col.w * 255.f));
186+
}
187+
};

include/omath/mat.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,19 @@ namespace omath
480480
{0.f, 0.f, -1.f, 0.f}};
481481
}
482482
} // namespace omath
483+
484+
template<size_t Rows, size_t Columns, class Type, omath::MatStoreType StoreType>
485+
struct std::formatter<omath::Mat<Rows, Columns, Type, StoreType>> // NOLINT(*-dcl58-cpp)
486+
{
487+
using MatType = omath::Mat<Rows, Columns, Type, StoreType>;
488+
[[nodiscard]]
489+
static constexpr auto parse(std::format_parse_context& ctx)
490+
{
491+
return ctx.begin();
492+
}
493+
[[nodiscard]]
494+
static auto format(const MatType& mat, std::format_context& ctx)
495+
{
496+
return std::format_to(ctx.out(), "{}", mat.to_string());
497+
}
498+
};

include/omath/vector2.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//
44

55
#pragma once
6+
#include "vector3.hpp"
67
#include <cmath>
8+
#include <format>
79
#include <tuple>
810

911
#ifdef OMATH_IMGUI_INTEGRATION
@@ -218,7 +220,6 @@ namespace omath
218220
{
219221
return std::make_tuple(x, y);
220222
}
221-
222223
#ifdef OMATH_IMGUI_INTEGRATION
223224
[[nodiscard]]
224225
ImVec2 to_im_vec2() const noexcept
@@ -233,3 +234,18 @@ namespace omath
233234
#endif
234235
};
235236
} // namespace omath
237+
238+
template<class Type>
239+
struct std::formatter<omath::Vector2<Type>> // NOLINT(*-dcl58-cpp)
240+
{
241+
[[nodiscard]]
242+
static constexpr auto parse(std::format_parse_context& ctx)
243+
{
244+
return ctx.begin();
245+
}
246+
[[nodiscard]]
247+
static auto format(const omath::Vector2<Type>& vec, std::format_context& ctx)
248+
{
249+
return std::format_to(ctx.out(), "[{}, {}]", vec.x, vec.y);
250+
}
251+
};

include/omath/vector3.hpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ namespace omath
159159
return Vector2<Type>::length();
160160
}
161161

162-
[[nodiscard]] Type distance_to(const Vector3& vOther) const noexcept
162+
[[nodiscard]] Type distance_to(const Vector3& v_other) const noexcept
163163
{
164-
return (*this - vOther).length();
164+
return (*this - v_other).length();
165165
}
166166
#endif
167167

@@ -279,21 +279,33 @@ namespace omath
279279
}
280280
};
281281
} // namespace omath
282-
// ReSharper disable once CppRedundantNamespaceDefinition
283-
namespace std
282+
283+
template<> struct std::hash<omath::Vector3<float>>
284284
{
285-
template<> struct hash<omath::Vector3<float>>
285+
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
286286
{
287-
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
288-
{
289-
std::size_t hash = 0;
290-
constexpr std::hash<float> hasher;
287+
std::size_t hash = 0;
288+
constexpr std::hash<float> hasher;
291289

292-
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
293-
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
294-
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
290+
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
291+
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
292+
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
295293

296-
return hash;
297-
}
298-
};
299-
} // namespace std
294+
return hash;
295+
}
296+
};
297+
298+
template<class Type>
299+
struct std::formatter<omath::Vector3<Type>> // NOLINT(*-dcl58-cpp)
300+
{
301+
[[nodiscard]]
302+
static constexpr auto parse(std::format_parse_context& ctx)
303+
{
304+
return ctx.begin();
305+
}
306+
[[nodiscard]]
307+
static auto format(const omath::Vector3<Type>& vec, std::format_context& ctx)
308+
{
309+
return std::format_to(ctx.out(), "[{}, {}, {}]", vec.x, vec.y, vec.z);
310+
}
311+
};

include/omath/vector4.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,18 @@ namespace omath
201201
#endif
202202
};
203203
} // namespace omath
204+
205+
template<class Type>
206+
struct std::formatter<omath::Vector4<Type>> // NOLINT(*-dcl58-cpp)
207+
{
208+
[[nodiscard]]
209+
static constexpr auto parse(std::format_parse_context& ctx)
210+
{
211+
return ctx.begin();
212+
}
213+
[[nodiscard]]
214+
static auto format(const omath::Vector4<Type>& vec, std::format_context& ctx)
215+
{
216+
return std::format_to(ctx.out(), "[{}, {}, {}, {}]", vec.x, vec.y, vec.z, vec.w);
217+
}
218+
};

tests/general/unit_test_mat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ TEST_F(UnitTestMat, Clear)
125125

126126
TEST_F(UnitTestMat, ToString)
127127
{
128-
const std::string str = m2.to_string();
128+
const std::string str = std::format("{}", m2);
129129
EXPECT_FALSE(str.empty());
130130
EXPECT_EQ(str, "[[ 1.000, 2.000]\n [ 3.000, 4.000]]");
131131
}

0 commit comments

Comments
 (0)