summaryrefslogtreecommitdiffstats
path: root/libktorrent/util/ptrmap.h
blob: 36e1c20b1217a7ce18f1c5a3dda33cbb4ce14d2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef BTPTRMAP_H
#define BTPTRMAP_H

#include <map>

namespace bt
{
	/**
	* @author Joris Guisson
	* @brief Map of pointers
	*
	* A Map where the data is a pointer. The PtrMap has an autodeletion feature.
	* When autodelete is on, every time we remove something from the map, the data
	* will be deleted.
	*/
	template <class Key,class Data>
	class PtrMap
	{
		bool autodel;
		std::map<Key,Data*> pmap;
	public:
		/**
		* Constructor.
		* @param auto_del Wether or not to enable auto deletion
		*/
		PtrMap(bool autodel = false) : autodel(autodel)
		{}
		
		/**
		* Destructor. Will delete all objects, if auto deletion is on.
		*/
		virtual ~PtrMap()
		{
			clear();
		}
		
		
		/**
		* Return the number of key data pairs in the map.
		*/
		unsigned int count() const {return pmap.size();}
		
		/**
		* Enable or disable auto deletion.
		* @param yes Enable if true, disable if false
		*/
		void setAutoDelete(bool yes)
		{
			autodel = yes;
		}
		
		typedef typename std::map<Key,Data*>::iterator iterator;
		typedef typename std::map<Key,Data*>::const_iterator const_iterator;
		
		iterator begin() {return pmap.begin();}
		iterator end() {return pmap.end();}
		
		const_iterator begin() const {return pmap.begin();}
		const_iterator end() const {return pmap.end();}
		
		/**
		* Remove all objects, will delete them if autodelete is on.
		*/
		void clear()
		{
			if (autodel)
			{
				for (iterator i = pmap.begin();i != pmap.end();i++)
				{
					delete i->second;
					i->second = 0;
				}
			}
			pmap.clear();
		}
		
		/**
		* Insert a key data pair.
		* @param k The key
		* @param d The data
		* @param overwrite Wether or not to overwrite
		* @return true if the insertion took place
		*/
		bool insert(const Key & k,Data* d,bool overwrite = true)
		{
			iterator itr =  pmap.find(k);
			if (itr != pmap.end())
			{
				if (overwrite)
				{
					if (autodel)
						delete itr->second;
					itr->second = d;
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				pmap[k] = d;
				return true;
			}
		}
	
		/**
		* Find a key in the map and returns it's data.
		* @param k The key
		* @return The data of the key, 0 if the key isn't in the map
		*/
		Data* find(const Key & k)
		{
			iterator i = pmap.find(k);
			return (i == pmap.end()) ? 0 : i->second;
		}
		
		/**
		* Find a key in the map and returns it's data.
		* @param k The key
		* @return The data of the key, 0 if the key isn't in the map
		*/
		const Data* find(const Key & k) const
		{
			const_iterator i = pmap.find(k);
			return (i == pmap.end()) ? 0 : i->second;
		}
		
		/**
		* Check to see if a key is in the map.
		* @param k The key
		* @return true if it is part of the map
		*/
		bool contains(const Key & k) const
		{
			const_iterator i = pmap.find(k);
			return i != pmap.end();
		}
		
		/**
		* Erase a key from the map. Will delete
		* the data if autodelete is on.
		* @param key The key
		* @return true if an erase took place
		*/
		bool erase(const Key & key)
		{
			iterator i = pmap.find(key);
			if (i == pmap.end())
				return false;
			
			if (autodel)
				delete i->second;
			pmap.erase(i);
			return true;
		}
	};

}

#endif