blob: acfbf2f3cd51fb2333e904e37a9a43957295071c [file] [log] [blame]
jat@google.com134be542009-08-03 15:30:11 +00001#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.comfa9d4d22009-11-10 00:00:39 +000019#include <map>
jat@google.com134be542009-08-03 15:30:11 +000020
21#include "Debug.h"
22
23#include "mozincludes.h"
24
25class LocalObjectTable {
26private:
jat@google.comfa9d4d22009-11-10 00:00:39 +000027 typedef std::map<int, NPObject*> ObjectMap;
jat@google.com134be542009-08-03 15:30:11 +000028
jat@google.comfa9d4d22009-11-10 00:00:39 +000029 int nextId;
30 ObjectMap objects;
jat@google.com134be542009-08-03 15:30:11 +000031 bool dontFree;
32
jat@google.com134be542009-08-03 15:30:11 +000033 void setFree(int id) {
codefu@google.com1e48c9b2011-07-07 17:09:23 +000034 if (objects.erase(id) != 1) {
jat@google.comfa9d4d22009-11-10 00:00:39 +000035 Debug::log(Debug::Error) << "setFree(id=" << id << "): object not in table"
36 << Debug::flush;
37 }
jat@google.com134be542009-08-03 15:30:11 +000038 }
jat@google.com97dcbe02009-09-08 23:09:51 +000039
jat@google.com134be542009-08-03 15:30:11 +000040public:
codefu@google.com1e48c9b2011-07-07 17:09:23 +000041 LocalObjectTable(): nextId(0), dontFree(false) {
jat@google.com134be542009-08-03 15:30:11 +000042 }
43
44 virtual ~LocalObjectTable();
45
jat@google.comf78175f2009-09-09 21:57:53 +000046 /**
47 * Add a new object, which must not be in the table, and return a new id for it.
48 */
jat@google.com134be542009-08-03 15:30:11 +000049 int add(NPObject* obj) {
jat@google.comfa9d4d22009-11-10 00:00:39 +000050 int id = nextId++;
51 set(id, obj);
jat@google.com134be542009-08-03 15:30:11 +000052 return id;
53 }
54
jat@google.comfa9d4d22009-11-10 00:00:39 +000055 void set(int id, NPObject* obj) {
codefu@google.com1e48c9b2011-07-07 17:09:23 +000056 Debug::log(Debug::Spam) << "LocalObjectTable::set(id=" << id << ",obj=" << (void*)obj
jat@google.comfa9d4d22009-11-10 00:00:39 +000057 << ")" << Debug::flush;
58 objects[id] = obj;
59 // keep track that we hold a reference in the table
codefu@google.com1e48c9b2011-07-07 17:09:23 +000060 NPN_RetainObject(obj);
jat@google.comfa9d4d22009-11-10 00:00:39 +000061 }
62
jat@google.com134be542009-08-03 15:30:11 +000063 void free(int id) {
codefu@google.com1e48c9b2011-07-07 17:09:23 +000064 Debug::log(Debug::Spam) << "LocalObjectTable::free(id=" << id << ")" << Debug::flush;
jat@google.comfa9d4d22009-11-10 00:00:39 +000065 ObjectMap::iterator it = objects.find(id);
66 if (it == objects.end()) {
jat@google.com134be542009-08-03 15:30:11 +000067 Debug::log(Debug::Error) << "Freeing freed object slot " << id << Debug::flush;
68 return;
69 }
jat@google.com134be542009-08-03 15:30:11 +000070 if (!dontFree) {
jat@google.comfa9d4d22009-11-10 00:00:39 +000071 NPObject* obj = it->second;
jat@google.com134be542009-08-03 15:30:11 +000072 NPN_ReleaseObject(obj);
73 }
codefu@google.com1e48c9b2011-07-07 17:09:23 +000074 setFree(id);
jat@google.com134be542009-08-03 15:30:11 +000075 }
76
77 void freeAll() {
jat@google.comfa9d4d22009-11-10 00:00:39 +000078 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.com134be542009-08-03 15:30:11 +000083 }
84 }
jat@google.comfa9d4d22009-11-10 00:00:39 +000085 objects.clear();
jat@google.com134be542009-08-03 15:30:11 +000086 }
87
codefu@google.com1e48c9b2011-07-07 17:09:23 +000088 NPObject* get(int id) {
jat@google.comfa9d4d22009-11-10 00:00:39 +000089 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.com134be542009-08-03 15:30:11 +000093 }
jat@google.comfa9d4d22009-11-10 00:00:39 +000094 return it->second;
jat@google.com134be542009-08-03 15:30:11 +000095 }
96
jat@google.com134be542009-08-03 15:30:11 +000097 void setDontFree(bool dontFree) {
98 this->dontFree = dontFree;
99 }
100};
101
102#endif