Simplify code

This commit is contained in:
Martchus 2021-12-31 00:30:11 +01:00
parent 31c1bb47e6
commit b740d2c1e0
2 changed files with 40 additions and 40 deletions

View File

@ -272,8 +272,7 @@ TagType *GenericContainer<FileInfoType, TagType, TrackType, ElementType>::create
} }
// a new tag must be created // a new tag must be created
m_tags.emplace_back(std::make_unique<TagType>()); const auto &tag = m_tags.emplace_back(std::make_unique<TagType>());
auto &tag = m_tags.back();
tag->setTarget(target); tag->setTarget(target);
return tag.get(); return tag.get();
} }
@ -281,7 +280,7 @@ TagType *GenericContainer<FileInfoType, TagType, TrackType, ElementType>::create
template <class FileInfoType, class TagType, class TrackType, class ElementType> template <class FileInfoType, class TagType, class TrackType, class ElementType>
bool GenericContainer<FileInfoType, TagType, TrackType, ElementType>::removeTag(Tag *tag) bool GenericContainer<FileInfoType, TagType, TrackType, ElementType>::removeTag(Tag *tag)
{ {
if (auto size = m_tags.size()) { if (const auto size = m_tags.size()) {
m_tags.erase(std::remove_if(m_tags.begin(), m_tags.end(), m_tags.erase(std::remove_if(m_tags.begin(), m_tags.end(),
[tag](const std::unique_ptr<TagType> &existingTag) -> bool { return static_cast<Tag *>(existingTag.get()) == tag; }), [tag](const std::unique_ptr<TagType> &existingTag) -> bool { return static_cast<Tag *>(existingTag.get()) == tag; }),
m_tags.end()); m_tags.end());
@ -315,43 +314,44 @@ inline void GenericContainer<FileInfoType, TagType, TrackType, ElementType>::rem
template <class FileInfoType, class TagType, class TrackType, class ElementType> template <class FileInfoType, class TagType, class TrackType, class ElementType>
bool GenericContainer<FileInfoType, TagType, TrackType, ElementType>::addTrack(TrackType *track) bool GenericContainer<FileInfoType, TagType, TrackType, ElementType>::addTrack(TrackType *track)
{ {
if (areTracksParsed() && supportsTrackModifications()) { if (!areTracksParsed() || !supportsTrackModifications()) {
// ensure ID is unique return false;
auto id = track->id();
ensureIdIsUnique:
for (const auto &existingTrack : m_tracks) {
if (existingTrack->id() == id) {
++id;
goto ensureIdIsUnique;
}
}
track->setId(id);
m_tracks.emplace_back(track);
return m_tracksAltered = true;
} }
return false; // ensure ID is unique
auto id = track->id();
ensureIdIsUnique:
for (const auto &existingTrack : m_tracks) {
if (existingTrack->id() == id) {
++id;
goto ensureIdIsUnique;
}
}
track->setId(id);
m_tracks.emplace_back(track);
return m_tracksAltered = true;
} }
template <class FileInfoType, class TagType, class TrackType, class ElementType> template <class FileInfoType, class TagType, class TrackType, class ElementType>
bool GenericContainer<FileInfoType, TagType, TrackType, ElementType>::removeTrack(AbstractTrack *track) bool GenericContainer<FileInfoType, TagType, TrackType, ElementType>::removeTrack(AbstractTrack *track)
{ {
bool removed = false; if (!areTracksParsed() || !supportsTrackModifications() || m_tracks.empty()) {
if (areTracksParsed() && supportsTrackModifications() && !m_tracks.empty()) { return false;
for (auto i = m_tracks.end() - 1, begin = m_tracks.begin();; --i) { }
if (static_cast<AbstractTrack *>(i->get()) == track) { auto removed = false;
i->release(); for (auto i = m_tracks.end() - 1, begin = m_tracks.begin();; --i) {
m_tracks.erase(i); if (static_cast<AbstractTrack *>(i->get()) == track) {
removed = true; i->release();
} m_tracks.erase(i);
if (i == begin) { removed = true;
break;
}
} }
if (removed) { if (i == begin) {
m_tracksAltered = true; break;
} }
} }
if (removed) {
m_tracksAltered = true;
}
return removed; return removed;
} }

View File

@ -955,10 +955,10 @@ void MatroskaContainer::internalMakeFile(Diagnostics &diag, AbortableProgressFee
// calculate size of "Tags"-element // calculate size of "Tags"-element
for (auto &tag : tags()) { for (auto &tag : tags()) {
try { try {
tagMaker.emplace_back(tag->prepareMaking(diag)); const auto &maker = tagMaker.emplace_back(tag->prepareMaking(diag));
if (tagMaker.back().requiredSize() > 3) { if (maker.requiredSize() > 3) {
// a tag of 3 bytes size is empty and can be skipped // a tag of 3 bytes size is empty and can be skipped
tagElementsSize += tagMaker.back().requiredSize(); tagElementsSize += maker.requiredSize();
} }
} catch (const Failure &) { } catch (const Failure &) {
} }
@ -969,10 +969,10 @@ void MatroskaContainer::internalMakeFile(Diagnostics &diag, AbortableProgressFee
for (auto &attachment : m_attachments) { for (auto &attachment : m_attachments) {
if (!attachment->isIgnored()) { if (!attachment->isIgnored()) {
try { try {
attachmentMaker.emplace_back(attachment->prepareMaking(diag)); const auto &maker = attachmentMaker.emplace_back(attachment->prepareMaking(diag));
if (attachmentMaker.back().requiredSize() > 3) { if (maker.requiredSize() > 3) {
// an attachment of 3 bytes size is empty and can be skipped // an attachment of 3 bytes size is empty and can be skipped
attachedFileElementsSize += attachmentMaker.back().requiredSize(); attachedFileElementsSize += maker.requiredSize();
} }
} catch (const Failure &) { } catch (const Failure &) {
} }
@ -984,10 +984,10 @@ void MatroskaContainer::internalMakeFile(Diagnostics &diag, AbortableProgressFee
// calculate size of "Tracks"-element // calculate size of "Tracks"-element
for (auto &track : tracks()) { for (auto &track : tracks()) {
try { try {
trackHeaderMaker.emplace_back(track->prepareMakingHeader(diag)); const auto &maker = trackHeaderMaker.emplace_back(track->prepareMakingHeader(diag));
if (trackHeaderMaker.back().requiredSize() > 3) { if (maker.requiredSize() > 3) {
// a track header of 3 bytes size is empty and can be skipped // a track header of 3 bytes size is empty and can be skipped
trackHeaderElementsSize += trackHeaderMaker.back().requiredSize(); trackHeaderElementsSize += maker.requiredSize();
} }
} catch (const Failure &) { } catch (const Failure &) {
} }