aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkTRefArray.h
blob: 8c2c7fdc463dc23763ab1ddd75a22626c1b2e660 (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
//
//  SkTRefArray.h
//  core
//
//  Created by Mike Reed on 7/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#ifndef SkTRefArray_DEFINED
#define SkTRefArray_DEFINED

#include "SkRefCnt.h"
#include <new>

/**
 *  Wrapper to manage thread-safe sharing of an array of T objects. The array
 *  cannot be grown or shrunk.
 */
template <typename T> class SkTRefArray : public SkRefCnt {
    /*
     *  Shared factory to allocate the space needed for our instance plus N
     *  T entries at the end. We call our constructor, but not the constructors
     *  for the elements. Those are called by the proper Create method.
     */
    static SkTRefArray<T>* Alloc(int count) {
        // space for us, and our [count] elements
        size_t size = sizeof(SkTRefArray<T>) + count * sizeof(T);
        SkTRefArray<T>* obj = (SkTRefArray<T>*)sk_malloc_throw(size);
        
        SkNEW_PLACEMENT(obj, SkTRefArray<T>);
        obj->fCount = count;
        return obj;
    }

public:
    /**
     *  Return a new array with 'count' elements, initialized to their default
     *  value. To change them to some other value, use writableBegin/End or
     *  writableAt(), but do that before this array is given to another thread.
     */
    static SkTRefArray<T>* Create(int count) {
        SkTRefArray<T>* obj = Alloc(count);
        T* array = const_cast<T*>(obj->begin());
        for (int i = 0; i < count; ++i) {
            SkNEW_PLACEMENT(&array[i], T);
        }
        return obj;
    }
    
    /**
     *  Return a new array with 'count' elements, initialized from the provided
     *  src array. To change them to some other value, use writableBegin/End or
     *  writableAt(), but do that before this array is given to another thread.
     */
    static SkTRefArray<T>* Create(const T src[], int count) {
        SkTRefArray<T>* obj = Alloc(count);
        T* array = const_cast<T*>(obj->begin());
        for (int i = 0; i < count; ++i) {
            SkNEW_PLACEMENT_ARGS(&array[i], T, (src[i]));
        }
        return obj;
    }
    
    int count() const { return fCount; }
    const T* begin() const { return (const T*)(this + 1); }
    const T* end() const { return this->begin() + fCount; }
    const T& at(int index) const {
        SkASSERT((unsigned)index < (unsigned)fCount);
        return this->begin()[index];
    }
    const T& operator[](int index) const { return this->at(index); }

    // For the writable methods, we assert that we are the only owner if we
    // call these, since other owners are not informed if we change an element.

    T* writableBegin() {
        SkASSERT(1 == this->getRefCnt());
        return (T*)(this + 1);
    }
    T* writableEnd() {
        return this->writableBegin() + fCount;
    }
    T& writableAt(int index) {
        SkASSERT((unsigned)index < (unsigned)fCount);
        return this->writableBegin()[index];
    }

protected:
    virtual void internal_dispose() const SK_OVERRIDE {
        T* array = const_cast<T*>(this->begin());
        int n = fCount;
        
        for (int i = 0; i < n; ++i) {
            array->~T();
            array += 1;
        }
        
        this->internal_dispose_restore_refcnt_to_1();
        this->~SkTRefArray<T>();
        sk_free((void*)this);
    }

private:
    int fCount;

    // hide this
    virtual ~SkTRefArray() {}

    typedef SkRefCnt INHERITED;
};

#endif