1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is C++ hashtable templates.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Benjamin Smedberg.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2002
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #ifndef nsInterfaceHashtable_h__
|
---|
39 | #define nsInterfaceHashtable_h__
|
---|
40 |
|
---|
41 | #include "nsBaseHashtable.h"
|
---|
42 | #include "nsHashKeys.h"
|
---|
43 | #include "nsCOMPtr.h"
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * templated hashtable class maps keys to interface pointers.
|
---|
47 | * See nsBaseHashtable for complete declaration.
|
---|
48 | * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
---|
49 | * for a complete specification.
|
---|
50 | * @param Interface the interface-type being wrapped
|
---|
51 | * @see nsDataHashtable, nsClassHashtable
|
---|
52 | */
|
---|
53 | template<class KeyClass,class Interface>
|
---|
54 | class nsInterfaceHashtable :
|
---|
55 | public nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* >
|
---|
56 | {
|
---|
57 | public:
|
---|
58 | typedef typename KeyClass::KeyType KeyType;
|
---|
59 | typedef Interface* UserDataType;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * @copydoc nsBaseHashtable::Get
|
---|
63 | * @param pData This is an XPCOM getter, so pData is already_addrefed.
|
---|
64 | * If the key doesn't exist, pData will be set to nsnull.
|
---|
65 | */
|
---|
66 | PRBool Get(KeyType aKey, UserDataType* pData) const;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Gets a weak reference to the hashtable entry.
|
---|
70 | * @param aFound If not nsnull, will be set to PR_TRUE if the entry is found,
|
---|
71 | * to PR_FALSE otherwise.
|
---|
72 | * @return The entry, or nsnull if not found. Do not release this pointer!
|
---|
73 | */
|
---|
74 | Interface* GetWeak(KeyType aKey, PRBool* aFound = nsnull) const;
|
---|
75 | };
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Thread-safe version of nsInterfaceHashtable
|
---|
79 | * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
---|
80 | * for a complete specification.
|
---|
81 | * @param Interface the interface-type being wrapped
|
---|
82 | */
|
---|
83 | template<class KeyClass,class Interface>
|
---|
84 | class nsInterfaceHashtableMT :
|
---|
85 | public nsBaseHashtableMT< KeyClass, nsCOMPtr<Interface> , Interface* >
|
---|
86 | {
|
---|
87 | public:
|
---|
88 | typedef typename KeyClass::KeyType KeyType;
|
---|
89 | typedef Interface* UserDataType;
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * @copydoc nsBaseHashtable::Get
|
---|
93 | * @param pData This is an XPCOM getter, so pData is already_addrefed.
|
---|
94 | * If the key doesn't exist, pData will be set to nsnull.
|
---|
95 | */
|
---|
96 | PRBool Get(KeyType aKey, UserDataType* pData) const;
|
---|
97 |
|
---|
98 | // GetWeak does not make sense on a multi-threaded hashtable, where another
|
---|
99 | // thread may remove the entry (and hence release it) as soon as GetWeak
|
---|
100 | // returns
|
---|
101 | };
|
---|
102 |
|
---|
103 |
|
---|
104 | //
|
---|
105 | // nsInterfaceHashtable definitions
|
---|
106 | //
|
---|
107 |
|
---|
108 | template<class KeyClass,class Interface>
|
---|
109 | PRBool
|
---|
110 | nsInterfaceHashtable<KeyClass,Interface>::Get
|
---|
111 | (KeyType aKey, UserDataType* pInterface) const
|
---|
112 | {
|
---|
113 | typename nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
|
---|
114 | this->GetEntry(aKey);
|
---|
115 |
|
---|
116 | if (ent)
|
---|
117 | {
|
---|
118 | if (pInterface)
|
---|
119 | {
|
---|
120 | *pInterface = ent->mData;
|
---|
121 |
|
---|
122 | NS_IF_ADDREF(*pInterface);
|
---|
123 | }
|
---|
124 |
|
---|
125 | return PR_TRUE;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // if the key doesn't exist, set *pInterface to null
|
---|
129 | // so that it is a valid XPCOM getter
|
---|
130 | if (pInterface)
|
---|
131 | *pInterface = nsnull;
|
---|
132 |
|
---|
133 | return PR_FALSE;
|
---|
134 | }
|
---|
135 |
|
---|
136 | template<class KeyClass,class Interface>
|
---|
137 | Interface*
|
---|
138 | nsInterfaceHashtable<KeyClass,Interface>::GetWeak
|
---|
139 | (KeyType aKey, PRBool* aFound) const
|
---|
140 | {
|
---|
141 | typename nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
|
---|
142 | this->GetEntry(aKey);
|
---|
143 |
|
---|
144 | if (ent)
|
---|
145 | {
|
---|
146 | if (aFound)
|
---|
147 | *aFound = PR_TRUE;
|
---|
148 |
|
---|
149 | return ent->mData;
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Key does not exist, return nsnull and set aFound to PR_FALSE
|
---|
153 | if (aFound)
|
---|
154 | *aFound = PR_FALSE;
|
---|
155 | return nsnull;
|
---|
156 | }
|
---|
157 |
|
---|
158 | //
|
---|
159 | // nsInterfaceHashtableMT definitions
|
---|
160 | //
|
---|
161 |
|
---|
162 | template<class KeyClass,class Interface>
|
---|
163 | PRBool
|
---|
164 | nsInterfaceHashtableMT<KeyClass,Interface>::Get
|
---|
165 | (KeyType aKey, UserDataType* pInterface) const
|
---|
166 | {
|
---|
167 | PR_Lock(this->mLock);
|
---|
168 |
|
---|
169 | typename nsBaseHashtableMT<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
|
---|
170 | this->GetEntry(aKey);
|
---|
171 |
|
---|
172 | if (ent)
|
---|
173 | {
|
---|
174 | if (pInterface)
|
---|
175 | {
|
---|
176 | *pInterface = ent->mData;
|
---|
177 |
|
---|
178 | NS_IF_ADDREF(*pInterface);
|
---|
179 | }
|
---|
180 |
|
---|
181 | PR_Unlock(this->mLock);
|
---|
182 |
|
---|
183 | return PR_TRUE;
|
---|
184 | }
|
---|
185 |
|
---|
186 | // if the key doesn't exist, set *pInterface to null
|
---|
187 | // so that it is a valid XPCOM getter
|
---|
188 | if (pInterface)
|
---|
189 | *pInterface = nsnull;
|
---|
190 |
|
---|
191 | PR_Unlock(this->mLock);
|
---|
192 |
|
---|
193 | return PR_FALSE;
|
---|
194 | }
|
---|
195 |
|
---|
196 | #endif // nsInterfaceHashtable_h__
|
---|