From 3a6f0d46aa3231dfd260505c7884fc03aec770ae Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Tue, 1 Nov 2016 11:47:37 +0900 Subject: [PATCH] Use a smart pointer in TagLib::Picture. --- taglib/toolkit/tpicture.cpp | 101 +++++++++++++++++++++--------------- taglib/toolkit/tpicture.h | 11 ++-- 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/taglib/toolkit/tpicture.cpp b/taglib/toolkit/tpicture.cpp index 00de5e49..3aa30746 100644 --- a/taglib/toolkit/tpicture.cpp +++ b/taglib/toolkit/tpicture.cpp @@ -23,22 +23,39 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ +#include +#include #include "tpicture.h" using namespace TagLib; -class Picture::PicturePrivate : public RefCounter +namespace +{ + struct PictureData + { + PictureData() : + type(Picture::Other) {} + + ByteVector data; + Picture::Type type; + String mime; + String description; + }; +} + +class Picture::PicturePrivate { public: PicturePrivate() : - RefCounter() {} + data(new PictureData()) {} - String description; - String mime; - Type type; - ByteVector data; + SHARED_PTR data; }; +//////////////////////////////////////////////////////////////////////////////// +// public members +//////////////////////////////////////////////////////////////////////////////// + Picture::Picture() : d(new PicturePrivate()) { @@ -50,56 +67,54 @@ Picture::Picture(const ByteVector &data, const String &description) : d(new PicturePrivate()) { - d->mime = mime; - d->description = description; - d->type = type; - d->data = data; + d->data->data = data; + d->data->type = type; + d->data->mime = mime; + d->data->description = description; } -Picture::Picture(const Picture &p) - : d(p.d) +Picture::Picture(const Picture &p) : + d(new PicturePrivate(*p.d)) { - d->ref(); -} - -String Picture::description() const -{ - return d->description; -} - -ByteVector Picture::data() const -{ - return d->data; -} - -String Picture::mime() const -{ - return d->mime; -} - -Picture::Type Picture::type() const -{ - return d->type; } Picture::~Picture() { - if(d->deref()) - delete d; + delete d; +} + +String Picture::description() const +{ + return d->data->description; +} + +ByteVector Picture::data() const +{ + return d->data->data; +} + +String Picture::mime() const +{ + return d->data->mime; +} + +Picture::Type Picture::type() const +{ + return d->data->type; +} + +void Picture::swap(Picture &other) +{ + using std::swap; + + swap(d, other.d); } /* =========== OPERATORS =========== */ Picture &Picture::operator =(const Picture &p) { - if(&p == this) - return *this; - - if(d && d->deref()) - delete d; - - d = p.d; - d->ref(); + Picture(p).swap(*this); return *this; } diff --git a/taglib/toolkit/tpicture.h b/taglib/toolkit/tpicture.h index a3a0768e..c3b7ddb3 100644 --- a/taglib/toolkit/tpicture.h +++ b/taglib/toolkit/tpicture.h @@ -119,7 +119,7 @@ namespace TagLib { String mime() const; /*! - * Returns the descritpion of the picture + * Returns the description of the picture */ String description() const; @@ -129,7 +129,7 @@ namespace TagLib { Type type() const; /*! - * Returns datas of the picture + * Returns data of the picture */ ByteVector data() const; @@ -139,7 +139,12 @@ namespace TagLib { */ Picture &operator=(const Picture &p); - private: + /*! + * Exchanges the content of the Picture by the content of \a p. + */ + void swap(Picture &p); + +private: PicturePrivate *d; };