diff --git a/CMakeLists.txt b/CMakeLists.txt index 8091f6011..de0b93117 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ option(FASTGLTF_ENABLE_EXAMPLES "Enables example targets for fastgltf" OFF) option(FASTGLTF_ENABLE_DOCS "Enables the configuration of targets that build/generate documentation" OFF) option(FASTGLTF_ENABLE_GLTF_RS "Enables the benchmark usage of gltf-rs" OFF) option(FASTGLTF_ENABLE_DEPRECATED_EXT "Enables support for deprecated extensions" OFF) +option(FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL "Disables the memory allocation algorithm based on polymorphic resources" OFF) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/add_source_directory.cmake) include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler_flags.cmake) @@ -105,6 +106,7 @@ endif() target_compile_definitions(fastgltf PUBLIC "FASTGLTF_USE_CUSTOM_SMALLVECTOR=$") target_compile_definitions(fastgltf PUBLIC "FASTGLTF_ENABLE_DEPRECATED_EXT=$") +target_compile_definitions(fastgltf PUBLIC "FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL=$") if (ANDROID) target_link_libraries(fastgltf PRIVATE android) diff --git a/include/fastgltf/parser.hpp b/include/fastgltf/parser.hpp index 2fe6c091c..e331858a1 100644 --- a/include/fastgltf/parser.hpp +++ b/include/fastgltf/parser.hpp @@ -355,6 +355,7 @@ namespace fastgltf { return ""; } +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL class ChunkMemoryResource : public std::pmr::memory_resource { /** * The default size of the individual blocks we allocate. @@ -415,6 +416,7 @@ namespace fastgltf { return this == std::addressof(other); } }; +#endif /** * A type that stores an error together with an expected value. @@ -649,7 +651,9 @@ namespace fastgltf { ParserInternalConfig config = {}; DataSource glbBuffer; +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL std::shared_ptr resourceAllocator; +#endif std::filesystem::path directory; Options options; diff --git a/include/fastgltf/types.hpp b/include/fastgltf/types.hpp index 3ad956f7e..36270490f 100644 --- a/include/fastgltf/types.hpp +++ b/include/fastgltf/types.hpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -40,6 +39,33 @@ // Utils header already includes some headers, which we'll try and avoid including twice. #include "util.hpp" +#ifndef FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL +#define FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL 0 +#endif + +#if __has_include() +#define FASTGLTF_MISSING_MEMORY_RESOURCE 0 +#else +#define FASTGLTF_MISSING_MEMORY_RESOURCE 1 +#define FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL 1 +#endif + +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL +#include +#endif + +#if FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL +#define FASTGLTF_STD_PMR_NS ::std +#define FASTGLTF_FG_PMR_NS ::fastgltf + +#define FASTGLTF_CONSTRUCT_PMR_RESOURCE(type, memoryResource, ...) type(__VA_ARGS__) +#else +#define FASTGLTF_STD_PMR_NS ::std::pmr +#define FASTGLTF_FG_PMR_NS ::fastgltf::pmr + +#define FASTGLTF_CONSTRUCT_PMR_RESOURCE(type, memoryResource, ...) type(__VA_ARGS__, memoryResource) +#endif + #if FASTGLTF_CPP_20 #include #endif @@ -688,10 +714,12 @@ namespace fastgltf { } }; +#if !FASTGLTF_MISSING_MEMORY_RESOURCE namespace pmr { template using SmallVector = SmallVector>; } // namespace pmr +#endif #ifndef FASTGLTF_USE_CUSTOM_SMALLVECTOR #define FASTGLTF_USE_CUSTOM_SMALLVECTOR 0 @@ -705,6 +733,7 @@ namespace fastgltf { using MaybeSmallVector = std::vector; #endif +#if !FASTGLTF_MISSING_MEMORY_RESOURCE namespace pmr { #if FASTGLTF_USE_CUSTOM_SMALLVECTOR template @@ -714,6 +743,7 @@ namespace fastgltf { using MaybeSmallVector = std::pmr::vector; #endif } // namespace pmr +#endif template struct OptionalFlagValue { @@ -1066,7 +1096,7 @@ namespace fastgltf { * also decodes any percent-encoded characters. */ class URI { - std::pmr::string uri; + std::string uri; URIView view; void readjustViews(const URIView& other); @@ -1088,7 +1118,7 @@ namespace fastgltf { operator URIView() const noexcept; - static void decodePercents(std::pmr::string& x) noexcept; + static void decodePercents(std::string& x) noexcept; [[nodiscard]] auto string() const noexcept -> std::string_view; @@ -1224,16 +1254,16 @@ namespace fastgltf { }; struct Animation { - pmr::MaybeSmallVector channels; - pmr::MaybeSmallVector samplers; + FASTGLTF_FG_PMR_NS::MaybeSmallVector channels; + FASTGLTF_FG_PMR_NS::MaybeSmallVector samplers; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct AssetInfo { - std::pmr::string gltfVersion; - std::pmr::string copyright; - std::pmr::string generator; + FASTGLTF_STD_PMR_NS::string gltfVersion; + FASTGLTF_STD_PMR_NS::string copyright; + FASTGLTF_STD_PMR_NS::string generator; }; struct Camera { @@ -1256,15 +1286,15 @@ namespace fastgltf { * and/or std::get_if to figure out which camera type is being used. */ std::variant camera; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Skin { - pmr::MaybeSmallVector joints; + FASTGLTF_FG_PMR_NS::MaybeSmallVector joints; Optional skeleton; Optional inverseBindMatrices; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Sampler { @@ -1273,13 +1303,13 @@ namespace fastgltf { Wrap wrapS; Wrap wrapT; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Scene { - pmr::MaybeSmallVector nodeIndices; + FASTGLTF_FG_PMR_NS::MaybeSmallVector nodeIndices; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Node { @@ -1292,8 +1322,8 @@ namespace fastgltf { */ Optional lightIndex; - pmr::MaybeSmallVector children; - pmr::MaybeSmallVector weights; + FASTGLTF_FG_PMR_NS::MaybeSmallVector children; + FASTGLTF_FG_PMR_NS::MaybeSmallVector weights; struct TRS { std::array translation; @@ -1312,9 +1342,9 @@ namespace fastgltf { /** * Only ever non-empty when EXT_mesh_gpu_instancing is enabled and used by the asset. */ - std::pmr::vector> instancingAttributes; + FASTGLTF_STD_PMR_NS::vector> instancingAttributes; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; [[nodiscard]] auto findInstancingAttribute(std::string_view attributeName) noexcept { for (auto it = instancingAttributes.begin(); it != instancingAttributes.end(); ++it) { @@ -1334,14 +1364,14 @@ namespace fastgltf { }; struct Primitive { - using attribute_type = std::pair; + using attribute_type = std::pair; // Instead of a map, we have a list of attributes here. Each pair contains // the name of the attribute and the corresponding accessor index. - pmr::SmallVector attributes; + FASTGLTF_FG_PMR_NS::SmallVector attributes; PrimitiveType type; - std::pmr::vector> targets; + FASTGLTF_STD_PMR_NS::vector> targets; Optional indicesAccessor; Optional materialIndex; @@ -1382,10 +1412,10 @@ namespace fastgltf { }; struct Mesh { - pmr::MaybeSmallVector primitives; - pmr::MaybeSmallVector weights; + FASTGLTF_FG_PMR_NS::MaybeSmallVector primitives; + FASTGLTF_FG_PMR_NS::MaybeSmallVector weights; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; /** @@ -1606,7 +1636,7 @@ namespace fastgltf { */ bool unlit; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Texture { @@ -1637,13 +1667,13 @@ namespace fastgltf { */ Optional webpImageIndex; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Image { DataSource data; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct SparseAccessor { @@ -1662,15 +1692,15 @@ namespace fastgltf { ComponentType componentType; bool normalized; - std::variant, std::pmr::vector> max; - std::variant, std::pmr::vector> min; + std::variant, FASTGLTF_STD_PMR_NS::vector> max; + std::variant, FASTGLTF_STD_PMR_NS::vector> min; // Could have no value for sparse morph targets Optional bufferViewIndex; Optional sparse; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct CompressedBufferView { @@ -1697,7 +1727,7 @@ namespace fastgltf { */ std::unique_ptr meshoptCompression; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Buffer { @@ -1705,7 +1735,7 @@ namespace fastgltf { DataSource data; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; struct Light { @@ -1721,7 +1751,7 @@ namespace fastgltf { Optional innerConeAngle; Optional outerConeAngle; - std::pmr::string name; + FASTGLTF_STD_PMR_NS::string name; }; class ChunkMemoryResource; @@ -1730,17 +1760,19 @@ namespace fastgltf { class Asset { friend class Parser; +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL // This has to be first in this struct so that it gets destroyed last, leaving all allocations // alive until the end. std::shared_ptr memoryResource; +#endif public: /** * This will only ever have no value if #Options::DontRequireValidAssetMember was specified. */ Optional assetInfo; - std::pmr::vector extensionsUsed; - std::pmr::vector extensionsRequired; + FASTGLTF_STD_PMR_NS::vector extensionsUsed; + FASTGLTF_STD_PMR_NS::vector extensionsRequired; Optional defaultScene; std::vector accessors; @@ -1764,7 +1796,9 @@ namespace fastgltf { explicit Asset() = default; explicit Asset(const Asset& other) = delete; Asset(Asset&& other) noexcept : +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL memoryResource(std::move(other.memoryResource)), +#endif assetInfo(std::move(other.assetInfo)), extensionsUsed(std::move(other.extensionsUsed)), extensionsRequired(std::move(other.extensionsRequired)), @@ -1787,7 +1821,9 @@ namespace fastgltf { Asset& operator=(const Asset& other) = delete; Asset& operator=(Asset&& other) noexcept { +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL memoryResource = std::move(other.memoryResource); +#endif assetInfo = std::move(other.assetInfo); extensionsUsed = std::move(other.extensionsUsed); extensionsRequired = std::move(other.extensionsRequired); diff --git a/src/fastgltf.cpp b/src/fastgltf.cpp index 0d0d8f922..10f9d4a78 100644 --- a/src/fastgltf.cpp +++ b/src/fastgltf.cpp @@ -457,12 +457,12 @@ bool fg::URIView::isDataUri() const noexcept { fg::URI::URI() noexcept = default; -fg::URI::URI(std::string uri) noexcept : uri(uri) { +fg::URI::URI(std::string uri) noexcept : uri(std::move(uri)) { decodePercents(this->uri); view = this->uri; // Also parses. } -fg::URI::URI(std::pmr::string uri) noexcept : uri(std::move(uri)) { +fg::URI::URI(std::pmr::string uri) noexcept : uri(uri) { decodePercents(this->uri); view = this->uri; // Also parses. } @@ -542,7 +542,7 @@ void fg::URI::readjustViews(const URIView& other) { view.view = uri; } -void fg::URI::decodePercents(std::pmr::string& x) noexcept { +void fg::URI::decodePercents(std::string& x) noexcept { for (auto it = x.begin(); it != x.end(); ++it) { if (*it == '%') { // Read the next two chars and store them. @@ -790,12 +790,12 @@ fg::Error fg::validate(const fastgltf::Asset& asset) { if (!std::holds_alternative(accessor.max)) { if ((accessor.componentType == ComponentType::Float || accessor.componentType == ComponentType::Double) - && !std::holds_alternative>(accessor.max)) + && !std::holds_alternative>(accessor.max)) return Error::InvalidGltf; } if (!std::holds_alternative(accessor.min)) { if ((accessor.componentType == ComponentType::Float || accessor.componentType == ComponentType::Double) - && !std::holds_alternative>(accessor.min)) + && !std::holds_alternative>(accessor.min)) return Error::InvalidGltf; } } @@ -1059,8 +1059,10 @@ fg::Expected fg::Parser::parse(simdjson::dom::object root, Category c Asset asset {}; +#if !FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL // Create a new chunk memory resource for each asset we parse. asset.memoryResource = resourceAllocator = std::make_shared(); +#endif if (!hasBit(options, Options::DontRequireValidAssetMember)) { dom::object assetInfo; @@ -1180,8 +1182,7 @@ fg::Expected fg::Parser::parse(simdjson::dom::object root, Category c for (auto usedValue : array) { std::string_view usedString; if (auto eError = usedValue.get_string().get(usedString); eError == SUCCESS) { - std::pmr::string string(usedString, resourceAllocator.get()); - asset.extensionsUsed.emplace_back(std::move(string)); + asset.extensionsUsed.emplace_back FASTGLTF_CONSTRUCT_PMR_RESOURCE(, resourceAllocator.get(), usedString); } else { error = Error::InvalidGltf; } @@ -1192,8 +1193,7 @@ fg::Expected fg::Parser::parse(simdjson::dom::object root, Category c for (auto requiredValue : array) { std::string_view requiredString; if (auto eError = requiredValue.get_string().get(requiredString); eError == SUCCESS) { - std::pmr::string string(requiredString, resourceAllocator.get()); - asset.extensionsRequired.emplace_back(std::move(string)); + asset.extensionsRequired.emplace_back FASTGLTF_CONSTRUCT_PMR_RESOURCE(, resourceAllocator.get(), requiredString); } else { error = Error::InvalidGltf; } @@ -1277,9 +1277,9 @@ fg::Error fg::Parser::parseAccessors(simdjson::dom::array& accessors, Asset& ass auto num = getNumComponents(accessor.type); if (accessor.componentType == ComponentType::Float || accessor.componentType == ComponentType::Double) { - variant = double_vec(num, resourceAllocator.get()); + variant = FASTGLTF_CONSTRUCT_PMR_RESOURCE(double_vec, resourceAllocator.get(), num); } else { - variant = int64_vec(num, resourceAllocator.get()); + variant = FASTGLTF_CONSTRUCT_PMR_RESOURCE(int64_vec, resourceAllocator.get(), num); } std::size_t idx = 0; @@ -1412,7 +1412,7 @@ fg::Error fg::Parser::parseAccessors(simdjson::dom::array& accessors, Asset& ass std::string_view name; if (accessorObject["name"].get_string().get(name) == SUCCESS) { - accessor.name = std::pmr::string(name, resourceAllocator.get()); + accessor.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(accessor.name), resourceAllocator.get(), name); } asset.accessors.emplace_back(std::move(accessor)); @@ -1438,7 +1438,7 @@ fg::Error fg::Parser::parseAnimations(simdjson::dom::array& animations, Asset& a return Error::InvalidGltf; } - animation.channels = decltype(animation.channels)(0, resourceAllocator.get()); + animation.channels = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(animation.channels), resourceAllocator.get(), 0); animation.channels.reserve(channels.size()); for (auto channelValue : channels) { dom::object channelObject; @@ -1489,7 +1489,7 @@ fg::Error fg::Parser::parseAnimations(simdjson::dom::array& animations, Asset& a return Error::InvalidGltf; } - animation.samplers = decltype(animation.samplers)(0, resourceAllocator.get()); + animation.samplers = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(animation.samplers), resourceAllocator.get(), 0); animation.samplers.reserve(samplers.size()); for (auto samplerValue : samplers) { dom::object samplerObject; @@ -1530,7 +1530,7 @@ fg::Error fg::Parser::parseAnimations(simdjson::dom::array& animations, Asset& a std::string_view name; if (animationObject["name"].get_string().get(name) == SUCCESS) { - animation.name = std::pmr::string(name, resourceAllocator.get()); + animation.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(animation.name), resourceAllocator.get(), name); } asset.animations.emplace_back(std::move(animation)); @@ -1601,7 +1601,7 @@ fg::Error fg::Parser::parseBuffers(simdjson::dom::array& buffers, Asset& asset) std::string_view name; if (bufferObject["name"].get_string().get(name) == SUCCESS) { - buffer.name = std::pmr::string(name, resourceAllocator.get()); + buffer.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(buffer.name), resourceAllocator.get(), name); } ++bufferIndex; @@ -1655,7 +1655,7 @@ fg::Error fg::Parser::parseBufferViews(simdjson::dom::array& bufferViews, Asset& std::string_view string; if (auto error = bufferViewObject["name"].get_string().get(string); error == SUCCESS) { - view.name = std::pmr::string(string, resourceAllocator.get()); + view.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(view.name), resourceAllocator.get(), string); } else if (error != NO_SUCH_FIELD) { return Error::InvalidJson; } @@ -1766,7 +1766,7 @@ fg::Error fg::Parser::parseCameras(simdjson::dom::array& cameras, Asset& asset) std::string_view name; if (cameraObject["name"].get_string().get(name) == SUCCESS) { - camera.name = std::pmr::string(name, resourceAllocator.get()); + camera.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(camera.name), resourceAllocator.get(), name); } std::string_view type; @@ -1956,7 +1956,7 @@ fg::Error fg::Parser::parseImages(simdjson::dom::array& images, Asset& asset) { // name is optional. std::string_view name; if (imageObject["name"].get_string().get(name) == SUCCESS) { - image.name = std::pmr::string(name, resourceAllocator.get()); + image.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(image.name), resourceAllocator.get(), name); } asset.images.emplace_back(std::move(image)); @@ -2058,7 +2058,7 @@ fg::Error fg::Parser::parseLights(simdjson::dom::array& lights, Asset& asset) { std::string_view name; if (lightObject["name"].get_string().get(name) == SUCCESS) { - light.name = std::pmr::string(name, resourceAllocator.get()); + light.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(light.name), resourceAllocator.get(), name); } asset.lights.emplace_back(std::move(light)); @@ -2191,7 +2191,7 @@ fg::Error fg::Parser::parseMaterials(simdjson::dom::array& materials, Asset& ass std::string_view name; if (materialObject["name"].get_string().get(name) == SUCCESS) { - material.name = std::pmr::string(name, resourceAllocator.get()); + material.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(material.name), resourceAllocator.get(), name); } material.unlit = false; @@ -2682,7 +2682,7 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) { } else if (meshError != Error::None) { return meshError; } else { - mesh.primitives = decltype(mesh.primitives)(0, resourceAllocator.get()); + mesh.primitives = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(mesh.primitives), resourceAllocator.get(), 0); mesh.primitives.reserve(array.size()); for (auto primitiveValue : array) { // Required fields: "attributes" @@ -2692,10 +2692,10 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) { return Error::InvalidGltf; } - auto parseAttributes = [this](dom::object& object, decltype(primitive.attributes)& attributes) -> auto { + auto parseAttributes = [](dom::object& object, decltype(primitive.attributes)& attributes) -> auto { // We iterate through the JSON object and write each key/pair value into the // attribute map. The keys are only validated in the validate() method. - attributes = decltype(primitive.attributes)(0, resourceAllocator.get()); + attributes = FASTGLTF_CONSTRUCT_PMR_RESOURCE(std::remove_reference_t, resourceAllocator.get(), 0); attributes.reserve(object.size()); for (const auto& field : object) { const auto key = field.key; @@ -2705,7 +2705,7 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) { return Error::InvalidGltf; } attributes.emplace_back( - std::make_pair(std::pmr::string(key, resourceAllocator.get()), static_cast(attributeIndex))); + std::make_pair(FASTGLTF_CONSTRUCT_PMR_RESOURCE(FASTGLTF_STD_PMR_NS::string, resourceAllocator.get(), key), static_cast(attributeIndex))); } return Error::None; }; @@ -2718,7 +2718,7 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) { dom::array targets; if (primitiveObject["targets"].get_array().get(targets) == SUCCESS) { - primitive.targets = decltype(primitive.targets)(0, resourceAllocator.get()); + primitive.targets = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(primitive.targets), resourceAllocator.get(), 0); primitive.targets.reserve(targets.size()); for (auto targetValue : targets) { if (targetValue.get_object().get(attributesObject) != SUCCESS) { @@ -2750,7 +2750,7 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) { } if (meshError = getJsonArray(meshObject, "weights", &array); meshError == Error::None) { - mesh.weights = decltype(mesh.weights)(0, resourceAllocator.get()); + mesh.weights = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(mesh.weights), resourceAllocator.get(), 0); mesh.weights.reserve(array.size()); for (auto weightValue : array) { double val; @@ -2765,7 +2765,7 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) { std::string_view name; if (meshObject["name"].get_string().get(name) == SUCCESS) { - mesh.name = std::pmr::string(name, resourceAllocator.get()); + mesh.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(mesh.name), resourceAllocator.get(), name); } asset.meshes.emplace_back(std::move(mesh)); @@ -2799,7 +2799,7 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) { dom::array array; auto childError = getJsonArray(nodeObject, "children", &array); if (childError == Error::None) { - node.children = decltype(node.children)(0, resourceAllocator.get()); + node.children = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(node.children), resourceAllocator.get(), 0); node.children.reserve(array.size()); for (auto childValue : array) { if (childValue.get_uint64().get(index) != SUCCESS) { @@ -2815,7 +2815,7 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) { auto weightsError = getJsonArray(nodeObject, "weights", &array); if (weightsError != Error::MissingField) { if (weightsError != Error::None) { - node.weights = decltype(node.weights)(0, resourceAllocator.get()); + node.weights = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(node.weights), resourceAllocator.get(), 0); node.weights.reserve(array.size()); for (auto weightValue : array) { double val; @@ -2915,7 +2915,7 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) { auto parseAttributes = [this](dom::object& object, decltype(node.instancingAttributes)& attributes) -> auto { // We iterate through the JSON object and write each key/pair value into the // attribute map. The keys are only validated in the validate() method. - attributes = decltype(node.instancingAttributes)(0, resourceAllocator.get()); + attributes = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(node.instancingAttributes), resourceAllocator.get(), 0); attributes.reserve(object.size()); for (const auto& field : object) { const auto key = field.key; @@ -2925,7 +2925,7 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) { return Error::InvalidGltf; } attributes.emplace_back( - std::make_pair(std::pmr::string(key, resourceAllocator.get()), static_cast(attributeIndex))); + std::make_pair(FASTGLTF_CONSTRUCT_PMR_RESOURCE(FASTGLTF_STD_PMR_NS::string, resourceAllocator.get(), key), static_cast(attributeIndex))); } return Error::None; }; @@ -2936,7 +2936,7 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) { std::string_view name; if (nodeObject["name"].get_string().get(name) == SUCCESS) { - node.name = std::pmr::string(name, resourceAllocator.get()); + node.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(node.name), resourceAllocator.get(), name); } asset.nodes.emplace_back(std::move(node)); @@ -2959,7 +2959,7 @@ fg::Error fg::Parser::parseSamplers(simdjson::dom::array& samplers, Asset& asset std::string_view name; if (samplerObject["name"].get_string().get(name) == SUCCESS) { - sampler.name = std::pmr::string(name, resourceAllocator.get()); + sampler.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(sampler.name), resourceAllocator.get(), name); } if (samplerObject["magFilter"].get_uint64().get(number) == SUCCESS) { @@ -3000,14 +3000,14 @@ fg::Error fg::Parser::parseScenes(simdjson::dom::array& scenes, Asset& asset) { std::string_view name; if (sceneObject["name"].get_string().get(name) == SUCCESS) { - scene.name = std::pmr::string(name, resourceAllocator.get()); + scene.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(scene.name), resourceAllocator.get(), name); } // Parse the array of nodes. dom::array nodes; auto nodeError = getJsonArray(sceneObject, "nodes", &nodes); if (nodeError == Error::None) { - scene.nodeIndices = decltype(scene.nodeIndices)(0, resourceAllocator.get()); + scene.nodeIndices = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(scene.nodeIndices), resourceAllocator.get(), 0); scene.nodeIndices.reserve(nodes.size()); for (auto nodeValue : nodes) { std::uint64_t index; @@ -3050,7 +3050,7 @@ fg::Error fg::Parser::parseSkins(simdjson::dom::array& skins, Asset& asset) { if (skinObject["joints"].get_array().get(jointsArray) != SUCCESS) { return Error::InvalidGltf; } - skin.joints = decltype(skin.joints)(0, resourceAllocator.get()); + skin.joints = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(skin.joints), resourceAllocator.get(), 0); skin.joints.reserve(jointsArray.size()); for (auto jointValue : jointsArray) { if (jointValue.get_uint64().get(index) != SUCCESS) { @@ -3061,7 +3061,7 @@ fg::Error fg::Parser::parseSkins(simdjson::dom::array& skins, Asset& asset) { std::string_view name; if (skinObject["name"].get_string().get(name) == SUCCESS) { - skin.name = std::pmr::string(name, resourceAllocator.get()); + skin.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(skin.name), resourceAllocator.get(), name); } asset.skins.emplace_back(std::move(skin)); } @@ -3108,7 +3108,7 @@ fg::Error fg::Parser::parseTextures(simdjson::dom::array& textures, Asset& asset std::string_view name; if (textureObject["name"].get_string().get(name) == SUCCESS) { - texture.name = std::pmr::string(name, resourceAllocator.get()); + texture.name = FASTGLTF_CONSTRUCT_PMR_RESOURCE(decltype(texture.name), resourceAllocator.get(), name); } asset.textures.emplace_back(std::move(texture)); diff --git a/tests/basic_test.cpp b/tests/basic_test.cpp index 6cf1552fd..a9b2ba467 100644 --- a/tests/basic_test.cpp +++ b/tests/basic_test.cpp @@ -520,8 +520,8 @@ TEST_CASE("Test accessors min/max", "[gltf-loader]") { { auto& firstAccessor = accessors[0]; - const auto* max = std::get_if>(&firstAccessor.max); - const auto* min = std::get_if>(&firstAccessor.min); + const auto* max = std::get_if>(&firstAccessor.max); + const auto* min = std::get_if>(&firstAccessor.min); REQUIRE(max != nullptr); REQUIRE(min != nullptr); REQUIRE(max->size() == fastgltf::getNumComponents(firstAccessor.type)); @@ -533,8 +533,8 @@ TEST_CASE("Test accessors min/max", "[gltf-loader]") { { auto& secondAccessor = accessors[1]; - const auto* max = std::get_if>(&secondAccessor.max); - const auto* min = std::get_if>(&secondAccessor.min); + const auto* max = std::get_if>(&secondAccessor.max); + const auto* min = std::get_if>(&secondAccessor.min); REQUIRE(max != nullptr); REQUIRE(min != nullptr); REQUIRE(max->size() == fastgltf::getNumComponents(secondAccessor.type)); @@ -552,8 +552,8 @@ TEST_CASE("Test accessors min/max", "[gltf-loader]") { { auto& fifthAccessor = accessors[4]; - const auto* max = std::get_if>(&fifthAccessor.max); - const auto* min = std::get_if>(&fifthAccessor.min); + const auto* max = std::get_if>(&fifthAccessor.max); + const auto* min = std::get_if>(&fifthAccessor.min); REQUIRE(max != nullptr); REQUIRE(min != nullptr); REQUIRE(max->size() == fastgltf::getNumComponents(fifthAccessor.type)); diff --git a/tests/uri_tests.cpp b/tests/uri_tests.cpp index 8b083bcf0..784734080 100644 --- a/tests/uri_tests.cpp +++ b/tests/uri_tests.cpp @@ -66,7 +66,7 @@ TEST_CASE("Test generic URIs", "[uri-tests]") { } TEST_CASE("Test percent decoding", "[uri-tests]") { - std::pmr::string test = "%22 %25"; + std::string test = "%22 %25"; fastgltf::URI::decodePercents(test); REQUIRE(test == "\" %"); }