1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "SkRefDict.h" 18 #include "SkString.h" 19 20 struct SkRefDict::Impl { 21 Impl* fNext; 22 SkString fName; 23 SkRefCnt* fData; 24 }; 25 SkRefDict()26SkRefDict::SkRefDict() : fImpl(NULL) {} 27 ~SkRefDict()28SkRefDict::~SkRefDict() { 29 this->removeAll(); 30 } 31 find(const char name[]) const32SkRefCnt* SkRefDict::find(const char name[]) const { 33 if (NULL == name) { 34 return NULL; 35 } 36 37 Impl* rec = fImpl; 38 while (rec) { 39 if (rec->fName.equals(name)) { 40 return rec->fData; 41 } 42 rec = rec->fNext; 43 } 44 return NULL; 45 } 46 set(const char name[],SkRefCnt * data)47void SkRefDict::set(const char name[], SkRefCnt* data) { 48 if (NULL == name) { 49 return; 50 } 51 52 Impl* rec = fImpl; 53 Impl* prev = NULL; 54 while (rec) { 55 if (rec->fName.equals(name)) { 56 if (data) { 57 // replace 58 data->ref(); 59 rec->fData->unref(); 60 rec->fData = data; 61 } else { 62 // remove 63 rec->fData->unref(); 64 if (prev) { 65 prev->fNext = rec->fNext; 66 } else { 67 fImpl = rec->fNext; 68 } 69 } 70 return; 71 } 72 prev = rec; 73 rec = rec->fNext; 74 } 75 76 // if get here, name was not found, so add it 77 data->ref(); 78 rec = new Impl; 79 rec->fName.set(name); 80 rec->fData = data; 81 // prepend to the head of our list 82 rec->fNext = fImpl; 83 fImpl = rec; 84 } 85 removeAll()86void SkRefDict::removeAll() { 87 Impl* rec = fImpl; 88 while (rec) { 89 Impl* next = rec->fNext; 90 rec->fData->unref(); 91 delete rec; 92 rec = next; 93 } 94 fImpl = NULL; 95 } 96 97