180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRefDict.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkString.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustruct SkRefDict::Impl {
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Impl*       fNext;
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkString    fName;
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkRefCnt*   fData;
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkRefDict::SkRefDict() : fImpl(NULL) {}
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkRefDict::~SkRefDict() {
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->removeAll();
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkRefCnt* SkRefDict::find(const char name[]) const {
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == name) {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL;
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Impl* rec = fImpl;
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    while (rec) {
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (rec->fName.equals(name)) {
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return rec->fData;
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec = rec->fNext;
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return NULL;
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkRefDict::set(const char name[], SkRefCnt* data) {
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == name) {
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return;
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Impl* rec = fImpl;
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Impl* prev = NULL;
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    while (rec) {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (rec->fName.equals(name)) {
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (data) {
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                // replace
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                data->ref();
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rec->fData->unref();
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rec->fData = data;
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            } else {
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                // remove
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                rec->fData->unref();
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                if (prev) {
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    prev->fNext = rec->fNext;
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                } else {
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                    fImpl = rec->fNext;
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                }
62e2022cc36e47b9f0d219eb5cd24be61772c28d3bDerek Sollenberger                delete rec;
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return;
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        prev = rec;
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec = rec->fNext;
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // if get here, name was not found, so add it
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    data->ref();
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    rec = new Impl;
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    rec->fName.set(name);
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    rec->fData = data;
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // prepend to the head of our list
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    rec->fNext = fImpl;
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fImpl = rec;
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkRefDict::removeAll() {
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    Impl* rec = fImpl;
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    while (rec) {
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        Impl* next = rec->fNext;
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec->fData->unref();
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        delete rec;
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec = next;
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fImpl = NULL;
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
90