Use unique_ptr to manage nif record lifetime

C++20
elsid 3 years ago
parent 2a7d28712f
commit d097c16206
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -17,17 +17,11 @@ NIFFile::NIFFile(Files::IStreamPtr stream, const std::string &name)
parse(stream);
}
NIFFile::~NIFFile()
{
for (Record* record : records)
delete record;
}
template <typename NodeType> static Record* construct() { return new NodeType; }
template <typename NodeType> static std::unique_ptr<Record> construct() { return std::make_unique<NodeType>(); }
struct RecordFactoryEntry {
using create_t = Record* (*)();
using create_t = std::unique_ptr<Record> (*)();
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<Record> 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<std::size_t>(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);
}

@ -55,7 +55,7 @@ class NIFFile final : public File
std::string hash;
/// Record list
std::vector<Record*> records;
std::vector<std::unique_ptr<Record>> records;
/// Root list. This is a select portion of the pointers from records
std::vector<Record*> 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(); }

Loading…
Cancel
Save