OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2014 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #ifndef SkTInternalSList_DEFINED | |
9 #define SkTInternalSList_DEFINED | |
10 | |
11 #include "SkTInternalLList.h" // for SkPtrWrapper | |
12 | |
13 /** | |
14 * This macro creates the methods required by the SkTInternalSList class. | |
15 * It should be instantiated in the private block of the class you want to put | |
16 * into an SkTInternalSList. | |
17 * For most use cases you should use SK_DECLARE_INTERNAL_SLIST_INTERFACE and not | |
18 * this macro. If you care about the field name, or want to re-use an existing | |
19 * field, then you can use this macro to declare the methods pointing to a | |
20 * specific field. | |
21 * Unlike SK_DECLARE_INTERNAL_SLIST_INTERFACE this does not declare the field | |
22 * itself. | |
23 * It also makes SkTInternalSList<ClassName> a friend to give it access to the | |
24 * methods. | |
25 */ | |
26 #define SK_DECLARE_INTERNAL_SLIST_ADAPTER(ClassName, field) \ | |
27 ClassName* getSListNext() { \ | |
robertphillips
2014/03/07 16:01:35
don't need this-> here
iancottrell
2014/03/07 16:11:14
There are cases with template expansion that will
| |
28 return this->field; \ | |
29 } \ | |
30 void setSListNext(ClassName* next) { \ | |
robertphillips
2014/03/07 16:01:35
same here
iancottrell
2014/03/07 16:11:14
as above
| |
31 this->field = next; \ | |
32 } \ | |
33 friend struct SkTInternalSList<ClassName> | |
34 | |
35 /** | |
36 * This macro declares an fSListNext that auto initializes to NULL and then | |
37 * uses SK_DECLARE_INTERNAL_SLIST_ADAPTER to add the methods needed by | |
38 * SkTInternalSList. | |
39 * It should be instantiated in the private block of the class you want to put | |
40 * into an SkTInternalSList. | |
41 */ | |
42 #define SK_DECLARE_INTERNAL_SLIST_INTERFACE(ClassName) \ | |
43 SK_DECLARE_INTERNAL_SLIST_ADAPTER(ClassName, fSListNext); \ | |
44 SkPtrWrapper<ClassName> fSListNext | |
45 | |
46 /** | |
47 * An implementation of an intrusive singly linked list. | |
48 * The type T must have a methods getSListNext and setSListNext that are visible | |
49 * to the list. The easiest way to do this is with | |
50 * SK_DECLARE_INTERNAL_SLIST_INTERFACE. | |
51 * The list does not maintain ownership of any of its elements, or ever delete | |
52 * them. | |
53 */ | |
54 template<typename T> class SkTInternalSList { | |
55 public: | |
56 SkTInternalSList() : fHead(NULL), fCount(0) {} | |
57 | |
58 /** | |
59 * Push an item onto the head of the list. | |
60 * This method is *not* thread safe. | |
61 */ | |
62 void push(T* entry) { | |
63 SkASSERT(entry->getSListNext() == NULL); | |
64 entry->setSListNext(fHead); | |
65 fHead = entry; | |
66 ++fCount; | |
67 } | |
68 | |
69 /** | |
70 * Takes all the items from another list. | |
71 * No ordering guarantees are made, and the other list will be emptied. | |
72 */ | |
73 void takeAll(SkTInternalSList<T>* other) { | |
robertphillips
2014/03/07 16:01:35
this->
iancottrell
2014/03/07 16:11:14
Done.
| |
74 if (isEmpty()) { | |
robertphillips
2014/03/07 16:01:35
this->
iancottrell
2014/03/07 16:11:14
Done.
| |
75 swap(other); | |
76 return; | |
77 } | |
78 while(!other->isEmpty()) { | |
robertphillips
2014/03/07 16:01:35
this->
iancottrell
2014/03/07 16:11:14
Done.
| |
79 push(other->pop()); | |
80 } | |
81 } | |
82 | |
83 /** | |
84 * Pop an item from the head of the list. | |
85 * Returns NULL if the list is empty. | |
86 * This method is *not* thread safe. | |
87 */ | |
88 T* pop() { | |
89 if (NULL == fHead) { | |
90 return NULL; | |
91 } | |
92 T* result = fHead; | |
93 fHead = result->getSListNext(); | |
94 result->setSListNext(NULL); | |
95 --fCount; | |
96 return result; | |
97 } | |
98 | |
99 T* head() const { | |
100 return fHead; | |
101 } | |
102 | |
103 /** | |
104 * Returns true if the list has no elements. | |
105 */ | |
106 bool isEmpty() const { | |
107 return NULL == fHead; | |
108 } | |
109 | |
110 /** | |
111 * Swaps the contents of this list with another one. | |
112 */ | |
113 void swap(SkTInternalSList<T>* other) { | |
114 SkTSwap(fHead, other->fHead); | |
115 SkTSwap(fCount, other->fCount); | |
116 } | |
117 | |
118 /** | |
119 * Returns the count of elements in the list. | |
120 */ | |
121 int getCount() const { | |
122 return fCount; | |
123 } | |
124 private: | |
125 T* fHead; | |
126 int fCount; | |
127 }; | |
128 | |
129 | |
130 #endif | |
OLD | NEW |