aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkTInternalSList.h
blob: 4695e5af292b612798943198d42e32b513a16e04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkTInternalSList_DEFINED
#define SkTInternalSList_DEFINED

#include "SkTInternalLList.h" // for SkPtrWrapper

/**
 * This macro creates the methods required by the SkTInternalSList class.
 * It should be instantiated in the private block of the class you want to put
 * into an SkTInternalSList.
 * For most use cases you should use SK_DECLARE_INTERNAL_SLIST_INTERFACE and not
 * this macro. If you care about the field name, or want to re-use an existing
 * field, then you can use this macro to declare the methods pointing to a
 * specific field.
 * Unlike SK_DECLARE_INTERNAL_SLIST_INTERFACE this does not declare the field
 * itself.
 * It also makes SkTInternalSList<ClassName> a friend to give it access to the
 * methods.
 */
#define SK_DECLARE_INTERNAL_SLIST_ADAPTER(ClassName, field)     \
    ClassName* getSListNext() {                                 \
        return this->field;                                     \
    }                                                           \
    void setSListNext(ClassName* next) {                        \
        this->field = next;                                     \
    }                                                           \
    friend class SkTInternalSList<ClassName>

/**
 * This macro declares an fSListNext that auto initializes to NULL and then
 * uses SK_DECLARE_INTERNAL_SLIST_ADAPTER to add the methods needed by
 * SkTInternalSList.
 * It should be instantiated in the private block of the class you want to put
 * into an SkTInternalSList.
 */
#define SK_DECLARE_INTERNAL_SLIST_INTERFACE(ClassName)          \
    SK_DECLARE_INTERNAL_SLIST_ADAPTER(ClassName, fSListNext);   \
    SkPtrWrapper<ClassName> fSListNext

/**
 * An implementation of an intrusive singly linked list.
 * The type T must have a methods getSListNext and setSListNext that are visible
 * to the list. The easiest way to do this is with
 * SK_DECLARE_INTERNAL_SLIST_INTERFACE.
 * The list does not maintain ownership of any of its elements, or ever delete
 * them.
 */
template<typename T> class SkTInternalSList {
public:
    SkTInternalSList() : fHead(NULL), fCount(0) {}

    /**
     * Push an item onto the head of the list.
     * This method is *not* thread safe.
     */
    void push(T* entry) {
        SkASSERT(entry->getSListNext() == NULL);
        entry->setSListNext(fHead);
        fHead = entry;
        ++fCount;
    }

    /**
     * Takes all the items from another list and pushes them into this list.
     * No ordering guarantees are made, the other list will be emptied.
     * This method is *not* thread safe.
     */
    void pushAll(SkTInternalSList<T>* other) {
        if (this->isEmpty()) {
            this->swap(other);
            return;
        }
        while (!other->isEmpty()) {
            this->push(other->pop());
        }
    }

    /**
     * Pop an item from the head of the list.
     * Returns NULL if the list is empty.
     * This method is *not* thread safe.
     */
    T* pop() {
        if (NULL == fHead) {
            return NULL;
        }
        T* result = fHead;
        fHead = result->getSListNext();
        result->setSListNext(NULL);
        --fCount;
        return result;
    }

    T* head() const {
        return fHead;
    }

    /**
     * Returns true if the list has no elements.
     */
    bool isEmpty() const {
        return NULL == fHead;
    }

    /**
     * Swaps the contents of this list with another one.
     * This method is *not* thread safe.
     */
    void swap(SkTInternalSList<T>* other) {
        SkTSwap(fHead, other->fHead);
        SkTSwap(fCount, other->fCount);
    }

    /**
     * Returns the count of elements in the list.
     */
    int getCount() const {
        return fCount;
    }
private:
    T* fHead;
    int fCount;
};


#endif