[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 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 | #ifndef TerminatedArrayBuilder_h | ||||
5 | #define TerminatedArrayBuilder_h | ||||
6 | |||||
7 | #include "wtf/OwnPtr.h" | ||||
8 | |||||
9 | namespace WTF { | ||||
10 | |||||
[email protected] | e8d25e6 | 2014-03-11 23:54:36 | [diff] [blame] | 11 | template<typename T, template <typename> class ArrayType = TerminatedArray> |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 12 | class TerminatedArrayBuilder { |
haraken | 706d7f0a | 2015-10-30 08:46:08 | [diff] [blame] | 13 | DISALLOW_NEW(); |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 14 | WTF_MAKE_NONCOPYABLE(TerminatedArrayBuilder); |
15 | public: | ||||
[email protected] | e8d25e6 | 2014-03-11 23:54:36 | [diff] [blame] | 16 | explicit TerminatedArrayBuilder(typename ArrayType<T>::Allocator::PassPtr array) |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 17 | : 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(); | ||||
sigbjornf | dbab3f0 | 2016-01-29 17:35:50 | [diff] [blame] | 24 | ASSERT(m_array->at(m_count - 1).isLastInArray()); |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 25 | } |
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] | e8d25e6 | 2014-03-11 23:54:36 | [diff] [blame] | 34 | m_array = ArrayType<T>::Allocator::create(m_capacity); |
sigbjornf | dbab3f0 | 2016-01-29 17:35:50 | [diff] [blame] | 35 | } 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] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 40 | } |
sigbjornf | dbab3f0 | 2016-01-29 17:35:50 | [diff] [blame] | 41 | m_array->at(m_capacity - 1).setLastInArray(true); |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 42 | } |
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; | ||||
sigbjornf | dbab3f0 | 2016-01-29 17:35:50 | [diff] [blame] | 49 | if (m_count == m_capacity) |
50 | m_array->at(m_capacity - 1).setLastInArray(true); | ||||
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 51 | } |
52 | |||||
[email protected] | e8d25e6 | 2014-03-11 23:54:36 | [diff] [blame] | 53 | typename ArrayType<T>::Allocator::PassPtr release() |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 54 | { |
55 | RELEASE_ASSERT(m_count == m_capacity); | ||||
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 56 | assertValid(); |
57 | return m_array.release(); | ||||
58 | } | ||||
59 | |||||
60 | private: | ||||
[email protected] | 98b7101 | 2014-07-17 23:17:21 | [diff] [blame] | 61 | #if ENABLE(ASSERT) |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 62 | 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] | e8d25e6 | 2014-03-11 23:54:36 | [diff] [blame] | 73 | typename ArrayType<T>::Allocator::Ptr m_array; |
[email protected] | 4f2ea59 | 2014-03-05 18:27:22 | [diff] [blame] | 74 | size_t m_count; |
75 | size_t m_capacity; | ||||
76 | }; | ||||
77 | |||||
78 | } // namespace WTF | ||||
79 | |||||
80 | using WTF::TerminatedArrayBuilder; | ||||
81 | |||||
82 | #endif // TerminatedArrayBuilder_h |