xoreos  0.0.5
ptrmap.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_PTRMAP_H
26 #define COMMON_PTRMAP_H
27 
28 #include <functional>
29 #include <map>
30 
31 #include <boost/noncopyable.hpp>
32 
33 #include "src/common/deallocator.h"
34 
35 namespace Common {
36 
41 template<typename Key, typename T, class Compare = std::less<Key>, class Deallocator = DeallocatorDefault>
42 class PtrMap : boost::noncopyable, public std::map<Key, T *, Compare> {
43 public:
44  ~PtrMap() {
45  clear();
46  }
47 
48  void clear() {
49  for (typename std::map<Key, T *, Compare>::iterator it = std::map<Key, T *, Compare>::begin();
50  it != std::map<Key, T *, Compare>::end(); ++it)
51  Deallocator::destroy(it->second);
52 
53  std::map<Key, T *, Compare>::clear();
54  }
55 
56  void erase(typename std::map<Key, T *, Compare>::iterator position) {
57  Deallocator::destroy(position->second);
58  std::map<Key, T *, Compare>::erase(position);
59  }
60 
61  typename std::map<Key, T *, Compare>::size_type
62  erase(const typename std::map<Key, T *, Compare>::key_type &k) {
63 
64  typename std::map<Key, T *, Compare>::iterator it(std::map<Key, T *, Compare>::find(k));
65  if (it == std::map<Key, T *, Compare>::end())
66  return 0;
67 
68  erase(it);
69  return 1;
70  }
71 
72  void erase(typename std::map<Key, T *, Compare>::iterator first,
73  typename std::map<Key, T *, Compare>::iterator last) {
74 
75  for (typename std::map<Key, T *, Compare>::iterator it = std::map<Key, T *, Compare>::begin();
76  it != std::map<Key, T *, Compare>::end(); ++it)
77  Deallocator::destroy(it->second);
78 
79  std::map<Key, T *, Compare>::erase(first, last);
80  }
81 };
82 
83 } // End of namespace Common
84 
85 #endif // COMMON_PTRMAP_H
void erase(typename std::map< Key, T *, Compare >::iterator position)
Definition: ptrmap.h:56
Definition: 2dafile.h:39
void erase(typename std::map< Key, T *, Compare >::iterator first, typename std::map< Key, T *, Compare >::iterator last)
Definition: ptrmap.h:72
void clear()
Definition: ptrmap.h:48
std::map< Key, T *, Compare >::size_type erase(const typename std::map< Key, T *, Compare >::key_type &k)
Definition: ptrmap.h:62
Simple deallocator concept.
A map of pointer to objects, with automatic deletion.
Definition: ptrmap.h:42