Skip to content

Commit 3566b00

Browse files
committed
Clean up attachments
Avoid use of raw pointers.
1 parent 98bc68d commit 3566b00

8 files changed

Lines changed: 210 additions & 57 deletions

examples/matroskareader.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ int main(int argc, char *argv[])
5656
if(attachments) {
5757
const TagLib::Matroska::Attachments::AttachedFileList &list = attachments->attachedFileList();
5858
printf("Found %u attachment(s)\n", list.size());
59-
for(auto attachedFile : list) {
60-
PRINT_PRETTY("Filename", attachedFile->fileName().toCString(true));
61-
const TagLib::String &description = attachedFile->description();
59+
for(const auto &attachedFile : list) {
60+
PRINT_PRETTY("Filename", attachedFile.fileName().toCString(true));
61+
const TagLib::String &description = attachedFile.description();
6262
PRINT_PRETTY("Description", !description.isEmpty() ? description.toCString(true) : "None");
63-
const TagLib::String &mediaType = attachedFile->mediaType();
63+
const TagLib::String &mediaType = attachedFile.mediaType();
6464
PRINT_PRETTY("Media Type", !mediaType.isEmpty() ? mediaType.toCString(false) : "None");
6565
PRINT_PRETTY("Data Size",
66-
TagLib::Utils::formatString("%u byte(s)",attachedFile->data().size()).toCString(false)
66+
TagLib::Utils::formatString("%u byte(s)",attachedFile.data().size()).toCString(false)
6767
);
6868
PRINT_PRETTY("UID",
69-
TagLib::Utils::formatString("%llu",attachedFile->uid()).toCString(false)
69+
TagLib::Utils::formatString("%llu",attachedFile.uid()).toCString(false)
7070
);
7171
}
7272
}

examples/matroskawriter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ int main(int argc, char *argv[])
4141
TagLib::FileStream image(argv[2]);
4242
auto data = image.readBlock(image.length());
4343
auto attachments = file.attachments(true);
44-
auto attachedFile = new TagLib::Matroska::AttachedFile();
45-
attachedFile->setFileName("cover.jpg");
46-
attachedFile->setMediaType("image/jpeg");
47-
attachedFile->setData(data);
48-
//attachedFile->setUID(5081000385627515072ull);
44+
TagLib::Matroska::AttachedFile attachedFile;
45+
attachedFile.setFileName("cover.jpg");
46+
attachedFile.setMediaType("image/jpeg");
47+
attachedFile.setData(data);
48+
//attachedFile.setUID(5081000385627515072ull);
4949
attachments->addAttachedFile(attachedFile);
5050

5151
file.save();

taglib/matroska/ebml/ebmlmkattachments.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ std::unique_ptr<Matroska::Attachments> EBML::MkAttachments::parse()
5959
if(!(filename && data))
6060
continue;
6161

62-
auto file = new Matroska::AttachedFile();
63-
file->setFileName(*filename);
64-
file->setData(*data);
62+
Matroska::AttachedFile file;
63+
file.setFileName(*filename);
64+
file.setData(*data);
6565
if(description)
66-
file->setDescription(*description);
66+
file.setDescription(*description);
6767
if(mediaType)
68-
file->setMediaType(*mediaType);
68+
file.setMediaType(*mediaType);
6969
if(uid)
70-
file->setUID(uid);
70+
file.setUID(uid);
7171

7272
attachments->addAttachedFile(file);
7373
}

taglib/matroska/matroskaattachedfile.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
#include <memory>
1+
/***************************************************************************
2+
* This library is free software; you can redistribute it and/or modify *
3+
* it under the terms of the GNU Lesser General Public License version *
4+
* 2.1 as published by the Free Software Foundation. *
5+
* *
6+
* This library is distributed in the hope that it will be useful, but *
7+
* WITHOUT ANY WARRANTY; without even the implied warranty of *
8+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
9+
* Lesser General Public License for more details. *
10+
* *
11+
* You should have received a copy of the GNU Lesser General Public *
12+
* License along with this library; if not, write to the Free Software *
13+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
14+
* 02110-1301 USA *
15+
* *
16+
* Alternatively, this file is available under the Mozilla Public *
17+
* License Version 1.1. You may obtain a copy of the License at *
18+
* http://www.mozilla.org/MPL/ *
19+
***************************************************************************/
20+
221
#include "matroskaattachedfile.h"
322
#include "tstring.h"
423
#include "tbytevector.h"
@@ -10,21 +29,46 @@ class Matroska::AttachedFile::AttachedFilePrivate
1029
public:
1130
AttachedFilePrivate() = default;
1231
~AttachedFilePrivate() = default;
13-
AttachedFilePrivate(const AttachedFilePrivate &) = delete;
14-
AttachedFilePrivate &operator=(const AttachedFilePrivate &) = delete;
1532
String fileName;
1633
String description;
1734
String mediaType;
1835
ByteVector data;
1936
UID uid = 0;
2037
};
2138

39+
////////////////////////////////////////////////////////////////////////////////
40+
// public members
41+
////////////////////////////////////////////////////////////////////////////////
42+
2243
Matroska::AttachedFile::AttachedFile() :
2344
d(std::make_unique<AttachedFilePrivate>())
2445
{
2546
}
47+
48+
Matroska::AttachedFile::AttachedFile(const AttachedFile &other) :
49+
d(std::make_unique<AttachedFilePrivate>(*other.d))
50+
{
51+
}
52+
53+
Matroska::AttachedFile::AttachedFile(AttachedFile&& other) noexcept = default;
54+
2655
Matroska::AttachedFile::~AttachedFile() = default;
2756

57+
Matroska::AttachedFile &Matroska::AttachedFile::operator=(AttachedFile &&other) = default;
58+
59+
Matroska::AttachedFile &Matroska::AttachedFile::operator=(const AttachedFile &other)
60+
{
61+
AttachedFile(other).swap(*this);
62+
return *this;
63+
}
64+
65+
void Matroska::AttachedFile::swap(AttachedFile &other) noexcept
66+
{
67+
using std::swap;
68+
69+
swap(d, other.d);
70+
}
71+
2872
void Matroska::AttachedFile::setFileName(const String &fileName)
2973
{
3074
d->fileName = fileName;

taglib/matroska/matroskaattachedfile.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,98 @@
2121
#ifndef TAGLIB_MATROSKAATTACHEDFILE_H
2222
#define TAGLIB_MATROSKAATTACHEDFILE_H
2323

24+
#include <memory>
2425
#include "taglib_export.h"
2526

2627
namespace TagLib {
2728
class String;
2829
class ByteVector;
2930
namespace Matroska {
31+
//! Attached file embedded into a Matroska file.
3032
class TAGLIB_EXPORT AttachedFile
3133
{
3234
public:
3335
using UID = unsigned long long;
3436
AttachedFile();
37+
38+
/*!
39+
* Construct an attached file as a copy of \a other.
40+
*/
41+
AttachedFile(const AttachedFile &other);
42+
43+
/*!
44+
* Construct an attached file moving from \a other.
45+
*/
46+
AttachedFile(AttachedFile &&other) noexcept;
47+
48+
/*!
49+
* Destroys this attached file.
50+
*/
3551
~AttachedFile();
3652

53+
/*!
54+
* Copies the contents of \a other into this object.
55+
*/
56+
AttachedFile &operator=(const AttachedFile &other);
57+
58+
/*!
59+
* Moves the contents of \a other into this object.
60+
*/
61+
AttachedFile &operator=(AttachedFile &&other);
62+
63+
/*!
64+
* Exchanges the content of the object with the content of \a other.
65+
*/
66+
void swap(AttachedFile &other) noexcept;
67+
68+
/*!
69+
* Set the \a fileName of the attached file.
70+
*/
3771
void setFileName(const String &fileName);
72+
73+
/*!
74+
* Returns the filename of the attached file.
75+
*/
3876
const String &fileName() const;
77+
78+
/*!
79+
* Set a human-friendly \a description for the attached file.
80+
*/
3981
void setDescription(const String &description);
82+
83+
/*!
84+
* Returns the human-friendly description for the attached file.
85+
*/
4086
const String &description() const;
87+
88+
/*!
89+
* Set the \a mediaType of the attached file.
90+
*/
4191
void setMediaType(const String &mediaType);
92+
93+
/*!
94+
* Returns the media type of the attached file.
95+
*/
4296
const String &mediaType() const;
97+
98+
/*!
99+
* Set the data of the attached file.
100+
*/
43101
void setData(const ByteVector &data);
102+
103+
/*!
104+
* Returns the data of the attached file.
105+
*/
44106
const ByteVector &data() const;
107+
108+
/*!
109+
* Set the \a uid representing the file, as random as possible.
110+
*/
45111
void setUID(UID uid);
112+
113+
/*!
114+
* Returns the UID of the attached file.
115+
*/
46116
UID uid() const;
47117

48118
private:

taglib/matroska/matroskaattachments.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/***************************************************************************
2+
* This library is free software; you can redistribute it and/or modify *
3+
* it under the terms of the GNU Lesser General Public License version *
4+
* 2.1 as published by the Free Software Foundation. *
5+
* *
6+
* This library is distributed in the hope that it will be useful, but *
7+
* WITHOUT ANY WARRANTY; without even the implied warranty of *
8+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
9+
* Lesser General Public License for more details. *
10+
* *
11+
* You should have received a copy of the GNU Lesser General Public *
12+
* License along with this library; if not, write to the Free Software *
13+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
14+
* 02110-1301 USA *
15+
* *
16+
* Alternatively, this file is available under the Mozilla Public *
17+
* License Version 1.1. You may obtain a copy of the License at *
18+
* http://www.mozilla.org/MPL/ *
19+
***************************************************************************/
20+
121
#include "matroskaattachments.h"
222
#include <memory>
323
#include "matroskaattachedfile.h"
@@ -18,27 +38,33 @@ class Matroska::Attachments::AttachmentsPrivate
1838
~AttachmentsPrivate() = default;
1939
AttachmentsPrivate(const AttachmentsPrivate &) = delete;
2040
AttachmentsPrivate &operator=(const AttachmentsPrivate &) = delete;
21-
List<AttachedFile *> files;
41+
AttachedFileList files;
2242
};
2343

44+
////////////////////////////////////////////////////////////////////////////////
45+
// public members
46+
////////////////////////////////////////////////////////////////////////////////
47+
2448
Matroska::Attachments::Attachments() :
2549
Element(static_cast<ID>(EBML::Element::Id::MkAttachments)),
2650
d(std::make_unique<AttachmentsPrivate>())
2751
{
28-
d->files.setAutoDelete(true);
2952
}
53+
3054
Matroska::Attachments::~Attachments() = default;
3155

32-
void Matroska::Attachments::addAttachedFile(AttachedFile *file)
56+
void Matroska::Attachments::addAttachedFile(const AttachedFile& file)
3357
{
3458
d->files.append(file);
3559
}
3660

37-
void Matroska::Attachments::removeAttachedFile(AttachedFile *file)
61+
void Matroska::Attachments::removeAttachedFile(unsigned long long uid)
3862
{
39-
auto it = d->files.find(file);
63+
auto it = std::find_if(d->files.begin(), d->files.end(),
64+
[uid](const AttachedFile& file) {
65+
return file.uid() == uid;
66+
});
4067
if(it != d->files.end()) {
41-
delete *it;
4268
d->files.erase(it);
4369
}
4470
}
@@ -53,6 +79,10 @@ const Matroska::Attachments::AttachedFileList &Matroska::Attachments::attachedFi
5379
return d->files;
5480
}
5581

82+
////////////////////////////////////////////////////////////////////////////////
83+
// private members
84+
////////////////////////////////////////////////////////////////////////////////
85+
5686
Matroska::Attachments::AttachedFileList &Matroska::Attachments::attachedFiles()
5787
{
5888
return d->files;
@@ -61,21 +91,21 @@ Matroska::Attachments::AttachedFileList &Matroska::Attachments::attachedFiles()
6191
bool Matroska::Attachments::render()
6292
{
6393
EBML::MkAttachments attachments;
64-
for(const auto attachedFile : d->files) {
94+
for(const auto &attachedFile : std::as_const(d->files)) {
6595
auto attachedFileElement = EBML::make_unique_element<EBML::Element::Id::MkAttachedFile>();
6696

6797
// Filename
6898
auto fileNameElement = EBML::make_unique_element<EBML::Element::Id::MkAttachedFileName>();
69-
fileNameElement->setValue(attachedFile->fileName());
99+
fileNameElement->setValue(attachedFile.fileName());
70100
attachedFileElement->appendElement(std::move(fileNameElement));
71101

72102
// Media/MIME type
73103
auto mediaTypeElement = EBML::make_unique_element<EBML::Element::Id::MkAttachedFileMediaType>();
74-
mediaTypeElement->setValue(attachedFile->mediaType());
104+
mediaTypeElement->setValue(attachedFile.mediaType());
75105
attachedFileElement->appendElement(std::move(mediaTypeElement));
76106

77107
// Description
78-
const String &description = attachedFile->description();
108+
const String &description = attachedFile.description();
79109
if(!description.isEmpty()) {
80110
auto descriptionElement = EBML::make_unique_element<EBML::Element::Id::MkAttachedFileDescription>();
81111
descriptionElement->setValue(description);
@@ -84,12 +114,12 @@ bool Matroska::Attachments::render()
84114

85115
// Data
86116
auto dataElement = EBML::make_unique_element<EBML::Element::Id::MkAttachedFileData>();
87-
dataElement->setValue(attachedFile->data());
117+
dataElement->setValue(attachedFile.data());
88118
attachedFileElement->appendElement(std::move(dataElement));
89119

90120
// UID
91121
auto uidElement = EBML::make_unique_element<EBML::Element::Id::MkAttachedFileUID>();
92-
AttachedFile::UID uid = attachedFile->uid();
122+
AttachedFile::UID uid = attachedFile.uid();
93123
if(!uid)
94124
uid = EBML::randomUID();
95125
uidElement->setValue(uid);

taglib/matroska/matroskaattachments.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,31 @@ namespace TagLib {
3434
namespace Matroska {
3535
class AttachedFile;
3636
class File;
37+
38+
//! Collection of attached files.
3739
class TAGLIB_EXPORT Attachments
3840
#ifndef DO_NOT_DOCUMENT
3941
: private Element
4042
#endif
4143
{
4244
public:
43-
using AttachedFileList = List<AttachedFile *>;
45+
using AttachedFileList = List<AttachedFile>;
46+
//! Construct attachments.
4447
Attachments();
45-
virtual ~Attachments();
4648

47-
void addAttachedFile(AttachedFile *file);
48-
void removeAttachedFile(AttachedFile *file);
49+
//! Destroy attachements.
50+
~Attachments();
51+
52+
//! Add an attached file.
53+
void addAttachedFile(const AttachedFile &file);
54+
55+
//! Remove an attached file.
56+
void removeAttachedFile(unsigned long long uid);
57+
58+
//! Remove an attached file.
4959
void clear();
60+
61+
//! Get list of all attached files.
5062
const AttachedFileList &attachedFileList() const;
5163

5264
private:

0 commit comments

Comments
 (0)