1 :
2 : /*
3 : * Copyright 2011 Google Inc.
4 : *
5 : * Use of this source code is governed by a BSD-style license that can be
6 : * found in the LICENSE file.
7 : */
8 :
9 :
10 : #include "SkRefDict.h"
11 : #include "SkString.h"
12 :
13 0 : struct SkRefDict::Impl {
14 : Impl* fNext;
15 : SkString fName;
16 : SkRefCnt* fData;
17 : };
18 :
19 0 : SkRefDict::SkRefDict() : fImpl(NULL) {}
20 :
21 0 : SkRefDict::~SkRefDict() {
22 0 : this->removeAll();
23 0 : }
24 :
25 0 : SkRefCnt* SkRefDict::find(const char name[]) const {
26 0 : if (NULL == name) {
27 0 : return NULL;
28 : }
29 :
30 0 : Impl* rec = fImpl;
31 0 : while (rec) {
32 0 : if (rec->fName.equals(name)) {
33 0 : return rec->fData;
34 : }
35 0 : rec = rec->fNext;
36 : }
37 0 : return NULL;
38 : }
39 :
40 0 : void SkRefDict::set(const char name[], SkRefCnt* data) {
41 0 : if (NULL == name) {
42 0 : return;
43 : }
44 :
45 0 : Impl* rec = fImpl;
46 0 : Impl* prev = NULL;
47 0 : while (rec) {
48 0 : if (rec->fName.equals(name)) {
49 0 : if (data) {
50 : // replace
51 0 : data->ref();
52 0 : rec->fData->unref();
53 0 : rec->fData = data;
54 : } else {
55 : // remove
56 0 : rec->fData->unref();
57 0 : if (prev) {
58 0 : prev->fNext = rec->fNext;
59 : } else {
60 0 : fImpl = rec->fNext;
61 : }
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 = NULL;
88 0 : }
89 :
|