Browse Source

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.
pull/638/head
Vladimír Vondruš 2 years ago
parent
commit
e8e321b273
  1. 12
      src/Magnum/Math/Test/VectorTest.cpp
  2. 15
      src/Magnum/Math/Vector.h

12
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)

15
src/Magnum/Math/Vector.h

@ -1316,15 +1316,24 @@ template<std::size_t size, class T> inline BitVector<size> notEqual(const Vector
#ifndef CORRADE_SINGLES_NO_DEBUG
/** @debugoperator{Vector} */
template<std::size_t size, class T> Debug& operator<<(Debug& debug, const Vector<size, T>& 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 */

Loading…
Cancel
Save