]> granicus.if.org Git - taglib/commitdiff
Add List::swap() and Map::swap().
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 4 Nov 2016 07:43:14 +0000 (16:43 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Fri, 4 Nov 2016 07:43:14 +0000 (16:43 +0900)
taglib/toolkit/tlist.h
taglib/toolkit/tlist.tcc
taglib/toolkit/tmap.h
taglib/toolkit/tmap.tcc

index 6b4aa0a52b9f57932d3da2395273e6c11d5dfb7e..377b824819ecd878bd945f107239f4e0c4f09561 100644 (file)
@@ -229,6 +229,11 @@ namespace TagLib {
      */
     List<T> &operator=(const List<T> &l);
 
+    /*!
+     * Exchanges the content of this list by the content of \a l.
+     */
+    void swap(List<T> &l);
+
     /*!
      * Compares this list with \a l and returns true if all of the elements are
      * the same.
index a55eb6201e2d3c16339c0a7d4e2b2ffba0ec9e39..478f09834467cdb00c899aa3c48e2edc225fac81 100644 (file)
@@ -89,9 +89,9 @@ public:
 ////////////////////////////////////////////////////////////////////////////////
 
 template <class T>
-List<T>::List()
+List<T>::List() :
+  d(new ListPrivate<T>())
 {
-  d = new ListPrivate<T>;
 }
 
 template <class T>
@@ -283,16 +283,18 @@ const T &List<T>::operator[](unsigned int i) const
 template <class T>
 List<T> &List<T>::operator=(const List<T> &l)
 {
-  if(&l == this)
-    return *this;
-
-  if(d->deref())
-    delete d;
-  d = l.d;
-  d->ref();
+  List<T>(l).swap(*this);
   return *this;
 }
 
+template <class T>
+void List<T>::swap(List<T> &l)
+{
+  using std::swap;
+
+  swap(d, l.d);
+}
+
 template <class T>
 bool List<T>::operator==(const List<T> &l) const
 {
index c24c763677b92068a14f0de7abe53f972a25771f..f54e5a2a928a6f50b7c5843a5697934718b1c8fe 100644 (file)
@@ -174,6 +174,11 @@ namespace TagLib {
      */
     Map<Key, T> &operator=(const Map<Key, T> &m);
 
+    /*!
+     * Exchanges the content of this map by the content of \a m.
+     */
+    void swap(Map<Key, T> &m);
+
   protected:
     /*
      * If this List is being shared via implicit sharing, do a deep copy of the
index c1227f3abfddea2f243b496e3b142bea1dae06aa..2e4ed5ebffd5307691fc3504c395d426be373492 100644 (file)
@@ -48,9 +48,9 @@ public:
 };
 
 template <class Key, class T>
-Map<Key, T>::Map()
+Map<Key, T>::Map() :
+  d(new MapPrivate<Key, T>())
 {
-  d = new MapPrivate<Key, T>;
 }
 
 template <class Key, class T>
@@ -171,16 +171,18 @@ 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)
 {
-  if(&m == this)
-    return *this;
-
-  if(d->deref())
-    delete(d);
-  d = m.d;
-  d->ref();
+  Map<Key, T>(m).swap(*this);
   return *this;
 }
 
+template <class Key, class T>
+void Map<Key, T>::swap(Map<Key, T> &m)
+{
+  using std::swap;
+
+  swap(d, m.d);
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // protected members
 ////////////////////////////////////////////////////////////////////////////////