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+
2448Matroska::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+
3054Matroska::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+
5686Matroska::Attachments::AttachedFileList &Matroska::Attachments::attachedFiles ()
5787{
5888 return d->files ;
@@ -61,21 +91,21 @@ Matroska::Attachments::AttachedFileList &Matroska::Attachments::attachedFiles()
6191bool 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);
0 commit comments