Index: Source/wtf/OwnPtrList.h |
diff --git a/Source/wtf/OwnPtrList.h b/Source/wtf/OwnPtrList.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b910258f0795da0f01cb2d0f894972f5cd60b4f2 |
--- /dev/null |
+++ b/Source/wtf/OwnPtrList.h |
@@ -0,0 +1,73 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef WTF_OwnPtrList_h |
+#define WTF_OwnPtrList_h |
+ |
+#include "wtf/OwnPtr.h" |
+#include "wtf/PassOwnPtr.h" |
+#include "wtf/Vector.h" |
+ |
+namespace WTF { |
+ |
+// OwnPtrList<T> is a list container for OwnPtr<T>. |
+// Vector<OwnPtr<T>> cannot be used because we cannot copy OwnPtr<T>. |
+template<typename T> |
+class OwnPtrList { |
+ WTF_MAKE_NONCOPYABLE(OwnPtrList); |
+public: |
+ OwnPtrList() { } |
+ void append(PassOwnPtr<T> ptr) |
+ { |
+ OwnPtr<OwnPtrListNode<T>> node = adoptPtr(new OwnPtrListNode<T>(ptr, m_head.release())); |
+ m_head = node.release(); |
+ } |
+ |
+ // call ptr->f() for each OwnPtr in the list, in the order of |append|. |
+ void applyEach(void(T::*f)()) const |
+ { |
+ Vector<T*> v; |
+ for (OwnPtrListNode<T>* p = m_head.get(); p; p = p->m_next.get()) { |
+ v.append(p->m_ptr.get()); |
+ } |
+ for (typename Vector<T*>::reverse_iterator it = v.rbegin(), itEnd = v.rend(); it != itEnd; ++it) |
+ ((*it)->*f)(); |
+ } |
+ |
+ void applyEach(void(T::*f)() const) const |
+ { |
+ Vector<T*> v; |
+ for (OwnPtrListNode<T>* p = m_head.get(); p; p = p->m_next.get()) { |
+ v.append(p->m_ptr.get()); |
+ } |
+ for (typename Vector<T*>::const_reverse_iterator it = v.rbegin(), itEnd = v.rend(); it != itEnd; ++it) |
+ ((*it)->*f)(); |
+ } |
+ |
+private: |
+ template<typename U> |
+ class OwnPtrListNode { |
+ WTF_MAKE_NONCOPYABLE(OwnPtrListNode); |
+ public: |
+ OwnPtrListNode(PassOwnPtr<U> ptr, PassOwnPtr<OwnPtrListNode<U>> next) |
+ : m_ptr(ptr) |
+ , m_next(next) |
+ { |
+ } |
+ private: |
+ OwnPtr<U> m_ptr; |
+ OwnPtr<OwnPtrListNode<U>> m_next; |
+ friend class OwnPtrList<U>; |
+ }; |
+ |
+ // The linked list starting from |m_head| stores the elements in the |
+ // reverse order of |append|. |
+ OwnPtr<OwnPtrListNode<T>> m_head; |
+}; |
+ |
+} // namespace WTF |
+ |
+using WTF::OwnPtrList; |
+ |
+#endif // WTF_OwnPtrList_h |