Improve TagValue

* Make more functions inline
* Fix use of std::size_t
This commit is contained in:
Martchus 2018-03-11 15:16:09 +01:00
parent c0336ed4bb
commit a26b98dcd7
2 changed files with 62 additions and 57 deletions

View File

@ -33,7 +33,7 @@ TagValue::TagValue(const TagValue &other)
, m_type(other.m_type) , m_type(other.m_type)
, m_desc(other.m_desc) , m_desc(other.m_desc)
, m_mimeType(other.m_mimeType) , m_mimeType(other.m_mimeType)
, m_lng(other.m_lng) , m_language(other.m_language)
, m_labeledAsReadonly(other.m_labeledAsReadonly) , m_labeledAsReadonly(other.m_labeledAsReadonly)
, m_encoding(other.m_encoding) , m_encoding(other.m_encoding)
, m_descEncoding(other.m_descEncoding) , m_descEncoding(other.m_descEncoding)
@ -54,7 +54,7 @@ TagValue &TagValue::operator=(const TagValue &other)
m_type = other.m_type; m_type = other.m_type;
m_desc = other.m_desc; m_desc = other.m_desc;
m_mimeType = other.m_mimeType; m_mimeType = other.m_mimeType;
m_lng = other.m_lng; m_language = other.m_language;
m_labeledAsReadonly = other.m_labeledAsReadonly; m_labeledAsReadonly = other.m_labeledAsReadonly;
m_encoding = other.m_encoding; m_encoding = other.m_encoding;
m_descEncoding = other.m_descEncoding; m_descEncoding = other.m_descEncoding;
@ -79,7 +79,7 @@ TagValue &TagValue::operator=(const TagValue &other)
bool TagValue::operator==(const TagValue &other) const bool TagValue::operator==(const TagValue &other) const
{ {
if (m_desc != other.m_desc || (!m_desc.empty() && m_descEncoding != other.m_descEncoding) || m_mimeType != other.m_mimeType if (m_desc != other.m_desc || (!m_desc.empty() && m_descEncoding != other.m_descEncoding) || m_mimeType != other.m_mimeType
|| m_lng != other.m_lng || m_labeledAsReadonly != other.m_labeledAsReadonly) { || m_language != other.m_language || m_labeledAsReadonly != other.m_labeledAsReadonly) {
return false; return false;
} }
if (m_type == other.m_type) { if (m_type == other.m_type) {
@ -122,13 +122,6 @@ bool TagValue::operator==(const TagValue &other) const
} }
} }
/*!
* \brief Destroys the TagValue.
*/
TagValue::~TagValue()
{
}
/*! /*!
* \brief Wipes assigned meta data. * \brief Wipes assigned meta data.
* - Clears description, mime type and language. * - Clears description, mime type and language.
@ -140,23 +133,12 @@ void TagValue::clearMetadata()
{ {
m_desc.clear(); m_desc.clear();
m_mimeType.clear(); m_mimeType.clear();
m_lng.clear(); m_language.clear();
m_labeledAsReadonly = false; m_labeledAsReadonly = false;
m_encoding = TagTextEncoding::Latin1; m_encoding = TagTextEncoding::Latin1;
m_type = TagDataType::Undefined; m_type = TagDataType::Undefined;
} }
/*!
* \brief Wipes assigned data including meta data.
* \sa clearData()
* \sa clearMetadata()
*/
void TagValue::clearDataAndMetadata()
{
clearData();
clearMetadata();
}
/*! /*!
* \brief Converts the value of the current TagValue object to its equivalent * \brief Converts the value of the current TagValue object to its equivalent
* integer representation. * integer representation.
@ -317,9 +299,9 @@ DateTime TagValue::toDateTime() const
case TagDataType::Integer: case TagDataType::Integer:
case TagDataType::DateTime: case TagDataType::DateTime:
if (m_size == sizeof(int32)) { if (m_size == sizeof(int32)) {
return DateTime(*(reinterpret_cast<int32 *>(m_ptr.get()))); return DateTime(*(reinterpret_cast<uint32 *>(m_ptr.get())));
} else if (m_size == sizeof(int64)) { } else if (m_size == sizeof(int64)) {
return DateTime(*(reinterpret_cast<int64 *>(m_ptr.get()))); return DateTime(*(reinterpret_cast<uint64 *>(m_ptr.get())));
} else { } else {
throw ConversionException("The assigned data is of unappropriate size."); throw ConversionException("The assigned data is of unappropriate size.");
} }
@ -625,17 +607,6 @@ void TagValue::assignInteger(int value)
m_encoding = TagTextEncoding::Latin1; m_encoding = TagTextEncoding::Latin1;
} }
/*!
* \brief Assigns the given standard genre \a index to be assigned.
* \param index Specifies the index to be assigned.
* \sa <a href="http://en.wikipedia.org/wiki/ID3#List_of_genres">List of genres - Wikipedia</a>
*/
void TagValue::assignStandardGenreIndex(int index)
{
assignInteger(index);
m_type = TagDataType::StandardGenreIndex;
}
/*! /*!
* \brief Assigns a copy of the given \a data. * \brief Assigns a copy of the given \a data.
* \param data Specifies the data to be assigned. * \param data Specifies the data to be assigned.

View File

@ -61,8 +61,6 @@ enum class TagDataType : unsigned int {
}; };
class TAG_PARSER_EXPORT TagValue { class TAG_PARSER_EXPORT TagValue {
friend class Id3v2Frame; // FIXME: make ensureHostByteOrder() public in next minor release
public: public:
// constructor, destructor // constructor, destructor
TagValue(); TagValue();
@ -100,16 +98,17 @@ public:
PositionInSet toPositionInSet() const; PositionInSet toPositionInSet() const;
ChronoUtilities::TimeSpan toTimeSpan() const; ChronoUtilities::TimeSpan toTimeSpan() const;
ChronoUtilities::DateTime toDateTime() const; ChronoUtilities::DateTime toDateTime() const;
size_t dataSize() const; std::size_t dataSize() const;
char *dataPointer() const; char *dataPointer();
const char *dataPointer() const;
const std::string &description() const; const std::string &description() const;
void setDescription(const std::string &value, TagTextEncoding encoding = TagTextEncoding::Latin1); void setDescription(const std::string &value, TagTextEncoding encoding = TagTextEncoding::Latin1);
const std::string &mimeType() const; const std::string &mimeType() const;
void setMimeType(const std::string &value); void setMimeType(const std::string &mimeType);
const std::string &language() const; const std::string &language() const;
void setLanguage(const std::string &value); void setLanguage(const std::string &language);
bool isLabeledAsReadonly() const; bool isLabeledAsReadonly() const;
void setReadonly(bool value); void setReadonly(bool readOnly);
TagTextEncoding dataEncoding() const; TagTextEncoding dataEncoding() const;
void convertDataEncoding(TagTextEncoding encoding); void convertDataEncoding(TagTextEncoding encoding);
void convertDataEncodingForTag(const Tag *tag); void convertDataEncodingForTag(const Tag *tag);
@ -129,16 +128,17 @@ public:
void assignTimeSpan(ChronoUtilities::TimeSpan value); void assignTimeSpan(ChronoUtilities::TimeSpan value);
void assignDateTime(ChronoUtilities::DateTime value); void assignDateTime(ChronoUtilities::DateTime value);
private:
static void stripBom(const char *&text, size_t &length, TagTextEncoding encoding); static void stripBom(const char *&text, size_t &length, TagTextEncoding encoding);
static void ensureHostByteOrder(std::u16string &u16str, TagTextEncoding currentEncoding); static void ensureHostByteOrder(std::u16string &u16str, TagTextEncoding currentEncoding);
private:
std::unique_ptr<char[]> m_ptr; std::unique_ptr<char[]> m_ptr;
std::string::size_type m_size; std::size_t m_size;
TagDataType m_type; TagDataType m_type;
std::string m_desc; std::string m_desc;
std::string m_mimeType; std::string m_mimeType;
std::string m_lng; std::string m_language;
bool m_labeledAsReadonly; bool m_labeledAsReadonly;
TagTextEncoding m_encoding; TagTextEncoding m_encoding;
TagTextEncoding m_descEncoding; TagTextEncoding m_descEncoding;
@ -156,6 +156,13 @@ inline TagValue::TagValue()
{ {
} }
/*!
* \brief Destroys the TagValue.
*/
inline TagValue::~TagValue()
{
}
/*! /*!
* \brief Constructs a new TagValue holding a copy of the given \a text. * \brief Constructs a new TagValue holding a copy of the given \a text.
* \param text Specifies the text to be assigned. * \param text Specifies the text to be assigned.
@ -207,7 +214,7 @@ inline TagValue::TagValue(int value)
* encoding will only be considered if a text is assigned. * encoding will only be considered if a text is assigned.
* \remarks Strips the BOM of the specified \a data if \a type is TagDataType::Text. * \remarks Strips the BOM of the specified \a data if \a type is TagDataType::Text.
*/ */
inline TagValue::TagValue(const char *data, size_t length, TagDataType type, TagTextEncoding encoding) inline TagValue::TagValue(const char *data, std::size_t length, TagDataType type, TagTextEncoding encoding)
: m_size(length) : m_size(length)
, m_type(type) , m_type(type)
, m_labeledAsReadonly(false) , m_labeledAsReadonly(false)
@ -226,7 +233,7 @@ inline TagValue::TagValue(const char *data, size_t length, TagDataType type, Tag
/*! /*!
* \brief Constructs a new TagValue holding with the given \a data. * \brief Constructs a new TagValue holding with the given \a data.
* *
* The data is not copied. It is moved. * The \a data is not copied. It is moved.
* *
* \param data Specifies a pointer to the data. * \param data Specifies a pointer to the data.
* \param length Specifies the length of the data. * \param length Specifies the length of the data.
@ -235,7 +242,7 @@ inline TagValue::TagValue(const char *data, size_t length, TagDataType type, Tag
* encoding will only be considered if a text is assigned. * encoding will only be considered if a text is assigned.
* \remarks Does not strip the BOM so for consistency the caller must ensure there is no BOM present. * \remarks Does not strip the BOM so for consistency the caller must ensure there is no BOM present.
*/ */
inline TagValue::TagValue(std::unique_ptr<char[]> &&data, size_t length, TagDataType type, TagTextEncoding encoding) inline TagValue::TagValue(std::unique_ptr<char[]> &&data, std::size_t length, TagDataType type, TagTextEncoding encoding)
: m_size(length) : m_size(length)
, m_type(type) , m_type(type)
, m_labeledAsReadonly(false) , m_labeledAsReadonly(false)
@ -308,6 +315,17 @@ inline void TagValue::assignDateTime(ChronoUtilities::DateTime value)
assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::DateTime); assignData(reinterpret_cast<const char *>(&value), sizeof(value), TagDataType::DateTime);
} }
/*!
* \brief Assigns the given standard genre \a index to be assigned.
* \param index Specifies the index to be assigned.
* \sa <a href="http://en.wikipedia.org/wiki/ID3#List_of_genres">List of genres - Wikipedia</a>
*/
inline void TagValue::assignStandardGenreIndex(int index)
{
assignInteger(index);
m_type = TagDataType::StandardGenreIndex;
}
/*! /*!
* \brief Returns the type of the assigned value. * \brief Returns the type of the assigned value.
*/ */
@ -364,6 +382,17 @@ inline void TagValue::clearData()
m_ptr.reset(); m_ptr.reset();
} }
/*!
* \brief Wipes assigned data including meta data.
* \sa clearData()
* \sa clearMetadata()
*/
inline void TagValue::clearDataAndMetadata()
{
clearData();
clearMetadata();
}
/*! /*!
* \brief Returns the size of the assigned value in bytes. * \brief Returns the size of the assigned value in bytes.
* \remarks Meta data such as description and MIME type is not considered as part of the assigned value. * \remarks Meta data such as description and MIME type is not considered as part of the assigned value.
@ -376,10 +405,15 @@ inline size_t TagValue::dataSize() const
/*! /*!
* \brief Returns a pointer to the raw data assigned to the current instance. * \brief Returns a pointer to the raw data assigned to the current instance.
* \remarks The instance keeps ownership over the data which will be invalidated when the * \remarks The instance keeps ownership over the data which will be invalidated when the
* it gets destroyed or an other value is assigned. * TagValue gets destroyed or another value is assigned.
* \remarks The raw data is not null terminated. See dataSize(). * \remarks The raw data is not null terminated. See dataSize().
*/ */
inline char *TagValue::dataPointer() const inline char *TagValue::dataPointer()
{
return m_ptr.get();
}
inline const char *TagValue::dataPointer() const
{ {
return m_ptr.get(); return m_ptr.get();
} }
@ -425,9 +459,9 @@ inline const std::string &TagValue::mimeType() const
* \remarks The usage of this meta information depends on the tag implementation. * \remarks The usage of this meta information depends on the tag implementation.
* \sa mimeType() * \sa mimeType()
*/ */
inline void TagValue::setMimeType(const std::string &value) inline void TagValue::setMimeType(const std::string &mimeType)
{ {
m_mimeType = value; m_mimeType = mimeType;
} }
/*! /*!
@ -437,7 +471,7 @@ inline void TagValue::setMimeType(const std::string &value)
*/ */
inline const std::string &TagValue::language() const inline const std::string &TagValue::language() const
{ {
return m_lng; return m_language;
} }
/*! /*!
@ -446,9 +480,9 @@ inline const std::string &TagValue::language() const
* \remarks The usage of this meta information depends on the tag implementation. * \remarks The usage of this meta information depends on the tag implementation.
* \sa language() * \sa language()
*/ */
inline void TagValue::setLanguage(const std::string &value) inline void TagValue::setLanguage(const std::string &language)
{ {
m_lng = value; m_language = language;
} }
/*! /*!
@ -472,9 +506,9 @@ inline bool TagValue::isLabeledAsReadonly() const
* assignments simply use the "const" keyword). * assignments simply use the "const" keyword).
* \sa isLabeledAsReadonly() * \sa isLabeledAsReadonly()
*/ */
inline void TagValue::setReadonly(bool value) inline void TagValue::setReadonly(bool readOnly)
{ {
m_labeledAsReadonly = value; m_labeledAsReadonly = readOnly;
} }
/*! /*!