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.
5 #ifndef CC_BASE_LIST_CONTAINER_HELPER_H_
6 #define CC_BASE_LIST_CONTAINER_HELPER_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "cc/base/cc_export.h"
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
{
19 friend class ListContainer
;
22 friend class RandomAccessListContainer
;
24 explicit ListContainerHelper(size_t max_size_for_derived_class
);
25 ListContainerHelper(size_t max_size_for_derived_class
,
26 size_t num_of_elements_to_reserve_for
);
27 ~ListContainerHelper();
29 // This class deals only with char* and void*. It does allocation and passing
30 // out raw pointers, as well as memory deallocation when being destroyed.
33 // This class points to a certain position inside memory of
34 // CharAllocator. It is a base class for ListContainer iterators.
35 struct CC_EXPORT PositionInCharAllocator
{
36 CharAllocator
* ptr_to_container
;
40 PositionInCharAllocator(const PositionInCharAllocator
& other
);
42 PositionInCharAllocator(CharAllocator
* container
,
46 bool operator==(const PositionInCharAllocator
& other
) const;
47 bool operator!=(const PositionInCharAllocator
& other
) const;
49 PositionInCharAllocator
Increment();
50 PositionInCharAllocator
ReverseIncrement();
53 // Iterator classes that can be used to access data.
54 /////////////////////////////////////////////////////////////////
55 class CC_EXPORT Iterator
: public PositionInCharAllocator
{
56 // This class is only defined to forward iterate through
59 Iterator(CharAllocator
* container
,
68 // This is used to track how many increment has happened since begin(). It
69 // is used to avoid double increment at places an index reference is
70 // needed. For iterator this means begin() corresponds to index 0 and end()
71 // corresponds to index |size|.
75 class CC_EXPORT ConstIterator
: public PositionInCharAllocator
{
76 // This class is only defined to forward iterate through
79 ConstIterator(CharAllocator
* container
,
83 ConstIterator(const Iterator
& other
); // NOLINT
89 // This is used to track how many increment has happened since begin(). It
90 // is used to avoid double increment at places an index reference is
91 // needed. For iterator this means begin() corresponds to index 0 and end()
92 // corresponds to index |size|.
96 class CC_EXPORT ReverseIterator
: public PositionInCharAllocator
{
97 // This class is only defined to reverse iterate through
100 ReverseIterator(CharAllocator
* container
,
106 size_t index() const;
109 // This is used to track how many increment has happened since rbegin(). It
110 // is used to avoid double increment at places an index reference is
111 // needed. For reverse iterator this means rbegin() corresponds to index 0
112 // and rend() corresponds to index |size|.
116 class CC_EXPORT ConstReverseIterator
: public PositionInCharAllocator
{
117 // This class is only defined to reverse iterate through
120 ConstReverseIterator(CharAllocator
* container
,
124 ConstReverseIterator(const ReverseIterator
& other
); // NOLINT
125 ~ConstReverseIterator();
127 size_t index() const;
130 // This is used to track how many increment has happened since rbegin(). It
131 // is used to avoid double increment at places an index reference is
132 // needed. For reverse iterator this means rbegin() corresponds to index 0
133 // and rend() corresponds to index |size|.
137 // Unlike the ListContainer methods, these do not invoke element destructors.
139 void EraseAndInvalidateAllPointers(Iterator
* position
);
140 void InsertBeforeAndInvalidateAllPointers(Iterator
* position
,
141 size_t number_of_elements
);
143 ConstReverseIterator
crbegin() const;
144 ConstReverseIterator
crend() const;
145 ReverseIterator
rbegin();
146 ReverseIterator
rend();
147 ConstIterator
cbegin() const;
148 ConstIterator
cend() const;
152 Iterator
IteratorAt(size_t index
);
153 ConstIterator
IteratorAt(size_t index
) const;
158 size_t MaxSizeForDerivedClass() const;
160 size_t GetCapacityInBytes() const;
162 // Unlike the ListContainer method, this one does not invoke element
166 size_t AvailableSizeWithoutAnotherAllocationForTesting() const;
168 // Hands out memory location for an element at the end of data structure.
169 void* Allocate(size_t size_of_actual_element_in_bytes
);
171 scoped_ptr
<CharAllocator
> data_
;
173 DISALLOW_COPY_AND_ASSIGN(ListContainerHelper
);
178 #endif // CC_BASE_LIST_CONTAINER_HELPER_H_