From 70334edd1998deeae98087578bcda06b5fcf565e Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Fri, 4 Nov 2016 16:43:14 +0900 Subject: [PATCH] Add List::swap() and Map::swap(). --- taglib/toolkit/tlist.h | 5 +++++ taglib/toolkit/tlist.tcc | 20 +++++++++++--------- taglib/toolkit/tmap.h | 5 +++++ taglib/toolkit/tmap.tcc | 20 +++++++++++--------- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/taglib/toolkit/tlist.h b/taglib/toolkit/tlist.h index 6b4aa0a5..377b8248 100644 --- a/taglib/toolkit/tlist.h +++ b/taglib/toolkit/tlist.h @@ -229,6 +229,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 a55eb620..478f0983 100644 --- a/taglib/toolkit/tlist.tcc +++ b/taglib/toolkit/tlist.tcc @@ -89,9 +89,9 @@ public: //////////////////////////////////////////////////////////////////////////////// template -List::List() +List::List() : + d(new ListPrivate()) { - d = new ListPrivate; } template @@ -283,16 +283,18 @@ const T &List::operator[](unsigned int 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 c24c7636..f54e5a2a 100644 --- a/taglib/toolkit/tmap.h +++ b/taglib/toolkit/tmap.h @@ -174,6 +174,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 c1227f3a..2e4ed5eb 100644 --- a/taglib/toolkit/tmap.tcc +++ b/taglib/toolkit/tmap.tcc @@ -48,9 +48,9 @@ public: }; template -Map::Map() +Map::Map() : + d(new MapPrivate()) { - d = new MapPrivate; } template @@ -171,16 +171,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 ////////////////////////////////////////////////////////////////////////////////