OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WTF_OwnPtrList_h |
| 6 #define WTF_OwnPtrList_h |
| 7 |
| 8 #include "wtf/OwnPtr.h" |
| 9 #include "wtf/PassOwnPtr.h" |
| 10 #include "wtf/Vector.h" |
| 11 |
| 12 namespace WTF { |
| 13 |
| 14 // OwnPtrList<T> is a list container for OwnPtr<T>. |
| 15 // Vector<OwnPtr<T>> cannot be used because we cannot copy OwnPtr<T>. |
| 16 template<typename T> |
| 17 class OwnPtrList { |
| 18 WTF_MAKE_NONCOPYABLE(OwnPtrList); |
| 19 public: |
| 20 OwnPtrList() { } |
| 21 void append(PassOwnPtr<T> ptr) |
| 22 { |
| 23 OwnPtr<OwnPtrListNode<T>> node = adoptPtr(new OwnPtrListNode<T>(ptr, m_h
ead.release())); |
| 24 m_head = node.release(); |
| 25 } |
| 26 |
| 27 // call ptr->f() for each OwnPtr in the list, in the order of |append|. |
| 28 void applyEach(void(T::*f)()) const |
| 29 { |
| 30 Vector<T*> v; |
| 31 for (OwnPtrListNode<T>* p = m_head.get(); p; p = p->m_next.get()) { |
| 32 v.append(p->m_ptr.get()); |
| 33 } |
| 34 for (typename Vector<T*>::reverse_iterator it = v.rbegin(), itEnd = v.re
nd(); it != itEnd; ++it) |
| 35 ((*it)->*f)(); |
| 36 } |
| 37 |
| 38 void applyEach(void(T::*f)() const) const |
| 39 { |
| 40 Vector<T*> v; |
| 41 for (OwnPtrListNode<T>* p = m_head.get(); p; p = p->m_next.get()) { |
| 42 v.append(p->m_ptr.get()); |
| 43 } |
| 44 for (typename Vector<T*>::const_reverse_iterator it = v.rbegin(), itEnd
= v.rend(); it != itEnd; ++it) |
| 45 ((*it)->*f)(); |
| 46 } |
| 47 |
| 48 private: |
| 49 template<typename U> |
| 50 class OwnPtrListNode { |
| 51 WTF_MAKE_NONCOPYABLE(OwnPtrListNode); |
| 52 public: |
| 53 OwnPtrListNode(PassOwnPtr<U> ptr, PassOwnPtr<OwnPtrListNode<U>> next) |
| 54 : m_ptr(ptr) |
| 55 , m_next(next) |
| 56 { |
| 57 } |
| 58 private: |
| 59 OwnPtr<U> m_ptr; |
| 60 OwnPtr<OwnPtrListNode<U>> m_next; |
| 61 friend class OwnPtrList<U>; |
| 62 }; |
| 63 |
| 64 // The linked list starting from |m_head| stores the elements in the |
| 65 // reverse order of |append|. |
| 66 OwnPtr<OwnPtrListNode<T>> m_head; |
| 67 }; |
| 68 |
| 69 } // namespace WTF |
| 70 |
| 71 using WTF::OwnPtrList; |
| 72 |
| 73 #endif // WTF_OwnPtrList_h |
OLD | NEW |