diff --git a/taglib/toolkit/tlist.h b/taglib/toolkit/tlist.h index 3b445d89..3a350d48 100644 --- a/taglib/toolkit/tlist.h +++ b/taglib/toolkit/tlist.h @@ -232,6 +232,11 @@ namespace TagLib { */ List &operator=(const List &l); + /*! + * Exchanges the content of this list by the content of \a l. + */ + void swap(List &l); + /*! * Compares this list with \a l and returns true if all of the elements are * the same. diff --git a/taglib/toolkit/tlist.tcc b/taglib/toolkit/tlist.tcc index 4affbbd3..858606de 100644 --- a/taglib/toolkit/tlist.tcc +++ b/taglib/toolkit/tlist.tcc @@ -118,8 +118,8 @@ private: //////////////////////////////////////////////////////////////////////////////// template -List::List() - : d(new ListPrivate()) +List::List() : + d(new ListPrivate()) { } @@ -316,16 +316,18 @@ const T &List::operator[](size_t i) const template List &List::operator=(const List &l) { - if(&l == this) - return *this; - - if(d->deref()) - delete d; - d = l.d; - d->ref(); + List(l).swap(*this); return *this; } +template +void List::swap(List &l) +{ + using std::swap; + + swap(d, l.d); +} + template bool List::operator==(const List &l) const { diff --git a/taglib/toolkit/tmap.h b/taglib/toolkit/tmap.h index 2dcd02a1..dd4e3811 100644 --- a/taglib/toolkit/tmap.h +++ b/taglib/toolkit/tmap.h @@ -178,6 +178,11 @@ namespace TagLib { */ Map &operator=(const Map &m); + /*! + * Exchanges the content of this map by the content of \a m. + */ + void swap(Map &m); + protected: /* * If this List is being shared via implicit sharing, do a deep copy of the diff --git a/taglib/toolkit/tmap.tcc b/taglib/toolkit/tmap.tcc index 9f06ee21..dcd98802 100644 --- a/taglib/toolkit/tmap.tcc +++ b/taglib/toolkit/tmap.tcc @@ -51,8 +51,8 @@ public: }; template -Map::Map() - : d(new MapPrivate()) +Map::Map() : + d(new MapPrivate()) { } @@ -174,16 +174,18 @@ T &Map::operator[](const Key &key) template Map &Map::operator=(const Map &m) { - if(&m == this) - return *this; - - if(d->deref()) - delete(d); - d = m.d; - d->ref(); + Map(m).swap(*this); return *this; } +template +void Map::swap(Map &m) +{ + using std::swap; + + swap(d, m.d); +} + //////////////////////////////////////////////////////////////////////////////// // protected members //////////////////////////////////////////////////////////////////////////////// diff --git a/tests/test_asf.cpp b/tests/test_asf.cpp index 73af3828..f98458de 100644 --- a/tests/test_asf.cpp +++ b/tests/test_asf.cpp @@ -310,10 +310,10 @@ public: { ASF::File f(copy.fileName().c_str()); - f.tag()->setTitle(std::string(128 * 1024, 'X').c_str()); + f.tag()->setTitle(longText(128 * 1024)); f.save(); CPPUNIT_ASSERT_EQUAL(297578LL, f.length()); - f.tag()->setTitle(std::string(16 * 1024, 'X').c_str()); + f.tag()->setTitle(longText(16 * 1024)); f.save(); CPPUNIT_ASSERT_EQUAL(68202LL, f.length()); } diff --git a/tests/test_flac.cpp b/tests/test_flac.cpp index c77d78f5..18a38508 100644 --- a/tests/test_flac.cpp +++ b/tests/test_flac.cpp @@ -259,7 +259,7 @@ public: ScopedFileCopy copy("no-tags", ".flac"); FLAC::File f(copy.fileName().c_str()); - f.xiphComment()->setTitle(std::string(8 * 1024, 'X').c_str()); + f.xiphComment()->setTitle(longText(8 * 1024)); f.save(); CPPUNIT_ASSERT_EQUAL(12862LL, f.length()); f.save(); @@ -372,7 +372,7 @@ public: { FLAC::File f(copy.fileName().c_str()); - f.xiphComment()->setTitle(std::wstring(128 * 1024, L'X').c_str()); + f.xiphComment()->setTitle(longText(128 * 1024)); f.save(); CPPUNIT_ASSERT(f.length() > 128 * 1024); } diff --git a/tests/test_id3v2.cpp b/tests/test_id3v2.cpp index 1c6f19bd..a97750a3 100644 --- a/tests/test_id3v2.cpp +++ b/tests/test_id3v2.cpp @@ -1147,7 +1147,7 @@ public: { MPEG::File f(newname.c_str()); - f.ID3v2Tag()->setTitle(std::wstring(64 * 1024, L'X').c_str()); + f.ID3v2Tag()->setTitle(longText(64 * 1024)); f.save(MPEG::File::ID3v2, true); } { diff --git a/tests/test_ogg.cpp b/tests/test_ogg.cpp index 801321ef..d271aac2 100644 --- a/tests/test_ogg.cpp +++ b/tests/test_ogg.cpp @@ -72,13 +72,11 @@ public: ScopedFileCopy copy("empty", ".ogg"); string newname = copy.fileName(); - String longText(std::string(128 * 1024, ' ').c_str()); - for (size_t i = 0; i < longText.length(); ++i) - longText[i] = static_cast(L'A' + (i % 26)); + const String text = longText(128 * 1024, true); { Ogg::Vorbis::File f(newname.c_str()); - f.tag()->setTitle(longText); + f.tag()->setTitle(text); f.save(); } { @@ -89,7 +87,7 @@ public: CPPUNIT_ASSERT_EQUAL((size_t)30, f.packet(0).size()); CPPUNIT_ASSERT_EQUAL((size_t)131127, f.packet(1).size()); CPPUNIT_ASSERT_EQUAL((size_t)3832, f.packet(2).size()); - CPPUNIT_ASSERT_EQUAL(longText, f.tag()->title()); + CPPUNIT_ASSERT_EQUAL(text, f.tag()->title()); CPPUNIT_ASSERT(f.audioProperties()); CPPUNIT_ASSERT_EQUAL(3685, f.audioProperties()->lengthInMilliseconds()); diff --git a/tests/test_oggflac.cpp b/tests/test_oggflac.cpp index aa9cedc1..3c18b5f6 100644 --- a/tests/test_oggflac.cpp +++ b/tests/test_oggflac.cpp @@ -77,13 +77,11 @@ public: ScopedFileCopy copy("empty_flac", ".oga"); string newname = copy.fileName(); - String longText(std::string(128 * 1024, ' ').c_str()); - for(size_t i = 0; i < longText.length(); ++i) - longText[i] = static_cast(L'A' + (i % 26)); + const String text = longText(128 * 1024, true); { Ogg::FLAC::File f(newname.c_str()); - f.tag()->setTitle(longText); + f.tag()->setTitle(text); f.save(); } { @@ -95,7 +93,7 @@ public: CPPUNIT_ASSERT_EQUAL((size_t)131126, f.packet(1).size()); CPPUNIT_ASSERT_EQUAL((size_t)22, f.packet(2).size()); CPPUNIT_ASSERT_EQUAL((size_t)8196, f.packet(3).size()); - CPPUNIT_ASSERT_EQUAL(longText, f.tag()->title()); + CPPUNIT_ASSERT_EQUAL(text, f.tag()->title()); CPPUNIT_ASSERT(f.audioProperties()); CPPUNIT_ASSERT_EQUAL(3705, f.audioProperties()->lengthInMilliseconds()); diff --git a/tests/test_opus.cpp b/tests/test_opus.cpp index ac34076c..e1a2f191 100644 --- a/tests/test_opus.cpp +++ b/tests/test_opus.cpp @@ -93,13 +93,11 @@ public: ScopedFileCopy copy("correctness_gain_silent_output", ".opus"); string newname = copy.fileName(); - String longText(std::string(128 * 1024, ' ').c_str()); - for(size_t i = 0; i < longText.length(); ++i) - longText[i] = static_cast(L'A' + (i % 26)); + const String text = longText(128 * 1024, true); { Ogg::Opus::File f(newname.c_str()); - f.tag()->setTitle(longText); + f.tag()->setTitle(text); f.save(); } { @@ -111,7 +109,7 @@ public: CPPUNIT_ASSERT_EQUAL((size_t)131380, f.packet(1).size()); CPPUNIT_ASSERT_EQUAL((size_t)5, f.packet(2).size()); CPPUNIT_ASSERT_EQUAL((size_t)5, f.packet(3).size()); - CPPUNIT_ASSERT_EQUAL(longText, f.tag()->title()); + CPPUNIT_ASSERT_EQUAL(text, f.tag()->title()); CPPUNIT_ASSERT(f.audioProperties()); CPPUNIT_ASSERT_EQUAL(7737, f.audioProperties()->lengthInMilliseconds()); diff --git a/tests/test_speex.cpp b/tests/test_speex.cpp index 8af9de16..72266ff8 100644 --- a/tests/test_speex.cpp +++ b/tests/test_speex.cpp @@ -58,13 +58,11 @@ public: ScopedFileCopy copy("empty", ".spx"); string newname = copy.fileName(); - String longText(std::string(128 * 1024, ' ').c_str()); - for (size_t i = 0; i < longText.length(); ++i) - longText[i] = static_cast(L'A' + (i % 26)); + const String text = longText(128 * 1024, true); { Ogg::Speex::File f(newname.c_str()); - f.tag()->setTitle(longText); + f.tag()->setTitle(text); f.save(); } { @@ -76,7 +74,7 @@ public: CPPUNIT_ASSERT_EQUAL((size_t)131116, f.packet(1).size()); CPPUNIT_ASSERT_EQUAL((size_t)93, f.packet(2).size()); CPPUNIT_ASSERT_EQUAL((size_t)93, f.packet(3).size()); - CPPUNIT_ASSERT_EQUAL(longText, f.tag()->title()); + CPPUNIT_ASSERT_EQUAL(text, f.tag()->title()); CPPUNIT_ASSERT(f.audioProperties()); CPPUNIT_ASSERT_EQUAL(3685, f.audioProperties()->lengthInMilliseconds()); diff --git a/tests/utils.h b/tests/utils.h index 7cfe9a8d..51d8862b 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -103,13 +103,34 @@ inline bool fileEqual(const string &filename1, const string &filename2) return stream1.good() == stream2.good(); } +#ifdef TAGLIB_STRING_H + +namespace TagLib { + + inline String longText(size_t length, bool random = false) + { + const wchar_t chars[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"; + + std::wstring text(length, L'X'); + + if(random) { + for(size_t i = 0; i < length; ++i) + text[i] = chars[rand() % 53]; + } + + return String(text); + } +} + +#endif + class ScopedFileCopy { public: - ScopedFileCopy(const string &filename, const string &ext, bool deleteFile=true) + ScopedFileCopy(const string &filename, const string &ext, bool deleteFile=true) : + m_deleteFile(deleteFile), + m_filename(copyFile(filename, ext)) { - m_deleteFile = deleteFile; - m_filename = copyFile(filename, ext); } ~ScopedFileCopy() @@ -118,12 +139,12 @@ public: deleteFile(m_filename); } - string fileName() + string fileName() const { return m_filename; } private: - bool m_deleteFile; - string m_filename; + const bool m_deleteFile; + const string m_filename; };