Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / toolkit / tmap.tcc
1 /***************************************************************************
2     copyright            : (C) 2002 - 2008 by Scott Wheeler
3     email                : wheeler@kde.org
4  ***************************************************************************/
5
6 /***************************************************************************
7  *   This library is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU Lesser General Public License version   *
9  *   2.1 as published by the Free Software Foundation.                     *
10  *                                                                         *
11  *   This library is distributed in the hope that it will be useful, but   *
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the Free Software   *
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
19  *   USA                                                                   *
20  *                                                                         *
21  *   Alternatively, this file is available under the Mozilla Public        *
22  *   License Version 1.1.  You may obtain a copy of the License at         *
23  *   http://www.mozilla.org/MPL/                                           *
24  ***************************************************************************/
25
26 namespace TagLib {
27
28 ////////////////////////////////////////////////////////////////////////////////
29 // public members
30 ////////////////////////////////////////////////////////////////////////////////
31
32 template <class Key, class T>
33 template <class KeyP, class TP>
34 class Map<Key, T>::MapPrivate : public RefCounter
35 {
36 public:
37   MapPrivate() : RefCounter() {}
38 #ifdef WANT_CLASS_INSTANTIATION_OF_MAP
39   MapPrivate(const std::map<class KeyP, class TP>& m) : RefCounter(), map(m) {}
40   std::map<class KeyP, class TP> map;
41 #else
42   MapPrivate(const std::map<KeyP, TP>& m) : RefCounter(), map(m) {}
43   std::map<KeyP, TP> map;
44 #endif
45 };
46
47 template <class Key, class T>
48 Map<Key, T>::Map()
49 {
50   d = new MapPrivate<Key, T>;
51 }
52
53 template <class Key, class T>
54 Map<Key, T>::Map(const Map<Key, T> &m) : d(m.d)
55 {
56   d->ref();
57 }
58
59 template <class Key, class T>
60 Map<Key, T>::~Map()
61 {
62   if(d->deref())
63     delete(d);
64 }
65
66 template <class Key, class T>
67 typename Map<Key, T>::Iterator Map<Key, T>::begin()
68 {
69   detach();
70   return d->map.begin();
71 }
72
73 template <class Key, class T>
74 typename Map<Key, T>::ConstIterator Map<Key, T>::begin() const
75 {
76   return d->map.begin();
77 }
78
79 template <class Key, class T>
80 typename Map<Key, T>::Iterator Map<Key, T>::end()
81 {
82   detach();
83   return d->map.end();
84 }
85
86 template <class Key, class T>
87 typename Map<Key, T>::ConstIterator Map<Key, T>::end() const
88 {
89   return d->map.end();
90 }
91
92 template <class Key, class T>
93 Map<Key, T> &Map<Key, T>::insert(const Key &key, const T &value)
94 {
95   detach();
96   d->map[key] = value;
97   return *this;
98 }
99
100 template <class Key, class T>
101 Map<Key, T> &Map<Key, T>::clear()
102 {
103   detach();
104   d->map.clear();
105   return *this;
106 }
107
108 template <class Key, class T>
109 bool Map<Key, T>::isEmpty() const
110 {
111   return d->map.empty();
112 }
113
114 template <class Key, class T>
115 typename Map<Key, T>::Iterator Map<Key, T>::find(const Key &key)
116 {
117   detach();
118   return d->map.find(key);
119 }
120
121 template <class Key, class T>
122 typename Map<Key,T>::ConstIterator Map<Key, T>::find(const Key &key) const
123 {
124   return d->map.find(key);
125 }
126
127 template <class Key, class T>
128 bool Map<Key, T>::contains(const Key &key) const
129 {
130   return d->map.find(key) != d->map.end();
131 }
132
133 template <class Key, class T>
134 Map<Key, T> &Map<Key,T>::erase(Iterator it)
135 {
136   detach();
137   d->map.erase(it);
138   return *this;
139 }
140
141 template <class Key, class T>
142 Map<Key, T> &Map<Key,T>::erase(const Key &key)
143 {
144   detach();
145   Iterator it = d->map.find(key);
146   if(it != d->map.end())
147     d->map.erase(it);
148   return *this;
149 }
150
151 template <class Key, class T>
152 TagLib::uint Map<Key, T>::size() const
153 {
154   return d->map.size();
155 }
156
157 template <class Key, class T>
158 const T &Map<Key, T>::operator[](const Key &key) const
159 {
160   return d->map[key];
161 }
162
163 template <class Key, class T>
164 T &Map<Key, T>::operator[](const Key &key)
165 {
166   detach();
167   return d->map[key];
168 }
169
170 template <class Key, class T>
171 Map<Key, T> &Map<Key, T>::operator=(const Map<Key, T> &m)
172 {
173   if(&m == this)
174     return *this;
175
176   if(d->deref())
177     delete(d);
178   d = m.d;
179   d->ref();
180   return *this;
181 }
182
183 ////////////////////////////////////////////////////////////////////////////////
184 // protected members
185 ////////////////////////////////////////////////////////////////////////////////
186
187 template <class Key, class T>
188 void Map<Key, T>::detach()
189 {
190   if(d->count() > 1) {
191     d->deref();
192     d = new MapPrivate<Key, T>(d->map);
193   }
194 }
195
196 } // namespace TagLib