From e1619e5aabd8ce811f1684c074ed0b93cdffdae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 24 Aug 2022 12:57:17 +0200 Subject: [PATCH] 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. --- src/Magnum/Trade/MaterialData.cpp | 133 ++++++++++++--------- src/Magnum/Trade/MaterialData.h | 81 +++++++++---- src/Magnum/Trade/Test/MaterialDataTest.cpp | 44 ++++++- 3 files changed, 175 insertions(+), 83 deletions(-) diff --git a/src/Magnum/Trade/MaterialData.cpp b/src/Magnum/Trade/MaterialData.cpp index 555984052..d9f2eecab 100644 --- a/src/Magnum/Trade/MaterialData.cpp +++ b/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) { return (UnsignedInt(swizzle) & 0xff000000u ? 1 : 0) + (UnsignedInt(swizzle) & 0x00ff0000u ? 1 : 0) + @@ -298,22 +331,6 @@ MaterialData::~MaterialData() = 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 { for(std::size_t i = 1; i < _layerOffsets.size(); ++i) { 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 { - const Containers::StringView string = layerString(layer); + const Containers::StringView string = Implementation::materialLayerNameInternal(layer); CORRADE_ASSERT(string, "Trade::MaterialData::hasLayer(): invalid name" << layer, {}); return hasLayer(string); } @@ -340,7 +357,7 @@ Containers::Optional MaterialData::findLayerId(const Containers::St } Containers::Optional 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, {}); return findLayerId(string); } @@ -353,7 +370,7 @@ UnsignedInt MaterialData::layerId(const Containers::StringView 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, {}); return layerId(string); } @@ -381,7 +398,7 @@ Float MaterialData::layerFactor(const Containers::StringView 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, {}); return layerFactor(string); } @@ -404,7 +421,7 @@ UnsignedInt MaterialData::layerFactorTexture(const Containers::StringView layer) } 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, {}); return layerFactorTexture(string); } @@ -431,7 +448,7 @@ MaterialTextureSwizzle MaterialData::layerFactorTextureSwizzle(const Containers: } 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, {}); return layerFactorTextureSwizzle(string); } @@ -464,7 +481,7 @@ Matrix3 MaterialData::layerFactorTextureMatrix(const Containers::StringView laye } 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, {}); return layerFactorTextureMatrix(string); } @@ -497,7 +514,7 @@ UnsignedInt MaterialData::layerFactorTextureCoordinates(const Containers::String } 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, {}); return layerFactorTextureCoordinates(string); } @@ -530,7 +547,7 @@ UnsignedInt MaterialData::layerFactorTextureLayer(const Containers::StringView l } 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, {}); return layerFactorTextureLayer(string); } @@ -551,7 +568,7 @@ UnsignedInt MaterialData::attributeCount(const Containers::StringView layer) con } 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, {}); 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 { - const Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << name, {}); 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 { - const Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::hasAttribute(): invalid name" << name, {}); return hasAttribute(layer, string); } 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, {}); return hasAttribute(string, name); } 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, {}); return hasAttribute(string, name); } @@ -613,7 +630,7 @@ Containers::Optional MaterialData::findAttributeId(const UnsignedIn } Containers::Optional 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, {}); return findAttributeId(layer, string); } @@ -627,19 +644,19 @@ Containers::Optional MaterialData::findAttributeId(const Containers } Containers::Optional 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, {}); return findAttributeId(layer, string); } Containers::Optional 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, {}); return findAttributeId(string, name); } Containers::Optional 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, {}); 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 { - const Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << name, {}); 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 { - const Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::attributeId(): invalid name" << name, {}); return attributeId(layer, string); } 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, {}); return attributeId(string, name); } 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, {}); 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 { - const Containers::StringView string = layerString(layer); + const Containers::StringView string = Implementation::materialLayerNameInternal(layer); CORRADE_ASSERT(string, "Trade::MaterialData::attributeName(): invalid name" << layer, {}); 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 { - const Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << name, {}); 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 { - const Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::attributeType(): invalid name" << name, {}); return attributeType(layer, string); } 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, {}); return attributeType(string, id); } 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, {}); return attributeType(string, name); } 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, {}); 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 Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << name, {}); return attribute(layer, string); } 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, {}); 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 Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::attribute(): invalid name" << name, {}); return attribute(layer, string); } 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, {}); return mutableAttribute(layer, string); } 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, {}); return attribute(string, 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, {}); return mutableAttribute(string, id); } 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, {}); return attribute(string, 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, {}); return mutableAttribute(string, name); } 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, {}); return attribute(string, 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, {}); 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 Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << name, {}); 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 Containers::StringView string = attributeString(name); + const Containers::StringView string = Implementation::materialAttributeNameInternal(name); CORRADE_ASSERT(string, "Trade::MaterialData::tryAttribute(): invalid name" << name, {}); return tryAttribute(layer, string); } 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, {}); return tryAttribute(string, name); } 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, {}); return tryAttribute(string, name); } diff --git a/src/Magnum/Trade/MaterialData.h b/src/Magnum/Trade/MaterialData.h index f6d79db0a..d1c00f021 100644 --- a/src/Magnum/Trade/MaterialData.h +++ b/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::LayerFactorTextureCoordinates attributes in addition to 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 { /* Zero used for an invalid value */ @@ -80,6 +81,21 @@ enum class MaterialLayer: UnsignedInt { 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} @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 additionally checked for type compatibility. Other than that, there is no difference to the string variants. -@see @ref MaterialAttributeData, @ref MaterialData +@see @ref MaterialAttributeData, @ref MaterialData, + @ref materialAttributeName() */ enum class MaterialAttribute: UnsignedInt { /* Zero used for an invalid value */ @@ -1052,6 +1069,22 @@ enum class MaterialAttribute: UnsignedInt { 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} @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 * want to create a material consisting of just a layer, use @cpp 0 @ce * for the first layer offset in the constructor. + * @see @ref materialLayerName() */ 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 * in that layer. The returned view always has * @relativeref{Corrade,Containers::StringViewFlag::NullTerminated} set. - * @see @ref attributeType() + * @see @ref attributeType(), @ref materialAttributeName() */ 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 * in that layer. - * @see @ref hasLayer() + * @see @ref hasLayer(), @ref materialAttributeName() */ Containers::StringView attributeName(Containers::StringView layer, UnsignedInt id) const; 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 * with @p layer set to @cpp 0 @ce. + * @see @ref materialAttributeName() */ Containers::StringView attributeName(UnsignedInt id) const { return attributeName(0, id); @@ -3011,8 +3046,6 @@ class MAGNUM_TRADE_EXPORT MaterialData { implementations. */ friend AbstractImporter; - static Containers::StringView layerString(MaterialLayer name); - static Containers::StringView attributeString(MaterialAttribute name); /* Internal helpers that don't assert, unlike layerId() / attributeId() */ UnsignedInt findLayerIdInternal(Containers::StringView layer) const; UnsignedInt layerOffset(UnsignedInt layer) const { @@ -3209,13 +3242,13 @@ template typename std::conditional 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, {}); return attribute(layer, string); } template typename std::conditional::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(this)); return mutableAttribute(layer, string); } @@ -3259,49 +3292,49 @@ template typename std::conditional 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, {}); return attribute(layer, string); } template typename std::conditional::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(this)); return mutableAttribute(layer, string); } template 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, {}); return attribute(string, id); } template typename std::conditional::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(this)); return mutableAttribute(string, id); } template 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, {}); return attribute(string, name); } template typename std::conditional::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(this)); return mutableAttribute(string, name); } template 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, {}); return attribute(string, name); } template typename std::conditional::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(this)); return mutableAttribute(string, name); } @@ -3315,7 +3348,7 @@ template Containers::Optional MaterialData::tryAttribute(const Unsig } template Containers::Optional 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, {}); return tryAttribute(layer, string); } @@ -3328,19 +3361,19 @@ template Containers::Optional MaterialData::tryAttribute(const Conta } template Containers::Optional 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, {}); return tryAttribute(layer, string); } template Containers::Optional 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, {}); return tryAttribute(string, name); } template Containers::Optional 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, {}); return tryAttribute(string, name); } @@ -3354,7 +3387,7 @@ template T MaterialData::attributeOr(const UnsignedInt layer, const Con } template 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, {}); return attributeOr(layer, string, defaultValue); } @@ -3367,19 +3400,19 @@ template T MaterialData::attributeOr(const Containers::StringView layer } template 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, {}); return attributeOr(layer, string, defaultValue); } template 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, {}); return attributeOr(string, name, defaultValue); } template 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, {}); return attributeOr(string, name, defaultValue); } diff --git a/src/Magnum/Trade/Test/MaterialDataTest.cpp b/src/Magnum/Trade/Test/MaterialDataTest.cpp index 07133c214..d071911ef 100644 --- a/src/Magnum/Trade/Test/MaterialDataTest.cpp +++ b/src/Magnum/Trade/Test/MaterialDataTest.cpp @@ -46,6 +46,10 @@ namespace Magnum { namespace Trade { namespace Test { namespace { struct MaterialDataTest: TestSuite::Tester { explicit MaterialDataTest(); + void layerName(); + void layerNameInvalid(); + void attributeName(); + void attributeNameInvalid(); void textureSwizzleComponentCount(); void attributeTypeSize(); void attributeTypeSizeInvalid(); @@ -172,10 +176,16 @@ struct MaterialDataTest: TestSuite::Tester { }; MaterialDataTest::MaterialDataTest() { - addTests({&MaterialDataTest::textureSwizzleComponentCount, + addTests({&MaterialDataTest::layerName, + &MaterialDataTest::layerNameInvalid, + &MaterialDataTest::attributeName, + &MaterialDataTest::attributeNameInvalid, + + &MaterialDataTest::textureSwizzleComponentCount, &MaterialDataTest::attributeTypeSize, &MaterialDataTest::attributeTypeSizeInvalid, + &MaterialDataTest::attributeMap, &MaterialDataTest::layerMap, @@ -325,6 +335,38 @@ MaterialDataTest::MaterialDataTest() { using namespace Containers::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() { CORRADE_COMPARE(materialTextureSwizzleComponentCount(MaterialTextureSwizzle::B), 1); CORRADE_COMPARE(materialTextureSwizzleComponentCount(MaterialTextureSwizzle::RG), 2);