From e8e321b2735b5c41635028c776280428451ed8ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 5 Mar 2024 20:08:13 +0100 Subject: [PATCH] Math: propagate Debug flags in Vector debug printer. Such as being able to print the contents as hexadecimal. Doing this just for Vector and not any other math types, as those are all floating point where it doesn't make sense. And for Nanoseconds, which are integers, hexadecimal printing makes no sense either. --- src/Magnum/Math/Test/VectorTest.cpp | 12 +++++++++++- src/Magnum/Math/Vector.h | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Magnum/Math/Test/VectorTest.cpp b/src/Magnum/Math/Test/VectorTest.cpp index bd90de797..da6da6297 100644 --- a/src/Magnum/Math/Test/VectorTest.cpp +++ b/src/Magnum/Math/Test/VectorTest.cpp @@ -119,6 +119,7 @@ struct VectorTest: TestSuite::Tester { void debug(); void debugPacked(); + void debugPropagateFlags(); }; /* What's a typedef and not a using differs from the typedefs in root Magnum @@ -197,7 +198,8 @@ VectorTest::VectorTest() { &VectorTest::strictWeakOrdering, &VectorTest::debug, - &VectorTest::debugPacked}); + &VectorTest::debugPacked, + &VectorTest::debugPropagateFlags}); } void VectorTest::construct() { @@ -1332,6 +1334,14 @@ void VectorTest::debugPacked() { CORRADE_COMPARE(out.str(), "{0.5, 15, 1, 1} Vector(0, 0, 0, 0)\n"); } +void VectorTest::debugPropagateFlags() { + std::ostringstream out; + /* The modifier shouldn't become persistent for values after. The nospace + modifier shouldn't get propagated. */ + Debug{&out} << ">" << Debug::nospace << Debug::hex << Vector2i(0xab, 0xcd) << Vector2i(12, 13); + CORRADE_COMPARE(out.str(), ">Vector(0xab, 0xcd) Vector(12, 13)\n"); +} + }}}} CORRADE_TEST_MAIN(Magnum::Math::Test::VectorTest) diff --git a/src/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index 7c6d603d0..8fbf68205 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -1316,15 +1316,24 @@ template inline BitVector notEqual(const Vector #ifndef CORRADE_SINGLES_NO_DEBUG /** @debugoperator{Vector} */ template Debug& operator<<(Debug& debug, const Vector& value) { - /** @todo might make sense to propagate the flags also, for hex value - printing etc */ + /* Nested values should get printed with the same flags, so make all + immediate flags temporarily global -- except NoSpace, unless it's also + set globally */ + const Utility::Debug::Flags prevFlags = debug.flags(); + debug.setFlags(prevFlags | (debug.immediateFlags() & ~Utility::Debug::Flag::NoSpace)); + const bool packed = debug.immediateFlags() >= Debug::Flag::Packed; debug << (packed ? "{" : "Vector(") << Debug::nospace; for(std::size_t i = 0; i != size; ++i) { if(i != 0) debug << Debug::nospace << ","; debug << value[i]; } - return debug << Debug::nospace << (packed ? "}" : ")"); + debug << Debug::nospace << (packed ? "}" : ")"); + + /* Reset the original flags back */ + debug.setFlags(prevFlags); + + return debug; } /* Explicit instantiation for commonly used types */