jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 1 | #ifndef H_LocalObjectTable |
| 2 | #define H_LocalObjectTable |
| 3 | /* |
| 4 | * Copyright 2008 Google Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 7 | * use this file except in compliance with the License. You may obtain a copy of |
| 8 | * the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | * License for the specific language governing permissions and limitations under |
| 16 | * the License. |
| 17 | */ |
| 18 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 19 | #include <map> |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 20 | |
| 21 | #include "Debug.h" |
| 22 | |
| 23 | #include "mozincludes.h" |
| 24 | |
| 25 | class LocalObjectTable { |
| 26 | private: |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 27 | typedef std::map<int, NPObject*> ObjectMap; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 28 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 29 | int nextId; |
| 30 | ObjectMap objects; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 31 | bool dontFree; |
| 32 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 33 | void setFree(int id) { |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 34 | if (objects.erase(id) != 1) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 35 | Debug::log(Debug::Error) << "setFree(id=" << id << "): object not in table" |
| 36 | << Debug::flush; |
| 37 | } |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 38 | } |
jat@google.com | 97dcbe0 | 2009-09-08 23:09:51 +0000 | [diff] [blame] | 39 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 40 | public: |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 41 | LocalObjectTable(): nextId(0), dontFree(false) { |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | virtual ~LocalObjectTable(); |
| 45 | |
jat@google.com | f78175f | 2009-09-09 21:57:53 +0000 | [diff] [blame] | 46 | /** |
| 47 | * Add a new object, which must not be in the table, and return a new id for it. |
| 48 | */ |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 49 | int add(NPObject* obj) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 50 | int id = nextId++; |
| 51 | set(id, obj); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 52 | return id; |
| 53 | } |
| 54 | |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 55 | void set(int id, NPObject* obj) { |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 56 | Debug::log(Debug::Spam) << "LocalObjectTable::set(id=" << id << ",obj=" << (void*)obj |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 57 | << ")" << Debug::flush; |
| 58 | objects[id] = obj; |
| 59 | // keep track that we hold a reference in the table |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 60 | NPN_RetainObject(obj); |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 61 | } |
| 62 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 63 | void free(int id) { |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 64 | Debug::log(Debug::Spam) << "LocalObjectTable::free(id=" << id << ")" << Debug::flush; |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 65 | ObjectMap::iterator it = objects.find(id); |
| 66 | if (it == objects.end()) { |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 67 | Debug::log(Debug::Error) << "Freeing freed object slot " << id << Debug::flush; |
| 68 | return; |
| 69 | } |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 70 | if (!dontFree) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 71 | NPObject* obj = it->second; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 72 | NPN_ReleaseObject(obj); |
| 73 | } |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 74 | setFree(id); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void freeAll() { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 78 | Debug::log(Debug::Info) << "LocalObjectTable::freeAll()" << Debug::flush; |
| 79 | for (ObjectMap::const_iterator it = objects.begin(); it != objects.end(); ++it) { |
| 80 | NPObject* obj = it->second; |
| 81 | if (!dontFree) { |
| 82 | NPN_ReleaseObject(obj); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 85 | objects.clear(); |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 86 | } |
| 87 | |
codefu@google.com | 1e48c9b | 2011-07-07 17:09:23 +0000 | [diff] [blame^] | 88 | NPObject* get(int id) { |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 89 | ObjectMap::iterator it = objects.find(id); |
| 90 | if (it == objects.end()) { |
| 91 | Debug::log(Debug::Error) << "LocalObjectTable::get(id=" << id |
| 92 | << "): no object found" << Debug::flush; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 93 | } |
jat@google.com | fa9d4d2 | 2009-11-10 00:00:39 +0000 | [diff] [blame] | 94 | return it->second; |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 95 | } |
| 96 | |
jat@google.com | 134be54 | 2009-08-03 15:30:11 +0000 | [diff] [blame] | 97 | void setDontFree(bool dontFree) { |
| 98 | this->dontFree = dontFree; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | #endif |