Browse Source

Trade: improve MaterialData type mismatch assertions.

Followup to 26a0bf94ff where it was done
for MeshData, forgot that this class suffers from the same issue.
pull/537/head
Vladimír Vondruš 5 years ago
parent
commit
5ef7bd26e7
  1. 4
      src/Magnum/Trade/MaterialData.h
  2. 39
      src/Magnum/Trade/Test/MaterialDataTest.cpp

4
src/Magnum/Trade/MaterialData.h

@ -2711,7 +2711,7 @@ constexpr MaterialAttributeData::MaterialAttributeData(const Containers::StringV
template<class T> T MaterialAttributeData::value() const { template<class T> T MaterialAttributeData::value() const {
CORRADE_ASSERT(Implementation::MaterialAttributeTypeFor<T>::type() == _data.type, CORRADE_ASSERT(Implementation::MaterialAttributeTypeFor<T>::type() == _data.type,
"Trade::MaterialAttributeData::value(): improper type requested for" << (_data.data + 1) << "of" << _data.type, {}); "Trade::MaterialAttributeData::value():" << (_data.data + 1) << "is" << _data.type << "but requested a type equivalent to" << Implementation::MaterialAttributeTypeFor<T>::type(), {});
return *reinterpret_cast<const T*>(value()); return *reinterpret_cast<const T*>(value());
} }
@ -2728,7 +2728,7 @@ template<class T> T MaterialData::attribute(const UnsignedInt layer, const Unsig
const Trade::MaterialAttributeData& data = _data[layerOffset(layer) + id]; const Trade::MaterialAttributeData& data = _data[layerOffset(layer) + id];
#endif #endif
CORRADE_ASSERT(Implementation::MaterialAttributeTypeFor<T>::type() == data._data.type, CORRADE_ASSERT(Implementation::MaterialAttributeTypeFor<T>::type() == data._data.type,
"Trade::MaterialData::attribute(): improper type requested for" << (data._data.data + 1) << "of" << data._data.type, {}); "Trade::MaterialData::attribute():" << (data._data.data + 1) << "is" << data._data.type << "but requested a type equivalent to" << Implementation::MaterialAttributeTypeFor<T>::type(), {});
return *reinterpret_cast<const T*>(value); return *reinterpret_cast<const T*>(value);
} }

39
src/Magnum/Trade/Test/MaterialDataTest.cpp

@ -750,7 +750,7 @@ void MaterialDataTest::constructAttributeWrongAccessType() {
std::ostringstream out; std::ostringstream out;
Error redirectError{&out}; Error redirectError{&out};
MaterialAttributeData{"thing3", Matrix4x3{}}.value<Int>(); MaterialAttributeData{"thing3", Matrix4x3{}}.value<Int>();
CORRADE_COMPARE(out.str(), "Trade::MaterialAttributeData::value(): improper type requested for thing3 of Trade::MaterialAttributeType::Matrix4x3\n"); CORRADE_COMPARE(out.str(), "Trade::MaterialAttributeData::value(): thing3 is Trade::MaterialAttributeType::Matrix4x3 but requested a type equivalent to Trade::MaterialAttributeType::Int\n");
} }
void MaterialDataTest::constructAttributeWrongAccessPointerType() { void MaterialDataTest::constructAttributeWrongAccessPointerType() {
@ -761,13 +761,22 @@ void MaterialDataTest::constructAttributeWrongAccessPointerType() {
Int a = 3; Int a = 3;
const Float b = 57.0f; const Float b = 57.0f;
MaterialAttributeData thing3{"thing3", &a};
MaterialAttributeData boom{"boom", &b};
/* These are fine (type is not checked) */
thing3.value<Float*>();
boom.value<const Int*>();
std::ostringstream out; std::ostringstream out;
Error redirectError{&out}; Error redirectError{&out};
MaterialAttributeData{"thing3", &a}.value<Int>(); thing3.value<Int>();
MaterialAttributeData{"boom", &b}.value<Float>(); thing3.value<const Int*>();
boom.value<Float*>();
CORRADE_COMPARE(out.str(), CORRADE_COMPARE(out.str(),
"Trade::MaterialAttributeData::value(): improper type requested for thing3 of Trade::MaterialAttributeType::MutablePointer\n" "Trade::MaterialAttributeData::value(): thing3 is Trade::MaterialAttributeType::MutablePointer but requested a type equivalent to Trade::MaterialAttributeType::Int\n"
"Trade::MaterialAttributeData::value(): improper type requested for boom of Trade::MaterialAttributeType::Pointer\n"); "Trade::MaterialAttributeData::value(): thing3 is Trade::MaterialAttributeType::MutablePointer but requested a type equivalent to Trade::MaterialAttributeType::Pointer\n"
"Trade::MaterialAttributeData::value(): boom is Trade::MaterialAttributeType::Pointer but requested a type equivalent to Trade::MaterialAttributeType::MutablePointer\n");
} }
void MaterialDataTest::constructAttributeWrongAccessTypeString() { void MaterialDataTest::constructAttributeWrongAccessTypeString() {
@ -1625,15 +1634,15 @@ void MaterialDataTest::accessWrongType() {
data.attributeOr(MaterialAttribute::DiffuseColor, Color3{1.0f}); data.attributeOr(MaterialAttribute::DiffuseColor, Color3{1.0f});
data.attributeOr("DiffuseColor", Color3{1.0f}); data.attributeOr("DiffuseColor", Color3{1.0f});
CORRADE_COMPARE(out.str(), CORRADE_COMPARE(out.str(),
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n" "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n"
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n" "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n"
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n" "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n"
/* tryAttribute() and attributeOr() delegate to attribute() so the /* tryAttribute() and attributeOr() delegate to attribute() so the
assert is the same */ assert is the same */
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n" "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n"
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n" "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n"
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n" "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n"
"Trade::MaterialData::attribute(): improper type requested for DiffuseColor of Trade::MaterialAttributeType::Vector4\n"); "Trade::MaterialData::attribute(): DiffuseColor is Trade::MaterialAttributeType::Vector4 but requested a type equivalent to Trade::MaterialAttributeType::Vector3\n");
} }
void MaterialDataTest::accessWrongPointerType() { void MaterialDataTest::accessWrongPointerType() {
@ -1655,11 +1664,13 @@ void MaterialDataTest::accessWrongPointerType() {
std::ostringstream out; std::ostringstream out;
Error redirectError{&out}; Error redirectError{&out};
data.attribute<Int>("mutablePointer");
data.attribute<const Int*>("mutablePointer"); data.attribute<const Int*>("mutablePointer");
data.attribute<Double*>("pointer"); data.attribute<Double*>("pointer");
CORRADE_COMPARE(out.str(), CORRADE_COMPARE(out.str(),
"Trade::MaterialData::attribute(): improper type requested for mutablePointer of Trade::MaterialAttributeType::MutablePointer\n" "Trade::MaterialData::attribute(): mutablePointer is Trade::MaterialAttributeType::MutablePointer but requested a type equivalent to Trade::MaterialAttributeType::Int\n"
"Trade::MaterialData::attribute(): improper type requested for pointer of Trade::MaterialAttributeType::Pointer\n"); "Trade::MaterialData::attribute(): mutablePointer is Trade::MaterialAttributeType::MutablePointer but requested a type equivalent to Trade::MaterialAttributeType::Pointer\n"
"Trade::MaterialData::attribute(): pointer is Trade::MaterialAttributeType::Pointer but requested a type equivalent to Trade::MaterialAttributeType::MutablePointer\n");
} }
void MaterialDataTest::accessWrongTypeString() { void MaterialDataTest::accessWrongTypeString() {

Loading…
Cancel
Save