From af8e297e0fc7c339aa5a3c05aeb1aa8ec5e68c00 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:10:06 +0200 Subject: [PATCH 1/7] BaseImporter: Replace ScopeGuard with std::unique_ptr --- code/BaseImporter.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/code/BaseImporter.cpp b/code/BaseImporter.cpp index 09b32e3ae..b9b9eeb71 100644 --- a/code/BaseImporter.cpp +++ b/code/BaseImporter.cpp @@ -89,12 +89,12 @@ aiScene* BaseImporter::ReadFile(const Importer* pImp, const std::string& pFile, FileSystemFilter filter(pFile,pIOHandler); // create a scene object to hold the data - ScopeGuard sc(new aiScene()); + std::unique_ptr sc(new aiScene()); // dispatch importing try { - InternReadFile( pFile, sc, &filter); + InternReadFile( pFile, sc.get(), &filter); } catch( const std::exception& err ) { // extract error description @@ -104,8 +104,7 @@ aiScene* BaseImporter::ReadFile(const Importer* pImp, const std::string& pFile, } // return what we gathered from the import. - sc.dismiss(); - return sc; + return sc.release(); } // ------------------------------------------------------------------------------------------------ From f35d5952dc27c78b84d87fa0c4060e506f3bf091 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:13:43 +0200 Subject: [PATCH 2/7] BlenderLoader: Replace ScopeGuard with std::unique_ptr --- code/BlenderLoader.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/code/BlenderLoader.cpp b/code/BlenderLoader.cpp index 6d4c97cbf..824954f56 100644 --- a/code/BlenderLoader.cpp +++ b/code/BlenderLoader.cpp @@ -1148,7 +1148,7 @@ void BlenderImporter::ConvertMesh(const Scene& /*in*/, const Object* /*obj*/, co // ------------------------------------------------------------------------------------------------ aiCamera* BlenderImporter::ConvertCamera(const Scene& /*in*/, const Object* obj, const Camera* cam, ConversionData& /*conv_data*/) { - ScopeGuard out(new aiCamera()); + std::unique_ptr out(new aiCamera()); out->mName = obj->id.name+2; out->mPosition = aiVector3D(0.f, 0.f, 0.f); out->mUp = aiVector3D(0.f, 1.f, 0.f); @@ -1159,13 +1159,13 @@ aiCamera* BlenderImporter::ConvertCamera(const Scene& /*in*/, const Object* obj, out->mClipPlaneNear = cam->clipsta; out->mClipPlaneFar = cam->clipend; - return out.dismiss(); + return out.release(); } // ------------------------------------------------------------------------------------------------ aiLight* BlenderImporter::ConvertLight(const Scene& /*in*/, const Object* obj, const Lamp* lamp, ConversionData& /*conv_data*/) { - ScopeGuard out(new aiLight()); + std::unique_ptr out(new aiLight()); out->mName = obj->id.name+2; switch (lamp->type) @@ -1203,7 +1203,7 @@ aiLight* BlenderImporter::ConvertLight(const Scene& /*in*/, const Object* obj, c out->mColorAmbient = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy; out->mColorSpecular = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy; out->mColorDiffuse = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy; - return out.dismiss(); + return out.release(); } // ------------------------------------------------------------------------------------------------ @@ -1221,7 +1221,7 @@ aiNode* BlenderImporter::ConvertNode(const Scene& in, const Object* obj, Convers ++it; } - ScopeGuard node(new aiNode(obj->id.name+2)); // skip over the name prefix 'OB' + std::unique_ptr node(new aiNode(obj->id.name+2)); // skip over the name prefix 'OB' if (obj->data) { switch (obj->type) { @@ -1305,14 +1305,14 @@ aiNode* BlenderImporter::ConvertNode(const Scene& in, const Object* obj, Convers aiNode** nd = node->mChildren = new aiNode*[node->mNumChildren](); for (const Object* nobj :children) { *nd = ConvertNode(in,nobj,conv_data,node->mTransformation * parentTransform); - (*nd++)->mParent = node; + (*nd++)->mParent = node.get(); } } // apply modifiers modifier_cache->ApplyModifiers(*node,conv_data,in,*obj); - return node.dismiss(); + return node.release(); } #endif // ASSIMP_BUILD_NO_BLEND_IMPORTER From 6f50be82aac12d1edb72b127ee9f6b4c1eda57d4 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:15:57 +0200 Subject: [PATCH 3/7] 3MF: Replace ScopeGuard with std::unique_ptr --- code/D3MFImporter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/D3MFImporter.cpp b/code/D3MFImporter.cpp index 2fcfefa6d..3d1846d21 100644 --- a/code/D3MFImporter.cpp +++ b/code/D3MFImporter.cpp @@ -106,7 +106,7 @@ public: private: aiNode* ReadObject(aiScene* scene) { - ScopeGuard node(new aiNode()); + std::unique_ptr node(new aiNode()); std::vector meshIds; @@ -146,7 +146,7 @@ private: std::copy(meshIds.begin(), meshIds.end(), node->mMeshes); - return node.dismiss(); + return node.release(); } From e8eccfa27da7d6be568e4e3773d61c9a2e9ea6a2 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:19:18 +0200 Subject: [PATCH 4/7] FBX: Replace ScopeGuard with std::unique_ptr --- code/FBXConverter.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index a5e44a607..ea911b270 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -2744,10 +2744,10 @@ aiNodeAnim* Converter::GenerateRotationNodeAnim( const std::string& name, double& max_time, double& min_time ) { - ScopeGuard na( new aiNodeAnim() ); + std::unique_ptr na( new aiNodeAnim() ); na->mNodeName.Set( name ); - ConvertRotationKeys( na, curves, layer_map, start, stop, max_time, min_time, target.RotationOrder() ); + ConvertRotationKeys( na.get(), curves, layer_map, start, stop, max_time, min_time, target.RotationOrder() ); // dummy scaling key na->mScalingKeys = new aiVectorKey[ 1 ]; @@ -2763,7 +2763,7 @@ aiNodeAnim* Converter::GenerateRotationNodeAnim( const std::string& name, na->mPositionKeys[ 0 ].mTime = 0.; na->mPositionKeys[ 0 ].mValue = aiVector3D(); - return na.dismiss(); + return na.release(); } aiNodeAnim* Converter::GenerateScalingNodeAnim( const std::string& name, @@ -2774,10 +2774,10 @@ aiNodeAnim* Converter::GenerateScalingNodeAnim( const std::string& name, double& max_time, double& min_time ) { - ScopeGuard na( new aiNodeAnim() ); + std::unique_ptr na( new aiNodeAnim() ); na->mNodeName.Set( name ); - ConvertScaleKeys( na, curves, layer_map, start, stop, max_time, min_time ); + ConvertScaleKeys( na.get(), curves, layer_map, start, stop, max_time, min_time ); // dummy rotation key na->mRotationKeys = new aiQuatKey[ 1 ]; @@ -2793,7 +2793,7 @@ aiNodeAnim* Converter::GenerateScalingNodeAnim( const std::string& name, na->mPositionKeys[ 0 ].mTime = 0.; na->mPositionKeys[ 0 ].mValue = aiVector3D(); - return na.dismiss(); + return na.release(); } @@ -2806,10 +2806,10 @@ aiNodeAnim* Converter::GenerateTranslationNodeAnim( const std::string& name, double& min_time, bool inverse ) { - ScopeGuard na( new aiNodeAnim() ); + std::unique_ptr na( new aiNodeAnim() ); na->mNodeName.Set( name ); - ConvertTranslationKeys( na, curves, layer_map, start, stop, max_time, min_time ); + ConvertTranslationKeys( na.get(), curves, layer_map, start, stop, max_time, min_time ); if ( inverse ) { for ( unsigned int i = 0; i < na->mNumPositionKeys; ++i ) { @@ -2831,7 +2831,7 @@ aiNodeAnim* Converter::GenerateTranslationNodeAnim( const std::string& name, na->mRotationKeys[ 0 ].mTime = 0.; na->mRotationKeys[ 0 ].mValue = aiQuaternion(); - return na.dismiss(); + return na.release(); } aiNodeAnim* Converter::GenerateSimpleNodeAnim( const std::string& name, @@ -2845,7 +2845,7 @@ aiNodeAnim* Converter::GenerateSimpleNodeAnim( const std::string& name, bool reverse_order ) { - ScopeGuard na( new aiNodeAnim() ); + std::unique_ptr na( new aiNodeAnim() ); na->mNodeName.Set( name ); const PropertyTable& props = target.Props(); @@ -2917,7 +2917,7 @@ aiNodeAnim* Converter::GenerateSimpleNodeAnim( const std::string& name, // which requires all of rotation, scaling and translation // to be set. if ( chain[ TransformationComp_Scaling ] != iter_end ) { - ConvertScaleKeys( na, ( *chain[ TransformationComp_Scaling ] ).second, + ConvertScaleKeys( na.get(), ( *chain[ TransformationComp_Scaling ] ).second, layer_map, start, stop, max_time, @@ -2933,7 +2933,7 @@ aiNodeAnim* Converter::GenerateSimpleNodeAnim( const std::string& name, } if ( chain[ TransformationComp_Rotation ] != iter_end ) { - ConvertRotationKeys( na, ( *chain[ TransformationComp_Rotation ] ).second, + ConvertRotationKeys( na.get(), ( *chain[ TransformationComp_Rotation ] ).second, layer_map, start, stop, max_time, @@ -2951,7 +2951,7 @@ aiNodeAnim* Converter::GenerateSimpleNodeAnim( const std::string& name, } if ( chain[ TransformationComp_Translation ] != iter_end ) { - ConvertTranslationKeys( na, ( *chain[ TransformationComp_Translation ] ).second, + ConvertTranslationKeys( na.get(), ( *chain[ TransformationComp_Translation ] ).second, layer_map, start, stop, max_time, @@ -2967,7 +2967,7 @@ aiNodeAnim* Converter::GenerateSimpleNodeAnim( const std::string& name, } } - return na.dismiss(); + return na.release(); } Converter::KeyFrameListList Converter::GetKeyframeList( const std::vector& nodes, int64_t start, int64_t stop ) From bd4f0245626a7466efdd35a5750dba224c8e51fe Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:22:19 +0200 Subject: [PATCH 5/7] XGLLoader: Replace ScopeGuard with std::unique_ptr --- code/XGLLoader.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/code/XGLLoader.cpp b/code/XGLLoader.cpp index df33229a6..8ef91afac 100644 --- a/code/XGLLoader.cpp +++ b/code/XGLLoader.cpp @@ -359,7 +359,7 @@ void XGLImporter::ReadLighting(TempScope& scope) // ------------------------------------------------------------------------------------------------ aiLight* XGLImporter::ReadDirectionalLight() { - ScopeGuard l(new aiLight()); + std::unique_ptr l(new aiLight()); l->mType = aiLightSource_DIRECTIONAL; while (ReadElementUpToClosing("directionallight")) { @@ -374,13 +374,13 @@ aiLight* XGLImporter::ReadDirectionalLight() l->mColorSpecular = ReadCol3(); } } - return l.dismiss(); + return l.release(); } // ------------------------------------------------------------------------------------------------ aiNode* XGLImporter::ReadObject(TempScope& scope, bool skipFirst, const char* closetag) { - ScopeGuard nd(new aiNode()); + std::unique_ptr nd(new aiNode()); std::vector children; std::vector meshes; @@ -463,11 +463,11 @@ aiNode* XGLImporter::ReadObject(TempScope& scope, bool skipFirst, const char* cl nd->mChildren = new aiNode*[nd->mNumChildren](); for(unsigned int i = 0; i < nd->mNumChildren; ++i) { nd->mChildren[i] = children[i]; - children[i]->mParent = nd; + children[i]->mParent = nd.get(); } } - return nd.dismiss(); + return nd.release(); } // ------------------------------------------------------------------------------------------------ @@ -539,7 +539,7 @@ aiMatrix4x4 XGLImporter::ReadTrafo() // ------------------------------------------------------------------------------------------------ aiMesh* XGLImporter::ToOutputMesh(const TempMaterialMesh& m) { - ScopeGuard mesh(new aiMesh()); + std::unique_ptr mesh(new aiMesh()); mesh->mNumVertices = static_cast(m.positions.size()); mesh->mVertices = new aiVector3D[mesh->mNumVertices]; @@ -576,7 +576,7 @@ aiMesh* XGLImporter::ToOutputMesh(const TempMaterialMesh& m) mesh->mPrimitiveTypes = m.pflags; mesh->mMaterialIndex = m.matid; - return mesh.dismiss(); + return mesh.release(); } // ------------------------------------------------------------------------------------------------ @@ -745,7 +745,7 @@ void XGLImporter::ReadMaterial(TempScope& scope) { const unsigned int mat_id = ReadIDAttr(); - ScopeGuard mat(new aiMaterial()); + std::unique_ptr mat(new aiMaterial()); while (ReadElementUpToClosing("mat")) { const std::string& s = GetElementName(); if (s == "amb") { @@ -774,8 +774,8 @@ void XGLImporter::ReadMaterial(TempScope& scope) } } - scope.materials[mat_id] = mat; - scope.materials_linear.push_back(mat.dismiss()); + scope.materials[mat_id] = mat.get(); + scope.materials_linear.push_back(mat.release()); } From b60d84a8a27a140e7a6ce4eb8b38aa6eab264ca5 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:23:49 +0200 Subject: [PATCH 6/7] C4D: Replace ScopeGuard with std::unique_ptr --- code/C4DImporter.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/C4DImporter.cpp b/code/C4DImporter.cpp index 39514f6b8..1344f32fb 100644 --- a/code/C4DImporter.cpp +++ b/code/C4DImporter.cpp @@ -185,11 +185,11 @@ void C4DImporter::InternReadFile( const std::string& pFile, if(mesh->mMaterialIndex >= mat_count) { ++mat_count; - ScopeGuard def_material(new aiMaterial()); + std::unique_ptr def_material(new aiMaterial()); const aiString name(AI_DEFAULT_MATERIAL_NAME); def_material->AddProperty(&name, AI_MATKEY_NAME); - materials.push_back(def_material.dismiss()); + materials.push_back(def_material.release()); break; } } @@ -412,7 +412,7 @@ aiMesh* C4DImporter::ReadMesh(BaseObject* object) const CPolygon* polys = polyObject->GetPolygonR(); ai_assert(polys != NULL); - ScopeGuard mesh(new aiMesh()); + std::unique_ptr mesh(new aiMesh()); mesh->mNumFaces = static_cast(polyCount); aiFace* face = mesh->mFaces = new aiFace[mesh->mNumFaces](); @@ -616,7 +616,7 @@ aiMesh* C4DImporter::ReadMesh(BaseObject* object) } mesh->mMaterialIndex = ResolveMaterial(polyObject); - return mesh.dismiss(); + return mesh.release(); } From 2c3558fdd04dd0242fae58a0a43a517617448a77 Mon Sep 17 00:00:00 2001 From: Turo Lamminen Date: Sat, 2 Dec 2017 17:24:19 +0200 Subject: [PATCH 7/7] Remove ScopeGuard --- code/BaseImporter.h | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/code/BaseImporter.h b/code/BaseImporter.h index cee54c1ad..b424d2f83 100644 --- a/code/BaseImporter.h +++ b/code/BaseImporter.h @@ -65,42 +65,6 @@ class IOStream; #define AI_MAKE_MAGIC(string) ((uint32_t)((string[0] << 24) + \ (string[1] << 16) + (string[2] << 8) + string[3])) -// --------------------------------------------------------------------------- -template -struct ScopeGuard -{ - explicit ScopeGuard(T* obj) : obj(obj), mdismiss() {} - ~ScopeGuard () throw() { - if (!mdismiss) { - delete obj; - } - obj = NULL; - } - - T* dismiss() { - mdismiss=true; - return obj; - } - - operator T*() { - return obj; - } - - T* operator -> () { - return obj; - } - -private: - // no copying allowed. - ScopeGuard(); - ScopeGuard( const ScopeGuard & ); - ScopeGuard &operator = ( const ScopeGuard & ); - - T* obj; - bool mdismiss; -}; - - // --------------------------------------------------------------------------- /** FOR IMPORTER PLUGINS ONLY: The BaseImporter defines a common interface