From: Tsuda Kageyu Date: Sun, 22 Nov 2015 11:11:08 +0000 (+0900) Subject: Reduce redundant ref()/deref() operations. X-Git-Tag: v1.11beta~101 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2b7d6fef4748b5bafdf4cb4f3184b73273f7b465;p=taglib Reduce redundant ref()/deref() operations. --- diff --git a/taglib/ape/apeitem.cpp b/taglib/ape/apeitem.cpp index d04cc1d1..80bed072 100644 --- a/taglib/ape/apeitem.cpp +++ b/taglib/ape/apeitem.cpp @@ -43,28 +43,32 @@ public: bool readOnly; }; -APE::Item::Item() +//////////////////////////////////////////////////////////////////////////////// +// public members +//////////////////////////////////////////////////////////////////////////////// + +APE::Item::Item() : + d(new ItemPrivate()) { - d = new ItemPrivate; } -APE::Item::Item(const String &key, const String &value) +APE::Item::Item(const String &key, const String &value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->key = key; d->text.append(value); } -APE::Item::Item(const String &key, const StringList &values) +APE::Item::Item(const String &key, const StringList &values) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->key = key; d->text = values; } -APE::Item::Item(const String &key, const ByteVector &value, bool binary) +APE::Item::Item(const String &key, const ByteVector &value, bool binary) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->key = key; if(binary) { d->type = Binary; @@ -74,9 +78,9 @@ APE::Item::Item(const String &key, const ByteVector &value, bool binary) d->text.append(value); } -APE::Item::Item(const Item &item) +APE::Item::Item(const Item &item) : + d(new ItemPrivate(*item.d)) { - d = new ItemPrivate(*item.d); } APE::Item::~Item() @@ -86,13 +90,17 @@ APE::Item::~Item() Item &APE::Item::operator=(const Item &item) { - if(&item != this) { - delete d; - d = new ItemPrivate(*item.d); - } + Item(item).swap(*this); return *this; } +void APE::Item::swap(Item &item) +{ + using std::swap; + + swap(d, item.d); +} + void APE::Item::setReadOnly(bool readOnly) { d->readOnly = readOnly; diff --git a/taglib/ape/apeitem.h b/taglib/ape/apeitem.h index 4dd77d60..cfd157c3 100644 --- a/taglib/ape/apeitem.h +++ b/taglib/ape/apeitem.h @@ -90,6 +90,11 @@ namespace TagLib { */ Item &operator=(const Item &item); + /*! + * Exchanges the content of this item by the content of \a item. + */ + void swap(Item &item); + /*! * Returns the key. */ diff --git a/taglib/asf/asfattribute.cpp b/taglib/asf/asfattribute.cpp index 7a40bea3..a70330b0 100644 --- a/taglib/asf/asfattribute.cpp +++ b/taglib/asf/asfattribute.cpp @@ -58,84 +58,86 @@ public: // public members //////////////////////////////////////////////////////////////////////////////// -ASF::Attribute::Attribute() +ASF::Attribute::Attribute() : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = UnicodeType; } -ASF::Attribute::Attribute(const ASF::Attribute &other) - : d(other.d) +ASF::Attribute::Attribute(const ASF::Attribute &other) : + d(other.d) { d->ref(); } -ASF::Attribute &ASF::Attribute::operator=(const ASF::Attribute &other) -{ - if(&other != this) { - if(d->deref()) - delete d; - d = other.d; - d->ref(); - } - return *this; -} - -ASF::Attribute::~Attribute() +ASF::Attribute::Attribute(const String &value) : + d(new AttributePrivate()) { - if(d->deref()) - delete d; -} - -ASF::Attribute::Attribute(const String &value) -{ - d = new AttributePrivate; d->type = UnicodeType; d->stringValue = value; } -ASF::Attribute::Attribute(const ByteVector &value) +ASF::Attribute::Attribute(const ByteVector &value) : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = BytesType; d->byteVectorValue = value; } -ASF::Attribute::Attribute(const ASF::Picture &value) +ASF::Attribute::Attribute(const ASF::Picture &value) : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = BytesType; d->pictureValue = value; } -ASF::Attribute::Attribute(unsigned int value) +ASF::Attribute::Attribute(unsigned int value) : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = DWordType; d->intValue = value; } -ASF::Attribute::Attribute(unsigned long long value) +ASF::Attribute::Attribute(unsigned long long value) : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = QWordType; d->longLongValue = value; } -ASF::Attribute::Attribute(unsigned short value) +ASF::Attribute::Attribute(unsigned short value) : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = WordType; d->shortValue = value; } -ASF::Attribute::Attribute(bool value) +ASF::Attribute::Attribute(bool value) : + d(new AttributePrivate()) { - d = new AttributePrivate; d->type = BoolType; d->boolValue = value; } +ASF::Attribute &ASF::Attribute::operator=(const ASF::Attribute &other) +{ + Attribute(other).swap(*this); + return *this; +} + +void ASF::Attribute::swap(Attribute &other) +{ + using std::swap; + + swap(d, other.d); +} + +ASF::Attribute::~Attribute() +{ + if(d->deref()) + delete d; +} + ASF::Attribute::AttributeTypes ASF::Attribute::type() const { return d->type; @@ -351,4 +353,3 @@ void ASF::Attribute::setStream(int value) { d->stream = value; } - diff --git a/taglib/asf/asfattribute.h b/taglib/asf/asfattribute.h index 54eb0c7d..64979216 100644 --- a/taglib/asf/asfattribute.h +++ b/taglib/asf/asfattribute.h @@ -115,6 +115,11 @@ namespace TagLib */ ASF::Attribute &operator=(const Attribute &other); + /*! + * Exchanges the content of the Attribute by the content of \a other. + */ + void swap(Attribute &other); + /*! * Destroys the attribute. */ diff --git a/taglib/asf/asfpicture.cpp b/taglib/asf/asfpicture.cpp index f772052f..5a3e4411 100644 --- a/taglib/asf/asfpicture.cpp +++ b/taglib/asf/asfpicture.cpp @@ -48,14 +48,14 @@ public: // Picture class members //////////////////////////////////////////////////////////////////////////////// -ASF::Picture::Picture() +ASF::Picture::Picture() : + d(new PicturePrivate()) { - d = new PicturePrivate(); d->valid = true; } -ASF::Picture::Picture(const Picture& other) - : d(other.d) +ASF::Picture::Picture(const Picture& other) : + d(other.d) { d->ref(); } @@ -120,15 +120,17 @@ int ASF::Picture::dataSize() const ASF::Picture& ASF::Picture::operator=(const ASF::Picture& other) { - if(other.d != d) { - if(d->deref()) - delete d; - d = other.d; - d->ref(); - } + Picture(other).swap(*this); return *this; } +void ASF::Picture::swap(Picture &other) +{ + using std::swap; + + swap(d, other.d); +} + ByteVector ASF::Picture::render() const { if(!isValid()) @@ -179,4 +181,3 @@ ASF::Picture ASF::Picture::fromInvalid() ret.d->valid = false; return ret; } - diff --git a/taglib/asf/asfpicture.h b/taglib/asf/asfpicture.h index b510c35f..17233ba9 100644 --- a/taglib/asf/asfpicture.h +++ b/taglib/asf/asfpicture.h @@ -117,6 +117,11 @@ namespace TagLib */ Picture& operator=(const Picture& other); + /*! + * Exchanges the content of the Picture by the content of \a other. + */ + void swap(Picture &other); + /*! * Returns true if Picture stores valid picture */ diff --git a/taglib/mp4/mp4coverart.cpp b/taglib/mp4/mp4coverart.cpp index f2152335..69c9e685 100644 --- a/taglib/mp4/mp4coverart.cpp +++ b/taglib/mp4/mp4coverart.cpp @@ -33,20 +33,27 @@ using namespace TagLib; class MP4::CoverArt::CoverArtPrivate : public RefCounter { public: - CoverArtPrivate() : RefCounter(), format(MP4::CoverArt::JPEG) {} + CoverArtPrivate() : + RefCounter(), + format(MP4::CoverArt::JPEG) {} Format format; ByteVector data; }; -MP4::CoverArt::CoverArt(Format format, const ByteVector &data) +//////////////////////////////////////////////////////////////////////////////// +// public members +//////////////////////////////////////////////////////////////////////////////// + +MP4::CoverArt::CoverArt(Format format, const ByteVector &data) : + d(new CoverArtPrivate()) { - d = new CoverArtPrivate; d->format = format; d->data = data; } -MP4::CoverArt::CoverArt(const CoverArt &item) : d(item.d) +MP4::CoverArt::CoverArt(const CoverArt &item) : + d(item.d) { d->ref(); } @@ -54,15 +61,18 @@ MP4::CoverArt::CoverArt(const CoverArt &item) : d(item.d) MP4::CoverArt & MP4::CoverArt::operator=(const CoverArt &item) { - if(&item != this) { - if(d->deref()) - delete d; - d = item.d; - d->ref(); - } + CoverArt(item).swap(*this); return *this; } +void +MP4::CoverArt::swap(CoverArt &item) +{ + using std::swap; + + swap(d, item.d); +} + MP4::CoverArt::~CoverArt() { if(d->deref()) { @@ -81,4 +91,3 @@ MP4::CoverArt::data() const { return d->data; } - diff --git a/taglib/mp4/mp4coverart.h b/taglib/mp4/mp4coverart.h index 64115b45..ebfb3f94 100644 --- a/taglib/mp4/mp4coverart.h +++ b/taglib/mp4/mp4coverart.h @@ -53,8 +53,17 @@ namespace TagLib { ~CoverArt(); CoverArt(const CoverArt &item); + + /*! + * Copies the contents of \a item into this CoverArt. + */ CoverArt &operator=(const CoverArt &item); + /*! + * Exchanges the content of the CoverArt by the content of \a item. + */ + void swap(CoverArt &item); + //! Format of the image Format format() const; diff --git a/taglib/mp4/mp4item.cpp b/taglib/mp4/mp4item.cpp index aa59feda..d36e5200 100644 --- a/taglib/mp4/mp4item.cpp +++ b/taglib/mp4/mp4item.cpp @@ -33,7 +33,10 @@ using namespace TagLib; class MP4::Item::ItemPrivate : public RefCounter { public: - ItemPrivate() : RefCounter(), valid(true), atomDataType(TypeUndefined) {} + ItemPrivate() : + RefCounter(), + valid(true), + atomDataType(TypeUndefined) {} bool valid; AtomDataType atomDataType; @@ -50,13 +53,14 @@ public: MP4::CoverArtList m_coverArtList; }; -MP4::Item::Item() +MP4::Item::Item() : + d(new ItemPrivate()) { - d = new ItemPrivate; d->valid = false; } -MP4::Item::Item(const Item &item) : d(item.d) +MP4::Item::Item(const Item &item) : + d(item.d) { d->ref(); } @@ -64,75 +68,76 @@ MP4::Item::Item(const Item &item) : d(item.d) MP4::Item & MP4::Item::operator=(const Item &item) { - if(&item != this) { - if(d->deref()) { - delete d; - } - d = item.d; - d->ref(); - } + Item(item).swap(*this); return *this; } +void +MP4::Item::swap(Item &item) +{ + using std::swap; + + swap(d, item.d); +} + MP4::Item::~Item() { - if(d->deref()) { + if(d->deref()) delete d; - } } -MP4::Item::Item(bool value) +MP4::Item::Item(bool value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_bool = value; } -MP4::Item::Item(int value) +MP4::Item::Item(int value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_int = value; } -MP4::Item::Item(uchar value) +MP4::Item::Item(uchar value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_byte = value; } -MP4::Item::Item(uint value) +MP4::Item::Item(uint value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_uint = value; } -MP4::Item::Item(long long value) +MP4::Item::Item(long long value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_longlong = value; } -MP4::Item::Item(int value1, int value2) +MP4::Item::Item(int value1, int value2) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_intPair.first = value1; d->m_intPair.second = value2; } -MP4::Item::Item(const ByteVectorList &value) +MP4::Item::Item(const ByteVectorList &value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_byteVectorList = value; } -MP4::Item::Item(const StringList &value) +MP4::Item::Item(const StringList &value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_stringList = value; } -MP4::Item::Item(const MP4::CoverArtList &value) +MP4::Item::Item(const MP4::CoverArtList &value) : + d(new ItemPrivate()) { - d = new ItemPrivate; d->m_coverArtList = value; } @@ -205,4 +210,3 @@ MP4::Item::isValid() const { return d->valid; } - diff --git a/taglib/mp4/mp4item.h b/taglib/mp4/mp4item.h index be7aa1a1..38b6c25e 100644 --- a/taglib/mp4/mp4item.h +++ b/taglib/mp4/mp4item.h @@ -43,7 +43,17 @@ namespace TagLib { Item(); Item(const Item &item); + + /*! + * Copies the contents of \a item into this Item. + */ Item &operator=(const Item &item); + + /*! + * Exchanges the content of the Item by the content of \a item. + */ + void swap(Item &item); + ~Item(); Item(int value);