| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef TerminatedArray_h | 4 #ifndef TerminatedArray_h |
| 5 #define TerminatedArray_h | 5 #define TerminatedArray_h |
| 6 | 6 |
| 7 #include "wtf/Allocator.h" | 7 #include "wtf/Allocator.h" |
| 8 #include "wtf/OwnPtr.h" | 8 #include "wtf/OwnPtr.h" |
| 9 #include "wtf/Partitions.h" | 9 #include "wtf/Partitions.h" |
| 10 | 10 |
| 11 namespace WTF { | 11 namespace WTF { |
| 12 | 12 |
| 13 // TerminatedArray<T> represents a sequence of elements of type T in which each | 13 // TerminatedArray<T> represents a sequence of elements of type T in which each |
| 14 // element knows whether it is the last element in the sequence or not. For this | 14 // element knows whether it is the last element in the sequence or not. For this |
| 15 // check type T must provide isLastInArray method. | 15 // check type T must provide isLastInArray method. |
| 16 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. | 16 // TerminatedArray<T> can only be constructed by TerminatedArrayBuilder<T>. |
| 17 template<typename T> | 17 template <typename T> |
| 18 class TerminatedArray { | 18 class TerminatedArray { |
| 19 WTF_MAKE_NONCOPYABLE(TerminatedArray); | 19 WTF_MAKE_NONCOPYABLE(TerminatedArray); |
| 20 public: | |
| 21 T& at(size_t index) { return reinterpret_cast<T*>(this)[index]; } | |
| 22 const T& at(size_t index) const { return reinterpret_cast<const T*>(this)[in
dex]; } | |
| 23 | 20 |
| 24 template<typename U> | 21 public: |
| 25 class iterator_base { | 22 T& at(size_t index) { return reinterpret_cast<T*>(this)[index]; } |
| 26 public: | 23 const T& at(size_t index) const { return reinterpret_cast<const T*>(this)[inde
x]; } |
| 27 iterator_base& operator++() | |
| 28 { | |
| 29 if (m_val->isLastInArray()) { | |
| 30 m_val = 0; | |
| 31 } else { | |
| 32 ++m_val; | |
| 33 } | |
| 34 return *this; | |
| 35 } | |
| 36 | 24 |
| 37 U& operator*() const { return *m_val; } | 25 template <typename U> |
| 38 | 26 class iterator_base { |
| 39 bool operator==(const iterator_base& other) const { return m_val == othe
r.m_val; } | 27 public: |
| 40 bool operator!=(const iterator_base& other) const { return !(*this == ot
her); } | 28 iterator_base& operator++() { |
| 41 | 29 if (m_val->isLastInArray()) { |
| 42 private: | 30 m_val = 0; |
| 43 iterator_base(U* val) : m_val(val) { } | 31 } else { |
| 44 | 32 ++m_val; |
| 45 U* m_val; | 33 } |
| 46 | 34 return *this; |
| 47 friend class TerminatedArray; | |
| 48 }; | |
| 49 | |
| 50 typedef iterator_base<T> iterator; | |
| 51 typedef iterator_base<const T> const_iterator; | |
| 52 | |
| 53 iterator begin() { return iterator(reinterpret_cast<T*>(this)); } | |
| 54 const_iterator begin() const { return const_iterator(reinterpret_cast<const
T*>(this)); } | |
| 55 | |
| 56 iterator end() { return iterator(0); } | |
| 57 const_iterator end() const { return const_iterator(0); } | |
| 58 | |
| 59 size_t size() const | |
| 60 { | |
| 61 size_t count = 0; | |
| 62 for (const_iterator it = begin(); it != end(); ++it) | |
| 63 count++; | |
| 64 return count; | |
| 65 } | 35 } |
| 66 | 36 |
| 67 // Match Allocator semantics to be able to use OwnPtr<TerminatedArray>. | 37 U& operator*() const { return *m_val; } |
| 68 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } | |
| 69 | 38 |
| 70 private: | 39 bool operator==(const iterator_base& other) const { return m_val == other.m_
val; } |
| 71 // Allocator describes how TerminatedArrayBuilder should create new instance
s | 40 bool operator!=(const iterator_base& other) const { return !(*this == other)
; } |
| 72 // of TerminateArray and manage their lifetimes. | |
| 73 struct Allocator { | |
| 74 typedef PassOwnPtr<TerminatedArray> PassPtr; | |
| 75 typedef OwnPtr<TerminatedArray> Ptr; | |
| 76 | 41 |
| 77 static PassPtr create(size_t capacity) | 42 private: |
| 78 { | 43 iterator_base(U* val) |
| 79 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastM
alloc(capacity * sizeof(T)))); | 44 : m_val(val) {} |
| 80 } | |
| 81 | 45 |
| 82 static PassPtr resize(PassPtr ptr, size_t capacity) | 46 U* m_val; |
| 83 { | |
| 84 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastR
ealloc(ptr.leakPtr(), capacity * sizeof(T)))); | |
| 85 } | |
| 86 }; | |
| 87 | 47 |
| 88 // Prohibit construction. Allocator makes TerminatedArray instances for | 48 friend class TerminatedArray; |
| 89 // TerminatedArrayBuilder by pointer casting. | 49 }; |
| 90 TerminatedArray(); | |
| 91 | 50 |
| 92 template<typename, template <typename> class> friend class TerminatedArrayBu
ilder; | 51 typedef iterator_base<T> iterator; |
| 52 typedef iterator_base<const T> const_iterator; |
| 53 |
| 54 iterator begin() { return iterator(reinterpret_cast<T*>(this)); } |
| 55 const_iterator begin() const { return const_iterator(reinterpret_cast<const T*
>(this)); } |
| 56 |
| 57 iterator end() { return iterator(0); } |
| 58 const_iterator end() const { return const_iterator(0); } |
| 59 |
| 60 size_t size() const { |
| 61 size_t count = 0; |
| 62 for (const_iterator it = begin(); it != end(); ++it) |
| 63 count++; |
| 64 return count; |
| 65 } |
| 66 |
| 67 // Match Allocator semantics to be able to use OwnPtr<TerminatedArray>. |
| 68 void operator delete(void* p) { ::WTF::Partitions::fastFree(p); } |
| 69 |
| 70 private: |
| 71 // Allocator describes how TerminatedArrayBuilder should create new instances |
| 72 // of TerminateArray and manage their lifetimes. |
| 73 struct Allocator { |
| 74 typedef PassOwnPtr<TerminatedArray> PassPtr; |
| 75 typedef OwnPtr<TerminatedArray> Ptr; |
| 76 |
| 77 static PassPtr create(size_t capacity) { |
| 78 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastMalloc(
capacity * sizeof(T)))); |
| 79 } |
| 80 |
| 81 static PassPtr resize(PassPtr ptr, size_t capacity) { |
| 82 return adoptPtr(static_cast<TerminatedArray*>(WTF::Partitions::fastRealloc
(ptr.leakPtr(), capacity * sizeof(T)))); |
| 83 } |
| 84 }; |
| 85 |
| 86 // Prohibit construction. Allocator makes TerminatedArray instances for |
| 87 // TerminatedArrayBuilder by pointer casting. |
| 88 TerminatedArray(); |
| 89 |
| 90 template <typename, template <typename> class> |
| 91 friend class TerminatedArrayBuilder; |
| 93 }; | 92 }; |
| 94 | 93 |
| 95 } // namespace WTF | 94 } // namespace WTF |
| 96 | 95 |
| 97 using WTF::TerminatedArray; | 96 using WTF::TerminatedArray; |
| 98 | 97 |
| 99 #endif // TerminatedArray_h | 98 #endif // TerminatedArray_h |
| OLD | NEW |