forked from Hacker0912/quickstep-datalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPtrList.hpp
279 lines (220 loc) · 6.61 KB
/
PtrList.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* 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_LIST_HPP_
#define QUICKSTEP_UTILITY_PTR_LIST_HPP_
#include <cstddef>
#include <iterator>
#include <list>
#include "utility/Macros.hpp"
namespace quickstep {
/** \addtogroup Utility
* @{
*/
/**
* @brief A templated STL-style container class which holds a list of pointers
* to objects which it owns.
**/
template <class T>
class PtrList {
public:
// TODO(chasseur): Attempt some template magic to get rid of the duplicated
// code between PtrListIterator and PtrListConstIterator.
class PtrListIterator : public std::iterator<std::forward_iterator_tag, T, int> {
public:
PtrListIterator() {
}
PtrListIterator(const PtrListIterator& other)
: internal_iterator_(other.internal_iterator_) {
}
PtrListIterator& operator=(const PtrListIterator& other) {
if (this != &other) {
internal_iterator_ = other.internal_iterator_;
}
return *this;
}
inline PtrListIterator& operator++() {
++internal_iterator_;
return *this;
}
PtrListIterator operator++(int) {
PtrListIterator result(*this);
++(*this);
return result;
}
inline PtrListIterator& operator--() {
--internal_iterator_;
return *this;
}
PtrListIterator operator--(int) {
PtrListIterator result(*this);
--(*this);
return result;
}
inline bool operator==(const PtrListIterator& other) const {
return internal_iterator_ == other.internal_iterator_;
}
inline bool operator!=(const PtrListIterator& other) const {
return internal_iterator_ != other.internal_iterator_;
}
inline T& operator*() const {
return **internal_iterator_;
}
inline T* operator->() const {
return *internal_iterator_;
}
private:
explicit PtrListIterator(typename std::list<T*>::iterator internal_iterator)
: internal_iterator_(internal_iterator) {
}
typename std::list<T*>::iterator internal_iterator_;
friend class PtrList;
friend class PtrListConstIterator;
};
class PtrListConstIterator : public std::iterator<std::input_iterator_tag, const T, int> {
public:
PtrListConstIterator() {
}
PtrListConstIterator(const PtrListConstIterator& other)
: internal_iterator_(other.internal_iterator_) {
}
PtrListConstIterator(const PtrListIterator& other) // NOLINT(runtime/explicit) - allow implicit conversion
: internal_iterator_(other.internal_iterator_) {
}
PtrListConstIterator& operator=(const PtrListConstIterator& other) {
if (this != &other) {
internal_iterator_ = other.internal_iterator_;
}
return *this;
}
PtrListConstIterator& operator=(const PtrListIterator& other) {
internal_iterator_ = other.internal_iterator_;
return *this;
}
inline PtrListConstIterator& operator++() {
++internal_iterator_;
return *this;
}
PtrListConstIterator operator++(int) {
PtrListConstIterator result(*this);
++(*this);
return result;
}
inline PtrListConstIterator& operator--() {
--internal_iterator_;
return *this;
}
PtrListConstIterator operator--(int) {
PtrListConstIterator result(*this);
--(*this);
return result;
}
inline bool operator==(const PtrListConstIterator& other) const {
return internal_iterator_ == other.internal_iterator_;
}
inline bool operator!=(const PtrListConstIterator& other) const {
return internal_iterator_ != other.internal_iterator_;
}
inline const T& operator*() const {
return **internal_iterator_;
}
inline const T* operator->() const {
return *internal_iterator_;
}
private:
explicit PtrListConstIterator(typename std::list<T*>::const_iterator internal_iterator)
: internal_iterator_(internal_iterator) {
}
typename std::list<T*>::const_iterator internal_iterator_;
friend class PtrList;
};
typedef PtrListIterator iterator;
typedef PtrListConstIterator const_iterator;
PtrList() {
}
~PtrList() {
clear();
}
// Add an element
inline void push_back(T* elt) {
internal_list_.push_back(elt);
}
inline void push_front(T* elt) {
internal_list_.push_front(elt);
}
// Get cardinality of list
inline std::size_t size() const {
return internal_list_.size();
}
// Test whether list is empty
inline bool empty() const {
return internal_list_.empty();
}
// Iterators
iterator begin() {
return PtrListIterator(internal_list_.begin());
}
iterator end() {
return PtrListIterator(internal_list_.end());
}
T& front() {
return *(internal_list_.front());
}
T& back() {
return *(internal_list_.back());
}
const_iterator begin() const {
return PtrListConstIterator(internal_list_.begin());
}
const_iterator end() const {
return PtrListConstIterator(internal_list_.end());
}
const T& front() const {
return *(internal_list_.front());
}
const T& back() const {
return *(internal_list_.back());
}
void splice(iterator position, PtrList<T> &source) { // NOLINT(runtime/references) - STL-style interface
internal_list_.splice(position.internal_iterator_, source.internal_list_);
}
/**
* @brief Clear contents but do not release memory. Don't call this unless
* you know what you're doing.
**/
void clearWithoutRelease() {
internal_list_.clear();
}
/**
* @brief Clear contents and release memory.
*/
void clear() {
for (typename std::list<T*>::iterator it = internal_list_.begin();
it != internal_list_.end();
++it) {
delete *it;
}
internal_list_.clear();
}
private:
std::list<T*> internal_list_;
DISALLOW_COPY_AND_ASSIGN(PtrList);
};
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_UTILITY_PTR_LIST_HPP_