OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_BASE_LIST_CONTAINER_HELPER_H_ |
| 6 #define CC_BASE_LIST_CONTAINER_HELPER_H_ |
| 7 |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "cc/base/cc_export.h" |
| 10 |
| 11 namespace cc { |
| 12 |
| 13 // Helper class for ListContainer non-templated logic. All methods are private, |
| 14 // and only exposed to friend classes. |
| 15 // For usage, see comments in ListContainer (list_container.h). |
| 16 class CC_EXPORT ListContainerHelper final { |
| 17 private: |
| 18 template <typename T> |
| 19 friend class ListContainer; |
| 20 |
| 21 explicit ListContainerHelper(size_t max_size_for_derived_class); |
| 22 ListContainerHelper(size_t max_size_for_derived_class, |
| 23 size_t num_of_elements_to_reserve_for); |
| 24 ~ListContainerHelper(); |
| 25 |
| 26 // This class deals only with char* and void*. It does allocation and passing |
| 27 // out raw pointers, as well as memory deallocation when being destroyed. |
| 28 class CharAllocator; |
| 29 |
| 30 // This class points to a certain position inside memory of |
| 31 // CharAllocator. It is a base class for ListContainer iterators. |
| 32 struct CC_EXPORT PositionInCharAllocator { |
| 33 CharAllocator* ptr_to_container; |
| 34 size_t vector_index; |
| 35 char* item_iterator; |
| 36 |
| 37 PositionInCharAllocator(const PositionInCharAllocator& other); |
| 38 |
| 39 PositionInCharAllocator(CharAllocator* container, |
| 40 size_t vector_ind, |
| 41 char* item_iter); |
| 42 |
| 43 bool operator==(const PositionInCharAllocator& other) const; |
| 44 bool operator!=(const PositionInCharAllocator& other) const; |
| 45 |
| 46 PositionInCharAllocator Increment(); |
| 47 PositionInCharAllocator ReverseIncrement(); |
| 48 }; |
| 49 |
| 50 // Iterator classes that can be used to access data. |
| 51 ///////////////////////////////////////////////////////////////// |
| 52 class CC_EXPORT Iterator : public PositionInCharAllocator { |
| 53 // This class is only defined to forward iterate through |
| 54 // CharAllocator. |
| 55 public: |
| 56 Iterator(CharAllocator* container, |
| 57 size_t vector_ind, |
| 58 char* item_iter, |
| 59 size_t index); |
| 60 ~Iterator(); |
| 61 |
| 62 size_t index() const; |
| 63 |
| 64 protected: |
| 65 // This is used to track how many increment has happened since begin(). It |
| 66 // is used to avoid double increment at places an index reference is |
| 67 // needed. For iterator this means begin() corresponds to index 0 and end() |
| 68 // corresponds to index |size|. |
| 69 size_t index_; |
| 70 }; |
| 71 |
| 72 class CC_EXPORT ConstIterator : public PositionInCharAllocator { |
| 73 // This class is only defined to forward iterate through |
| 74 // CharAllocator. |
| 75 public: |
| 76 ConstIterator(CharAllocator* container, |
| 77 size_t vector_ind, |
| 78 char* item_iter, |
| 79 size_t index); |
| 80 ConstIterator(const Iterator& other); // NOLINT |
| 81 ~ConstIterator(); |
| 82 |
| 83 size_t index() const; |
| 84 |
| 85 protected: |
| 86 // This is used to track how many increment has happened since begin(). It |
| 87 // is used to avoid double increment at places an index reference is |
| 88 // needed. For iterator this means begin() corresponds to index 0 and end() |
| 89 // corresponds to index |size|. |
| 90 size_t index_; |
| 91 }; |
| 92 |
| 93 class CC_EXPORT ReverseIterator : public PositionInCharAllocator { |
| 94 // This class is only defined to reverse iterate through |
| 95 // CharAllocator. |
| 96 public: |
| 97 ReverseIterator(CharAllocator* container, |
| 98 size_t vector_ind, |
| 99 char* item_iter, |
| 100 size_t index); |
| 101 ~ReverseIterator(); |
| 102 |
| 103 size_t index() const; |
| 104 |
| 105 protected: |
| 106 // This is used to track how many increment has happened since rbegin(). It |
| 107 // is used to avoid double increment at places an index reference is |
| 108 // needed. For reverse iterator this means rbegin() corresponds to index 0 |
| 109 // and rend() corresponds to index |size|. |
| 110 size_t index_; |
| 111 }; |
| 112 |
| 113 class CC_EXPORT ConstReverseIterator : public PositionInCharAllocator { |
| 114 // This class is only defined to reverse iterate through |
| 115 // CharAllocator. |
| 116 public: |
| 117 ConstReverseIterator(CharAllocator* container, |
| 118 size_t vector_ind, |
| 119 char* item_iter, |
| 120 size_t index); |
| 121 ConstReverseIterator(const ReverseIterator& other); // NOLINT |
| 122 ~ConstReverseIterator(); |
| 123 |
| 124 size_t index() const; |
| 125 |
| 126 protected: |
| 127 // This is used to track how many increment has happened since rbegin(). It |
| 128 // is used to avoid double increment at places an index reference is |
| 129 // needed. For reverse iterator this means rbegin() corresponds to index 0 |
| 130 // and rend() corresponds to index |size|. |
| 131 size_t index_; |
| 132 }; |
| 133 |
| 134 // Unlike the ListContainer methods, these do not invoke element destructors. |
| 135 void RemoveLast(); |
| 136 void EraseAndInvalidateAllPointers(Iterator* position); |
| 137 void InsertBeforeAndInvalidateAllPointers(Iterator* position, |
| 138 size_t number_of_elements); |
| 139 |
| 140 ConstReverseIterator crbegin() const; |
| 141 ConstReverseIterator crend() const; |
| 142 ReverseIterator rbegin(); |
| 143 ReverseIterator rend(); |
| 144 ConstIterator cbegin() const; |
| 145 ConstIterator cend() const; |
| 146 Iterator begin(); |
| 147 Iterator end(); |
| 148 |
| 149 Iterator IteratorAt(size_t index); |
| 150 ConstIterator IteratorAt(size_t index) const; |
| 151 |
| 152 size_t size() const; |
| 153 bool empty() const; |
| 154 |
| 155 size_t MaxSizeForDerivedClass() const; |
| 156 |
| 157 size_t GetCapacityInBytes() const; |
| 158 |
| 159 // Unlike the ListContainer method, this one does not invoke element |
| 160 // destructors. |
| 161 void clear(); |
| 162 |
| 163 size_t AvailableSizeWithoutAnotherAllocationForTesting() const; |
| 164 |
| 165 // Hands out memory location for an element at the end of data structure. |
| 166 void* Allocate(size_t size_of_actual_element_in_bytes); |
| 167 |
| 168 scoped_ptr<CharAllocator> data_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(ListContainerHelper); |
| 171 }; |
| 172 |
| 173 } // namespace cc |
| 174 |
| 175 #endif // CC_BASE_LIST_CONTAINER_HELPER_H_ |
OLD | NEW |