1515
1616#pragma once
1717
18+ #include < cstring>
1819#include < vector>
1920
2021#include " mongo/base/disallow_copying.h"
@@ -31,8 +32,24 @@ namespace mongo {
3132 MONGO_DISALLOW_COPYING (OwnedPointerVector);
3233
3334 public:
34- OwnedPointerVector ();
35- ~OwnedPointerVector ();
35+ OwnedPointerVector () {}
36+ ~OwnedPointerVector () { clear (); }
37+
38+ /* *
39+ * Takes ownership of all pointers contained in 'other'.
40+ * NOTE: argument is intentionally taken by value.
41+ */
42+ OwnedPointerVector (std::vector<T*> other) { _vector.swap (other); }
43+
44+ /* *
45+ * Takes ownership of all pointers contained in 'other'.
46+ * NOTE: argument is intentionally taken by value.
47+ */
48+ OwnedPointerVector& operator =(std::vector<T*> other) {
49+ clear ();
50+ _vector.swap (other);
51+ return *this ;
52+ }
3653
3754 typedef typename std::vector<T*>::const_iterator const_iterator;
3855 typedef typename std::vector<T*>::const_reverse_iterator const_reverse_iterator;
@@ -49,23 +66,69 @@ namespace mongo {
4966 const_iterator end () const { return _vector.end (); }
5067 const_reverse_iterator rend () const { return _vector.rend (); }
5168
69+ T* operator [] (size_t i) const { return _vector[i]; }
70+ T* back () const { return _vector.back (); }
71+ T* front () const { return _vector.front (); }
72+
73+ void push_back (T* ptr) { _vector.push_back (ptr); }
74+
75+ /* *
76+ * Deletes all pointers in the vector, then sets its size to 0.
77+ */
5278 void clear ();
5379
80+ /* *
81+ * Deletes the pointer at 'it', then erases it from the vector.
82+ */
83+ void erase (const_iterator it) {
84+ delete *it;
85+ // vector::erase(const_iterator) is new in c++11, so converting to non-const iterator.
86+ _vector.erase (_vector.begin () + (it - begin ()));
87+ }
88+
89+ //
90+ // extensions
91+ //
92+
93+ /* *
94+ * Releases the entire vector to allow you to transfer ownership.
95+ *
96+ * Leaves the OwnedPointerVector empty.
97+ * Named after the similar method and pattern in std::auto_ptr.
98+ */
99+ std::vector<T*> release () {
100+ std::vector<T*> out;
101+ out.swap (_vector);
102+ return out;
103+ }
104+
105+ /* *
106+ * Releases ownership of a single element.
107+ *
108+ * Sets that element to NULL and does not change size().
109+ */
110+ T* releaseAt (size_t i) {
111+ T* out = _vector[i];
112+ _vector[i] = NULL ;
113+ return out;
114+ }
115+
116+ T* popAndReleaseBack () {
117+ T* out = _vector.back ();
118+ _vector.pop_back ();
119+ return out;
120+ }
121+
122+ void popAndDeleteBack () {
123+ delete popAndReleaseBack ();
124+ }
125+
54126 private:
55127 std::vector<T*> _vector;
56128 };
57129
58130 template <class T >
59- OwnedPointerVector<T>::OwnedPointerVector() {
60- }
61-
62- template <class T >
63- OwnedPointerVector<T>::~OwnedPointerVector () {
64- clear ();
65- }
66-
67- template <class T >
68- void OwnedPointerVector<T>::clear() {
131+ inline void OwnedPointerVector<T>::clear() {
69132 for ( typename std::vector<T*>::iterator i = _vector.begin (); i != _vector.end (); ++i ) {
70133 delete *i;
71134 }
0 commit comments