Line data Source code
1 : /*
2 : * Copyright 2011 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 :
9 : #include "SkRefDict.h"
10 : #include "SkString.h"
11 :
12 0 : struct SkRefDict::Impl {
13 : Impl* fNext;
14 : SkString fName;
15 : SkRefCnt* fData;
16 : };
17 :
18 0 : SkRefDict::SkRefDict() : fImpl(nullptr) {}
19 :
20 0 : SkRefDict::~SkRefDict() {
21 0 : this->removeAll();
22 0 : }
23 :
24 0 : SkRefCnt* SkRefDict::find(const char name[]) const {
25 0 : if (nullptr == name) {
26 0 : return nullptr;
27 : }
28 :
29 0 : Impl* rec = fImpl;
30 0 : while (rec) {
31 0 : if (rec->fName.equals(name)) {
32 0 : return rec->fData;
33 : }
34 0 : rec = rec->fNext;
35 : }
36 0 : return nullptr;
37 : }
38 :
39 0 : void SkRefDict::set(const char name[], SkRefCnt* data) {
40 0 : if (nullptr == name) {
41 0 : return;
42 : }
43 :
44 0 : Impl* rec = fImpl;
45 0 : Impl* prev = nullptr;
46 0 : while (rec) {
47 0 : if (rec->fName.equals(name)) {
48 0 : if (data) {
49 : // replace
50 0 : data->ref();
51 0 : rec->fData->unref();
52 0 : rec->fData = data;
53 : } else {
54 : // remove
55 0 : rec->fData->unref();
56 0 : if (prev) {
57 0 : prev->fNext = rec->fNext;
58 : } else {
59 0 : fImpl = rec->fNext;
60 : }
61 0 : delete rec;
62 : }
63 0 : return;
64 : }
65 0 : prev = rec;
66 0 : rec = rec->fNext;
67 : }
68 :
69 : // if get here, name was not found, so add it
70 0 : data->ref();
71 0 : rec = new Impl;
72 0 : rec->fName.set(name);
73 0 : rec->fData = data;
74 : // prepend to the head of our list
75 0 : rec->fNext = fImpl;
76 0 : fImpl = rec;
77 : }
78 :
79 0 : void SkRefDict::removeAll() {
80 0 : Impl* rec = fImpl;
81 0 : while (rec) {
82 0 : Impl* next = rec->fNext;
83 0 : rec->fData->unref();
84 0 : delete rec;
85 0 : rec = next;
86 : }
87 0 : fImpl = nullptr;
88 0 : }
|