Replace RefCounter with shared_ptr (#1100)

* clang-tidy: replace RefCounter with shared_ptr

The latter is C++11.

Found with clang-analyzer-webkit.NoUncountedMemberChecker

Signed-off-by: Rosen Penev <rosenp@gmail.com>

* remove trefcounter

It is now unused.

Signed-off-by: Rosen Penev <rosenp@gmail.com>

---------

Signed-off-by: Rosen Penev <rosenp@gmail.com>
This commit is contained in:
Rosen Penev
2023-08-07 06:40:24 -07:00
committed by GitHub
parent dcef356e3f
commit 843a8aac80
24 changed files with 124 additions and 352 deletions

View File

@ -53,7 +53,6 @@ set(tag_HDRS
toolkit/tmap.h
toolkit/tmap.tcc
toolkit/tpropertymap.h
toolkit/trefcounter.h
toolkit/tdebuglistener.h
mpeg/mpegfile.h
mpeg/mpegproperties.h
@ -305,7 +304,6 @@ set(toolkit_SRCS
toolkit/tfilestream.cpp
toolkit/tdebug.cpp
toolkit/tpropertymap.cpp
toolkit/trefcounter.cpp
toolkit/tdebuglistener.cpp
toolkit/tzlib.cpp
)

View File

@ -27,14 +27,13 @@
#include "taglib.h"
#include "tdebug.h"
#include "trefcounter.h"
#include "asffile.h"
#include "asfutils.h"
using namespace TagLib;
class ASF::Attribute::AttributePrivate : public RefCounter
class ASF::Attribute::AttributePrivate
{
public:
AttributePrivate() :
@ -56,61 +55,57 @@ public:
////////////////////////////////////////////////////////////////////////////////
ASF::Attribute::Attribute() :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = UnicodeType;
}
ASF::Attribute::Attribute(const ASF::Attribute &other) :
d(other.d)
{
d->ref();
}
ASF::Attribute::Attribute(const ASF::Attribute &other) = default;
ASF::Attribute::Attribute(const String &value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = UnicodeType;
d->stringValue = value;
}
ASF::Attribute::Attribute(const ByteVector &value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = BytesType;
d->byteVectorValue = value;
}
ASF::Attribute::Attribute(const ASF::Picture &value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = BytesType;
d->pictureValue = value;
}
ASF::Attribute::Attribute(unsigned int value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = DWordType;
d->numericValue = value;
}
ASF::Attribute::Attribute(unsigned long long value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = QWordType;
d->numericValue = value;
}
ASF::Attribute::Attribute(unsigned short value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = WordType;
d->numericValue = value;
}
ASF::Attribute::Attribute(bool value) :
d(new AttributePrivate())
d(std::make_shared<AttributePrivate>())
{
d->type = BoolType;
d->numericValue = value;
@ -129,11 +124,7 @@ void ASF::Attribute::swap(Attribute &other)
swap(d, other.d);
}
ASF::Attribute::~Attribute()
{
if(d->deref())
delete d;
}
ASF::Attribute::~Attribute() = default;
ASF::Attribute::AttributeTypes ASF::Attribute::type() const
{

View File

@ -197,7 +197,7 @@ namespace TagLib
ByteVector render(const String &name, int kind = 0) const;
class AttributePrivate;
AttributePrivate *d;
std::shared_ptr<AttributePrivate> d;
};
} // namespace ASF
} // namespace TagLib

View File

@ -27,7 +27,6 @@
#include "taglib.h"
#include "tdebug.h"
#include "trefcounter.h"
#include "asfattribute.h"
#include "asffile.h"
@ -35,7 +34,7 @@
using namespace TagLib;
class ASF::Picture::PicturePrivate : public RefCounter
class ASF::Picture::PicturePrivate
{
public:
bool valid;
@ -50,22 +49,14 @@ public:
////////////////////////////////////////////////////////////////////////////////
ASF::Picture::Picture() :
d(new PicturePrivate())
d(std::make_shared<PicturePrivate>())
{
d->valid = true;
}
ASF::Picture::Picture(const Picture& other) :
d(other.d)
{
d->ref();
}
ASF::Picture::Picture(const Picture &other) = default;
ASF::Picture::~Picture()
{
if(d->deref())
delete d;
}
ASF::Picture::~Picture() = default;
bool ASF::Picture::isValid() const
{

View File

@ -214,7 +214,7 @@ namespace TagLib
private:
class PicturePrivate;
PicturePrivate *d;
std::shared_ptr<PicturePrivate> d;
};
} // namespace ASF
} // namespace TagLib

View File

@ -31,11 +31,10 @@
#include <cstring>
#include "tdebug.h"
#include "tfile.h"
#include "tfilestream.h"
#include "tstring.h"
#include "tdebug.h"
#include "trefcounter.h"
#include "asffile.h"
#include "mpegfile.h"
@ -292,14 +291,15 @@ namespace
}
} // namespace
class FileRef::FileRefPrivate : public RefCounter
class FileRef::FileRefPrivate
{
public:
FileRefPrivate() :
file(nullptr),
stream(nullptr) {}
~FileRefPrivate() {
~FileRefPrivate()
{
delete file;
delete stream;
}
@ -313,40 +313,32 @@ public:
////////////////////////////////////////////////////////////////////////////////
FileRef::FileRef() :
d(new FileRefPrivate())
d(std::make_shared<FileRefPrivate>())
{
}
FileRef::FileRef(FileName fileName, bool readAudioProperties,
AudioProperties::ReadStyle audioPropertiesStyle) :
d(new FileRefPrivate())
d(std::make_shared<FileRefPrivate>())
{
parse(fileName, readAudioProperties, audioPropertiesStyle);
}
FileRef::FileRef(IOStream* stream, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle) :
d(new FileRefPrivate())
FileRef::FileRef(IOStream *stream, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle) :
d(std::make_shared<FileRefPrivate>())
{
parse(stream, readAudioProperties, audioPropertiesStyle);
}
FileRef::FileRef(File *file) :
d(new FileRefPrivate())
d(std::make_shared<FileRefPrivate>())
{
d->file = file;
}
FileRef::FileRef(const FileRef &ref) :
d(ref.d)
{
d->ref();
}
FileRef::FileRef(const FileRef &ref) = default;
FileRef::~FileRef()
{
if(d->deref())
delete d;
}
FileRef::~FileRef() = default;
Tag *FileRef::tag() const
{

View File

@ -304,7 +304,7 @@ namespace TagLib {
void parse(IOStream *stream, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle);
class FileRefPrivate;
FileRefPrivate *d;
std::shared_ptr<FileRefPrivate> d;
};
} // namespace TagLib

View File

@ -26,11 +26,10 @@
#include "mp4coverart.h"
#include "taglib.h"
#include "tdebug.h"
#include "trefcounter.h"
using namespace TagLib;
class MP4::CoverArt::CoverArtPrivate : public RefCounter
class MP4::CoverArt::CoverArtPrivate
{
public:
CoverArtPrivate() :
@ -45,17 +44,13 @@ public:
////////////////////////////////////////////////////////////////////////////////
MP4::CoverArt::CoverArt(Format format, const ByteVector &data) :
d(new CoverArtPrivate())
d(std::make_shared<CoverArtPrivate>())
{
d->format = format;
d->data = data;
}
MP4::CoverArt::CoverArt(const CoverArt &item) :
d(item.d)
{
d->ref();
}
MP4::CoverArt::CoverArt(const CoverArt &item) = default;
MP4::CoverArt &
MP4::CoverArt::operator=(const CoverArt &item)
@ -72,12 +67,7 @@ MP4::CoverArt::swap(CoverArt &item)
swap(d, item.d);
}
MP4::CoverArt::~CoverArt()
{
if(d->deref()) {
delete d;
}
}
MP4::CoverArt::~CoverArt() = default;
MP4::CoverArt::Format
MP4::CoverArt::format() const

View File

@ -70,7 +70,7 @@ namespace TagLib {
private:
class CoverArtPrivate;
CoverArtPrivate *d;
std::shared_ptr<CoverArtPrivate> d;
};
typedef List<CoverArt> CoverArtList;

View File

@ -26,11 +26,10 @@
#include "mp4item.h"
#include "taglib.h"
#include "tdebug.h"
#include "trefcounter.h"
using namespace TagLib;
class MP4::Item::ItemPrivate : public RefCounter
class MP4::Item::ItemPrivate
{
public:
ItemPrivate() :
@ -53,16 +52,12 @@ public:
};
MP4::Item::Item() :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->valid = false;
}
MP4::Item::Item(const Item &item) :
d(item.d)
{
d->ref();
}
MP4::Item::Item(const Item &item) = default;
MP4::Item &
MP4::Item::operator=(const Item &item)
@ -79,63 +74,59 @@ MP4::Item::swap(Item &item)
swap(d, item.d);
}
MP4::Item::~Item()
{
if(d->deref())
delete d;
}
MP4::Item::~Item() = default;
MP4::Item::Item(bool value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_bool = value;
}
MP4::Item::Item(int value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_int = value;
}
MP4::Item::Item(unsigned char value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_byte = value;
}
MP4::Item::Item(unsigned int value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_uint = value;
}
MP4::Item::Item(long long value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_longlong = value;
}
MP4::Item::Item(int value1, int value2) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_intPair.first = value1;
d->m_intPair.second = value2;
}
MP4::Item::Item(const ByteVectorList &value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_byteVectorList = value;
}
MP4::Item::Item(const StringList &value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_stringList = value;
}
MP4::Item::Item(const MP4::CoverArtList &value) :
d(new ItemPrivate())
d(std::make_shared<ItemPrivate>())
{
d->m_coverArtList = value;
}

View File

@ -81,7 +81,7 @@ namespace TagLib {
private:
class ItemPrivate;
ItemPrivate *d;
std::shared_ptr<ItemPrivate> d;
};
} // namespace MP4
} // namespace TagLib

View File

@ -26,10 +26,9 @@
#include "mpegheader.h"
#include "tbytevector.h"
#include "tstring.h"
#include "tfile.h"
#include "tdebug.h"
#include "trefcounter.h"
#include "tfile.h"
#include "tstring.h"
#include "mpegutils.h"
@ -37,7 +36,7 @@
using namespace TagLib;
class MPEG::Header::HeaderPrivate : public RefCounter
class MPEG::Header::HeaderPrivate
{
public:
HeaderPrivate() :
@ -73,22 +72,14 @@ public:
////////////////////////////////////////////////////////////////////////////////
MPEG::Header::Header(File *file, offset_t offset, bool checkLength) :
d(new HeaderPrivate())
d(std::make_shared<HeaderPrivate>())
{
parse(file, offset, checkLength);
}
MPEG::Header::Header(const Header &h) :
d(h.d)
{
d->ref();
}
MPEG::Header::Header(const Header &h) = default;
MPEG::Header::~Header()
{
if(d->deref())
delete d;
}
MPEG::Header::~Header() = default;
bool MPEG::Header::isValid() const
{
@ -155,11 +146,7 @@ MPEG::Header &MPEG::Header::operator=(const Header &h)
if(&h == this)
return *this;
if(d->deref())
delete d;
d = h.d;
d->ref();
return *this;
}

View File

@ -29,6 +29,8 @@
#include "taglib.h"
#include "taglib_export.h"
#include <memory>
namespace TagLib {
class ByteVector;
@ -164,7 +166,7 @@ namespace TagLib {
void parse(File *file, offset_t offset, bool checkLength);
class HeaderPrivate;
HeaderPrivate *d;
std::shared_ptr<HeaderPrivate> d;
};
} // namespace MPEG
} // namespace TagLib

View File

@ -33,9 +33,8 @@
#include <iostream>
#include <limits>
#include "tstring.h"
#include "tdebug.h"
#include "trefcounter.h"
#include "tstring.h"
#include "tutils.h"
// This is a bit ugly to keep writing over and over again.
@ -254,36 +253,23 @@ class ByteVector::ByteVectorPrivate
{
public:
ByteVectorPrivate(unsigned int l, char c) :
counter(new RefCounter()),
data(new std::vector<char>(l, c)),
data(std::make_shared<std::vector<char>>(l, c)),
offset(0),
length(l) {}
length(l) { }
ByteVectorPrivate(const char *s, unsigned int l) :
counter(new RefCounter()),
data(new std::vector<char>(s, s + l)),
data(std::make_shared<std::vector<char>>(s, s + l)),
offset(0),
length(l) {}
length(l) { }
ByteVectorPrivate(const ByteVectorPrivate &d, unsigned int o, unsigned int l) :
counter(d.counter),
data(d.data),
offset(d.offset + o),
length(l)
{
counter->ref();
}
~ByteVectorPrivate()
{
if(counter->deref()) {
delete counter;
delete data;
}
}
RefCounter *counter;
std::vector<char> *data;
std::shared_ptr<std::vector<char>> data;
unsigned int offset;
unsigned int length;
};
@ -967,7 +953,7 @@ ByteVector ByteVector::toBase64() const
void ByteVector::detach()
{
if(d->counter->count() > 1) {
if(d->data.use_count() > 1) {
if(!isEmpty())
ByteVector(&d->data->front() + d->offset, d->length).swap(*this);
else

View File

@ -29,8 +29,9 @@
#include "taglib.h"
#include "taglib_export.h"
#include <vector>
#include <iostream>
#include <memory>
#include <vector>
namespace TagLib {

View File

@ -29,6 +29,7 @@
#include "taglib.h"
#include <list>
#include <memory>
namespace TagLib {
@ -273,7 +274,7 @@ namespace TagLib {
private:
#ifndef DO_NOT_DOCUMENT
template <class TP> class ListPrivate;
ListPrivate<T> *d;
std::shared_ptr<ListPrivate<T>> d;
#endif
};

View File

@ -24,7 +24,7 @@
***************************************************************************/
#include <algorithm>
#include "trefcounter.h"
#include <memory>
namespace TagLib {
@ -39,11 +39,10 @@ namespace TagLib {
// A base for the generic and specialized private class types. New
// non-templatized members should be added here.
class ListPrivateBase : public RefCounter
class ListPrivateBase
{
public:
ListPrivateBase() : autoDelete(false) {}
bool autoDelete;
bool autoDelete{};
};
// A generic implementation
@ -74,8 +73,8 @@ public:
}
void clear() {
if(autoDelete) {
for(auto it = list.begin(); it != list.end(); ++it)
delete *it;
for(auto &m : list)
delete m;
}
list.clear();
}
@ -88,22 +87,17 @@ public:
template <class T>
List<T>::List() :
d(new ListPrivate<T>())
d(std::make_shared<ListPrivate<T>>())
{
}
template <class T>
List<T>::List(const List<T> &l) : d(l.d)
{
d->ref();
}
template <class T>
List<T>::~List()
{
if(d->deref())
delete d;
}
List<T>::~List() = default;
template <class T>
typename List<T>::Iterator List<T>::begin()
@ -297,11 +291,7 @@ const T &List<T>::operator[](unsigned int i) const
}
template <class T>
List<T> &List<T>::operator=(const List<T> &l)
{
List<T>(l).swap(*this);
return *this;
}
List<T> &List<T>::operator=(const List<T> &) = default;
template <class T>
void List<T>::swap(List<T> &l)
@ -330,9 +320,8 @@ bool List<T>::operator!=(const List<T> &l) const
template <class T>
void List<T>::detach()
{
if(d->count() > 1) {
d->deref();
d = new ListPrivate<T>(d->list);
if(d.use_count() > 1) {
d = std::make_shared<ListPrivate<T>>(d->list);
}
}

View File

@ -27,6 +27,7 @@
#define TAGLIB_MAP_H
#include <map>
#include <memory>
#include "taglib.h"
@ -210,7 +211,7 @@ namespace TagLib {
private:
#ifndef DO_NOT_DOCUMENT
template <class KeyP, class TP> class MapPrivate;
MapPrivate<Key, T> *d;
std::shared_ptr<MapPrivate<Key, T>> d;
#endif
};

View File

@ -23,8 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "trefcounter.h"
namespace TagLib {
////////////////////////////////////////////////////////////////////////////////
@ -33,37 +31,32 @@ namespace TagLib {
template <class Key, class T>
template <class KeyP, class TP>
class Map<Key, T>::MapPrivate : public RefCounter
class Map<Key, T>::MapPrivate
{
public:
MapPrivate() : RefCounter() {}
MapPrivate() = default;
#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) : map(m) {}
std::map<class KeyP, class TP> map;
#else
MapPrivate(const std::map<KeyP, TP>& m) : RefCounter(), map(m) {}
MapPrivate(const std::map<KeyP, TP>& m) : map(m) {}
std::map<KeyP, TP> map;
#endif
};
template <class Key, class T>
Map<Key, T>::Map() :
d(new MapPrivate<Key, T>())
d(std::make_shared<MapPrivate<Key, T>>())
{
}
template <class Key, class T>
Map<Key, T>::Map(const Map<Key, T> &m) : d(m.d)
{
d->ref();
}
template <class Key, class T>
Map<Key, T>::~Map()
{
if(d->deref())
delete(d);
}
Map<Key, T>::~Map() = default;
template <class Key, class T>
typename Map<Key, T>::Iterator Map<Key, T>::begin()
@ -211,9 +204,8 @@ void Map<Key, T>::swap(Map<Key, T> &m)
template <class Key, class T>
void Map<Key, T>::detach()
{
if(d->count() > 1) {
d->deref();
d = new MapPrivate<Key, T>(d->map);
if(d.use_count() > 1) {
d = std::make_shared<MapPrivate<Key, T>>(d->map);
}
}

View File

@ -1,68 +0,0 @@
/***************************************************************************
copyright : (C) 2013 by Tsuda Kageyu
email : tsuda.kageyu@gmail.com
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "trefcounter.h"
#include <atomic>
namespace TagLib
{
class RefCounter::RefCounterPrivate
{
public:
RefCounterPrivate() :
refCount(1)
{
}
std::atomic_int refCount;
};
RefCounter::RefCounter() :
d(new RefCounterPrivate())
{
}
RefCounter::~RefCounter()
{
delete d;
}
void RefCounter::ref()
{
d->refCount.fetch_add(1);
}
bool RefCounter::deref()
{
return d->refCount.fetch_sub(1) == 1;
}
int RefCounter::count() const
{
return d->refCount.load();
}
} // namespace TagLib

View File

@ -1,60 +0,0 @@
/***************************************************************************
copyright : (C) 2013 by Tsuda Kageyu
email : tsuda.kageyu@gmail.com
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifndef TAGLIB_REFCOUNTER_H
#define TAGLIB_REFCOUNTER_H
#include "taglib_export.h"
#include "taglib.h"
#ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class.
/*!
* \internal
* This is just used as a base class for shared classes in TagLib.
*
* \warning This <b>is not</b> part of the TagLib public API!
*/
namespace TagLib
{
class TAGLIB_EXPORT RefCounter
{
public:
RefCounter();
~RefCounter();
void ref();
bool deref();
int count() const;
private:
class RefCounterPrivate;
RefCounterPrivate *d;
};
} // namespace TagLib
#endif // DO_NOT_DOCUMENT
#endif

View File

@ -32,7 +32,6 @@
#include "tdebug.h"
#include "tstringlist.h"
#include "trefcounter.h"
#include "tutils.h"
namespace
@ -137,20 +136,18 @@ namespace
namespace TagLib {
class String::StringPrivate : public RefCounter
{
public:
StringPrivate() = default;
class String::StringPrivate
{
public:
/*!
* Stores string in UTF-16. The byte order depends on the CPU endian.
*/
TagLib::wstring data;
/*!
* Stores string in UTF-16. The byte order depends on the CPU endian.
*/
TagLib::wstring data;
/*!
* This is only used to hold the the most recent value of toCString().
*/
std::string cstring;
/*!
* This is only used to hold the the most recent value of toCString().
*/
std::string cstring;
};
////////////////////////////////////////////////////////////////////////////////
@ -158,18 +155,14 @@ public:
////////////////////////////////////////////////////////////////////////////////
String::String() :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
}
String::String(const String &s) :
d(s.d)
{
d->ref();
}
String::String(const String &s) = default;
String::String(const std::string &s, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(t == Latin1)
copyFromLatin1(d->data, s.c_str(), s.length());
@ -181,7 +174,7 @@ String::String(const std::string &s, Type t) :
}
String::String(const wstring &s, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
// This looks ugly but needed for the compatibility with TagLib1.8.
@ -199,7 +192,7 @@ String::String(const wstring &s, Type t) :
}
String::String(const wchar_t *s, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(t == UTF16 || t == UTF16BE || t == UTF16LE) {
// This looks ugly but needed for the compatibility with TagLib1.8.
@ -217,7 +210,7 @@ String::String(const wchar_t *s, Type t) :
}
String::String(const char *s, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(t == Latin1)
copyFromLatin1(d->data, s, ::strlen(s));
@ -229,7 +222,7 @@ String::String(const char *s, Type t) :
}
String::String(wchar_t c, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(t == UTF16 || t == UTF16BE || t == UTF16LE)
copyFromUTF16(d->data, &c, 1, t);
@ -239,7 +232,7 @@ String::String(wchar_t c, Type t) :
}
String::String(char c, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(t == Latin1)
copyFromLatin1(d->data, &c, 1);
@ -251,7 +244,7 @@ String::String(char c, Type t) :
}
String::String(const ByteVector &v, Type t) :
d(new StringPrivate())
d(std::make_shared<StringPrivate>())
{
if(v.isEmpty())
return;
@ -269,11 +262,7 @@ String::String(const ByteVector &v, Type t) :
////////////////////////////////////////////////////////////////////////////////
String::~String()
{
if(d->deref())
delete d;
}
String::~String() = default;
std::string String::to8Bit(bool unicode) const
{
@ -696,7 +685,7 @@ bool String::operator<(const String &s) const
void String::detach()
{
if(d->count() > 1)
if(d.use_count() > 1)
String(d->data.c_str()).swap(*this);
}

View File

@ -30,8 +30,8 @@
#include "taglib.h"
#include "tbytevector.h"
#include <string>
#include <iostream>
#include <string>
/*!
* \relates TagLib::String
@ -512,7 +512,7 @@ namespace TagLib {
private:
class StringPrivate;
StringPrivate *d;
std::shared_ptr<StringPrivate> d;
};
} // namespace TagLib

View File

@ -105,7 +105,6 @@
#include "tlist.h"
#include "tmap.h"
#include "tpropertymap.h"
#include "trefcounter.h"
#include "trueaudiofile.h"
#include "trueaudioproperties.h"
#include "tstring.h"
@ -148,14 +147,14 @@ public:
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::APE::Item));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::APE::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::APE::Tag));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::ASF::Attribute));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::ASF::Attribute));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::ASF::File));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::ASF::Picture));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::ASF::Picture));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::ASF::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::ASF::Tag));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::AudioProperties));
CPPUNIT_ASSERT_EQUAL(classSize(0, false), sizeof(TagLib::ByteVector));
CPPUNIT_ASSERT_EQUAL(classSize(1, false), sizeof(TagLib::ByteVectorList));
CPPUNIT_ASSERT_EQUAL(classSize(2, false), sizeof(TagLib::ByteVectorList));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::ByteVectorStream));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::DebugListener));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::FLAC::File));
@ -164,7 +163,7 @@ public:
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::FLAC::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::FLAC::UnknownMetadataBlock));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::File));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::FileRef));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::FileRef));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::FileRef::FileTypeResolver));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::FileRef::StreamTypeResolver));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::FileStream));
@ -200,19 +199,19 @@ public:
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::IOStream));
CPPUNIT_ASSERT_EQUAL(classSize(2, true), sizeof(TagLib::IT::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::IT::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(0, false), sizeof(TagLib::List<int>));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::MP4::CoverArt));
CPPUNIT_ASSERT_EQUAL(classSize(1, false), sizeof(TagLib::List<int>));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MP4::CoverArt));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MP4::File));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::MP4::Item));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MP4::Item));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MP4::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MP4::Tag));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MPC::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MPC::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MPEG::File));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::MPEG::Header));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MPEG::Header));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::MPEG::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::MPEG::XingHeader));
CPPUNIT_ASSERT_EQUAL(classSize(0, false), sizeof(TagLib::Map<int, int>));
CPPUNIT_ASSERT_EQUAL(classSize(1, false), sizeof(TagLib::Map<int, int>));
CPPUNIT_ASSERT_EQUAL(classSize(2, true), sizeof(TagLib::Mod::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::Mod::FileBase));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::Mod::Properties));
@ -228,7 +227,7 @@ public:
CPPUNIT_ASSERT_EQUAL(classSize(2, true), sizeof(TagLib::Ogg::Vorbis::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::Ogg::Vorbis::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::Ogg::XiphComment));
CPPUNIT_ASSERT_EQUAL(classSize(1, false), sizeof(TagLib::PropertyMap));
CPPUNIT_ASSERT_EQUAL(classSize(2, false), sizeof(TagLib::PropertyMap));
CPPUNIT_ASSERT_EQUAL(classSize(2, true), sizeof(TagLib::RIFF::AIFF::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::RIFF::AIFF::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::RIFF::File));
@ -238,8 +237,8 @@ public:
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::RIFF::WAV::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(2, true), sizeof(TagLib::S3M::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::S3M::Properties));
CPPUNIT_ASSERT_EQUAL(classSize(0, false), sizeof(TagLib::String));
CPPUNIT_ASSERT_EQUAL(classSize(1, false), sizeof(TagLib::StringList));
CPPUNIT_ASSERT_EQUAL(classSize(1, false), sizeof(TagLib::String));
CPPUNIT_ASSERT_EQUAL(classSize(2, false), sizeof(TagLib::StringList));
CPPUNIT_ASSERT_EQUAL(classSize(0, true), sizeof(TagLib::Tag));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::TrueAudio::File));
CPPUNIT_ASSERT_EQUAL(classSize(1, true), sizeof(TagLib::TrueAudio::Properties));