diff options
author | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-09 07:22:45 +0000 |
---|---|---|
committer | danakj@chromium.org <danakj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-09 07:22:45 +0000 |
commit | ead39c5d73da35d0e0e4e9c1a02c53ec3727cb53 (patch) | |
tree | b42ce1909161c94b5e72eae7bd4a6ec878d5c6b7 /cc/scoped_ptr_vector.h | |
parent | 46ad823d93a1223e34ea3160284d45163ab7a2d0 (diff) | |
download | chromium_src-ead39c5d73da35d0e0e4e9c1a02c53ec3727cb53.zip chromium_src-ead39c5d73da35d0e0e4e9c1a02c53ec3727cb53.tar.gz chromium_src-ead39c5d73da35d0e0e4e9c1a02c53ec3727cb53.tar.bz2 |
cc: Make the ScopedPtrVector and ScopedPtrDeque containers act like STL vector and deque.
These classes used methods to match the webcore classes and ease
migration. Now they are at odds with the STL versions of these containers.
Rename the methods, and change arguments from indexes to iterators, to
match the STL containers.
isEmpty() => empty()
last() => back()
first() => front()
Peek() => at()
append() => push_back()
insert(index, value) => insert(iterator, value)
remove(index) => erase(iterator) and erase(iterator, iterator)
take(index) => take(iterator)
takeFront() => take_front()
takeBack() => take_back()
Review URL: https://chromiumcodereview.appspot.com/11418108
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175722 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cc/scoped_ptr_vector.h')
-rw-r--r-- | cc/scoped_ptr_vector.h | 101 |
1 files changed, 76 insertions, 25 deletions
diff --git a/cc/scoped_ptr_vector.h b/cc/scoped_ptr_vector.h index 80f4496..2a8d9610 100644 --- a/cc/scoped_ptr_vector.h +++ b/cc/scoped_ptr_vector.h @@ -17,12 +17,25 @@ namespace cc { template <typename T> class ScopedPtrVector { public: - typedef typename std::vector<T*>::iterator iterator; typedef typename std::vector<T*>::const_iterator const_iterator; typedef typename std::vector<T*>::reverse_iterator reverse_iterator; typedef typename std::vector<T*>::const_reverse_iterator const_reverse_iterator; +#if defined(OS_ANDROID) + // On Android the iterator is not a class, so we can't block assignment. + typedef typename std::vector<T*>::iterator iterator; +#else + // Ban setting values on the iterator directly. New pointers must be passed + // to methods on the ScopedPtrVector class to appear in the vector. + class iterator : public std::vector<T*>::iterator { + public: + iterator(const typename std::vector<T*>::iterator& other) + : std::vector<T*>::iterator(other) {} + T* const& operator*() { return std::vector<T*>::iterator::operator*(); } + }; +#endif + ScopedPtrVector() {} ~ScopedPtrVector() { clear(); } @@ -31,40 +44,64 @@ class ScopedPtrVector { return data_.size(); } - T* Peek(size_t index) const { + T* at(size_t index) const { DCHECK(index < size()); return data_[index]; } T* operator[](size_t index) const { - return Peek(index); + return at(index); } - T* first() const { - DCHECK(!isEmpty()); - return Peek(0); + T* front() const { + DCHECK(!empty()); + return at(0); } - T* last() const { - DCHECK(!isEmpty()); - return Peek(size() - 1); + T* back() const { + DCHECK(!empty()); + return at(size() - 1); } - bool isEmpty() const { - return size() == 0; + bool empty() const { + return data_.empty(); } - scoped_ptr<T> take(size_t index) { - DCHECK(index < size()); - scoped_ptr<T> ret(data_[index]); - data_[index] = NULL; + scoped_ptr<T> take(iterator position) { + if (position == end()) + return scoped_ptr<T>(NULL); + DCHECK(position < end()); + + typename std::vector<T*>::iterator writable_position = position; + scoped_ptr<T> ret(*writable_position); + *writable_position = NULL; return ret.Pass(); } - void remove(size_t index) { - DCHECK(index < size()); - delete data_[index]; - data_.erase(data_.begin() + index); + scoped_ptr<T> take_back() { + DCHECK(!empty()); + if (empty()) + return scoped_ptr<T>(NULL); + return take(end() - 1); + } + + void erase(iterator position) { + if (position == end()) + return; + typename std::vector<T*>::iterator writable_position = position; + delete *writable_position; + data_.erase(position); + } + + void erase(iterator first, iterator last) { + DCHECK(first <= last); + for (iterator it = first; it != last; ++it) { + DCHECK(it < end()); + + typename std::vector<T*>::iterator writable_it = it; + delete *writable_it; + } + data_.erase(first, last); } void reserve(size_t size) { @@ -75,22 +112,36 @@ class ScopedPtrVector { STLDeleteElements(&data_); } - void append(scoped_ptr<T> item) { + void push_back(scoped_ptr<T> item) { data_.push_back(item.release()); } - void insert(size_t index, scoped_ptr<T> item) { - DCHECK(index <= size()); - data_.insert(data_.begin() + index, item.release()); + void pop_back() { + data_.pop_back(); + } + + void insert(iterator position, scoped_ptr<T> item) { + DCHECK(position <= end()); + data_.insert(position, item.release()); } void swap(ScopedPtrVector<T>& other) { data_.swap(other.data_); } - iterator begin() { return data_.begin(); } + void swap(iterator a, iterator b) { + DCHECK(a < end()); + DCHECK(b < end()); + if (a == end() || b == end() || a == b) + return; + typename std::vector<T*>::iterator writable_a = a; + typename std::vector<T*>::iterator writable_b = b; + std::swap(*writable_a, *writable_b); + } + + iterator begin() { return static_cast<iterator>(data_.begin()); } const_iterator begin() const { return data_.begin(); } - iterator end() { return data_.end(); } + iterator end() { return static_cast<iterator>(data_.end()); } const_iterator end() const { return data_.end(); } reverse_iterator rbegin() { return data_.rbegin(); } |