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