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