Permalink
Browse files

Replace RefCounter with std::shared_ptr if possible

  • Loading branch information...
1 parent 42a74ba commit 96cf90823279b4191b917cb36a00f8a219bf374b @TsudaKageyu committed Mar 18, 2013
View
69 taglib/asf/asfattribute.cpp
@@ -60,81 +60,108 @@ class ASF::Attribute::AttributePrivate : public RefCounter
////////////////////////////////////////////////////////////////////////////////
ASF::Attribute::Attribute()
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = UnicodeType;
}
ASF::Attribute::Attribute(const ASF::Attribute &other)
: d(other.d)
{
- d->ref();
-}
+#ifndef TAGLIB_USE_CXX11
-ASF::Attribute &ASF::Attribute::operator=(const ASF::Attribute &other)
-{
- if(d->deref())
- delete d;
- d = other.d;
d->ref();
- return *this;
-}
-ASF::Attribute::~Attribute()
-{
- if(d->deref())
- delete d;
+#endif
}
ASF::Attribute::Attribute(const String &value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = UnicodeType;
d->stringValue = value;
}
ASF::Attribute::Attribute(const ByteVector &value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = BytesType;
d->byteVectorValue = value;
}
ASF::Attribute::Attribute(const ASF::Picture &value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = BytesType;
d->pictureValue = value;
}
ASF::Attribute::Attribute(unsigned int value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = DWordType;
d->intValue = value;
}
ASF::Attribute::Attribute(unsigned long long value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = QWordType;
d->longLongValue = value;
}
ASF::Attribute::Attribute(unsigned short value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = WordType;
d->shortValue = value;
}
ASF::Attribute::Attribute(bool value)
+ : d(new AttributePrivate())
{
- d = new AttributePrivate;
d->type = BoolType;
d->boolValue = value;
}
+ASF::Attribute::~Attribute()
+{
+#ifndef TAGLIB_USE_CXX11
+
+ if(d->deref())
+ delete d;
+
+#endif
+}
+
+ASF::Attribute &ASF::Attribute::operator=(const ASF::Attribute &other)
+{
+#ifdef TAGLIB_USE_CXX11
+
+ d = other.d;
+
+#else
+
+ if(d->deref())
+ delete d;
+ d = other.d;
+ d->ref();
+
+#endif
+
+ return *this;
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+ASF::Attribute &ASF::Attribute::operator=(ASF::Attribute &&other)
+{
+ d = std::move(other.d);
+ return *this;
+}
+
+#endif
+
ASF::Attribute::AttributeTypes ASF::Attribute::type() const
{
return d->type;
View
14 taglib/asf/asfattribute.h
@@ -115,6 +115,15 @@ namespace TagLib
*/
ASF::Attribute &operator=(const Attribute &other);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves the contents of \a other into this item.
+ */
+ ASF::Attribute &operator=(Attribute &&other);
+
+#endif
+
/*!
* Destroys the attribute.
*/
@@ -194,7 +203,12 @@ namespace TagLib
ByteVector render(const String &name, int kind = 0) const;
class AttributePrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<AttributePrivate> d;
+#else
AttributePrivate *d;
+#endif
};
}
View
22 taglib/asf/asffile.cpp
@@ -60,17 +60,17 @@ class ASF::File::FilePrivate
ASF::File::MetadataLibraryObject *metadataLibraryObject;
};
-static ByteVector headerGuid("\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C", 16);
-static ByteVector filePropertiesGuid("\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65", 16);
-static ByteVector streamPropertiesGuid("\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65", 16);
-static ByteVector contentDescriptionGuid("\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C", 16);
-static ByteVector extendedContentDescriptionGuid("\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50", 16);
-static ByteVector headerExtensionGuid("\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se", 16);
-static ByteVector metadataGuid("\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA", 16);
-static ByteVector metadataLibraryGuid("\224\034#D\230\224\321I\241A\x1d\x13NEpT", 16);
-static ByteVector contentEncryptionGuid("\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E", 16);
-static ByteVector extendedContentEncryptionGuid("\x14\xE6\x8A\x29\x22\x26 \x17\x4C\xB9\x35\xDA\xE0\x7E\xE9\x28\x9C", 16);
-static ByteVector advancedContentEncryptionGuid("\xB6\x9B\x07\x7A\xA4\xDA\x12\x4E\xA5\xCA\x91\xD3\x8D\xC1\x1A\x8D", 16);
+static const ByteVector headerGuid("\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C", 16);
+static const ByteVector filePropertiesGuid("\xA1\xDC\xAB\x8C\x47\xA9\xCF\x11\x8E\xE4\x00\xC0\x0C\x20\x53\x65", 16);
+static const ByteVector streamPropertiesGuid("\x91\x07\xDC\xB7\xB7\xA9\xCF\x11\x8E\xE6\x00\xC0\x0C\x20\x53\x65", 16);
+static const ByteVector contentDescriptionGuid("\x33\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C", 16);
+static const ByteVector extendedContentDescriptionGuid("\x40\xA4\xD0\xD2\x07\xE3\xD2\x11\x97\xF0\x00\xA0\xC9\x5E\xA8\x50", 16);
+static const ByteVector headerExtensionGuid("\xb5\x03\xbf_.\xa9\xcf\x11\x8e\xe3\x00\xc0\x0c Se", 16);
+static const ByteVector metadataGuid("\xEA\xCB\xF8\xC5\xAF[wH\204g\xAA\214D\xFAL\xCA", 16);
+static const ByteVector metadataLibraryGuid("\224\034#D\230\224\321I\241A\x1d\x13NEpT", 16);
+static const ByteVector contentEncryptionGuid("\xFB\xB3\x11\x22\x23\xBD\xD2\x11\xB4\xB7\x00\xA0\xC9\x55\xFC\x6E", 16);
+static const ByteVector extendedContentEncryptionGuid("\x14\xE6\x8A\x29\x22\x26 \x17\x4C\xB9\x35\xDA\xE0\x7E\xE9\x28\x9C", 16);
+static const ByteVector advancedContentEncryptionGuid("\xB6\x9B\x07\x7A\xA4\xDA\x12\x4E\xA5\xCA\x91\xD3\x8D\xC1\x1A\x8D", 16);
class ASF::File::BaseObject
{
View
39 taglib/asf/asfpicture.cpp
@@ -50,21 +50,38 @@ class ASF::Picture::PicturePrivate : public RefCounter
////////////////////////////////////////////////////////////////////////////////
ASF::Picture::Picture()
+ : d(new PicturePrivate())
{
- d = new PicturePrivate();
d->valid = true;
}
ASF::Picture::Picture(const Picture& other)
: d(other.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+ASF::Picture::Picture(Picture &&other)
+ : d(std::move(other.d))
+{
}
+#endif
+
ASF::Picture::~Picture()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref())
delete d;
+
+#endif
}
bool ASF::Picture::isValid() const
@@ -121,15 +138,35 @@ int ASF::Picture::dataSize() const
ASF::Picture& ASF::Picture::operator=(const ASF::Picture& other)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = other.d;
+
+#else
+
if(other.d != d) {
if(d->deref())
delete d;
d = other.d;
d->ref();
}
+
+#endif
+
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+ASF::Picture& ASF::Picture::operator=(ASF::Picture &&other)
+{
+ d = std::move(other.d);
+ return *this;
+}
+
+#endif
+
+
ByteVector ASF::Picture::render() const
{
if(!isValid())
View
33 taglib/asf/asfpicture.h
@@ -103,10 +103,19 @@ namespace TagLib
Picture();
/*!
- * Construct an picture as a copy of \a other.
+ * Constructs an picture as a copy of \a other.
*/
Picture(const Picture& other);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Constructs an picture equivalent to \a other.
+ */
+ Picture(Picture &&other);
+
+#endif
+
/*!
* Destroys the picture.
*/
@@ -117,6 +126,15 @@ namespace TagLib
*/
Picture& operator=(const Picture& other);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves the contents of \a other into this picture.
+ */
+ Picture& operator=(Picture &&other);
+
+#endif
+
/*!
* Returns true if Picture stores valid picture
*/
@@ -207,10 +225,15 @@ namespace TagLib
static Picture fromInvalid();
friend class Attribute;
#endif
- private:
- class PicturePrivate;
- PicturePrivate *d;
- };
+ private:
+ class PicturePrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<PicturePrivate> d;
+#else
+ PicturePrivate *d;
+#endif
+ };
}
}
View
105 taglib/fileref.cpp
@@ -31,6 +31,10 @@
#include <config.h>
#endif
+#ifdef _WIN32
+# include <Shlwapi.h>
+#endif
+
#include <tfile.h>
#include <tstring.h>
#include <tdebug.h>
@@ -60,12 +64,24 @@ using namespace TagLib;
class FileRef::FileRefPrivate : public RefCounter
{
public:
- FileRefPrivate(File *f) : RefCounter(), file(f) {}
- ~FileRefPrivate() {
+ FileRefPrivate(File *f)
+ : RefCounter(), file(f) {}
+
+ ~FileRefPrivate()
+ {
+#ifndef TAGLIB_USE_CXX11
+
delete file;
+
+#endif
}
+#ifdef TAGLIB_USE_CXX11
+ std::unique_ptr<File> file;
+#else
File *file;
+#endif
+
static List<const FileTypeResolver *> fileTypeResolvers;
};
@@ -76,30 +92,48 @@ List<const FileRef::FileTypeResolver *> FileRef::FileRefPrivate::fileTypeResolve
////////////////////////////////////////////////////////////////////////////////
FileRef::FileRef()
+ : d(new FileRefPrivate(0))
{
- d = new FileRefPrivate(0);
}
-FileRef::FileRef(FileName fileName, bool readAudioProperties,
- AudioProperties::ReadStyle audioPropertiesStyle)
+FileRef::FileRef(FileName fileName,
+ bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle)
+ : d(new FileRefPrivate(create(fileName, readAudioProperties, audioPropertiesStyle)))
{
- d = new FileRefPrivate(create(fileName, readAudioProperties, audioPropertiesStyle));
}
FileRef::FileRef(File *file)
+ : d(new FileRefPrivate(file))
{
- d = new FileRefPrivate(file);
}
-FileRef::FileRef(const FileRef &ref) : d(ref.d)
+FileRef::FileRef(const FileRef &ref)
+ : d(ref.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+FileRef::FileRef(FileRef &&ref)
+ : d(std::move(ref.d))
+{
}
+#endif
+
FileRef::~FileRef()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref())
delete d;
+
+#endif
}
Tag *FileRef::tag() const
@@ -122,7 +156,15 @@ AudioProperties *FileRef::audioProperties() const
File *FileRef::file() const
{
+#ifdef TAGLIB_USE_CXX11
+
+ return d->file.get();
+
+#else
+
return d->file;
+
+#endif
}
bool FileRef::save()
@@ -182,6 +224,12 @@ bool FileRef::isNull() const
FileRef &FileRef::operator=(const FileRef &ref)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = ref.d;
+
+#else
+
if(&ref == this)
return *this;
@@ -191,9 +239,21 @@ FileRef &FileRef::operator=(const FileRef &ref)
d = ref.d;
d->ref();
+#endif
+
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+FileRef &FileRef::operator=(FileRef &&ref)
+{
+ d = std::move(ref.d);
+ return *this;
+}
+
+#endif
+
bool FileRef::operator==(const FileRef &ref) const
{
return ref.d->file == d->file;
@@ -216,23 +276,36 @@ File *FileRef::create(FileName fileName, bool readAudioProperties,
return file;
}
- // Ok, this is really dumb for now, but it works for testing.
-
- String s;
+ String ext;
#ifdef _WIN32
- s = (wcslen((const wchar_t *) fileName) > 0) ? String((const wchar_t *) fileName) : String((const char *) fileName);
+ // Avoids direct conversion from FileName to String
+ // because String can't accept non-Latin-1 string in char array.
+
+ if(!fileName.wstr().empty()) {
+ const wchar_t *pext = PathFindExtensionW(fileName.wstr().c_str());
+ if(*pext == L'.')
+ ext = String(pext + 1).upper();
+ }
+ else {
+ const char *pext = PathFindExtensionA(fileName.str().c_str());
+ if(*pext == '.')
+ ext = String(pext + 1).upper();
+ }
#else
- s = fileName;
+ {
+ String s = fileName;
+ int pos = s.rfind(".");
+ if(pos != -1)
+ ext = s.substr(pos + 1).upper();
+ }
#endif
// If this list is updated, the method defaultFileExtensions() should also be
// updated. However at some point that list should be created at the same time
// that a default file type resolver is created.
- int pos = s.rfind(".");
- if(pos != -1) {
- String ext = s.substr(pos + 1).upper();
+ if(!ext.isEmpty()) {
if(ext == "MP3")
return new MPEG::File(fileName, readAudioProperties, audioPropertiesStyle);
if(ext == "OGG")
View
25 taglib/fileref.h
@@ -92,7 +92,7 @@ namespace TagLib {
class TAGLIB_EXPORT FileTypeResolver
{
public:
- virtual ~FileTypeResolver();
+ virtual ~FileTypeResolver() {}
/*!
* This method must be overridden to provide an additional file type
@@ -139,6 +139,15 @@ namespace TagLib {
*/
FileRef(const FileRef &ref);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Move \a ref into the FileRef.
+ */
+ FileRef(FileRef &&ref);
+
+#endif
+
/*!
* Destroys this FileRef instance.
*/
@@ -227,6 +236,15 @@ namespace TagLib {
*/
FileRef &operator=(const FileRef &ref);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Assign the file pointed to by \a ref to this FileRef.
+ */
+ FileRef &operator=(FileRef &&ref);
+
+#endif
+
/*!
* Returns true if this FileRef and \a ref point to the same File object.
*/
@@ -256,7 +274,12 @@ namespace TagLib {
private:
class FileRefPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<FileRefPrivate> d;
+#else
FileRefPrivate *d;
+#endif
};
} // namespace TagLib
View
32 taglib/mp4/mp4coverart.cpp
@@ -43,33 +43,61 @@ class MP4::CoverArt::CoverArtPrivate : public RefCounter
};
MP4::CoverArt::CoverArt(Format format, const ByteVector &data)
+ : d(new CoverArtPrivate())
{
- d = new CoverArtPrivate;
d->format = format;
d->data = data;
}
MP4::CoverArt::CoverArt(const CoverArt &item) : d(item.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
}
MP4::CoverArt &
-MP4::CoverArt::operator=(const CoverArt &item)
+ MP4::CoverArt::operator=(const CoverArt &item)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = item.d;
+
+#else
+
if(d->deref()) {
delete d;
}
d = item.d;
d->ref();
+
+#endif
+
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+MP4::CoverArt &
+ MP4::CoverArt::operator=(CoverArt &&item)
+{
+ d = std::move(item.d);
+ return *this;
+}
+
+#endif
+
MP4::CoverArt::~CoverArt()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref()) {
delete d;
}
+
+#endif
}
MP4::CoverArt::Format
View
8 taglib/mp4/mp4coverart.h
@@ -54,6 +54,9 @@ namespace TagLib {
CoverArt(const CoverArt &item);
CoverArt &operator=(const CoverArt &item);
+#ifdef TAGLIB_USE_CXX11
+ CoverArt &operator=(CoverArt &&item);
+#endif
//! Format of the image
Format format() const;
@@ -63,7 +66,12 @@ namespace TagLib {
private:
class CoverArtPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<CoverArtPrivate> d;
+#else
CoverArtPrivate *d;
+#endif
};
typedef List<CoverArt> CoverArtList;
View
62 taglib/mp4/mp4item.cpp
@@ -62,94 +62,132 @@ class MP4::Item::ItemPrivate : public RefCounter
};
MP4::Item::Item()
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->valid = false;
}
-MP4::Item::Item(const Item &item) : d(item.d)
+MP4::Item::Item(const Item &item)
+ : d(item.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+MP4::Item::Item(Item &&item)
+ : d(std::move(item.d))
+{
}
+#endif
+
MP4::Item &
-MP4::Item::operator=(const Item &item)
+ MP4::Item::operator=(const Item &item)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = item.d;
+
+#else
+
if(d->deref()) {
delete d;
}
d = item.d;
d->ref();
+
+#endif
+
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+MP4::Item &
+ MP4::Item::operator=(Item &&item)
+{
+ d = std::move(item.d);
+ return *this;
+}
+
+#endif
+
MP4::Item::~Item()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref()) {
delete d;
}
+
+#endif
}
MP4::Item::Item(bool value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_bool = value;
d->type = TypeBool;
}
MP4::Item::Item(int value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_int = value;
d->type = TypeInt;
}
MP4::Item::Item(uchar value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_byte = value;
d->type = TypeByte;
}
MP4::Item::Item(uint value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_uint = value;
d->type = TypeUInt;
}
MP4::Item::Item(long long value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_longlong = value;
d->type = TypeLongLong;
}
MP4::Item::Item(int value1, int value2)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_intPair.first = value1;
d->m_intPair.second = value2;
d->type = TypeIntPair;
}
MP4::Item::Item(const ByteVectorList &value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_byteVectorList = value;
d->type = TypeByteVectorList;
}
MP4::Item::Item(const StringList &value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_stringList = value;
d->type = TypeStringList;
}
MP4::Item::Item(const MP4::CoverArtList &value)
+ : d(new ItemPrivate())
{
- d = new ItemPrivate;
d->m_coverArtList = value;
d->type = TypeCoverArtList;
}
View
12 taglib/mp4/mp4item.h
@@ -56,7 +56,14 @@ namespace TagLib {
Item();
Item(const Item &item);
+#ifdef TAGLIB_USE_CXX11
+ Item(Item &&item);
+#endif
+
Item &operator=(const Item &item);
+#ifdef TAGLIB_USE_CXX11
+ Item &operator=(Item &&item);
+#endif
~Item();
Item(int value);
@@ -90,7 +97,12 @@ namespace TagLib {
private:
class ItemPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<ItemPrivate> d;
+#else
ItemPrivate *d;
+#endif
};
}
View
41 taglib/mpeg/mpegheader.cpp
@@ -68,20 +68,38 @@ class MPEG::Header::HeaderPrivate : public RefCounter
////////////////////////////////////////////////////////////////////////////////
MPEG::Header::Header(const ByteVector &data)
+ : d(new HeaderPrivate())
{
- d = new HeaderPrivate;
parse(data);
}
-MPEG::Header::Header(const Header &h) : d(h.d)
+MPEG::Header::Header(const Header &h)
+ : d(h.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+MPEG::Header::Header(Header &&h)
+ : d(std::move(h.d))
+{
}
+#endif
+
MPEG::Header::~Header()
{
+#ifndef TAGLIB_USE_CXX11
+
if (d->deref())
delete d;
+
+#endif
}
bool MPEG::Header::isValid() const
@@ -146,6 +164,12 @@ int MPEG::Header::samplesPerFrame() const
MPEG::Header &MPEG::Header::operator=(const Header &h)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = h.d;
+
+#else
+
if(&h == this)
return *this;
@@ -154,9 +178,22 @@ MPEG::Header &MPEG::Header::operator=(const Header &h)
d = h.d;
d->ref();
+
+#endif
+
+ return *this;
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+MPEG::Header &MPEG::Header::operator=(Header &&h)
+{
+ d = std::move(h.d);
return *this;
}
+#endif
+
////////////////////////////////////////////////////////////////////////////////
// private members
////////////////////////////////////////////////////////////////////////////////
View
23 taglib/mpeg/mpegheader.h
@@ -56,6 +56,15 @@ namespace TagLib {
*/
Header(const Header &h);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a h into the \e Header.
+ */
+ Header(Header &&h);
+
+#endif
+
/*!
* Destroys this Header instance.
*/
@@ -154,11 +163,25 @@ namespace TagLib {
*/
Header &operator=(const Header &h);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a h into the \e Header.
+ */
+ Header &operator=(Header &&h);
+
+#endif
+
private:
void parse(const ByteVector &data);
class HeaderPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<HeaderPrivate> d;
+#else
HeaderPrivate *d;
+#endif
};
}
}
View
82 taglib/toolkit/taglib.h
@@ -38,34 +38,55 @@
#include <string>
-#ifndef _WIN32
+#ifdef _WIN32
+# if !defined(NOMINMAX)
+# define NOMINMAX
+# endif
+# include <windows.h>
+#else
+# ifndef _LARGEFILE_SOURCE
+# define _LARGEFILE_SOURCE
+# endif
+# define _FILE_OFFSET_BITS 64
+# include <sys/types.h>
+
+#endif
-# ifndef _LARGEFILE_SOURCE
-# define _LARGEFILE_SOURCE
-# endif
+// Determines if std::shared_ptr<T> and rvalue reference are available.
+// Replaces RefCounter capability with std::shared_ptr<T> if available.
-# define _FILE_OFFSET_BITS 64
-# include <sys/types.h>
+#ifndef __has_feature
+# define __has_feature(x) 0
+#endif
+
+#ifndef TAGLIB_USE_CXX11
+# if (defined(_MSC_VER) && _MSC_VER >= 1600) \
+ || (defined(__clang__) && __has_feature(cxx_rvalue_references)) \
+ || (defined(__GNUC__) && defined(__GXX_EXPERIMENTAL_CXX0X__))
+
+# define TAGLIB_USE_CXX11
+# endif
+#endif
+#ifdef TAGLIB_USE_CXX11
+# include <memory>
#endif
-#ifdef __APPLE__
-# include <libkern/OSAtomic.h>
-# define TAGLIB_ATOMIC_MAC
-#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
-# if !defined(NOMINMAX)
-# define NOMINMAX
-# endif
-# include <windows.h>
-# define TAGLIB_ATOMIC_WIN
-#elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 401) \
+#ifndef TAGLIB_USE_CXX11
+# ifdef __APPLE__
+# include <libkern/OSAtomic.h>
+# define TAGLIB_ATOMIC_MAC
+# elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
+# define TAGLIB_ATOMIC_WIN
+# elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 401) \
&& (defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__) || defined(__x86_64) || defined(__ia64)) \
&& !defined(__INTEL_COMPILER)
-# define TAGLIB_ATOMIC_GCC
-#elif defined(__ia64) && defined(__INTEL_COMPILER)
-# include <ia64intrin.h>
-# define TAGLIB_ATOMIC_GCC
+# define TAGLIB_ATOMIC_GCC
+# elif defined(__ia64) && defined(__INTEL_COMPILER)
+# include <ia64intrin.h>
+# define TAGLIB_ATOMIC_GCC
+# endif
#endif
//! A namespace for all TagLib related classes and functions
@@ -110,39 +131,46 @@ namespace TagLib {
* \warning This <b>is not</b> part of the TagLib public API!
*/
+
class RefCounter
{
+ // RefCounter is a mere dummy if std::shared_ptr<T> is available.
+
+#ifndef TAGLIB_USE_CXX11
+
public:
RefCounter() : refCount(1) {}
-
-#ifdef TAGLIB_ATOMIC_MAC
+
+# ifdef TAGLIB_ATOMIC_MAC
void ref() { OSAtomicIncrement32Barrier(const_cast<int32_t*>(&refCount)); }
bool deref() { return ! OSAtomicDecrement32Barrier(const_cast<int32_t*>(&refCount)); }
int32_t count() { return refCount; }
private:
volatile int32_t refCount;
-#elif defined(TAGLIB_ATOMIC_WIN)
+# elif defined(TAGLIB_ATOMIC_WIN)
void ref() { InterlockedIncrement(&refCount); }
bool deref() { return ! InterlockedDecrement(&refCount); }
long count() { return refCount; }
private:
volatile long refCount;
-#elif defined(TAGLIB_ATOMIC_GCC)
+# elif defined(TAGLIB_ATOMIC_GCC)
void ref() { __sync_add_and_fetch(&refCount, 1); }
bool deref() { return ! __sync_sub_and_fetch(&refCount, 1); }
int count() { return refCount; }
private:
volatile int refCount;
-#else
+# else
void ref() { refCount++; }
bool deref() { return ! --refCount; }
int count() { return refCount; }
private:
uint refCount;
+# endif
#endif
-
+
+ public:
+ virtual ~RefCounter() {}
};
-
#endif // DO_NOT_DOCUMENT
}
View
91 taglib/toolkit/tbytevector.cpp
@@ -244,6 +244,13 @@ class ByteVector::ByteVectorPrivate : public RefCounter
ByteVectorPrivate(const std::vector<char> &v)
: RefCounter(), data(v), size(static_cast<TagLib::uint>(v.size())) {}
+#ifdef TAGLIB_USE_CXX11
+
+ ByteVectorPrivate(std::vector<char> &&v)
+ : RefCounter(), data(v), size(static_cast<TagLib::uint>(v.size())) {}
+
+#endif
+
ByteVectorPrivate(TagLib::uint len, char value)
: RefCounter(), data(len, value), size(len) {}
@@ -292,43 +299,61 @@ ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFir
////////////////////////////////////////////////////////////////////////////////
ByteVector::ByteVector()
+ : d(new ByteVectorPrivate())
{
- d = new ByteVectorPrivate;
}
ByteVector::ByteVector(uint size, char value)
+ : d(new ByteVectorPrivate(size, value))
{
- d = new ByteVectorPrivate(size, value);
}
-ByteVector::ByteVector(const ByteVector &v) : d(v.d)
+ByteVector::ByteVector(const ByteVector &v)
+ : d(v.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
}
+#ifdef TAGLIB_USE_CXX11
+
+ByteVector::ByteVector(ByteVector &&v)
+ : d(std::move(v.d))
+{
+}
+
+#endif
+
ByteVector::ByteVector(char c)
+ : d(new ByteVectorPrivate())
{
- d = new ByteVectorPrivate;
d->data.push_back(c);
d->size = 1;
}
ByteVector::ByteVector(const char *data, uint length)
+ : d(new ByteVectorPrivate())
{
- d = new ByteVectorPrivate;
setData(data, length);
}
ByteVector::ByteVector(const char *data)
+ : d(new ByteVectorPrivate())
{
- d = new ByteVectorPrivate;
setData(data);
}
ByteVector::~ByteVector()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref())
delete d;
+
+#endif
}
ByteVector &ByteVector::setData(const char *data, uint length)
@@ -488,11 +513,19 @@ ByteVector &ByteVector::replace(const ByteVector &pattern, const ByteVector &wit
}
// replace private data:
+#ifdef TAGLIB_USE_CXX11
+
+ d.reset(newData);
+
+#else
+
if(d->deref())
delete d;
d = newData;
+#endif
+
return *this;
}
@@ -676,6 +709,12 @@ ByteVector ByteVector::operator+(const ByteVector &v) const
ByteVector &ByteVector::operator=(const ByteVector &v)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = v.d;
+
+#else
+
if(&v == this)
return *this;
@@ -684,18 +723,49 @@ ByteVector &ByteVector::operator=(const ByteVector &v)
d = v.d;
d->ref();
+
+#endif
+
+ return *this;
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+ByteVector &ByteVector::operator=(ByteVector &&v)
+{
+ d = std::move(v.d);
return *this;
}
+#endif
+
ByteVector &ByteVector::operator=(char c)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = ByteVector(c).d;
+
+#else
+
*this = ByteVector(c);
+
+#endif
+
return *this;
}
ByteVector &ByteVector::operator=(const char *data)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = ByteVector(data).d;
+
+#else
+
*this = ByteVector(data);
+
+#endif
+
return *this;
}
@@ -719,10 +789,19 @@ ByteVector ByteVector::toHex() const
void ByteVector::detach()
{
+#ifdef TAGLIB_USE_CXX11
+
+ if(!d.unique())
+ d.reset(new ByteVectorPrivate(d->data));
+
+#else
+
if(d->count() > 1) {
d->deref();
d = new ByteVectorPrivate(d->data);
}
+
+#endif
}
////////////////////////////////////////////////////////////////////////////////
View
27 taglib/toolkit/tbytevector.h
@@ -62,12 +62,21 @@ namespace TagLib {
ByteVector(uint size, char value = 0);
/*!
- * Contructs a byte vector that is a copy of \a v.
+ * Constructs a byte vector that is a copy of \a v.
*/
ByteVector(const ByteVector &v);
+#ifdef TAGLIB_USE_CXX11
+
/*!
- * Contructs a byte vector that contains \a c.
+ * Constructs a byte vector equivalent to \a v.
+ */
+ ByteVector(ByteVector &&v);
+
+#endif
+
+ /*!
+ * Constructs a byte vector that contains \a c.
*/
ByteVector(char c);
@@ -380,6 +389,15 @@ namespace TagLib {
*/
ByteVector &operator=(const ByteVector &v);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a v into the ByteVector.
+ */
+ ByteVector &operator=(ByteVector &&v);
+
+#endif
+
/*!
* Copies ByteVector \a v.
*/
@@ -411,7 +429,12 @@ namespace TagLib {
private:
class ByteVectorPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<ByteVectorPrivate> d;
+#else
ByteVectorPrivate *d;
+#endif
};
/*!
View
20 taglib/toolkit/tbytevectorlist.cpp
@@ -71,14 +71,32 @@ ByteVectorList::ByteVectorList() : List<ByteVector>()
ByteVectorList::ByteVectorList(const ByteVectorList &l) : List<ByteVector>(l)
{
+}
+
+#ifdef TAGLIB_USE_CXX11
+ByteVectorList::ByteVectorList(ByteVectorList &&l) : List<ByteVector>(l)
+{
}
-ByteVectorList::~ByteVectorList()
+#endif
+
+ByteVectorList &ByteVectorList::operator=(const ByteVectorList &l)
{
+ List<ByteVector>::operator=(l);
+ return *this;
+}
+
+#ifdef TAGLIB_USE_CXX11
+ByteVectorList &ByteVectorList::operator=(ByteVectorList &&l)
+{
+ List<ByteVector>::operator=(l);
+ return *this;
}
+#endif
+
ByteVector ByteVectorList::toByteVector(const ByteVector &separator) const
{
ByteVector v;
View
29 taglib/toolkit/tbytevectorlist.h
@@ -48,16 +48,36 @@ namespace TagLib {
ByteVectorList();
/*!
- * Destroys this ByteVectorList instance.
+ * Make a shallow, implicitly shared, copy of \a l. Because this is
+ * implicitly shared, this method is lightweight and suitable for
+ * pass-by-value usage.
*/
- virtual ~ByteVectorList();
+ ByteVectorList(const ByteVectorList &l);
+
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a l into the StringList.
+ */
+ ByteVectorList(ByteVectorList &&l);
+
+#endif
/*!
* Make a shallow, implicitly shared, copy of \a l. Because this is
* implicitly shared, this method is lightweight and suitable for
* pass-by-value usage.
*/
- ByteVectorList(const ByteVectorList &l);
+ ByteVectorList &operator=(const ByteVectorList &l);
+
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a l into the \e ByteVectorList.
+ */
+ ByteVectorList &operator=(ByteVectorList &&l);
+
+#endif
/*!
* Convert the ByteVectorList to a ByteVector separated by \a separator. By
@@ -74,9 +94,6 @@ namespace TagLib {
*/
static ByteVectorList split(const ByteVector &v, const ByteVector &pattern,
int byteAlign = 1, int max = 0);
- private:
- class ByteVectorListPrivate;
- ByteVectorListPrivate *d;
};
}
View
18 taglib/toolkit/tfilestream.cpp
@@ -55,10 +55,10 @@ namespace {
{
DWORD access = readOnly ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE);
- if(wcslen(path) > 0)
- return CreateFileW(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+ if(!path.wstr().empty())
+ return CreateFileW(path.wstr().c_str(), access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
else
- return CreateFileA(path, access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
+ return CreateFileA(path.str().c_str(), access, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
}
size_t fread(void *ptr, size_t size, size_t nmemb, HANDLE stream)
@@ -136,7 +136,17 @@ FileStream::FileStreamPrivate::FileStreamPrivate(FileName fileName, bool openRea
file = openFile(name, true);
if(file == InvalidFile) {
- debug("Could not open file " + String((const char *) name));
+#ifdef _WIN32
+ String n;
+ if(!name.wstr().empty())
+ n = name.wstr();
+ else
+ n = name.str();
+#else
+ String n(name);
+#endif // DEBUG
+
+ debug("Could not open file " + n);
}
}
View
15 taglib/toolkit/tiostream.h
@@ -30,6 +30,11 @@
#include "taglib.h"
#include "tbytevector.h"
+#ifdef _WIN32
+# include "tstring.h"
+# include "tdebug.h"
+#endif
+
namespace TagLib {
#ifdef _WIN32
@@ -38,11 +43,13 @@ namespace TagLib {
public:
FileName(const wchar_t *name) : m_wname(name) {}
FileName(const char *name) : m_name(name) {}
- operator const wchar_t *() const { return m_wname.c_str(); }
- operator const char *() const { return m_name.c_str(); }
+
+ const std::wstring &wstr() const { return m_wname; }
+ const std::string &str() const { return m_name; }
+
private:
- std::string m_name;
- std::wstring m_wname;
+ const std::string m_name;
+ const std::wstring m_wname;
};
#else
typedef const char *FileName;
View
55 taglib/toolkit/tlist.h
@@ -70,6 +70,15 @@ namespace TagLib {
*/
List(const List<T> &l);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a l into the \e List.
+ */
+ List(List<T> &&l);
+
+#endif
+
/*!
* Destroys this List instance. If auto deletion is enabled and this list
* contains a pointer type all of the memebers are also deleted.
@@ -124,6 +133,22 @@ namespace TagLib {
*/
List<T> &append(const List<T> &l);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Appends \a item to the end of the list and returns a reference to the
+ * list.
+ */
+ List<T> &append(T &&item);
+
+ /*!
+ * Appends all of the values in \a l to the end of the list and returns a
+ * reference to the list.
+ */
+ List<T> &append(List<T> &&l);
+
+#endif
+
/*!
* Prepends \a item to the beginning list and returns a reference to the
* list.
@@ -136,6 +161,22 @@ namespace TagLib {
*/
List<T> &prepend(const List<T> &l);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Prepends \a item to the beginning list and returns a reference to the
+ * list.
+ */
+ List<T> &prepend(T &&item);
+
+ /*!
+ * Prepends all of the items in \a l to the beginning list and returns a
+ * reference to the list.
+ */
+ List<T> &prepend(List<T> &&l);
+
+#endif
+
/*!
* Clears the list. If auto deletion is enabled and this list contains a
* pointer type the members are also deleted.
@@ -221,6 +262,15 @@ namespace TagLib {
*/
List<T> &operator=(const List<T> &l);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a l into the \e List.
+ */
+ List<T> &operator=(List<T> &&l);
+
+#endif
+
/*!
* Compares this list with \a l and returns true if all of the elements are
* the same.
@@ -243,7 +293,12 @@ namespace TagLib {
private:
#ifndef DO_NOT_DOCUMENT
template <class TP> class ListPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<ListPrivate<T>> d;
+#else
ListPrivate<T> *d;
+#endif
#endif
};
View
135 taglib/toolkit/tlist.tcc
@@ -53,8 +53,15 @@ template <class TP> class List<T>::ListPrivate : public ListPrivateBase
public:
ListPrivate() : ListPrivateBase() {}
ListPrivate(const std::list<TP> &l) : ListPrivateBase(), list(l) {}
+
+#ifdef TAGLIB_USE_CXX11
+
+ ListPrivate(std::list<TP> &&l) : ListPrivateBase(), list(l) {}
+
+#endif
+
void clear() {
- list.clear();
+ std::list<TP>().swap(list);
}
std::list<TP> list;
};
@@ -68,18 +75,33 @@ template <class TP> class List<T>::ListPrivate<TP *> : public ListPrivateBase
public:
ListPrivate() : ListPrivateBase() {}
ListPrivate(const std::list<TP *> &l) : ListPrivateBase(), list(l) {}
+
+#ifdef TAGLIB_USE_CXX11
+
+ ListPrivate(std::list<TP *> &&l) : ListPrivateBase(), list(l) {}
+
+#endif
+
~ListPrivate() {
- clear();
+ deletePointers();
}
+
void clear() {
- if(autoDelete) {
- typename std::list<TP *>::const_iterator it = list.begin();
- for(; it != list.end(); ++it)
- delete *it;
- }
- list.clear();
+ deletePointers();
+ std::list<TP *>().swap(list);
}
+
std::list<TP *> list;
+
+private:
+ void deletePointers() {
+ if(!autoDelete)
+ return;
+
+ typename std::list<TP *>::const_iterator it = list.begin();
+ for(; it != list.end(); ++it)
+ delete *it;
+ }
};
////////////////////////////////////////////////////////////////////////////////
@@ -88,21 +110,40 @@ public:
template <class T>
List<T>::List()
+ : d(new ListPrivate<T>())
{
- d = new ListPrivate<T>;
}
template <class T>
-List<T>::List(const List<T> &l) : d(l.d)
+List<T>::List(const List<T> &l)
+: d(l.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+template <class T>
+List<T>::List(List<T> &&l)
+ : d(std::move(l.d))
+{
}
+#endif
+
template <class T>
List<T>::~List()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref())
delete d;
+
+#endif
}
template <class T>
@@ -167,6 +208,29 @@ List<T> &List<T>::append(const List<T> &l)
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+template <class T>
+List<T> &List<T>::append(T &&item)
+{
+ detach();
+ d->list.push_back(item);
+ return *this;
+}
+
+template <class T>
+List<T> &List<T>::append(List<T> &&l)
+{
+ detach();
+
+ for(Iterator it = l.begin(); it != l.end(); ++it)
+ d->list.push_back(std::move(*it));
+
+ return *this;
+}
+
+#endif
+
template <class T>
List<T> &List<T>::prepend(const T &item)
{
@@ -183,6 +247,29 @@ List<T> &List<T>::prepend(const List<T> &l)
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+template <class T>
+List<T> &List<T>::prepend(T &&item)
+{
+ detach();
+ d->list.push_front(item);
+ return *this;
+}
+
+template <class T>
+List<T> &List<T>::prepend(List<T> &&l)
+{
+ detach();
+
+ for(Iterator it = l.rbegin(); it != l.rend(); ++it)
+ d->list.push_front(std::move(*it));
+
+ return *this;
+}
+
+#endif
+
template <class T>
List<T> &List<T>::clear()
{
@@ -284,16 +371,35 @@ const T &List<T>::operator[](uint i) const
template <class T>
List<T> &List<T>::operator=(const List<T> &l)
{
+ #ifdef TAGLIB_USE_CXX11
+
+ d = l.d;
+
+ #else
+
if(&l == this)
return *this;
if(d->deref())
delete d;
d = l.d;
d->ref();
+#endif
+
+ return *this;
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+template <class T>
+List<T> &List<T>::operator=(List<T> &&l)
+{
+ d = std::move(l.d);
return *this;
}
+#endif
+
template <class T>
bool List<T>::operator==(const List<T> &l) const
{
@@ -313,10 +419,19 @@ bool List<T>::operator!=(const List<T> &l) const
template <class T>
void List<T>::detach()
{
+#ifdef TAGLIB_USE_CXX11
+
+ if(!d.unique())
+ d.reset(new ListPrivate<T>(d->list));
+
+#else
+
if(d->count() > 1) {
d->deref();
d = new ListPrivate<T>(d->list);
}
+
+#endif
}
} // namespace TagLib
View
35 taglib/toolkit/tmap.h
@@ -73,6 +73,15 @@ namespace TagLib {
*/
Map(const Map<Key, T> &m);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a m into the Map.
+ */
+ Map(Map<Key, T> &&m);
+
+#endif
+
/*!
* Destroys this instance of the Map.
*/
@@ -108,6 +117,16 @@ namespace TagLib {
*/
Map<Key, T> &insert(const Key &key, const T &value);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Inserts \a value under \a key in the map. If a value for \a key already
+ * exists it will be overwritten.
+ */
+ Map<Key, T> &insert(const Key &key, T &&value);
+
+#endif
+
/*!
* Removes all of the elements from elements from the map. This however
* will not delete pointers if the mapped type is a pointer type.
@@ -174,6 +193,15 @@ namespace TagLib {
*/
Map<Key, T> &operator=(const Map<Key, T> &m);
+#ifdef TAGLIB_USE_CXX11
+
+ /*!
+ * Moves \a m into the Map.
+ */
+ Map<Key, T> &operator=(Map<Key, T> &&m);
+
+#endif
+
protected:
/*
* If this List is being shared via implicit sharing, do a deep copy of the
@@ -185,10 +213,15 @@ namespace TagLib {
private:
#ifndef DO_NOT_DOCUMENT
template <class KeyP, class TP> class MapPrivate;
+
+#ifdef TAGLIB_USE_CXX11
+ std::shared_ptr<MapPrivate<Key, T>> d;
+#else
MapPrivate<Key, T> *d;
#endif
- };
+#endif
+ };
}
// Since GCC doesn't support the "export" keyword, we have to include the
View
93 taglib/toolkit/tmap.tcc
@@ -35,32 +35,78 @@ class Map<Key, T>::MapPrivate : public RefCounter
{
public:
MapPrivate() : RefCounter() {}
+
#ifdef WANT_CLASS_INSTANTIATION_OF_MAP
- MapPrivate(const std::map<class KeyP, class TP>& m) : RefCounter(), map(m) {}
+
+ MapPrivate(const std::map<class KeyP, class TP> &m) : RefCounter(), map(m) {}
+
+# ifdef TAGLIB_USE_CXX11
+
+ MapPrivate(std::map<class KeyP, class TP> &&m) : RefCounter(), map(m) {}
+
+# endif
+
+ void clear() {
+ std::map<class KeyP, class TP>().swap(map);
+ }
+
std::map<class KeyP, class TP> map;
+
#else
+
MapPrivate(const std::map<KeyP, TP>& m) : RefCounter(), map(m) {}
+
+# ifdef TAGLIB_USE_CXX11
+
+ MapPrivate(std::map<KeyP, TP> &&m) : RefCounter(), map(m) {}
+
+# endif
+
+ void clear() {
+ std::map<KeyP, TP>().swap(map);
+ }
+
std::map<KeyP, TP> map;
+
#endif
};
template <class Key, class T>
Map<Key, T>::Map()
+ : d(new MapPrivate<Key, T>())
{
- d = new MapPrivate<Key, T>;
}
template <class Key, class T>
-Map<Key, T>::Map(const Map<Key, T> &m) : d(m.d)
+Map<Key, T>::Map(const Map<Key, T> &m)
+ : d(m.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
}
+#ifdef TAGLIB_USE_CXX11
+
+template <class Key, class T>
+TagLib::Map<Key, T>::Map(Map<Key, T> &&m)
+ : d(std::move(m.d))
+{
+}
+
+#endif
+
template <class Key, class T>
Map<Key, T>::~Map()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref())
delete(d);
+
+#endif
}
template <class Key, class T>
@@ -97,6 +143,18 @@ Map<Key, T> &Map<Key, T>::insert(const Key &key, const T &value)
return *this;
}
+#ifdef TAGLIB_USE_CXX11
+
+template <class Key, class T>
+Map<Key, T> &Map<Key, T>::insert(const Key &key, T &&value)
+{
+ detach();
+ d->map[key] = value;
+ return *this;
+}
+
+#endif
+
template <class Key, class T>
Map<Key, T> &Map<Key, T>::clear()
{
@@ -170,27 +228,56 @@ T &Map<Key, T>::operator[](const Key &key)
template <class Key, class T>
Map<Key, T> &Map<Key, T>::operator=(const Map<Key, T> &m)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = m.d;
+
+#else
+
if(&m == this)
return *this;
if(d->deref())
delete(d);
d = m.d;
d->ref();
+
+#endif
+
+ return *this;
+}
+
+#ifdef TAGLIB_USE_CXX11
+
+template <class Key, class T>
+Map<Key, T> &Map<Key, T>::operator=(Map<Key, T> &&m)
+{
+ d = std::move(m.d);
return *this;
}
+#endif
+
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////
template <class Key, class T>
void Map<Key, T>::detach()
{
+#ifdef TAGLIB_USE_CXX11
+
+ if(!d.unique())
+ d.reset(new MapPrivate<Key, T>(d->map));
+
+#else
+
if(d->count() > 1) {
d->deref();
d = new MapPrivate<Key, T>(d->map);
}
+
+#endif
}
} // namespace TagLib
View
161 taglib/toolkit/tstring.cpp
@@ -83,13 +83,25 @@ namespace {
class String::StringPrivate : public RefCounter
{
public:
+
+#ifdef TAGLIB_USE_CXX11
+
+ StringPrivate() {}
+ StringPrivate(const wstring &s) : data(s) {}
+ StringPrivate(wstring &&s) : data(s) {}
+
+#else
+
+public:
StringPrivate(const wstring &s) :
RefCounter(),
data(s) {}
StringPrivate() :
RefCounter() {}
+#endif
+
/*!
* Stores string in UTF-16. The byte order depends on the CPU endian.
*/
@@ -105,20 +117,33 @@ String String::null;
////////////////////////////////////////////////////////////////////////////////
-String::String()
+String::String()
+ : d(new StringPrivate())
{
- d = new StringPrivate;
}
-String::String(const String &s) : d(s.d)
+String::String(const String &s)
+ : d(s.d)
{
+#ifndef TAGLIB_USE_CXX11
+
d->ref();
+
+#endif
}
-String::String(const std::string &s, Type t)
+#ifdef TAGLIB_USE_CXX11
+
+String::String(String &&s)
+ : d(std::move(s.d))
{
- d = new StringPrivate;
+}
+
+#endif
+String::String(const std::string &s, Type t)
+ : d(new StringPrivate())
+{
if(t == Latin1)
copyFromLatin1(&s[0], s.length());
else if(t == String::UTF8)
@@ -129,9 +154,8 @@ String::String(const std::string &s, Type t)
}
String::String(const wstring &s, Type t)
+ : d(new StringPrivate())
{
- d = new StringPrivate;
-
if(t == UTF16 || t == UTF16BE || t == UTF16LE)
copyFromUTF16(s.c_str(), s.length(), t);
else {
@@ -140,9 +164,8 @@ String::String(const wstring &s, Type t)
}
String::String(const wchar_t *s, Type t)
+ : d(new StringPrivate())
{
- d = new StringPrivate;
-
if(t == UTF16 || t == UTF16BE || t == UTF16LE)
copyFromUTF16(s, ::wcslen(s), t);
else {
@@ -151,9 +174,8 @@ String::String(const wchar_t *s, Type t)
}
String::String(const char *s, Type t)
+ : d(new StringPrivate())
{
- d = new StringPrivate;
-
if(t == Latin1)
copyFromLatin1(s, ::strlen(s));
else if(t == String::UTF8)
@@ -164,9 +186,8 @@ String::String(const char *s, Type t)
}
String::String(wchar_t c, Type t)
+ : d(new StringPrivate())
{
- d = new StringPrivate;
-
if(t == UTF16 || t == UTF16BE || t == UTF16LE)
copyFromUTF16(&c, 1, t);
else {
@@ -175,9 +196,8 @@ String::String(wchar_t c, Type t)
}
String::String(char c, Type t)
+ : d(new StringPrivate())
{
- d = new StringPrivate;
-
if(t == Latin1 || t == UTF8) {
d->data.resize(1);
d->data[0] = static_cast<uchar>(c);
@@ -188,9 +208,8 @@ String::String(char c, Type t)
}
String::String(const ByteVector &v, Type t)
+ : d(new StringPrivate())
{
- d = new StringPrivate;
-
if(v.isEmpty())
return;
@@ -206,8 +225,12 @@ String::String(const ByteVector &v, Type t)
String::~String()
{
+#ifndef TAGLIB_USE_CXX11
+
if(d->deref())
delete d;
+
+#endif
}
std::string String::to8Bit(bool unicode) const
@@ -663,54 +686,118 @@ String &String::operator+=(char c)
String &String::operator=(const String &s)
{
+#ifdef TAGLIB_USE_CXX11
+
+ d = s.d;
+
+#else
+
if(&s == this)
return *this;
if(d->deref())
delete d;
d = s.d;
d->ref();
+
+#endif