blob: 6bd84e8c56f9eecad955187175b91cf7e4ff2fea (
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
|
#ifndef MERT_SCOPED_VECTOR_H_
#define MERT_SCOPED_VECTOR_H_
#include <vector>
template <class T>
class ScopedVector {
public:
typedef typename std::vector<T*>::iterator iterator;
typedef typename std::vector<T*>::const_iterator const_iterator;
ScopedVector() {}
virtual ~ScopedVector() { reset(); }
bool empty() const { return vec_.empty(); }
void push_back(T *e) { vec_.push_back(e); }
void reset() {
for (iterator it = vec_.begin(); it != vec_.end(); ++it) {
delete *it;
}
vec_.clear();
}
void reserve(size_t capacity) { vec_.reserve(capacity); }
void resize(size_t size) { vec_.resize(size); }
size_t size() const {return vec_.size(); }
iterator begin() { return vec_.begin(); }
const_iterator begin() const { return vec_.begin(); }
iterator end() { return vec_.end(); }
const_iterator end() const { return vec_.end(); }
std::vector<T*>& get() { return vec_; }
const std::vector<T*>& get() const { return vec_; }
std::vector<T*>* operator->() { return &vec_; }
const std::vector<T*>* operator->() const { return &vec_; }
T*& operator[](size_t i) { return vec_[i]; }
const T* operator[](size_t i) const { return vec_[i]; }
private:
std::vector<T*> vec_;
// no copying allowed.
ScopedVector<T>(const ScopedVector<T>&);
void operator=(const ScopedVector<T>&);
};
#endif // MERT_SCOPED_VECTOR_H_
|