diff --git a/apps/esmtool/esmtool.cpp b/apps/esmtool/esmtool.cpp index aaa5e16be5..4fe0b248af 100644 --- a/apps/esmtool/esmtool.cpp +++ b/apps/esmtool/esmtool.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -27,7 +28,7 @@ struct ESMData unsigned int version; std::vector masters; - std::deque mRecords; + std::deque> mRecords; // Value: (Reference, Deleted flag) std::map > > mCellRefs; std::map mRecordStats; @@ -363,7 +364,7 @@ int load(Arguments& info) uint32_t flags; esm.getRecHeader(flags); - EsmTool::RecordBase *record = EsmTool::RecordBase::create(n); + auto record = EsmTool::RecordBase::create(n); if (record == nullptr) { if (skipped.count(n.toInt()) == 0) @@ -408,11 +409,7 @@ int load(Arguments& info) if (save) { - info.data.mRecords.push_back(record); - } - else - { - delete record; + info.data.mRecords.push_back(std::move(record)); } ++info.data.mRecordStats[n.toInt()]; } @@ -420,9 +417,6 @@ int load(Arguments& info) } catch(std::exception &e) { std::cout << "\nERROR:\n\n " << e.what() << std::endl; - for (const EsmTool::RecordBase* record : info.data.mRecords) - delete record; - info.data.mRecords.clear(); return 1; } @@ -485,7 +479,7 @@ int clone(Arguments& info) esm.save(save); int saved = 0; - for (EsmTool::RecordBase* record : info.data.mRecords) + for (auto& record : info.data.mRecords) { if (i <= 0) break; diff --git a/apps/esmtool/record.cpp b/apps/esmtool/record.cpp index 171f64eabe..f9c3dea5d4 100644 --- a/apps/esmtool/record.cpp +++ b/apps/esmtool/record.cpp @@ -171,226 +171,227 @@ void printTransport(const std::vector& transport) namespace EsmTool { -RecordBase * -RecordBase::create(const ESM::NAME type) +std::unique_ptr RecordBase::create(const ESM::NAME type) { - RecordBase *record = nullptr; + std::unique_ptr record; - switch (type.toInt()) { + switch (type.toInt()) + { case ESM::REC_ACTI: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_ALCH: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_APPA: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_ARMO: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_BODY: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_BOOK: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_BSGN: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_CELL: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_CLAS: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_CLOT: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_CONT: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_CREA: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_DIAL: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_DOOR: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_ENCH: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_FACT: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_GLOB: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_GMST: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_INFO: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_INGR: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_LAND: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_LEVI: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_LEVC: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_LIGH: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_LOCK: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_LTEX: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_MISC: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_MGEF: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_NPC_: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_PGRD: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_PROB: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_RACE: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_REGN: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_REPA: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_SCPT: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_SKIL: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_SNDG: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_SOUN: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_SPEL: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_STAT: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_WEAP: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } case ESM::REC_SSCR: { - record = new EsmTool::Record; + record = std::make_unique>(); break; } default: - record = nullptr; + break; } - if (record) { + if (record) + { record->mType = type; } return record; diff --git a/apps/esmtool/record.hpp b/apps/esmtool/record.hpp index bbb3dd0988..ef90dd1310 100644 --- a/apps/esmtool/record.hpp +++ b/apps/esmtool/record.hpp @@ -2,6 +2,7 @@ #define OPENMW_ESMTOOL_RECORD_H #include +#include #include @@ -54,7 +55,7 @@ namespace EsmTool virtual void save(ESM::ESMWriter &esm) = 0; virtual void print() = 0; - static RecordBase *create(ESM::NAME type); + static std::unique_ptr create(ESM::NAME type); // just make it a bit shorter template