forked from UWQuickstep/quickstep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPtrMap.hpp
137 lines (111 loc) · 3.39 KB
/
PtrMap.hpp
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_UTILITY_PTR_MAP_HPP_
#define QUICKSTEP_UTILITY_PTR_MAP_HPP_
#include <stdexcept>
#include <unordered_map>
#include <utility>
#include "utility/Macros.hpp"
namespace quickstep {
/** \addtogroup Utility
* @{
*/
/**
* @brief A map (unordered_map where possible) whose values are pointers which
* are automatically deleted when the map goes out of scope.
* @note Iterators are regular iterators from the underlying map type, meaning
* that they are iterators of pair<const K, V*>.
**/
template <typename K, typename V>
class PtrMap {
public:
typedef typename std::unordered_map<K, V*>::iterator iterator;
typedef typename std::unordered_map<K, V*>::const_iterator const_iterator;
typedef typename std::unordered_map<K, V*>::size_type size_type;
PtrMap() {
}
~PtrMap() {
for (typename std::unordered_map<K, V*>::iterator it = internal_map_.begin();
it != internal_map_.end();
++it) {
delete it->second;
}
}
inline bool empty() const {
return internal_map_.empty();
}
inline size_type size() const {
return internal_map_.size();
}
// Iterators.
iterator begin() {
return internal_map_.begin();
}
const_iterator begin() const {
return internal_map_.begin();
}
iterator end() {
return internal_map_.end();
}
const_iterator end() const {
return internal_map_.end();
}
// Insertion.
std::pair<iterator, bool> insert(const K &key, V *value) {
return internal_map_.insert(std::pair<const K, V*>(key, value));
}
// Lookup.
inline iterator find(const K &key) {
return internal_map_.find(key);
}
inline const_iterator find(const K &key) const {
return internal_map_.find(key);
}
// Element access.
inline V& at(const K &key) {
iterator it = find(key);
if (it != end()) {
return *(it->second);
} else {
throw std::out_of_range("PtrMap::at() with nonexistent key");
}
}
inline const V& at(const K &key) const {
const_iterator it = find(key);
if (it != end()) {
return *(it->second);
} else {
throw std::out_of_range("PtrMap::at() with nonexistent key");
}
}
inline const V& atUnchecked(const K &key) const {
return *(find(key)->second);
}
// Remove all entries from this map, but do not delete values (value pointers
// should be retained and freed elsewhere to avoid leaking memory).
void clearAndRelease() {
internal_map_.clear();
}
private:
std::unordered_map<K, V*> internal_map_;
DISALLOW_COPY_AND_ASSIGN(PtrMap);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_UTILITY_PTR_MAP_HPP_