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., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 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#include "trefcounter.h"
27
28namespace TagLib {
29
30////////////////////////////////////////////////////////////////////////////////
31// public members
32////////////////////////////////////////////////////////////////////////////////
33
34// BIC change to RefCounter
35template <class Key, class T>
36template <class KeyP, class TP>
37class Map<Key, T>::MapPrivate : public RefCounterOld
38{
39public:
40 MapPrivate() : RefCounterOld() {}
41#ifdef WANT_CLASS_INSTANTIATION_OF_MAP
42 MapPrivate(const std::map<class KeyP, class TP>& m) : RefCounterOld(), map(m) {}
43 std::map<class KeyP, class TP> map;
44#else
45 MapPrivate(const std::map<KeyP, TP>& m) : RefCounterOld(), map(m) {}
46 std::map<KeyP, TP> map;
47#endif
48};
49
50template <class Key, class T>
51Map<Key, T>::Map()
52{
53 d = new MapPrivate<Key, T>;
54}
55
56template <class Key, class T>
57Map<Key, T>::Map(const Map<Key, T> &m) : d(m.d)
58{
59 d->ref();
60}
61
62template <class Key, class T>
63Map<Key, T>::~Map()
64{
65 if(d->deref())
66 delete(d);
67}
68
69template <class Key, class T>
70typename Map<Key, T>::Iterator Map<Key, T>::begin()
71{
72 detach();
73 return d->map.begin();
74}
75
76template <class Key, class T>
77typename Map<Key, T>::ConstIterator Map<Key, T>::begin() const
78{
79 return d->map.begin();
80}
81
82template <class Key, class T>
83typename Map<Key, T>::Iterator Map<Key, T>::end()
84{
85 detach();
86 return d->map.end();
87}
88
89template <class Key, class T>
90typename Map<Key, T>::ConstIterator Map<Key, T>::end() const
91{
92 return d->map.end();
93}
94
95template <class Key, class T>
96Map<Key, T> &Map<Key, T>::insert(const Key &key, const T &value)
97{
98 detach();
99 d->map[key] = value;
100 return *this;
101}
102
103template <class Key, class T>
104Map<Key, T> &Map<Key, T>::clear()
105{
106 detach();
107 d->map.clear();
108 return *this;
109}
110
111template <class Key, class T>
112bool Map<Key, T>::isEmpty() const
113{
114 return d->map.empty();
115}
116
117template <class Key, class T>
118typename Map<Key, T>::Iterator Map<Key, T>::find(const Key &key)
119{
120 detach();
121 return d->map.find(key);
122}
123
124template <class Key, class T>
125typename Map<Key,T>::ConstIterator Map<Key, T>::find(const Key &key) const
126{
127 return d->map.find(key);
128}
129
130template <class Key, class T>
131bool Map<Key, T>::contains(const Key &key) const
132{
133 return d->map.find(key) != d->map.end();
134}
135
136template <class Key, class T>
137Map<Key, T> &Map<Key,T>::erase(Iterator it)
138{
139 detach();
140 d->map.erase(it);
141 return *this;
142}
143
144template <class Key, class T>
145Map<Key, T> &Map<Key,T>::erase(const Key &key)
146{
147 detach();
148 d->map.erase(key);
149 return *this;
150}
151
152template <class Key, class T>
153unsigned int Map<Key, T>::size() const
154{
155 return d->map.size();
156}
157
158template <class Key, class T>
159const T &Map<Key, T>::operator[](const Key &key) const
160{
161 return d->map[key];
162}
163
164template <class Key, class T>
165T &Map<Key, T>::operator[](const Key &key)
166{
167 detach();
168 return d->map[key];
169}
170
171template <class Key, class T>
172Map<Key, T> &Map<Key, T>::operator=(const Map<Key, T> &m)
173{
174 if(&m == this)
175 return *this;
176
177 if(d->deref())
178 delete(d);
179 d = m.d;
180 d->ref();
181 return *this;
182}
183
184////////////////////////////////////////////////////////////////////////////////
185// protected members
186////////////////////////////////////////////////////////////////////////////////
187
188template <class Key, class T>
189void Map<Key, T>::detach()
190{
191 if(d->count() > 1) {
192 d->deref();
193 d = new MapPrivate<Key, T>(d->map);
194 }
195}
196
197} // namespace TagLib
198