From e1bab44e19d797a9372669a3d3dd333784e26000 Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Wed, 15 Jul 2020 15:02:27 +0100 Subject: [PATCH] Exception safety --- code/AssetLib/glTF2/glTF2Importer.cpp | 6 +++--- include/assimp/BaseImporter.h | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/code/AssetLib/glTF2/glTF2Importer.cpp b/code/AssetLib/glTF2/glTF2Importer.cpp index 441219feb..18c95d42b 100644 --- a/code/AssetLib/glTF2/glTF2Importer.cpp +++ b/code/AssetLib/glTF2/glTF2Importer.cpp @@ -347,7 +347,7 @@ static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsign void glTF2Importer::ImportMeshes(glTF2::Asset &r) { ASSIMP_LOG_DEBUG_F("Importing ", r.meshes.Size(), " meshes"); - std::vector meshes; + std::vector> meshes; unsigned int k = 0; meshOffsets.clear(); @@ -361,8 +361,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) { for (unsigned int p = 0; p < mesh.primitives.size(); ++p) { Mesh::Primitive &prim = mesh.primitives[p]; - aiMesh *aim = new aiMesh(); - meshes.push_back(aim); + meshes.emplace_back(std::make_unique()); + aiMesh *aim = meshes.back().get(); aim->mName = mesh.name.empty() ? mesh.id : mesh.name; diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index ed146168d..cc14f2f03 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -57,6 +57,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include struct aiScene; struct aiImporterDesc; @@ -391,6 +392,24 @@ public: // static utilities } } + // ------------------------------------------------------------------- + /** Utility function to move a std::vector of unique_ptrs into a aiScene array + * @param vec The vector of unique_ptrs to be moved + * @param out The output pointer to the allocated array. + * @param numOut The output count of elements copied. */ + template + AI_FORCE_INLINE static void CopyVector( + std::vector> &vec, + T **&out, + unsigned int &outLength) { + outLength = unsigned(vec.size()); + if (outLength) { + out = new T*[outLength]; + T** outPtr = out; + std::for_each(vec.begin(), vec.end(), [&outPtr](auto& uPtr){*outPtr = uPtr.release(); ++outPtr; }); + } + } + protected: /// Error description in case there was one. std::string m_ErrorText;