MongoDB C++ Driver  legacy-1.1.2
owned_pointer_vector.h
1 /* Copyright 2012 10gen Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <cstring>
19 #include <vector>
20 
21 #include "mongo/base/disallow_copying.h"
22 
23 namespace mongo {
24 
30 template <class T>
32  MONGO_DISALLOW_COPYING(OwnedPointerVector);
33 
34 public:
37  clear();
38  }
39 
44  OwnedPointerVector(std::vector<T*> other) {
45  _vector.swap(other);
46  }
47 
52  OwnedPointerVector& operator=(std::vector<T*> other) {
53  clear();
54  _vector.swap(other);
55  return *this;
56  }
57 
58  typedef typename std::vector<T*>::const_iterator const_iterator;
59  typedef typename std::vector<T*>::const_reverse_iterator const_reverse_iterator;
60 
62  const std::vector<T*>& vector() const {
63  return _vector;
64  }
65  std::vector<T*>& mutableVector() {
66  return _vector;
67  }
68 
69  std::size_t size() const {
70  return _vector.size();
71  }
72  bool empty() const {
73  return _vector.empty();
74  }
75 
76  const_iterator begin() const {
77  return _vector.begin();
78  }
79  const_reverse_iterator rbegin() const {
80  return _vector.rbegin();
81  }
82  const_iterator end() const {
83  return _vector.end();
84  }
85  const_reverse_iterator rend() const {
86  return _vector.rend();
87  }
88 
89  T* operator[](size_t i) const {
90  return _vector[i];
91  }
92  T* back() const {
93  return _vector.back();
94  }
95  T* front() const {
96  return _vector.front();
97  }
98 
99  void push_back(T* ptr) {
100  _vector.push_back(ptr);
101  }
102 
106  void clear();
107 
111  void erase(const_iterator it) {
112  delete *it;
113  _vector.erase(toNonConstIter(it));
114  }
115 
116  void erase(const_iterator begin, const_iterator end) {
117  for (const_iterator it = begin; it != end; ++it) {
118  delete *it;
119  }
120  _vector.erase(toNonConstIter(begin), toNonConstIter(end));
121  }
122 
123  //
124  // extensions
125  //
126 
133  std::vector<T*> release() {
134  std::vector<T*> out;
135  out.swap(_vector);
136  return out;
137  }
138 
144  T* releaseAt(size_t i) {
145  T* out = _vector[i];
146  _vector[i] = NULL;
147  return out;
148  }
149 
150  T* popAndReleaseBack() {
151  T* out = _vector.back();
152  _vector.pop_back();
153  return out;
154  }
155 
156  void popAndDeleteBack() {
157  delete popAndReleaseBack();
158  }
159 
160 private:
161  typename std::vector<T*>::iterator toNonConstIter(const_iterator it) {
162  // This is needed for a few cases where c++03 vectors require non-const iterators that
163  // were relaxed in c++11 to allow const_iterators. It can go away when we require c++11.
164  return _vector.begin() + (it - begin());
165  }
166 
167  std::vector<T*> _vector;
168 };
169 
170 template <class T>
172  for (typename std::vector<T*>::iterator i = _vector.begin(); i != _vector.end(); ++i) {
173  delete *i;
174  }
175  _vector.clear();
176 }
177 
178 } // namespace mongo
OwnedPointerVector(std::vector< T * > other)
Takes ownership of all pointers contained in 'other'.
Definition: owned_pointer_vector.h:44
void clear()
Deletes all pointers in the vector, then sets its size to 0.
Definition: owned_pointer_vector.h:171
const std::vector< T * > & vector() const
Access the vector.
Definition: owned_pointer_vector.h:62
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
An std::vector wrapper that deletes pointers within a vector on destruction.
Definition: owned_pointer_vector.h:31
OwnedPointerVector & operator=(std::vector< T * > other)
Takes ownership of all pointers contained in 'other'.
Definition: owned_pointer_vector.h:52
std::vector< T * > release()
Releases the entire vector to allow you to transfer ownership.
Definition: owned_pointer_vector.h:133
void erase(const_iterator it)
Deletes the pointer at 'it', then erases it from the vector.
Definition: owned_pointer_vector.h:111
T * releaseAt(size_t i)
Releases ownership of a single element.
Definition: owned_pointer_vector.h:144