From 26a0bf94fff61cde34dc0d626a9ba41fd89cd0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 30 Aug 2021 15:10:39 +0200 Subject: [PATCH] Trade: improve MeshData type mismatch assertions. We *do* know what type the user requested, so why not print it as well. And for the array access the message was especially lazy. --- src/Magnum/Trade/MeshData.h | 12 ++++++----- src/Magnum/Trade/Test/MeshDataTest.cpp | 28 +++++++++++++------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Magnum/Trade/MeshData.h b/src/Magnum/Trade/MeshData.h index 073298fbe..79a1b84e6 100644 --- a/src/Magnum/Trade/MeshData.h +++ b/src/Magnum/Trade/MeshData.h @@ -2121,7 +2121,7 @@ template Containers::ArrayView MeshData::indices() const { if(!data.stride()[1]) return {}; #endif CORRADE_ASSERT(Implementation::meshIndexTypeFor() == _indexType, - "Trade::MeshData::indices(): improper type requested for" << _indexType, nullptr); + "Trade::MeshData::indices(): indices are" << _indexType << "but requested" << Implementation::meshIndexTypeFor(), nullptr); return Containers::arrayCast<1, const T>(data).asContiguous(); } @@ -2133,7 +2133,7 @@ template Containers::ArrayView MeshData::mutableIndices() { if(!data.stride()[1]) return {}; #endif CORRADE_ASSERT(Implementation::meshIndexTypeFor() == _indexType, - "Trade::MeshData::mutableIndices(): improper type requested for" << _indexType, nullptr); + "Trade::MeshData::mutableIndices(): indices are" << _indexType << "but requested" << Implementation::meshIndexTypeFor(), nullptr); return Containers::arrayCast<1, T>(data).asContiguous(); } @@ -2142,9 +2142,11 @@ template bool MeshData::checkVertexFormatCompatibility(const MeshAttrib CORRADE_ASSERT(!isVertexFormatImplementationSpecific(attribute._format), prefix << "can't cast data from an implementation-specific vertex format" << reinterpret_cast(vertexFormatUnwrap(attribute._format)), false); CORRADE_ASSERT(Implementation::isVertexFormatCompatible::type>(attribute._format), - prefix << "improper type requested for" << attribute._name << "of format" << attribute._format, false); - CORRADE_ASSERT(std::is_array::value == !!attribute._arraySize, - prefix << "use T[] to access an array attribute", false); + prefix << attribute._name << "is" << attribute._format << "but requested a type equivalent to" << Implementation::vertexFormatFor::type>(), false); + if(attribute._arraySize) CORRADE_ASSERT(std::is_array::value, + prefix << attribute._name << "is an array attribute, use T[] to access it", false); + else CORRADE_ASSERT(!std::is_array::value, + prefix << attribute._name << "is not an array attribute, can't use T[] to access it", false); return true; } #endif diff --git a/src/Magnum/Trade/Test/MeshDataTest.cpp b/src/Magnum/Trade/Test/MeshDataTest.cpp index d7e895fa4..2a1702ce8 100644 --- a/src/Magnum/Trade/Test/MeshDataTest.cpp +++ b/src/Magnum/Trade/Test/MeshDataTest.cpp @@ -2750,14 +2750,14 @@ void MeshDataTest::arrayAttributeWrongAccess() { data.mutableAttribute(MeshAttribute::Position); data.mutableAttribute(meshAttributeCustom(35)); CORRADE_COMPARE(out.str(), - "Trade::MeshData::attribute(): use T[] to access an array attribute\n" - "Trade::MeshData::attribute(): use T[] to access an array attribute\n" - "Trade::MeshData::mutableAttribute(): use T[] to access an array attribute\n" - "Trade::MeshData::mutableAttribute(): use T[] to access an array attribute\n" - "Trade::MeshData::attribute(): use T[] to access an array attribute\n" - "Trade::MeshData::attribute(): use T[] to access an array attribute\n" - "Trade::MeshData::mutableAttribute(): use T[] to access an array attribute\n" - "Trade::MeshData::mutableAttribute(): use T[] to access an array attribute\n"); + "Trade::MeshData::attribute(): Trade::MeshAttribute::Position is not an array attribute, can't use T[] to access it\n" + "Trade::MeshData::attribute(): Trade::MeshAttribute::Custom(35) is an array attribute, use T[] to access it\n" + "Trade::MeshData::mutableAttribute(): Trade::MeshAttribute::Position is not an array attribute, can't use T[] to access it\n" + "Trade::MeshData::mutableAttribute(): Trade::MeshAttribute::Custom(35) is an array attribute, use T[] to access it\n" + "Trade::MeshData::attribute(): Trade::MeshAttribute::Position is not an array attribute, can't use T[] to access it\n" + "Trade::MeshData::attribute(): Trade::MeshAttribute::Custom(35) is an array attribute, use T[] to access it\n" + "Trade::MeshData::mutableAttribute(): Trade::MeshAttribute::Position is not an array attribute, can't use T[] to access it\n" + "Trade::MeshData::mutableAttribute(): Trade::MeshAttribute::Custom(35) is an array attribute, use T[] to access it\n"); } void MeshDataTest::mutableAccessNotAllowed() { @@ -2841,8 +2841,8 @@ void MeshDataTest::indicesWrongType() { data.indices(); data.mutableIndices(); CORRADE_COMPARE(out.str(), - "Trade::MeshData::indices(): improper type requested for MeshIndexType::UnsignedShort\n" - "Trade::MeshData::mutableIndices(): improper type requested for MeshIndexType::UnsignedShort\n"); + "Trade::MeshData::indices(): indices are MeshIndexType::UnsignedShort but requested MeshIndexType::UnsignedByte\n" + "Trade::MeshData::mutableIndices(): indices are MeshIndexType::UnsignedShort but requested MeshIndexType::UnsignedByte\n"); } void MeshDataTest::attributeNotFound() { @@ -2964,10 +2964,10 @@ void MeshDataTest::attributeWrongType() { data.mutableAttribute(MeshAttribute::Position); data.mutableAttribute(MeshAttribute::Position); CORRADE_COMPARE(out.str(), - "Trade::MeshData::attribute(): improper type requested for Trade::MeshAttribute::Position of format VertexFormat::Vector3\n" - "Trade::MeshData::attribute(): improper type requested for Trade::MeshAttribute::Position of format VertexFormat::Vector3\n" - "Trade::MeshData::mutableAttribute(): improper type requested for Trade::MeshAttribute::Position of format VertexFormat::Vector3\n" - "Trade::MeshData::mutableAttribute(): improper type requested for Trade::MeshAttribute::Position of format VertexFormat::Vector3\n"); + "Trade::MeshData::attribute(): Trade::MeshAttribute::Position is VertexFormat::Vector3 but requested a type equivalent to VertexFormat::Vector4\n" + "Trade::MeshData::attribute(): Trade::MeshAttribute::Position is VertexFormat::Vector3 but requested a type equivalent to VertexFormat::Vector4\n" + "Trade::MeshData::mutableAttribute(): Trade::MeshAttribute::Position is VertexFormat::Vector3 but requested a type equivalent to VertexFormat::Vector4\n" + "Trade::MeshData::mutableAttribute(): Trade::MeshAttribute::Position is VertexFormat::Vector3 but requested a type equivalent to VertexFormat::Vector4\n"); } void MeshDataTest::releaseIndexData() {