Browse Source

Trade: expose materialLayerName() and materialAttributeName().

The enum-to-string conversion was just a private API and we need to use
it in various material filtering code. It was also a private class member
for some reason, even though it has no relation to / dependency on the
MaterialData class. So it's made a free function instead.
pull/565/merge
Vladimír Vondruš 4 years ago
parent
commit
e1619e5aab
  1. 133
      src/Magnum/Trade/MaterialData.cpp
  2. 81
      src/Magnum/Trade/MaterialData.h
  3. 44
      src/Magnum/Trade/Test/MaterialDataTest.cpp

133
src/Magnum/Trade/MaterialData.cpp

@ -65,6 +65,39 @@ constexpr struct {
} }
namespace Implementation {
Containers::StringView materialLayerNameInternal(const MaterialLayer layer) {
#ifndef CORRADE_NO_ASSERT
if(UnsignedInt(layer) - 1 >= Containers::arraySize(LayerMap))
return nullptr;
#endif
return LayerMap[UnsignedInt(layer) - 1];
}
Containers::StringView materialAttributeNameInternal(const MaterialAttribute attribute) {
#ifndef CORRADE_NO_ASSERT
if(UnsignedInt(attribute) - 1 >= Containers::arraySize(AttributeMap))
return nullptr;
#endif
return AttributeMap[UnsignedInt(attribute) - 1].name;
}
}
Containers::StringView materialLayerName(const MaterialLayer layer) {
CORRADE_ASSERT(UnsignedInt(layer) - 1 < Containers::arraySize(LayerMap),
"Trade::materialLayerName(): invalid layer" << layer, {});
return LayerMap[UnsignedInt(layer) - 1];
}
Containers::StringView materialAttributeName(const MaterialAttribute attribute) {
CORRADE_ASSERT(UnsignedInt(attribute) - 1 < Containers::arraySize(AttributeMap),
"Trade::materialAttributeName(): invalid attribute" << attribute, {});
return AttributeMap[UnsignedInt(attribute) - 1].name;
}
UnsignedInt materialTextureSwizzleComponentCount(const MaterialTextureSwizzle swizzle) { UnsignedInt materialTextureSwizzleComponentCount(const MaterialTextureSwizzle swizzle) {
return (UnsignedInt(swizzle) & 0xff000000u ? 1 : 0) + return (UnsignedInt(swizzle) & 0xff000000u ? 1 : 0) +
(UnsignedInt(swizzle) & 0x00ff0000u ? 1 : 0) + (UnsignedInt(swizzle) & 0x00ff0000u ? 1 : 0) +
@ -298,22 +331,6 @@ MaterialData::~MaterialData() = default;
MaterialData& MaterialData::operator=(MaterialData&&) noexcept = default; MaterialData& MaterialData::operator=(MaterialData&&) noexcept = default;
Containers::StringView MaterialData::layerString(const MaterialLayer name) {
#ifndef CORRADE_NO_ASSERT
if(UnsignedInt(name) - 1 >= Containers::arraySize(LayerMap))
return nullptr;
#endif
return LayerMap[UnsignedInt(name) - 1];
}
Containers::StringView MaterialData::attributeString(const MaterialAttribute name) {
#ifndef CORRADE_NO_ASSERT
if(UnsignedInt(name) - 1 >= Containers::arraySize(AttributeMap))
return nullptr;
#endif
return AttributeMap[UnsignedInt(name) - 1].name;
}
UnsignedInt MaterialData::findLayerIdInternal(const Containers::StringView layer) const { UnsignedInt MaterialData::findLayerIdInternal(const Containers::StringView layer) const {
for(std::size_t i = 1; i < _layerOffsets.size(); ++i) { for(std::size_t i = 1; i < _layerOffsets.size(); ++i) {
if(_layerOffsets[i] > _layerOffsets[i - 1] && if(_layerOffsets[i] > _layerOffsets[i - 1] &&
@ -329,7 +346,7 @@ bool MaterialData::hasLayer(const Containers::StringView layer) const {
} }
bool MaterialData::hasLayer(const MaterialLayer layer) const { bool MaterialData::hasLayer(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::hasLayer(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::hasLayer(): invalid name" << layer, {});
return hasLayer(string); return hasLayer(string);
} }
@ -340,7 +357,7 @@ Containers::Optional<UnsignedInt> MaterialData::findLayerId(const Containers::St
} }
Containers::Optional<UnsignedInt> MaterialData::findLayerId(const MaterialLayer layer) const { Containers::Optional<UnsignedInt> MaterialData::findLayerId(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::findLayerId(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::findLayerId(): invalid name" << layer, {});
return findLayerId(string); return findLayerId(string);
} }
@ -353,7 +370,7 @@ UnsignedInt MaterialData::layerId(const Containers::StringView layer) const {
} }
UnsignedInt MaterialData::layerId(const MaterialLayer layer) const { UnsignedInt MaterialData::layerId(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerId(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerId(): invalid name" << layer, {});
return layerId(string); return layerId(string);
} }
@ -381,7 +398,7 @@ Float MaterialData::layerFactor(const Containers::StringView layer) const {
} }
Float MaterialData::layerFactor(const MaterialLayer layer) const { Float MaterialData::layerFactor(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerFactor(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerFactor(): invalid name" << layer, {});
return layerFactor(string); return layerFactor(string);
} }
@ -404,7 +421,7 @@ UnsignedInt MaterialData::layerFactorTexture(const Containers::StringView layer)
} }
UnsignedInt MaterialData::layerFactorTexture(const MaterialLayer layer) const { UnsignedInt MaterialData::layerFactorTexture(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTexture(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTexture(): invalid name" << layer, {});
return layerFactorTexture(string); return layerFactorTexture(string);
} }
@ -431,7 +448,7 @@ MaterialTextureSwizzle MaterialData::layerFactorTextureSwizzle(const Containers:
} }
MaterialTextureSwizzle MaterialData::layerFactorTextureSwizzle(const MaterialLayer layer) const { MaterialTextureSwizzle MaterialData::layerFactorTextureSwizzle(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureSwizzle(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureSwizzle(): invalid name" << layer, {});
return layerFactorTextureSwizzle(string); return layerFactorTextureSwizzle(string);
} }
@ -464,7 +481,7 @@ Matrix3 MaterialData::layerFactorTextureMatrix(const Containers::StringView laye
} }
Matrix3 MaterialData::layerFactorTextureMatrix(const MaterialLayer layer) const { Matrix3 MaterialData::layerFactorTextureMatrix(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureMatrix(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureMatrix(): invalid name" << layer, {});
return layerFactorTextureMatrix(string); return layerFactorTextureMatrix(string);
} }
@ -497,7 +514,7 @@ UnsignedInt MaterialData::layerFactorTextureCoordinates(const Containers::String
} }
UnsignedInt MaterialData::layerFactorTextureCoordinates(const MaterialLayer layer) const { UnsignedInt MaterialData::layerFactorTextureCoordinates(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureCoordinates(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureCoordinates(): invalid name" << layer, {});
return layerFactorTextureCoordinates(string); return layerFactorTextureCoordinates(string);
} }
@ -530,7 +547,7 @@ UnsignedInt MaterialData::layerFactorTextureLayer(const Containers::StringView l
} }
UnsignedInt MaterialData::layerFactorTextureLayer(const MaterialLayer layer) const { UnsignedInt MaterialData::layerFactorTextureLayer(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureLayer(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::layerFactorTextureLayer(): invalid name" << layer, {});
return layerFactorTextureLayer(string); return layerFactorTextureLayer(string);
} }
@ -551,7 +568,7 @@ UnsignedInt MaterialData::attributeCount(const Containers::StringView layer) con
} }
UnsignedInt MaterialData::attributeCount(const MaterialLayer layer) const { UnsignedInt MaterialData::attributeCount(const MaterialLayer layer) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeCount(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeCount(): invalid name" << layer, {});
return attributeCount(string); return attributeCount(string);
} }
@ -575,7 +592,7 @@ bool MaterialData::hasAttribute(const UnsignedInt layer, const Containers::Strin
} }
bool MaterialData::hasAttribute(const UnsignedInt layer, const MaterialAttribute name) const { bool MaterialData::hasAttribute(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << name, {});
return hasAttribute(layer, string); return hasAttribute(layer, string);
} }
@ -588,19 +605,19 @@ bool MaterialData::hasAttribute(const Containers::StringView layer, const Contai
} }
bool MaterialData::hasAttribute(const Containers::StringView layer, const MaterialAttribute name) const { bool MaterialData::hasAttribute(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << name, {});
return hasAttribute(layer, string); return hasAttribute(layer, string);
} }
bool MaterialData::hasAttribute(const MaterialLayer layer, const Containers::StringView name) const { bool MaterialData::hasAttribute(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << layer, {});
return hasAttribute(string, name); return hasAttribute(string, name);
} }
bool MaterialData::hasAttribute(const MaterialLayer layer, const MaterialAttribute name) const { bool MaterialData::hasAttribute(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << layer, {});
return hasAttribute(string, name); return hasAttribute(string, name);
} }
@ -613,7 +630,7 @@ Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const UnsignedIn
} }
Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const UnsignedInt layer, const MaterialAttribute name) const { Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << name, {});
return findAttributeId(layer, string); return findAttributeId(layer, string);
} }
@ -627,19 +644,19 @@ Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const Containers
} }
Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const Containers::StringView layer, const MaterialAttribute name) const { Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << name, {});
return findAttributeId(layer, string); return findAttributeId(layer, string);
} }
Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const MaterialLayer layer, const Containers::StringView name) const { Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << layer, {});
return findAttributeId(string, name); return findAttributeId(string, name);
} }
Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const MaterialLayer layer, const MaterialAttribute name) const { Containers::Optional<UnsignedInt> MaterialData::findAttributeId(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::findAttributeId(): invalid name" << layer, {});
return findAttributeId(string, name); return findAttributeId(string, name);
} }
@ -662,7 +679,7 @@ UnsignedInt MaterialData::attributeId(const UnsignedInt layer, const Containers:
} }
UnsignedInt MaterialData::attributeId(const UnsignedInt layer, const MaterialAttribute name) const { UnsignedInt MaterialData::attributeId(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << name, {});
return attributeId(layer, string); return attributeId(layer, string);
} }
@ -678,19 +695,19 @@ UnsignedInt MaterialData::attributeId(const Containers::StringView layer, const
} }
UnsignedInt MaterialData::attributeId(const Containers::StringView layer, const MaterialAttribute name) const { UnsignedInt MaterialData::attributeId(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << name, {});
return attributeId(layer, string); return attributeId(layer, string);
} }
UnsignedInt MaterialData::attributeId(const MaterialLayer layer, const Containers::StringView name) const { UnsignedInt MaterialData::attributeId(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << layer, {});
return attributeId(string, name); return attributeId(string, name);
} }
UnsignedInt MaterialData::attributeId(const MaterialLayer layer, const MaterialAttribute name) const { UnsignedInt MaterialData::attributeId(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << layer, {});
return attributeId(string, name); return attributeId(string, name);
} }
@ -713,7 +730,7 @@ Containers::StringView MaterialData::attributeName(const Containers::StringView
} }
Containers::StringView MaterialData::attributeName(const MaterialLayer layer, const UnsignedInt id) const { Containers::StringView MaterialData::attributeName(const MaterialLayer layer, const UnsignedInt id) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeName(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeName(): invalid name" << layer, {});
return attributeName(string, id); return attributeName(string, id);
} }
@ -736,7 +753,7 @@ MaterialAttributeType MaterialData::attributeType(const UnsignedInt layer, const
} }
MaterialAttributeType MaterialData::attributeType(const UnsignedInt layer, const MaterialAttribute name) const { MaterialAttributeType MaterialData::attributeType(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << name, {});
return attributeType(layer, string); return attributeType(layer, string);
} }
@ -761,25 +778,25 @@ MaterialAttributeType MaterialData::attributeType(const Containers::StringView l
} }
MaterialAttributeType MaterialData::attributeType(const Containers::StringView layer, const MaterialAttribute name) const { MaterialAttributeType MaterialData::attributeType(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << name, {});
return attributeType(layer, string); return attributeType(layer, string);
} }
MaterialAttributeType MaterialData::attributeType(const MaterialLayer layer, const UnsignedInt id) const { MaterialAttributeType MaterialData::attributeType(const MaterialLayer layer, const UnsignedInt id) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << layer, {});
return attributeType(string, id); return attributeType(string, id);
} }
MaterialAttributeType MaterialData::attributeType(const MaterialLayer layer, const Containers::StringView name) const { MaterialAttributeType MaterialData::attributeType(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << layer, {});
return attributeType(string, name); return attributeType(string, name);
} }
MaterialAttributeType MaterialData::attributeType(const MaterialLayer layer, const MaterialAttribute name) const { MaterialAttributeType MaterialData::attributeType(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << layer, {});
return attributeType(string, name); return attributeType(string, name);
} }
@ -823,13 +840,13 @@ void* MaterialData::mutableAttribute(const UnsignedInt layer, const Containers::
} }
const void* MaterialData::attribute(const UnsignedInt layer, const MaterialAttribute name) const { const void* MaterialData::attribute(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << name, {});
return attribute(layer, string); return attribute(layer, string);
} }
void* MaterialData::mutableAttribute(const UnsignedInt layer, const MaterialAttribute name) { void* MaterialData::mutableAttribute(const UnsignedInt layer, const MaterialAttribute name) {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << name, {});
return mutableAttribute(layer, string); return mutableAttribute(layer, string);
} }
@ -877,49 +894,49 @@ void* MaterialData::mutableAttribute(const Containers::StringView layer, const C
} }
const void* MaterialData::attribute(const Containers::StringView layer, const MaterialAttribute name) const { const void* MaterialData::attribute(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << name, {});
return attribute(layer, string); return attribute(layer, string);
} }
void* MaterialData::mutableAttribute(const Containers::StringView layer, const MaterialAttribute name) { void* MaterialData::mutableAttribute(const Containers::StringView layer, const MaterialAttribute name) {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << name, {});
return mutableAttribute(layer, string); return mutableAttribute(layer, string);
} }
const void* MaterialData::attribute(const MaterialLayer layer, const UnsignedInt id) const { const void* MaterialData::attribute(const MaterialLayer layer, const UnsignedInt id) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << layer, {});
return attribute(string, id); return attribute(string, id);
} }
void* MaterialData::mutableAttribute(const MaterialLayer layer, const UnsignedInt id) { void* MaterialData::mutableAttribute(const MaterialLayer layer, const UnsignedInt id) {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << layer, {});
return mutableAttribute(string, id); return mutableAttribute(string, id);
} }
const void* MaterialData::attribute(const MaterialLayer layer, const Containers::StringView name) const { const void* MaterialData::attribute(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << layer, {});
return attribute(string, name); return attribute(string, name);
} }
void* MaterialData::mutableAttribute(const MaterialLayer layer, const Containers::StringView name) { void* MaterialData::mutableAttribute(const MaterialLayer layer, const Containers::StringView name) {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << layer, {});
return mutableAttribute(string, name); return mutableAttribute(string, name);
} }
const void* MaterialData::attribute(const MaterialLayer layer, const MaterialAttribute name) const { const void* MaterialData::attribute(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << layer, {});
return attribute(string, name); return attribute(string, name);
} }
void* MaterialData::mutableAttribute(const MaterialLayer layer, const MaterialAttribute name) { void* MaterialData::mutableAttribute(const MaterialLayer layer, const MaterialAttribute name) {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::mutableAttribute(): invalid name" << layer, {});
return mutableAttribute(string, name); return mutableAttribute(string, name);
} }
@ -965,7 +982,7 @@ const void* MaterialData::tryAttribute(const UnsignedInt layer, const Containers
} }
const void* MaterialData::tryAttribute(const UnsignedInt layer, const MaterialAttribute name) const { const void* MaterialData::tryAttribute(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << name, {});
return tryAttribute(layer, string); return tryAttribute(layer, string);
} }
@ -980,19 +997,19 @@ const void* MaterialData::tryAttribute(const Containers::StringView layer, const
} }
const void* MaterialData::tryAttribute(const Containers::StringView layer, const MaterialAttribute name) const { const void* MaterialData::tryAttribute(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << name, {});
return tryAttribute(layer, string); return tryAttribute(layer, string);
} }
const void* MaterialData::tryAttribute(const MaterialLayer layer, const Containers::StringView name) const { const void* MaterialData::tryAttribute(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << layer, {});
return tryAttribute(string, name); return tryAttribute(string, name);
} }
const void* MaterialData::tryAttribute(const MaterialLayer layer, const MaterialAttribute name) const { const void* MaterialData::tryAttribute(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << layer, {});
return tryAttribute(string, name); return tryAttribute(string, name);
} }

81
src/Magnum/Trade/MaterialData.h

@ -57,7 +57,8 @@ for @cpp "ClearCoat" @ce. Each layer is expected to contain (a subset of) the
@ref MaterialAttribute::LayerFactorTextureMatrix, @ref MaterialAttribute::LayerFactorTextureMatrix,
@ref MaterialAttribute::LayerFactorTextureCoordinates attributes in addition to @ref MaterialAttribute::LayerFactorTextureCoordinates attributes in addition to
what's specified for a particular named layer. what's specified for a particular named layer.
@see @ref MaterialData, @ref MaterialData::layerName(), @ref MaterialLayerData @see @ref MaterialData, @ref MaterialData::layerName(), @ref MaterialLayerData,
@ref materialLayerName()
*/ */
enum class MaterialLayer: UnsignedInt { enum class MaterialLayer: UnsignedInt {
/* Zero used for an invalid value */ /* Zero used for an invalid value */
@ -80,6 +81,21 @@ enum class MaterialLayer: UnsignedInt {
ClearCoat = 1, ClearCoat = 1,
}; };
namespace Implementation {
/* Compared to materialLayerName() below returns an empty string for
invalid layers, used internally to provide better assertion messages */
MAGNUM_TRADE_EXPORT Containers::StringView materialLayerNameInternal(MaterialLayer layer);
}
/**
@brief Material layer name as a string
Expects that @p layer is a valid @ref MaterialLayer value. The returned view
has both @relativeref{Corrade,Containers::StringViewFlag::Global} and
@relativeref{Corrade::Containers::StringViewFlag,NullTerminated} set.
*/
MAGNUM_TRADE_EXPORT Containers::StringView materialLayerName(MaterialLayer layer);
/** /**
@debugoperatorenum{MaterialLayer} @debugoperatorenum{MaterialLayer}
@m_since_latest @m_since_latest
@ -99,7 +115,8 @@ only exception is @ref MaterialAttribute::LayerName which is
When this enum is used in @ref MaterialAttributeData constructors, the data are When this enum is used in @ref MaterialAttributeData constructors, the data are
additionally checked for type compatibility. Other than that, there is no additionally checked for type compatibility. Other than that, there is no
difference to the string variants. difference to the string variants.
@see @ref MaterialAttributeData, @ref MaterialData @see @ref MaterialAttributeData, @ref MaterialData,
@ref materialAttributeName()
*/ */
enum class MaterialAttribute: UnsignedInt { enum class MaterialAttribute: UnsignedInt {
/* Zero used for an invalid value */ /* Zero used for an invalid value */
@ -1052,6 +1069,22 @@ enum class MaterialAttribute: UnsignedInt {
TextureLayer, TextureLayer,
}; };
namespace Implementation {
/* Compared to materialLayerName() below returns an empty string for
invalid layers, used internally to provide better assertion messages */
MAGNUM_TRADE_EXPORT Containers::StringView materialAttributeNameInternal(MaterialAttribute attribute);
}
/**
@brief Material layer name as a string
@m_since_latest
Expects that @p attribute is a valid @ref MaterialAttribute value. The returned
view has both @relativeref{Corrade,Containers::StringViewFlag::Global} and
@relativeref{Corrade::Containers::StringViewFlag,NullTerminated} set.
*/
MAGNUM_TRADE_EXPORT Containers::StringView materialAttributeName(MaterialAttribute attribute);
/** /**
@debugoperatorenum{MaterialAttribute} @debugoperatorenum{MaterialAttribute}
@m_since_latest @m_since_latest
@ -2075,6 +2108,7 @@ class MAGNUM_TRADE_EXPORT MaterialData {
* @cpp 0 @ce) to avoid confsing base material with a layer. If you * @cpp 0 @ce) to avoid confsing base material with a layer. If you
* want to create a material consisting of just a layer, use @cpp 0 @ce * want to create a material consisting of just a layer, use @cpp 0 @ce
* for the first layer offset in the constructor. * for the first layer offset in the constructor.
* @see @ref materialLayerName()
*/ */
Containers::StringView layerName(UnsignedInt layer) const; Containers::StringView layerName(UnsignedInt layer) const;
@ -2372,7 +2406,7 @@ class MAGNUM_TRADE_EXPORT MaterialData {
* and the @p id is expected to be smaller than @ref attributeCount(UnsignedInt) const * and the @p id is expected to be smaller than @ref attributeCount(UnsignedInt) const
* in that layer. The returned view always has * in that layer. The returned view always has
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated} set. * @relativeref{Corrade,Containers::StringViewFlag::NullTerminated} set.
* @see @ref attributeType() * @see @ref attributeType(), @ref materialAttributeName()
*/ */
Containers::StringView attributeName(UnsignedInt layer, UnsignedInt id) const; Containers::StringView attributeName(UnsignedInt layer, UnsignedInt id) const;
@ -2381,7 +2415,7 @@ class MAGNUM_TRADE_EXPORT MaterialData {
* *
* The @p layer is expected to exist and the @p id is expected to be smaller than @ref attributeCount(UnsignedInt) const * The @p layer is expected to exist and the @p id is expected to be smaller than @ref attributeCount(UnsignedInt) const
* in that layer. * in that layer.
* @see @ref hasLayer() * @see @ref hasLayer(), @ref materialAttributeName()
*/ */
Containers::StringView attributeName(Containers::StringView layer, UnsignedInt id) const; Containers::StringView attributeName(Containers::StringView layer, UnsignedInt id) const;
Containers::StringView attributeName(MaterialLayer layer, UnsignedInt id) const; /**< @overload */ Containers::StringView attributeName(MaterialLayer layer, UnsignedInt id) const; /**< @overload */
@ -2391,6 +2425,7 @@ class MAGNUM_TRADE_EXPORT MaterialData {
* *
* Equivalent to calling @ref attributeName(UnsignedInt, UnsignedInt) const * Equivalent to calling @ref attributeName(UnsignedInt, UnsignedInt) const
* with @p layer set to @cpp 0 @ce. * with @p layer set to @cpp 0 @ce.
* @see @ref materialAttributeName()
*/ */
Containers::StringView attributeName(UnsignedInt id) const { Containers::StringView attributeName(UnsignedInt id) const {
return attributeName(0, id); return attributeName(0, id);
@ -3011,8 +3046,6 @@ class MAGNUM_TRADE_EXPORT MaterialData {
implementations. */ implementations. */
friend AbstractImporter; friend AbstractImporter;
static Containers::StringView layerString(MaterialLayer name);
static Containers::StringView attributeString(MaterialAttribute name);
/* Internal helpers that don't assert, unlike layerId() / attributeId() */ /* Internal helpers that don't assert, unlike layerId() / attributeId() */
UnsignedInt findLayerIdInternal(Containers::StringView layer) const; UnsignedInt findLayerIdInternal(Containers::StringView layer) const;
UnsignedInt layerOffset(UnsignedInt layer) const { UnsignedInt layerOffset(UnsignedInt layer) const {
@ -3209,13 +3242,13 @@ template<class T> typename std::conditional<std::is_same<T, Containers::MutableS
} }
template<class T> T MaterialData::attribute(const UnsignedInt layer, const MaterialAttribute name) const { template<class T> T MaterialData::attribute(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << name, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << name, {});
return attribute<T>(layer, string); return attribute<T>(layer, string);
} }
template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const UnsignedInt layer, const MaterialAttribute name) { template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const UnsignedInt layer, const MaterialAttribute name) {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << name, *reinterpret_cast<T*>(this)); CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << name, *reinterpret_cast<T*>(this));
return mutableAttribute<T>(layer, string); return mutableAttribute<T>(layer, string);
} }
@ -3259,49 +3292,49 @@ template<class T> typename std::conditional<std::is_same<T, Containers::MutableS
} }
template<class T> T MaterialData::attribute(const Containers::StringView layer, const MaterialAttribute name) const { template<class T> T MaterialData::attribute(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << name, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << name, {});
return attribute<T>(layer, string); return attribute<T>(layer, string);
} }
template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const Containers::StringView layer, const MaterialAttribute name) { template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const Containers::StringView layer, const MaterialAttribute name) {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << name, *reinterpret_cast<T*>(this)); CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << name, *reinterpret_cast<T*>(this));
return mutableAttribute<T>(layer, string); return mutableAttribute<T>(layer, string);
} }
template<class T> T MaterialData::attribute(const MaterialLayer layer, const UnsignedInt id) const { template<class T> T MaterialData::attribute(const MaterialLayer layer, const UnsignedInt id) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << layer, {});
return attribute<T>(string, id); return attribute<T>(string, id);
} }
template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const MaterialLayer layer, const UnsignedInt id) { template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const MaterialLayer layer, const UnsignedInt id) {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << layer, *reinterpret_cast<T*>(this)); CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << layer, *reinterpret_cast<T*>(this));
return mutableAttribute<T>(string, id); return mutableAttribute<T>(string, id);
} }
template<class T> T MaterialData::attribute(const MaterialLayer layer, const Containers::StringView name) const { template<class T> T MaterialData::attribute(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << layer, {});
return attribute<T>(string, name); return attribute<T>(string, name);
} }
template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const MaterialLayer layer, const Containers::StringView name) { template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const MaterialLayer layer, const Containers::StringView name) {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << layer, *reinterpret_cast<T*>(this)); CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << layer, *reinterpret_cast<T*>(this));
return mutableAttribute<T>(string, name); return mutableAttribute<T>(string, name);
} }
template<class T> T MaterialData::attribute(const MaterialLayer layer, const MaterialAttribute name) const { template<class T> T MaterialData::attribute(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attribute(): invalid name" << layer, {});
return attribute<T>(string, name); return attribute<T>(string, name);
} }
template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const MaterialLayer layer, const MaterialAttribute name) { template<class T> typename std::conditional<std::is_same<T, Containers::MutableStringView>::value, Containers::MutableStringView, T&>::type MaterialData::mutableAttribute(const MaterialLayer layer, const MaterialAttribute name) {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << layer, *reinterpret_cast<T*>(this)); CORRADE_ASSERT(string.data(), "Trade::MaterialData::mutableAttribute(): invalid name" << layer, *reinterpret_cast<T*>(this));
return mutableAttribute<T>(string, name); return mutableAttribute<T>(string, name);
} }
@ -3315,7 +3348,7 @@ template<class T> Containers::Optional<T> MaterialData::tryAttribute(const Unsig
} }
template<class T> Containers::Optional<T> MaterialData::tryAttribute(const UnsignedInt layer, const MaterialAttribute name) const { template<class T> Containers::Optional<T> MaterialData::tryAttribute(const UnsignedInt layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << name, {});
return tryAttribute<T>(layer, string); return tryAttribute<T>(layer, string);
} }
@ -3328,19 +3361,19 @@ template<class T> Containers::Optional<T> MaterialData::tryAttribute(const Conta
} }
template<class T> Containers::Optional<T> MaterialData::tryAttribute(const Containers::StringView layer, const MaterialAttribute name) const { template<class T> Containers::Optional<T> MaterialData::tryAttribute(const Containers::StringView layer, const MaterialAttribute name) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << name, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << name, {});
return tryAttribute<T>(layer, string); return tryAttribute<T>(layer, string);
} }
template<class T> Containers::Optional<T> MaterialData::tryAttribute(const MaterialLayer layer, const Containers::StringView name) const { template<class T> Containers::Optional<T> MaterialData::tryAttribute(const MaterialLayer layer, const Containers::StringView name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << layer, {});
return tryAttribute<T>(string, name); return tryAttribute<T>(string, name);
} }
template<class T> Containers::Optional<T> MaterialData::tryAttribute(const MaterialLayer layer, const MaterialAttribute name) const { template<class T> Containers::Optional<T> MaterialData::tryAttribute(const MaterialLayer layer, const MaterialAttribute name) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::tryAttribute(): invalid name" << layer, {});
return tryAttribute<T>(string, name); return tryAttribute<T>(string, name);
} }
@ -3354,7 +3387,7 @@ template<class T> T MaterialData::attributeOr(const UnsignedInt layer, const Con
} }
template<class T> T MaterialData::attributeOr(const UnsignedInt layer, const MaterialAttribute name, const T& defaultValue) const { template<class T> T MaterialData::attributeOr(const UnsignedInt layer, const MaterialAttribute name, const T& defaultValue) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << name, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << name, {});
return attributeOr<T>(layer, string, defaultValue); return attributeOr<T>(layer, string, defaultValue);
} }
@ -3367,19 +3400,19 @@ template<class T> T MaterialData::attributeOr(const Containers::StringView layer
} }
template<class T> T MaterialData::attributeOr(const Containers::StringView layer, const MaterialAttribute name, const T& defaultValue) const { template<class T> T MaterialData::attributeOr(const Containers::StringView layer, const MaterialAttribute name, const T& defaultValue) const {
const Containers::StringView string = attributeString(name); const Containers::StringView string = Implementation::materialAttributeNameInternal(name);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << name, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << name, {});
return attributeOr<T>(layer, string, defaultValue); return attributeOr<T>(layer, string, defaultValue);
} }
template<class T> T MaterialData::attributeOr(const MaterialLayer layer, const Containers::StringView name, const T& defaultValue) const { template<class T> T MaterialData::attributeOr(const MaterialLayer layer, const Containers::StringView name, const T& defaultValue) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << layer, {});
return attributeOr<T>(string, name, defaultValue); return attributeOr<T>(string, name, defaultValue);
} }
template<class T> T MaterialData::attributeOr(const MaterialLayer layer, const MaterialAttribute name, const T& defaultValue) const { template<class T> T MaterialData::attributeOr(const MaterialLayer layer, const MaterialAttribute name, const T& defaultValue) const {
const Containers::StringView string = layerString(layer); const Containers::StringView string = Implementation::materialLayerNameInternal(layer);
CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << layer, {}); CORRADE_ASSERT(string.data(), "Trade::MaterialData::attributeOr(): invalid name" << layer, {});
return attributeOr<T>(string, name, defaultValue); return attributeOr<T>(string, name, defaultValue);
} }

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

@ -46,6 +46,10 @@ namespace Magnum { namespace Trade { namespace Test { namespace {
struct MaterialDataTest: TestSuite::Tester { struct MaterialDataTest: TestSuite::Tester {
explicit MaterialDataTest(); explicit MaterialDataTest();
void layerName();
void layerNameInvalid();
void attributeName();
void attributeNameInvalid();
void textureSwizzleComponentCount(); void textureSwizzleComponentCount();
void attributeTypeSize(); void attributeTypeSize();
void attributeTypeSizeInvalid(); void attributeTypeSizeInvalid();
@ -172,10 +176,16 @@ struct MaterialDataTest: TestSuite::Tester {
}; };
MaterialDataTest::MaterialDataTest() { MaterialDataTest::MaterialDataTest() {
addTests({&MaterialDataTest::textureSwizzleComponentCount, addTests({&MaterialDataTest::layerName,
&MaterialDataTest::layerNameInvalid,
&MaterialDataTest::attributeName,
&MaterialDataTest::attributeNameInvalid,
&MaterialDataTest::textureSwizzleComponentCount,
&MaterialDataTest::attributeTypeSize, &MaterialDataTest::attributeTypeSize,
&MaterialDataTest::attributeTypeSizeInvalid, &MaterialDataTest::attributeTypeSizeInvalid,
&MaterialDataTest::attributeMap, &MaterialDataTest::attributeMap,
&MaterialDataTest::layerMap, &MaterialDataTest::layerMap,
@ -325,6 +335,38 @@ MaterialDataTest::MaterialDataTest() {
using namespace Containers::Literals; using namespace Containers::Literals;
using namespace Math::Literals; using namespace Math::Literals;
void MaterialDataTest::layerName() {
CORRADE_COMPARE(materialLayerName(MaterialLayer::ClearCoat), "ClearCoat");
}
void MaterialDataTest::layerNameInvalid() {
CORRADE_SKIP_IF_NO_ASSERT();
std::ostringstream out;
Error redirectError{&out};
materialLayerName(MaterialLayer(0x0));
materialLayerName(MaterialLayer(0xdeadbeef));
CORRADE_COMPARE(out.str(),
"Trade::materialLayerName(): invalid layer Trade::MaterialLayer(0x0)\n"
"Trade::materialLayerName(): invalid layer Trade::MaterialLayer(0xdeadbeef)\n");
}
void MaterialDataTest::attributeName() {
CORRADE_COMPARE(materialAttributeName(MaterialAttribute::BaseColorTextureMatrix), "BaseColorTextureMatrix");
}
void MaterialDataTest::attributeNameInvalid() {
CORRADE_SKIP_IF_NO_ASSERT();
std::ostringstream out;
Error redirectError{&out};
materialAttributeName(MaterialAttribute(0x0));
materialAttributeName(MaterialAttribute(0xdeadbeef));
CORRADE_COMPARE(out.str(),
"Trade::materialAttributeName(): invalid attribute Trade::MaterialAttribute(0x0)\n"
"Trade::materialAttributeName(): invalid attribute Trade::MaterialAttribute(0xdeadbeef)\n");
}
void MaterialDataTest::textureSwizzleComponentCount() { void MaterialDataTest::textureSwizzleComponentCount() {
CORRADE_COMPARE(materialTextureSwizzleComponentCount(MaterialTextureSwizzle::B), 1); CORRADE_COMPARE(materialTextureSwizzleComponentCount(MaterialTextureSwizzle::B), 1);
CORRADE_COMPARE(materialTextureSwizzleComponentCount(MaterialTextureSwizzle::RG), 2); CORRADE_COMPARE(materialTextureSwizzleComponentCount(MaterialTextureSwizzle::RG), 2);

Loading…
Cancel
Save