From 7692eec9af1a62d40fc9c01b5afac30b58e2a73b Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 30 Jan 2021 19:12:04 +0100 Subject: [PATCH] Make it easier to customize getting internal values --- fieldbasedtag.h | 18 +++++++++++++++--- id3/id3v2tag.cpp | 20 ++++++++------------ id3/id3v2tag.h | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/fieldbasedtag.h b/fieldbasedtag.h index 7d5ac04..cbda291 100644 --- a/fieldbasedtag.h +++ b/fieldbasedtag.h @@ -68,6 +68,7 @@ protected: using CRTPBase = FieldMapBasedTag; const TagValue &internallyGetValue(const IdentifierType &id) const; + void internallyGetValuesFromField(const FieldType &field, std::vector &values) const; std::vector internallyGetValues(const IdentifierType &id) const; bool internallySetValue(const IdentifierType &id, const TagValue &value); bool internallySetValues(const IdentifierType &id, const std::vector &values); @@ -126,6 +127,19 @@ template const TagValue &FieldMapBasedTagsecond.value() : TagValue::empty(); } +/*! + * \brief Default way to gather values from a field in internallyGetValues(). + * \remarks Shadow in subclass to provide custom implementation. + */ +template +void FieldMapBasedTag::internallyGetValuesFromField( + const FieldMapBasedTag::FieldType &field, std::vector &values) const +{ + if (!field.value().isEmpty()) { + values.emplace_back(&field.value()); + } +} + /*! * \brief Default implementation for values(). * \remarks Shadow in subclass to provide custom implementation. @@ -136,9 +150,7 @@ std::vector FieldMapBasedTag::internallyGe auto range = m_fields.equal_range(id); std::vector values; for (auto i = range.first; i != range.second; ++i) { - if (!i->second.value().isEmpty()) { - values.push_back(&i->second.value()); - } + static_cast(this)->internallyGetValuesFromField(i->second, values); } return values; } diff --git a/id3/id3v2tag.cpp b/id3/id3v2tag.cpp index 64ff563..7065d86 100644 --- a/id3/id3v2tag.cpp +++ b/id3/id3v2tag.cpp @@ -68,22 +68,18 @@ void Id3v2Tag::ensureTextValuesAreProperlyEncoded() } /*! - * \brief Works like the default implementation but adds additional values as well. + * \brief Adds additional values as well. */ -std::vector Id3v2Tag::internallyGetValues(const IdentifierType &id) const +void Id3v2Tag::internallyGetValuesFromField(const Id3v2Tag::FieldType &field, std::vector &values) const { - auto range = fields().equal_range(id); - std::vector values; - for (auto i = range.first; i != range.second; ++i) { - const auto &frame(i->second); - if (!frame.value().isEmpty()) { - values.push_back(&frame.value()); - } - for (const auto &value : frame.additionalValues()) { - values.push_back(&value); + if (!field.value().isEmpty()) { + values.emplace_back(&field.value()); + } + for (const auto &value : field.additionalValues()) { + if (!value.isEmpty()) { + values.emplace_back(&value); } } - return values; } /*! diff --git a/id3/id3v2tag.h b/id3/id3v2tag.h index d4a35d9..01aa883 100644 --- a/id3/id3v2tag.h +++ b/id3/id3v2tag.h @@ -95,7 +95,7 @@ protected: IdentifierType internallyGetFieldId(KnownField field) const; KnownField internallyGetKnownField(const IdentifierType &id) const; TagDataType internallyGetProposedDataType(const std::uint32_t &id) const; - std::vector internallyGetValues(const IdentifierType &id) const; + void internallyGetValuesFromField(const FieldType &field, std::vector &values) const; bool internallySetValues(const IdentifierType &id, const std::vector &values); private: