libktorrent  2.2.0
ptrmap.h
1 /***************************************************************************
2  * Copyright (C) 2005 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 #ifndef BTPTRMAP_H
21 #define BTPTRMAP_H
22 
23 #include <map>
24 #include <ktorrent_export.h>
25 
26 namespace bt
27 {
36  template <class Key,class Data>
37  class PtrMap
38  {
39  bool autodel;
40  std::map<Key,Data*> pmap;
41  public:
46  PtrMap(bool autodel = false) : autodel(autodel)
47  {}
48 
52  virtual ~PtrMap()
53  {
54  clear();
55  }
56 
57 
61  unsigned int count() const {return pmap.size();}
62 
67  void setAutoDelete(bool yes)
68  {
69  autodel = yes;
70  }
71 
72  typedef typename std::map<Key,Data*>::iterator iterator;
73  typedef typename std::map<Key,Data*>::const_iterator const_iterator;
74 
75  iterator begin() {return pmap.begin();}
76  iterator end() {return pmap.end();}
77 
78  const_iterator begin() const {return pmap.begin();}
79  const_iterator end() const {return pmap.end();}
80 
84  void clear()
85  {
86  if (autodel)
87  {
88  for (iterator i = pmap.begin();i != pmap.end();i++)
89  {
90  delete i->second;
91  i->second = 0;
92  }
93  }
94  pmap.clear();
95  }
96 
104  bool insert(const Key & k,Data* d,bool overwrite = true)
105  {
106  iterator itr = pmap.find(k);
107  if (itr != pmap.end())
108  {
109  if (overwrite)
110  {
111  if (autodel)
112  delete itr->second;
113  itr->second = d;
114  return true;
115  }
116  else
117  {
118  return false;
119  }
120  }
121  else
122  {
123  pmap[k] = d;
124  return true;
125  }
126  }
127 
133  Data* find(const Key & k)
134  {
135  iterator i = pmap.find(k);
136  return (i == pmap.end()) ? 0 : i->second;
137  }
138 
144  const Data* find(const Key & k) const
145  {
146  const_iterator i = pmap.find(k);
147  return (i == pmap.end()) ? 0 : i->second;
148  }
149 
155  bool contains(const Key & k) const
156  {
157  const_iterator i = pmap.find(k);
158  return i != pmap.end();
159  }
160 
167  virtual bool erase(const Key & key)
168  {
169  iterator i = pmap.find(key);
170  if (i == pmap.end())
171  return false;
172 
173  if (autodel)
174  delete i->second;
175  pmap.erase(i);
176  return true;
177  }
178 
182  iterator erase(iterator i)
183  {
184  iterator j = i;
185  j++;
186  pmap.erase(i);
187  return j;
188  }
189  };
190 
191 }
192 
193 #endif
bt::PtrMap::count
unsigned int count() const
Definition: ptrmap.h:97
bt::PtrMap::clear
void clear()
Definition: ptrmap.h:120
bt::PtrMap::insert
bool insert(const Key &k, Data *d, bool overwrite=true)
Definition: ptrmap.h:140
bt::PtrMap::~PtrMap
virtual ~PtrMap()
Definition: ptrmap.h:88
bt::PtrMap::erase
virtual bool erase(const Key &key)
Definition: ptrmap.h:203
bt::PtrMap::contains
bool contains(const Key &k) const
Definition: ptrmap.h:191
bt::PtrMap::find
Data * find(const Key &k)
Definition: ptrmap.h:169
bt::PtrMap::setAutoDelete
void setAutoDelete(bool yes)
Definition: ptrmap.h:103
bt::PtrMap::PtrMap
PtrMap(bool autodel=false)
Definition: ptrmap.h:82