General: Use std::make_unique where applicable

Makes for a little less reading in certain cases.
This commit is contained in:
Lioncash 2019-08-30 05:02:35 -04:00
parent df1e450728
commit a8753e273f
9 changed files with 144 additions and 93 deletions

View File

@ -110,11 +110,12 @@ std::unique_ptr<IPartReadStream> Node::beginReadStream(uint64_t offset) const {
std::unique_ptr<uint8_t[]> Node::getBuf() const { std::unique_ptr<uint8_t[]> Node::getBuf() const {
if (m_kind != Kind::File) { if (m_kind != Kind::File) {
LogModule.report(logvisor::Error, fmt("unable to buffer a non-file {}"), m_name); LogModule.report(logvisor::Error, fmt("unable to buffer a non-file {}"), m_name);
return std::unique_ptr<uint8_t[]>(); return nullptr;
} }
uint8_t* buf = new uint8_t[m_discLength];
beginReadStream()->read(buf, m_discLength); auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[m_discLength]);
return std::unique_ptr<uint8_t[]>(buf); beginReadStream()->read(buf.get(), m_discLength);
return buf;
} }
bool Node::extractToDirectory(SystemStringView basePath, const ExtractionContext& ctx) const { bool Node::extractToDirectory(SystemStringView basePath, const ExtractionContext& ctx) const {

View File

@ -97,10 +97,13 @@ public:
}; };
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const override { std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IPartReadStream>(new PartReadStream(*this, offset, Err)); auto ret = std::make_unique<PartReadStream>(*this, offset, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
}; };
@ -110,7 +113,7 @@ DiscGCN::DiscGCN(std::unique_ptr<IDiscIO>&& dio, bool& err) : DiscBase(std::move
return; return;
/* One lone partition for GCN */ /* One lone partition for GCN */
m_partitions.emplace_back(new PartitionGCN(*this, 0, err)); m_partitions.emplace_back(std::make_unique<PartitionGCN>(*this, 0, err));
} }
DiscBuilderGCN DiscGCN::makeMergeBuilder(SystemStringView outPath, FProgress progressCB) { DiscBuilderGCN DiscGCN::makeMergeBuilder(SystemStringView outPath, FProgress progressCB) {
@ -165,10 +168,13 @@ public:
uint32_t packOffset(uint64_t offset) const override { return offset; } uint32_t packOffset(uint64_t offset) const override { return offset; }
std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) override { std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) override {
bool Err = false; bool err = false;
std::unique_ptr<IPartWriteStream> ret = std::make_unique<PartWriteStream>(*this, offset, Err); auto ret = std::make_unique<PartWriteStream>(*this, offset, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
@ -370,8 +376,7 @@ uint64_t DiscBuilderGCN::CalculateTotalSizeRequired(SystemStringView dirIn) {
DiscBuilderGCN::DiscBuilderGCN(SystemStringView outPath, FProgress progressCB) DiscBuilderGCN::DiscBuilderGCN(SystemStringView outPath, FProgress progressCB)
: DiscBuilderBase(outPath, 0x57058000, progressCB) { : DiscBuilderBase(outPath, 0x57058000, progressCB) {
PartitionBuilderGCN* partBuilder = new PartitionBuilderGCN(*this); m_partitions.emplace_back(std::make_unique<PartitionBuilderGCN>(*this));
m_partitions.emplace_back(partBuilder);
} }
DiscMergerGCN::DiscMergerGCN(SystemStringView outPath, DiscGCN& sourceDisc, FProgress progressCB) DiscMergerGCN::DiscMergerGCN(SystemStringView outPath, DiscGCN& sourceDisc, FProgress progressCB)

View File

@ -26,10 +26,13 @@ public:
}; };
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override { std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_fio->beginReadStream(offset), Err)); auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_fio->beginReadStream(offset), err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
@ -46,14 +49,17 @@ public:
}; };
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_fio->beginWriteStream(offset), Err)); auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_fio->beginWriteStream(offset), err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
}; };
std::unique_ptr<IDiscIO> NewDiscIOISO(SystemStringView path) { return std::unique_ptr<IDiscIO>(new DiscIOISO(path)); } std::unique_ptr<IDiscIO> NewDiscIOISO(SystemStringView path) { return std::make_unique<DiscIOISO>(path); }
} // namespace nod } // namespace nod

View File

@ -259,18 +259,19 @@ public:
}; };
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override { std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, NewFileIO(filepath)->beginReadStream(), offset, Err)); auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, NewFileIO(filepath)->beginReadStream(), offset, err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { return nullptr; }
return std::unique_ptr<IWriteStream>();
}
}; };
std::unique_ptr<IDiscIO> NewDiscIOWBFS(SystemStringView path) { return std::unique_ptr<IDiscIO>(new DiscIOWBFS(path)); } std::unique_ptr<IDiscIO> NewDiscIOWBFS(SystemStringView path) { return std::make_unique<DiscIOWBFS>(path); }
} // namespace nod } // namespace nod

View File

@ -390,10 +390,13 @@ public:
}; };
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const override { std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IPartReadStream>(new PartReadStream(*this, m_dataOff, offset, Err)); auto ret = std::make_unique<PartReadStream>(*this, m_dataOff, offset, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
@ -402,21 +405,23 @@ public:
std::unique_ptr<uint8_t[]> readPartitionHeaderBuf(size_t& szOut) const { std::unique_ptr<uint8_t[]> readPartitionHeaderBuf(size_t& szOut) const {
{ {
std::unique_ptr<IReadStream> rs = m_parent.getDiscIO().beginReadStream(m_offset + 0x2B4); std::unique_ptr<IReadStream> rs = m_parent.getDiscIO().beginReadStream(m_offset + 0x2B4);
if (!rs) if (!rs) {
return {}; return nullptr;
}
uint32_t h3; uint32_t h3;
if (rs->read(&h3, 4) != 4) { if (rs->read(&h3, 4) != 4) {
LogModule.report(logvisor::Error, fmt(_SYS_STR("unable to read H3 offset apploader"))); LogModule.report(logvisor::Error, fmt(_SYS_STR("unable to read H3 offset apploader")));
return {}; return nullptr;
} }
h3 = SBig(h3); h3 = SBig(h3);
szOut = uint64_t(h3) << 2; szOut = uint64_t(h3) << 2;
} }
std::unique_ptr<IReadStream> rs = m_parent.getDiscIO().beginReadStream(m_offset); std::unique_ptr<IReadStream> rs = m_parent.getDiscIO().beginReadStream(m_offset);
if (!rs) if (!rs) {
return {}; return nullptr;
}
std::unique_ptr<uint8_t[]> buf(new uint8_t[szOut]); std::unique_ptr<uint8_t[]> buf(new uint8_t[szOut]);
rs->read(buf.get(), szOut); rs->read(buf.get(), szOut);
@ -528,7 +533,7 @@ DiscWii::DiscWii(std::unique_ptr<IDiscIO>&& dio, bool& err) : DiscBase(std::move
err = true; err = true;
return; return;
} }
m_partitions.emplace_back(new PartitionWii(*this, kind, part.partDataOff << 2, err)); m_partitions.emplace_back(std::make_unique<PartitionWii>(*this, kind, part.partDataOff << 2, err));
if (err) if (err)
return; return;
} }
@ -757,11 +762,13 @@ public:
uint32_t packOffset(uint64_t offset) const override { return uint32_t(offset >> uint64_t(2)); } uint32_t packOffset(uint64_t offset) const override { return uint32_t(offset >> uint64_t(2)); }
std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) override { std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) override {
bool Err = false; bool err = false;
std::unique_ptr<IPartWriteStream> ret = auto ret = std::make_unique<PartWriteStream>(*this, m_baseOffset + m_userOffset, offset, err);
std::make_unique<PartWriteStream>(*this, m_baseOffset + m_userOffset, offset, Err);
if (Err) if (err) {
return {}; return nullptr;
}
return ret; return ret;
} }
@ -1237,8 +1244,7 @@ uint64_t DiscBuilderWii::CalculateTotalSizeRequired(SystemStringView dirIn, bool
DiscBuilderWii::DiscBuilderWii(SystemStringView outPath, bool dualLayer, FProgress progressCB) DiscBuilderWii::DiscBuilderWii(SystemStringView outPath, bool dualLayer, FProgress progressCB)
: DiscBuilderBase(outPath, dualLayer ? 0x1FB4E0000 : 0x118240000, progressCB) { : DiscBuilderBase(outPath, dualLayer ? 0x1FB4E0000 : 0x118240000, progressCB) {
PartitionBuilderWii* partBuilder = new PartitionBuilderWii(*this, PartitionKind::Data, 0x200000); m_partitions.emplace_back(std::make_unique<PartitionBuilderWii>(*this, PartitionKind::Data, 0x200000));
m_partitions.emplace_back(partBuilder);
} }
DiscMergerWii::DiscMergerWii(SystemStringView outPath, DiscWii& sourceDisc, bool dualLayer, FProgress progressCB) DiscMergerWii::DiscMergerWii(SystemStringView outPath, DiscWii& sourceDisc, bool dualLayer, FProgress progressCB)

View File

@ -68,18 +68,26 @@ public:
return fwrite(buf, 1, length, fp); return fwrite(buf, 1, length, fp);
} }
}; };
std::unique_ptr<IWriteStream> beginWriteStream() const override { std::unique_ptr<IWriteStream> beginWriteStream() const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err)); auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err)); auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
@ -120,24 +128,32 @@ public:
return written; return written;
} }
}; };
std::unique_ptr<IReadStream> beginReadStream() const override { std::unique_ptr<IReadStream> beginReadStream() const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err)); auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override { std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err)); auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, err));
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
}; };
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) { std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) {
return std::unique_ptr<IFileIO>(new FileIOFILE(path, maxWriteSize)); return std::make_unique<FileIOFILE>(path, maxWriteSize);
} }
} // namespace nod } // namespace nod

View File

@ -95,17 +95,23 @@ public:
} }
}; };
std::unique_ptr<IWriteStream> beginWriteStream() const override { std::unique_ptr<IWriteStream> beginWriteStream() const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err)); auto ret = std::make_unique<WriteStream>(m_path, m_maxWriteSize, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err)); auto ret = std::make_unique<WriteStream>(m_path, offset, m_maxWriteSize, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
@ -166,24 +172,32 @@ public:
return written; return written;
} }
}; };
std::unique_ptr<IReadStream> beginReadStream() const override { std::unique_ptr<IReadStream> beginReadStream() const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err)); auto ret = std::make_unique<ReadStream>(m_path, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override { std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false; bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err)); auto ret = std::make_unique<ReadStream>(m_path, offset, err);
if (Err)
return {}; if (err) {
return nullptr;
}
return ret; return ret;
} }
}; };
std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) { std::unique_ptr<IFileIO> NewFileIO(SystemStringView path, int64_t maxWriteSize) {
return std::unique_ptr<IFileIO>(new FileIOWin32(path, maxWriteSize)); return std::make_unique<FileIOWin32>(path, maxWriteSize);
} }
} // namespace nod } // namespace nod

View File

@ -563,11 +563,11 @@ std::unique_ptr<IAES> NewAES() {
#endif #endif
} }
if (HAS_AES_NI) if (HAS_AES_NI)
return std::unique_ptr<IAES>(new NiAES); return std::make_unique<NiAES>();
else else
return std::unique_ptr<IAES>(new SoftwareAES); return std::make_unique<SoftwareAES>();
#else #else
return std::unique_ptr<IAES>(new SoftwareAES); return std::make_unique<SoftwareAES>();
#endif #endif
} }

View File

@ -51,18 +51,20 @@ std::unique_ptr<DiscBase> OpenDiscFromImage(SystemStringView path, bool& isWii)
return {}; return {};
} }
bool Err = false; bool err = false;
std::unique_ptr<DiscBase> ret; std::unique_ptr<DiscBase> ret;
if (isWii) { if (isWii) {
ret = std::unique_ptr<DiscBase>(new DiscWii(std::move(discIO), Err)); ret = std::make_unique<DiscWii>(std::move(discIO), err);
if (Err) if (err) {
return {}; return nullptr;
}
return ret; return ret;
} }
ret = std::unique_ptr<DiscBase>(new DiscGCN(std::move(discIO), Err)); ret = std::make_unique<DiscGCN>(std::move(discIO), err);
if (Err) if (err) {
return {}; return nullptr;
}
return ret; return ret;
} }