Index: third_party/WebKit/Source/wtf/TerminatedArrayBuilder.h |
diff --git a/third_party/WebKit/Source/wtf/TerminatedArrayBuilder.h b/third_party/WebKit/Source/wtf/TerminatedArrayBuilder.h |
index 531d6501d44881c14913eebc9688baaa0feaa99a..020626132993512de9f6a8ceed36261336bde9b6 100644 |
--- a/third_party/WebKit/Source/wtf/TerminatedArrayBuilder.h |
+++ b/third_party/WebKit/Source/wtf/TerminatedArrayBuilder.h |
@@ -1,9 +1,78 @@ |
-// Copyright 2017 The Chromium Authors. All rights reserved. |
+// 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 TerminatedArrayBuilder_h |
+#define TerminatedArrayBuilder_h |
-#include "platform/wtf/TerminatedArrayBuilder.h" |
+#include "wtf/Allocator.h" |
-// The contents of this header was moved to platform/wtf as part of |
-// WTF migration project. See the following post for details: |
-// https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gYCAAJ |
+namespace WTF { |
+ |
+template <typename T, template <typename> class ArrayType = TerminatedArray> |
+class TerminatedArrayBuilder { |
+ STACK_ALLOCATED(); |
+ WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder); |
+ |
+ public: |
+ explicit TerminatedArrayBuilder( |
+ typename ArrayType<T>::Allocator::PassPtr array) |
+ : m_array(array), m_count(0), m_capacity(0) { |
+ if (!m_array) |
+ return; |
+ m_capacity = m_count = m_array->size(); |
+ DCHECK(m_array->at(m_count - 1).isLastInArray()); |
+ } |
+ |
+ void grow(size_t count) { |
+ DCHECK(count); |
+ if (!m_array) { |
+ DCHECK(!m_count); |
+ DCHECK(!m_capacity); |
+ m_capacity = count; |
+ m_array = ArrayType<T>::Allocator::create(m_capacity); |
+ } else { |
+ DCHECK(m_array->at(m_count - 1).isLastInArray()); |
+ m_capacity += count; |
+ m_array = ArrayType<T>::Allocator::resize( |
+ ArrayType<T>::Allocator::release(m_array), m_capacity); |
+ m_array->at(m_count - 1).setLastInArray(false); |
+ } |
+ m_array->at(m_capacity - 1).setLastInArray(true); |
+ } |
+ |
+ void append(const T& item) { |
+ RELEASE_ASSERT(m_count < m_capacity); |
+ DCHECK(!item.isLastInArray()); |
+ m_array->at(m_count++) = item; |
+ if (m_count == m_capacity) |
+ m_array->at(m_capacity - 1).setLastInArray(true); |
+ } |
+ |
+ typename ArrayType<T>::Allocator::PassPtr release() { |
+ RELEASE_ASSERT(m_count == m_capacity); |
+ assertValid(); |
+ return ArrayType<T>::Allocator::release(m_array); |
+ } |
+ |
+ private: |
+#if DCHECK_IS_ON() |
+ void assertValid() { |
+ for (size_t i = 0; i < m_count; ++i) { |
+ bool isLastInArray = (i + 1 == m_count); |
+ DCHECK_EQ(m_array->at(i).isLastInArray(), isLastInArray); |
+ } |
+ } |
+#else |
+ void assertValid() {} |
+#endif |
+ |
+ typename ArrayType<T>::Allocator::Ptr m_array; |
+ size_t m_count; |
+ size_t m_capacity; |
+}; |
+ |
+} // namespace WTF |
+ |
+using WTF::TerminatedArrayBuilder; |
+ |
+#endif // TerminatedArrayBuilder_h |