| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef CC_QUADS_LIST_CONTAINER_H_ | |
| 6 #define CC_QUADS_LIST_CONTAINER_H_ | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 | |
| 11 namespace cc { | |
| 12 class SharedQuadState; | |
| 13 class DrawQuad; | |
| 14 | |
| 15 // This class is a container type that handles allocating contiguous memory for | |
| 16 // new elements and traversing through elements with either iterator or reverse | |
| 17 // iterator. Since this container hands out raw pointers of its elements, it is | |
| 18 // very important that this container never reallocate its memory so those raw | |
| 19 // pointer will continue to be valid. This class is used to contain | |
| 20 // SharedQuadState or DrawQuad. Since the size of each DrawQuad varies, to hold | |
| 21 // DrawQuads, the allocations size of each element in this class is | |
| 22 // LargestDrawQuadSize while BaseElementType is DrawQuad. | |
| 23 template <class BaseElementType> | |
| 24 class ListContainer { | |
| 25 public: | |
| 26 // BaseElementType is the type of raw pointers this class hands out; however, | |
| 27 // its derived classes might require different memory sizes. | |
| 28 // max_size_for_derived_class the largest memory size required for all the | |
| 29 // derived classes to use for allocation. | |
| 30 explicit ListContainer(size_t max_size_for_derived_class); | |
| 31 // This constructor omits input variable for max_size_for_derived_class. This | |
| 32 // is used when there is no derived classes from BaseElementType we need to | |
| 33 // worry about, and allocation size is just sizeof(BaseElementType). | |
| 34 ListContainer(); | |
| 35 // This constructor reserves the requested memory up front so only single | |
| 36 // allocation is needed. When num_of_elements_to_reserve_for is zero, use the | |
| 37 // default size. | |
| 38 ListContainer(size_t max_size_for_derived_class, | |
| 39 size_t num_of_elements_to_reserve_for); | |
| 40 | |
| 41 ~ListContainer(); | |
| 42 | |
| 43 // This class deals only with char* and void*. It does allocation and passing | |
| 44 // out raw pointers, as well as memory deallocation when being destroyed. | |
| 45 class ListContainerCharAllocator; | |
| 46 | |
| 47 // This class points to a certain position inside memory of | |
| 48 // ListContainerCharAllocator. It is a base class for ListContainer iterators. | |
| 49 struct PositionInListContainerCharAllocator { | |
| 50 ListContainerCharAllocator* ptr_to_container; | |
| 51 size_t vector_index; | |
| 52 char* item_iterator; | |
| 53 | |
| 54 PositionInListContainerCharAllocator( | |
| 55 const PositionInListContainerCharAllocator& other); | |
| 56 | |
| 57 PositionInListContainerCharAllocator(ListContainerCharAllocator* container, | |
| 58 size_t vector_ind, | |
| 59 char* item_iter); | |
| 60 | |
| 61 bool operator==(const PositionInListContainerCharAllocator& other) const; | |
| 62 bool operator!=(const PositionInListContainerCharAllocator& other) const; | |
| 63 | |
| 64 PositionInListContainerCharAllocator Increment(); | |
| 65 PositionInListContainerCharAllocator ReverseIncrement(); | |
| 66 }; | |
| 67 | |
| 68 // Iterator classes that can be used to access data. | |
| 69 ///////////////////////////////////////////////////////////////// | |
| 70 class Iterator : public PositionInListContainerCharAllocator { | |
| 71 // This class is only defined to forward iterate through | |
| 72 // ListContainerCharAllocator. | |
| 73 public: | |
| 74 Iterator(ListContainerCharAllocator* container, | |
| 75 size_t vector_ind, | |
| 76 char* item_iter, | |
| 77 size_t index); | |
| 78 ~Iterator(); | |
| 79 BaseElementType* operator->() const; | |
| 80 BaseElementType* operator*() const; | |
| 81 Iterator operator++(int unused_post_increment); | |
| 82 Iterator& operator++(); | |
| 83 | |
| 84 size_t index() const; | |
| 85 | |
| 86 private: | |
| 87 // This is used to track how many increment has happened since begin(). It | |
| 88 // is used to avoid double increment at places an index reference is | |
| 89 // needed. For iterator this means begin() corresponds to index 0 and end() | |
| 90 // corresponds to index |size|. | |
| 91 size_t index_; | |
| 92 }; | |
| 93 | |
| 94 class ConstIterator : public PositionInListContainerCharAllocator { | |
| 95 // This class is only defined to forward iterate through | |
| 96 // ListContainerCharAllocator. | |
| 97 public: | |
| 98 ConstIterator(ListContainerCharAllocator* container, | |
| 99 size_t vector_ind, | |
| 100 char* item_iter, | |
| 101 size_t index); | |
| 102 ConstIterator(const Iterator& other); // NOLINT | |
| 103 ~ConstIterator(); | |
| 104 const BaseElementType* operator->() const; | |
| 105 const BaseElementType* operator*() const; | |
| 106 ConstIterator operator++(int unused_post_increment); | |
| 107 ConstIterator& operator++(); | |
| 108 | |
| 109 size_t index() const; | |
| 110 | |
| 111 private: | |
| 112 // This is used to track how many increment has happened since begin(). It | |
| 113 // is used to avoid double increment at places an index reference is | |
| 114 // needed. For iterator this means begin() corresponds to index 0 and end() | |
| 115 // corresponds to index |size|. | |
| 116 size_t index_; | |
| 117 }; | |
| 118 | |
| 119 class ReverseIterator : public PositionInListContainerCharAllocator { | |
| 120 // This class is only defined to reverse iterate through | |
| 121 // ListContainerCharAllocator. | |
| 122 public: | |
| 123 ReverseIterator(ListContainerCharAllocator* container, | |
| 124 size_t vector_ind, | |
| 125 char* item_iter, | |
| 126 size_t index); | |
| 127 ~ReverseIterator(); | |
| 128 BaseElementType* operator->() const; | |
| 129 BaseElementType* operator*() const; | |
| 130 ReverseIterator operator++(int unused_post_increment); | |
| 131 ReverseIterator& operator++(); | |
| 132 | |
| 133 size_t index() const; | |
| 134 | |
| 135 private: | |
| 136 // This is used to track how many increment has happened since rbegin(). It | |
| 137 // is used to avoid double increment at places an index reference is | |
| 138 // needed. For reverse iterator this means rbegin() corresponds to index 0 | |
| 139 // and rend() corresponds to index |size|. | |
| 140 size_t index_; | |
| 141 }; | |
| 142 | |
| 143 class ConstReverseIterator : public PositionInListContainerCharAllocator { | |
| 144 // This class is only defined to reverse iterate through | |
| 145 // ListContainerCharAllocator. | |
| 146 public: | |
| 147 ConstReverseIterator(ListContainerCharAllocator* container, | |
| 148 size_t vector_ind, | |
| 149 char* item_iter, | |
| 150 size_t index); | |
| 151 ConstReverseIterator(const ReverseIterator& other); // NOLINT | |
| 152 ~ConstReverseIterator(); | |
| 153 const BaseElementType* operator->() const; | |
| 154 const BaseElementType* operator*() const; | |
| 155 ConstReverseIterator operator++(int unused_post_increment); | |
| 156 ConstReverseIterator& operator++(); | |
| 157 | |
| 158 size_t index() const; | |
| 159 | |
| 160 private: | |
| 161 // This is used to track how many increment has happened since rbegin(). It | |
| 162 // is used to avoid double increment at places an index reference is | |
| 163 // needed. For reverse iterator this means rbegin() corresponds to index 0 | |
| 164 // and rend() corresponds to index |size|. | |
| 165 size_t index_; | |
| 166 }; | |
| 167 | |
| 168 // When called, all raw pointers that have been handed out are no longer | |
| 169 // valid. Use with caution. | |
| 170 // This function does not deallocate memory. | |
| 171 void EraseAndInvalidateAllPointers(Iterator position); | |
| 172 | |
| 173 ConstReverseIterator crbegin() const; | |
| 174 ConstReverseIterator crend() const; | |
| 175 ConstReverseIterator rbegin() const; | |
| 176 ConstReverseIterator rend() const; | |
| 177 ReverseIterator rbegin(); | |
| 178 ReverseIterator rend(); | |
| 179 ConstIterator cbegin() const; | |
| 180 ConstIterator cend() const; | |
| 181 ConstIterator begin() const; | |
| 182 ConstIterator end() const; | |
| 183 Iterator begin(); | |
| 184 Iterator end(); | |
| 185 | |
| 186 // TODO(weiliangc): front(), back() and ElementAt() function should return | |
| 187 // reference, consistent with container-of-object. | |
| 188 BaseElementType* front(); | |
| 189 BaseElementType* back(); | |
| 190 const BaseElementType* front() const; | |
| 191 const BaseElementType* back() const; | |
| 192 | |
| 193 BaseElementType* ElementAt(size_t index); | |
| 194 const BaseElementType* ElementAt(size_t index) const; | |
| 195 | |
| 196 // Take in derived element type and construct it at location generated by | |
| 197 // Allocate(). | |
| 198 template <typename DerivedElementType> | |
| 199 DerivedElementType* AllocateAndConstruct() { | |
| 200 return new (Allocate(sizeof(DerivedElementType))) DerivedElementType; | |
| 201 } | |
| 202 // Take in derived element type and copy construct it at location generated by | |
| 203 // Allocate(). | |
| 204 template <typename DerivedElementType> | |
| 205 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) { | |
| 206 return new (Allocate(sizeof(DerivedElementType))) | |
| 207 DerivedElementType(*source); | |
| 208 } | |
| 209 | |
| 210 size_t size() const; | |
| 211 bool empty() const; | |
| 212 void clear(); | |
| 213 | |
| 214 size_t AvailableSizeWithoutAnotherAllocationForTesting() const; | |
| 215 | |
| 216 private: | |
| 217 // Hands out memory location for an element at the end of data structure. | |
| 218 BaseElementType* Allocate(size_t size_of_actual_element_in_bytes); | |
| 219 | |
| 220 scoped_ptr<ListContainerCharAllocator> data_; | |
| 221 | |
| 222 DISALLOW_COPY_AND_ASSIGN(ListContainer); | |
| 223 }; | |
| 224 | |
| 225 #if !defined(COMPILER_MSVC) | |
| 226 extern template class ListContainer<SharedQuadState>; | |
| 227 extern template class ListContainer<DrawQuad>; | |
| 228 #endif | |
| 229 } // namespace cc | |
| 230 | |
| 231 #endif // CC_QUADS_LIST_CONTAINER_H_ | |
| OLD | NEW |