diff --git a/components/nif/niffile.cpp b/components/nif/niffile.cpp index 1a1bbd7217..e976a9d96a 100644 --- a/components/nif/niffile.cpp +++ b/components/nif/niffile.cpp @@ -17,17 +17,11 @@ NIFFile::NIFFile(Files::IStreamPtr stream, const std::string &name) parse(stream); } -NIFFile::~NIFFile() -{ - for (Record* record : records) - delete record; -} - -template static Record* construct() { return new NodeType; } +template static std::unique_ptr construct() { return std::make_unique(); } struct RecordFactoryEntry { - using create_t = Record* (*)(); + using create_t = std::unique_ptr (*)(); create_t mCreate; RecordType mType; @@ -290,7 +284,7 @@ void NIFFile::parse(Files::IStreamPtr stream) const bool hasRecordSeparators = ver >= NIFStream::generateVersion(10,0,0,0) && ver < NIFStream::generateVersion(10,2,0,0); for (std::size_t i = 0; i < recNum; i++) { - Record *r = nullptr; + std::unique_ptr r; std::string rec = hasRecTypeListings ? recTypes[recTypeIndices[i]] : nif.getString(); if(rec.empty()) @@ -315,7 +309,7 @@ void NIFFile::parse(Files::IStreamPtr stream) if (entry != factories.end()) { - r = entry->second.mCreate (); + r = entry->second.mCreate(); r->recType = entry->second.mType; } else @@ -328,8 +322,8 @@ void NIFFile::parse(Files::IStreamPtr stream) assert(r->recType != RC_MISSING); r->recName = rec; r->recIndex = i; - records[i] = r; r->read(&nif); + records[i] = std::move(r); } const std::size_t rootNum = nif.getUInt(); @@ -341,7 +335,7 @@ void NIFFile::parse(Files::IStreamPtr stream) int idx = nif.getInt(); if (idx >= 0 && static_cast(idx) < records.size()) { - roots[i] = records[idx]; + roots[i] = records[idx].get(); } else { @@ -351,7 +345,7 @@ void NIFFile::parse(Files::IStreamPtr stream) } // Once parsing is done, do post-processing. - for (Record* record : records) + for (const auto& record : records) record->post(this); } diff --git a/components/nif/niffile.hpp b/components/nif/niffile.hpp index 6884f51d58..d9f46795c1 100644 --- a/components/nif/niffile.hpp +++ b/components/nif/niffile.hpp @@ -55,7 +55,7 @@ class NIFFile final : public File std::string hash; /// Record list - std::vector records; + std::vector> records; /// Root list. This is a select portion of the pointers from records std::vector roots; @@ -107,13 +107,11 @@ public: /// Open a NIF stream. The name is used for error messages. NIFFile(Files::IStreamPtr stream, const std::string &name); - ~NIFFile(); /// Get a given record Record *getRecord(size_t index) const override { - Record *res = records.at(index); - return res; + return records.at(index).get(); } /// Number of records size_t numRecords() const override { return records.size(); }