Merge pull request #11 from lioncash/make

General: Use std::make_unique where applicable
This commit is contained in:
Phillip Stephens 2019-08-30 16:28:53 -07:00 committed by GitHub
commit 55301dd505
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 144 additions and 93 deletions

View File

@ -118,11 +118,12 @@ std::unique_ptr<IPartReadStream> Node::beginReadStream(uint64_t offset) const {
std::unique_ptr<uint8_t[]> Node::getBuf() const {
if (m_kind != Kind::File) {
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);
return std::unique_ptr<uint8_t[]>(buf);
auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[m_discLength]);
beginReadStream()->read(buf.get(), m_discLength);
return buf;
}
bool Node::extractToDirectory(SystemStringView basePath, const ExtractionContext& ctx) const {

View File

@ -107,10 +107,13 @@ public:
};
std::unique_ptr<IPartReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IPartReadStream>(new PartReadStream(*this, offset, Err));
if (Err)
return {};
bool err = false;
auto ret = std::make_unique<PartReadStream>(*this, offset, err);
if (err) {
return nullptr;
}
return ret;
}
};
@ -120,7 +123,7 @@ DiscGCN::DiscGCN(std::unique_ptr<IDiscIO>&& dio, bool& err) : DiscBase(std::move
return;
/* 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) {
@ -175,10 +178,13 @@ public:
uint32_t packOffset(uint64_t offset) const override { return offset; }
std::unique_ptr<IPartWriteStream> beginWriteStream(uint64_t offset) override {
bool Err = false;
std::unique_ptr<IPartWriteStream> ret = std::make_unique<PartWriteStream>(*this, offset, Err);
if (Err)
return {};
bool err = false;
auto ret = std::make_unique<PartWriteStream>(*this, offset, err);
if (err) {
return nullptr;
}
return ret;
}
@ -380,8 +386,7 @@ uint64_t DiscBuilderGCN::CalculateTotalSizeRequired(SystemStringView dirIn) {
DiscBuilderGCN::DiscBuilderGCN(SystemStringView outPath, FProgress progressCB)
: DiscBuilderBase(outPath, 0x57058000, progressCB) {
PartitionBuilderGCN* partBuilder = new PartitionBuilderGCN(*this);
m_partitions.emplace_back(partBuilder);
m_partitions.emplace_back(std::make_unique<PartitionBuilderGCN>(*this));
}
DiscMergerGCN::DiscMergerGCN(SystemStringView outPath, DiscGCN& sourceDisc, FProgress progressCB)

View File

@ -25,10 +25,13 @@ public:
};
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_fio->beginReadStream(offset), Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_fio->beginReadStream(offset), err));
if (err) {
return nullptr;
}
return ret;
}
@ -45,14 +48,17 @@ public:
};
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_fio->beginWriteStream(offset), Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_fio->beginWriteStream(offset), err));
if (err) {
return nullptr;
}
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

View File

@ -264,18 +264,19 @@ public:
};
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, NewFileIO(filepath)->beginReadStream(), offset, Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(*this, NewFileIO(filepath)->beginReadStream(), offset, err));
if (err) {
return nullptr;
}
return ret;
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
return std::unique_ptr<IWriteStream>();
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override { return nullptr; }
};
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

View File

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

View File

@ -72,18 +72,26 @@ public:
return fwrite(buf, 1, length, fp);
}
};
std::unique_ptr<IWriteStream> beginWriteStream() const override {
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, err));
if (err) {
return nullptr;
}
return ret;
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, err));
if (err) {
return nullptr;
}
return ret;
}
@ -124,24 +132,32 @@ public:
return written;
}
};
std::unique_ptr<IReadStream> beginReadStream() const override {
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, err));
if (err) {
return nullptr;
}
return ret;
}
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
if (Err)
return {};
bool err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, err));
if (err) {
return nullptr;
}
return ret;
}
};
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

View File

@ -99,17 +99,23 @@ public:
}
};
std::unique_ptr<IWriteStream> beginWriteStream() const override {
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, m_maxWriteSize, Err));
if (Err)
return {};
bool err = false;
auto ret = std::make_unique<WriteStream>(m_path, m_maxWriteSize, err);
if (err) {
return nullptr;
}
return ret;
}
std::unique_ptr<IWriteStream> beginWriteStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IWriteStream>(new WriteStream(m_path, offset, m_maxWriteSize, Err));
if (Err)
return {};
bool err = false;
auto ret = std::make_unique<WriteStream>(m_path, offset, m_maxWriteSize, err);
if (err) {
return nullptr;
}
return ret;
}
@ -170,24 +176,32 @@ public:
return written;
}
};
std::unique_ptr<IReadStream> beginReadStream() const override {
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, Err));
if (Err)
return {};
bool err = false;
auto ret = std::make_unique<ReadStream>(m_path, err);
if (err) {
return nullptr;
}
return ret;
}
std::unique_ptr<IReadStream> beginReadStream(uint64_t offset) const override {
bool Err = false;
auto ret = std::unique_ptr<IReadStream>(new ReadStream(m_path, offset, Err));
if (Err)
return {};
bool err = false;
auto ret = std::make_unique<ReadStream>(m_path, offset, err);
if (err) {
return nullptr;
}
return ret;
}
};
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

View File

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

View File

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