From e275abb8a328504137435f04126d083d8423874b Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Fri, 22 Sep 2023 21:46:05 -0700 Subject: [PATCH] Convert private raw pointers to unique_ptr (#1127) Simplifies the code quite a bit. --- taglib/ape/apefile.cpp | 20 +++++------------- taglib/asf/asffile.cpp | 18 +++++++--------- taglib/flac/flacfile.cpp | 14 +++---------- taglib/mp4/mp4file.cpp | 33 +++++++++--------------------- taglib/mpc/mpcfile.cpp | 21 +++++-------------- taglib/mpeg/id3v2/id3v2tag.cpp | 17 ++++----------- taglib/mpeg/mpegfile.cpp | 14 +++---------- taglib/mpeg/mpegproperties.cpp | 16 +++------------ taglib/ogg/flac/oggflacfile.cpp | 26 +++++++---------------- taglib/ogg/oggfile.cpp | 21 ++++++------------- taglib/ogg/opus/opusfile.cpp | 24 +++++++--------------- taglib/ogg/speex/speexfile.cpp | 24 +++++++--------------- taglib/ogg/vorbis/vorbisfile.cpp | 24 +++++++--------------- taglib/riff/aiff/aifffile.cpp | 24 +++++++--------------- taglib/riff/wav/wavfile.cpp | 15 +++----------- taglib/trueaudio/trueaudiofile.cpp | 14 +++---------- taglib/wavpack/wavpackfile.cpp | 15 +++----------- 17 files changed, 90 insertions(+), 250 deletions(-) diff --git a/taglib/ape/apefile.cpp b/taglib/ape/apefile.cpp index 80468661..5552c957 100644 --- a/taglib/ape/apefile.cpp +++ b/taglib/ape/apefile.cpp @@ -52,28 +52,18 @@ namespace class APE::File::FilePrivate { public: - FilePrivate() = default; - ~FilePrivate() - { - delete ID3v2Header; - delete properties; - } - - FilePrivate(const FilePrivate &) = delete; - FilePrivate &operator=(const FilePrivate &) = delete; - offset_t APELocation { -1 }; long APESize { 0 }; offset_t ID3v1Location { -1 }; - ID3v2::Header *ID3v2Header { nullptr }; + std::unique_ptr ID3v2Header; offset_t ID3v2Location { -1 }; long ID3v2Size { 0 }; TagUnion tag; - Properties *properties { nullptr }; + std::unique_ptr properties; }; //////////////////////////////////////////////////////////////////////////////// @@ -135,7 +125,7 @@ PropertyMap APE::File::setProperties(const PropertyMap &properties) APE::Properties *APE::File::audioProperties() const { - return d->properties; + return d->properties.get(); } bool APE::File::save() @@ -254,7 +244,7 @@ void APE::File::read(bool readProperties) if(d->ID3v2Location >= 0) { seek(d->ID3v2Location); - d->ID3v2Header = new ID3v2::Header(readBlock(ID3v2::Header::size())); + d->ID3v2Header = std::make_unique(readBlock(ID3v2::Header::size())); d->ID3v2Size = d->ID3v2Header->completeTagSize(); } @@ -299,6 +289,6 @@ void APE::File::read(bool readProperties) seek(0); } - d->properties = new Properties(this, streamLength); + d->properties = std::make_unique(this, streamLength); } } diff --git a/taglib/asf/asffile.cpp b/taglib/asf/asffile.cpp index 7fcf46c1..139edbd7 100644 --- a/taglib/asf/asffile.cpp +++ b/taglib/asf/asffile.cpp @@ -56,19 +56,15 @@ public: objects.setAutoDelete(true); } - ~FilePrivate() - { - delete tag; - delete properties; - } + ~FilePrivate() = default; FilePrivate(const FilePrivate &) = delete; FilePrivate &operator=(const FilePrivate &) = delete; unsigned long long headerSize { 0 }; - ASF::Tag *tag { nullptr }; - ASF::Properties *properties { nullptr }; + std::unique_ptr tag; + std::unique_ptr properties; List objects; @@ -503,7 +499,7 @@ ASF::File::~File() = default; ASF::Tag *ASF::File::tag() const { - return d->tag; + return d->tag.get(); } PropertyMap ASF::File::properties() const @@ -523,7 +519,7 @@ PropertyMap ASF::File::setProperties(const PropertyMap &properties) ASF::Properties *ASF::File::audioProperties() const { - return d->properties; + return d->properties.get(); } bool ASF::File::save() @@ -617,8 +613,8 @@ void ASF::File::read() return; } - d->tag = new ASF::Tag(); - d->properties = new ASF::Properties(); + d->tag = std::make_unique(); + d->properties = std::make_unique(); bool ok; d->headerSize = readQWORD(this, &ok); diff --git a/taglib/flac/flacfile.cpp b/taglib/flac/flacfile.cpp index e6a6bd83..6b17acc4 100644 --- a/taglib/flac/flacfile.cpp +++ b/taglib/flac/flacfile.cpp @@ -59,14 +59,6 @@ public: blocks.setAutoDelete(true); } - ~FilePrivate() - { - delete properties; - } - - FilePrivate(const FilePrivate &) = delete; - FilePrivate &operator=(const FilePrivate &) = delete; - const ID3v2::FrameFactory *ID3v2FrameFactory; offset_t ID3v2Location { -1 }; long ID3v2OriginalSize { 0 }; @@ -75,7 +67,7 @@ public: TagUnion tag; - Properties *properties { nullptr }; + std::unique_ptr properties; ByteVector xiphCommentData; List blocks; @@ -150,7 +142,7 @@ PropertyMap FLAC::File::setProperties(const PropertyMap &properties) FLAC::Properties *FLAC::File::audioProperties() const { - return d->properties; + return d->properties.get(); } bool FLAC::File::save() @@ -431,7 +423,7 @@ void FLAC::File::read(bool readProperties) else streamLength = length() - d->streamStart; - d->properties = new Properties(infoData, streamLength); + d->properties = std::make_unique(infoData, streamLength); } } diff --git a/taglib/mp4/mp4file.cpp b/taglib/mp4/mp4file.cpp index 9ed8a197..02353718 100644 --- a/taglib/mp4/mp4file.cpp +++ b/taglib/mp4/mp4file.cpp @@ -43,20 +43,9 @@ namespace class MP4::File::FilePrivate { public: - FilePrivate() = default; - ~FilePrivate() - { - delete atoms; - delete tag; - delete properties; - } - - FilePrivate(const FilePrivate &) = delete; - FilePrivate &operator=(const FilePrivate &) = delete; - - MP4::Tag *tag { nullptr }; - MP4::Atoms *atoms { nullptr }; - MP4::Properties *properties { nullptr }; + std::unique_ptr tag; + std::unique_ptr atoms; + std::unique_ptr properties; }; //////////////////////////////////////////////////////////////////////////////// @@ -93,10 +82,9 @@ MP4::File::File(IOStream *stream, bool readProperties, AudioProperties::ReadStyl MP4::File::~File() = default; -MP4::Tag * -MP4::File::tag() const +MP4::Tag *MP4::File::tag() const { - return d->tag; + return d->tag.get(); } PropertyMap MP4::File::properties() const @@ -114,10 +102,9 @@ PropertyMap MP4::File::setProperties(const PropertyMap &properties) return d->tag->setProperties(properties); } -MP4::Properties * -MP4::File::audioProperties() const +MP4::Properties *MP4::File::audioProperties() const { - return d->properties; + return d->properties.get(); } void @@ -126,7 +113,7 @@ MP4::File::read(bool readProperties) if(!isValid()) return; - d->atoms = new Atoms(this); + d->atoms = std::make_unique(this); if(!checkValid(d->atoms->atoms)) { setValid(false); return; @@ -138,9 +125,9 @@ MP4::File::read(bool readProperties) return; } - d->tag = new Tag(this, d->atoms); + d->tag = std::make_unique(this, d->atoms.get()); if(readProperties) { - d->properties = new Properties(this, d->atoms); + d->properties = std::make_unique(this, d->atoms.get()); } } diff --git a/taglib/mpc/mpcfile.cpp b/taglib/mpc/mpcfile.cpp index 75e8b82c..22df9184 100644 --- a/taglib/mpc/mpcfile.cpp +++ b/taglib/mpc/mpcfile.cpp @@ -44,28 +44,18 @@ namespace class MPC::File::FilePrivate { public: - FilePrivate() = default; - ~FilePrivate() - { - delete ID3v2Header; - delete properties; - } - - FilePrivate(const FilePrivate &) = delete; - FilePrivate &operator=(const FilePrivate &) = delete; - offset_t APELocation { -1 }; long APESize { 0 }; offset_t ID3v1Location { -1 }; - ID3v2::Header *ID3v2Header { nullptr }; + std::unique_ptr ID3v2Header; offset_t ID3v2Location { -1 }; long ID3v2Size { 0 }; TagUnion tag; - Properties *properties { nullptr }; + std::unique_ptr properties; }; //////////////////////////////////////////////////////////////////////////////// @@ -128,7 +118,7 @@ PropertyMap MPC::File::setProperties(const PropertyMap &properties) MPC::Properties *MPC::File::audioProperties() const { - return d->properties; + return d->properties.get(); } bool MPC::File::save() @@ -240,7 +230,6 @@ void MPC::File::strip(int tags) APETag(true); if(tags & ID3v2) { - delete d->ID3v2Header; d->ID3v2Header = nullptr; } } @@ -267,7 +256,7 @@ void MPC::File::read(bool readProperties) if(d->ID3v2Location >= 0) { seek(d->ID3v2Location); - d->ID3v2Header = new ID3v2::Header(readBlock(ID3v2::Header::size())); + d->ID3v2Header = std::make_unique(readBlock(ID3v2::Header::size())); d->ID3v2Size = d->ID3v2Header->completeTagSize(); } @@ -312,6 +301,6 @@ void MPC::File::read(bool readProperties) seek(0); } - d->properties = new Properties(this, streamLength); + d->properties = std::make_unique(this, streamLength); } } diff --git a/taglib/mpeg/id3v2/id3v2tag.cpp b/taglib/mpeg/id3v2/id3v2tag.cpp index ca38ac55..ce358f35 100644 --- a/taglib/mpeg/id3v2/id3v2tag.cpp +++ b/taglib/mpeg/id3v2/id3v2tag.cpp @@ -73,23 +73,14 @@ public: frameList.setAutoDelete(true); } - ~TagPrivate() - { - delete extendedHeader; - delete footer; - } - - TagPrivate(const TagPrivate &) = delete; - TagPrivate &operator=(const TagPrivate &) = delete; - const FrameFactory *factory { nullptr }; File *file { nullptr }; offset_t tagOffset { 0 }; Header header; - ExtendedHeader *extendedHeader { nullptr }; - Footer *footer { nullptr }; + std::unique_ptr extendedHeader; + std::unique_ptr