Browse Source

Replace unique_ptr and add custom deleter

Joshua Hyatt 5 years ago
parent
commit
638499a278
2 changed files with 15 additions and 13 deletions
  1. 7 6
      code/AssetLib/FBX/FBXImporter.cpp
  2. 8 7
      code/AssetLib/Obj/ObjFileImporter.cpp

+ 7 - 6
code/AssetLib/FBX/FBXImporter.cpp

@@ -141,8 +141,11 @@ void FBXImporter::SetupProperties(const Importer *pImp) {
 // ------------------------------------------------------------------------------------------------
 // Imports the given file into the given scene structure.
 void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
-	IOStream* pStream = pIOHandler->Open(pFile, "rb");
-	if (!pStream) {
+	auto streamCloser = [&](IOStream *pStream) {
+		pIOHandler->Close(pStream);
+	};
+	std::unique_ptr<IOStream, decltype(streamCloser)> stream(pIOHandler->Open(pFile, "rb"), streamCloser);
+	if (!stream) {
 		ThrowException("Could not open file for reading");
 	}
 
@@ -154,13 +157,11 @@ void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSy
 	// streaming for its output data structures so the net win with
 	// streaming input data would be very low.
 	std::vector<char> contents;
-	contents.resize(pStream->FileSize() + 1);
-	pStream->Read(&*contents.begin(), 1, contents.size() - 1);
+	contents.resize(stream->FileSize() + 1);
+	stream->Read(&*contents.begin(), 1, contents.size() - 1);
 	contents[contents.size() - 1] = 0;
 	const char *const begin = &*contents.begin();
 
-	pIOHandler->Close(pStream);
-
 	// broadphase tokenizing pass in which we identify the core
 	// syntax elements of FBX (brackets, commas, key:value mappings)
 	TokenList tokens;

+ 8 - 7
code/AssetLib/Obj/ObjFileImporter.cpp

@@ -107,22 +107,25 @@ const aiImporterDesc *ObjFileImporter::GetInfo() const {
 void ObjFileImporter::InternReadFile(const std::string &file, aiScene *pScene, IOSystem *pIOHandler) {
     // Read file into memory
     static const std::string mode = "rb";
-    IOStream *pFileStream = pIOHandler->Open(file, mode);
-    if (!pFileStream) {
+    auto streamCloser = [&](IOStream *pStream) {
+        pIOHandler->Close(pStream);
+    };
+    std::unique_ptr<IOStream, decltype(streamCloser)> fileStream(pIOHandler->Open(file, mode), streamCloser);
+    if (!fileStream.get()) {
         throw DeadlyImportError("Failed to open file " + file + ".");
     }
 
     // Get the file-size and validate it, throwing an exception when fails
-    size_t fileSize = pFileStream->FileSize();
+    size_t fileSize = fileStream->FileSize();
     if (fileSize < ObjMinSize) {
         throw DeadlyImportError("OBJ-file is too small.");
     }
 
     IOStreamBuffer<char> streamedBuffer;
-    streamedBuffer.open(pFileStream);
+    streamedBuffer.open(fileStream.get());
 
     // Allocate buffer and read file into it
-    //TextFileToBuffer( pFileStream,m_Buffer);
+    //TextFileToBuffer( fileStream.get(),m_Buffer);
 
     // Get the model name
     std::string modelName, folderName;
@@ -145,8 +148,6 @@ void ObjFileImporter::InternReadFile(const std::string &file, aiScene *pScene, I
 
     streamedBuffer.close();
 
-    pIOHandler->Close(pFileStream);
-
     // Clean up allocated storage for the next import
     m_Buffer.clear();