blob: eec013109a064e12c8b59c5da88753b02d5039d4 [file] [log] [blame]
[email protected]4f2ea592014-03-05 18:27:221// 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#ifndef TerminatedArrayBuilder_h
5#define TerminatedArrayBuilder_h
6
7#include "wtf/OwnPtr.h"
8
9namespace WTF {
10
[email protected]e8d25e62014-03-11 23:54:3611template<typename T, template <typename> class ArrayType = TerminatedArray>
[email protected]4f2ea592014-03-05 18:27:2212class TerminatedArrayBuilder {
haraken706d7f0a2015-10-30 08:46:0813 DISALLOW_NEW();
[email protected]4f2ea592014-03-05 18:27:2214 WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder);
15public:
[email protected]e8d25e62014-03-11 23:54:3616 explicit TerminatedArrayBuilder(typename ArrayType<T>::Allocator::PassPtr array)
[email protected]4f2ea592014-03-05 18:27:2217 : m_array(array)
18 , m_count(0)
19 , m_capacity(0)
20 {
21 if (!m_array)
22 return;
23 m_capacity = m_count = m_array->size();
sigbjornfdbab3f02016-01-29 17:35:5024 ASSERT(m_array->at(m_count - 1).isLastInArray());
[email protected]4f2ea592014-03-05 18:27:2225 }
26
27 void grow(size_t count)
28 {
29 ASSERT(count);
30 if (!m_array) {
31 ASSERT(!m_count);
32 ASSERT(!m_capacity);
33 m_capacity = count;
[email protected]e8d25e62014-03-11 23:54:3634 m_array = ArrayType<T>::Allocator::create(m_capacity);
sigbjornfdbab3f02016-01-29 17:35:5035 } else {
36 ASSERT(m_array->at(m_count - 1).isLastInArray());
37 m_capacity += count;
38 m_array = ArrayType<T>::Allocator::resize(m_array.release(), m_capacity);
39 m_array->at(m_count - 1).setLastInArray(false);
[email protected]4f2ea592014-03-05 18:27:2240 }
sigbjornfdbab3f02016-01-29 17:35:5041 m_array->at(m_capacity - 1).setLastInArray(true);
[email protected]4f2ea592014-03-05 18:27:2242 }
43
44 void append(const T& item)
45 {
46 RELEASE_ASSERT(m_count < m_capacity);
47 ASSERT(!item.isLastInArray());
48 m_array->at(m_count++) = item;
sigbjornfdbab3f02016-01-29 17:35:5049 if (m_count == m_capacity)
50 m_array->at(m_capacity - 1).setLastInArray(true);
[email protected]4f2ea592014-03-05 18:27:2251 }
52
[email protected]e8d25e62014-03-11 23:54:3653 typename ArrayType<T>::Allocator::PassPtr release()
[email protected]4f2ea592014-03-05 18:27:2254 {
55 RELEASE_ASSERT(m_count == m_capacity);
[email protected]4f2ea592014-03-05 18:27:2256 assertValid();
57 return m_array.release();
58 }
59
60private:
[email protected]98b71012014-07-17 23:17:2161#if ENABLE(ASSERT)
[email protected]4f2ea592014-03-05 18:27:2262 void assertValid()
63 {
64 for (size_t i = 0; i < m_count; ++i) {
65 bool isLastInArray = (i + 1 == m_count);
66 ASSERT(m_array->at(i).isLastInArray() == isLastInArray);
67 }
68 }
69#else
70 void assertValid() { }
71#endif
72
[email protected]e8d25e62014-03-11 23:54:3673 typename ArrayType<T>::Allocator::Ptr m_array;
[email protected]4f2ea592014-03-05 18:27:2274 size_t m_count;
75 size_t m_capacity;
76};
77
78} // namespace WTF
79
80using WTF::TerminatedArrayBuilder;
81
82#endif // TerminatedArrayBuilder_h