gfx/skia/trunk/src/core/SkRefDict.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkRefDict.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#include "SkRefDict.h"
    1.14 +#include "SkString.h"
    1.15 +
    1.16 +struct SkRefDict::Impl {
    1.17 +    Impl*       fNext;
    1.18 +    SkString    fName;
    1.19 +    SkRefCnt*   fData;
    1.20 +};
    1.21 +
    1.22 +SkRefDict::SkRefDict() : fImpl(NULL) {}
    1.23 +
    1.24 +SkRefDict::~SkRefDict() {
    1.25 +    this->removeAll();
    1.26 +}
    1.27 +
    1.28 +SkRefCnt* SkRefDict::find(const char name[]) const {
    1.29 +    if (NULL == name) {
    1.30 +        return NULL;
    1.31 +    }
    1.32 +
    1.33 +    Impl* rec = fImpl;
    1.34 +    while (rec) {
    1.35 +        if (rec->fName.equals(name)) {
    1.36 +            return rec->fData;
    1.37 +        }
    1.38 +        rec = rec->fNext;
    1.39 +    }
    1.40 +    return NULL;
    1.41 +}
    1.42 +
    1.43 +void SkRefDict::set(const char name[], SkRefCnt* data) {
    1.44 +    if (NULL == name) {
    1.45 +        return;
    1.46 +    }
    1.47 +
    1.48 +    Impl* rec = fImpl;
    1.49 +    Impl* prev = NULL;
    1.50 +    while (rec) {
    1.51 +        if (rec->fName.equals(name)) {
    1.52 +            if (data) {
    1.53 +                // replace
    1.54 +                data->ref();
    1.55 +                rec->fData->unref();
    1.56 +                rec->fData = data;
    1.57 +            } else {
    1.58 +                // remove
    1.59 +                rec->fData->unref();
    1.60 +                if (prev) {
    1.61 +                    prev->fNext = rec->fNext;
    1.62 +                } else {
    1.63 +                    fImpl = rec->fNext;
    1.64 +                }
    1.65 +                delete rec;
    1.66 +            }
    1.67 +            return;
    1.68 +        }
    1.69 +        prev = rec;
    1.70 +        rec = rec->fNext;
    1.71 +    }
    1.72 +
    1.73 +    // if get here, name was not found, so add it
    1.74 +    data->ref();
    1.75 +    rec = new Impl;
    1.76 +    rec->fName.set(name);
    1.77 +    rec->fData = data;
    1.78 +    // prepend to the head of our list
    1.79 +    rec->fNext = fImpl;
    1.80 +    fImpl = rec;
    1.81 +}
    1.82 +
    1.83 +void SkRefDict::removeAll() {
    1.84 +    Impl* rec = fImpl;
    1.85 +    while (rec) {
    1.86 +        Impl* next = rec->fNext;
    1.87 +        rec->fData->unref();
    1.88 +        delete rec;
    1.89 +        rec = next;
    1.90 +    }
    1.91 +    fImpl = NULL;
    1.92 +}

mercurial