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
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/
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
15 * The Original Code is C++ hashtable templates.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
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.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsInterfaceHashtable_h__
39 #define nsInterfaceHashtable_h__
41 #include "nsBaseHashtable.h"
42 #include "nsHashKeys.h"
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
53 template<class KeyClass
,class Interface
>
54 class nsInterfaceHashtable
:
55 public nsBaseHashtable
< KeyClass
, nsCOMPtr
<Interface
> , Interface
* >
58 typedef typename
KeyClass::KeyType KeyType
;
59 typedef Interface
* UserDataType
;
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.
66 PRBool
Get(KeyType aKey
, UserDataType
* pData NS_OUTPARAM
) const;
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!
74 Interface
* GetWeak(KeyType aKey
, PRBool
* aFound
= nsnull
) const;
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
83 template<class KeyClass
,class Interface
>
84 class nsInterfaceHashtableMT
:
85 public nsBaseHashtableMT
< KeyClass
, nsCOMPtr
<Interface
> , Interface
* >
88 typedef typename
KeyClass::KeyType KeyType
;
89 typedef Interface
* UserDataType
;
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.
96 PRBool
Get(KeyType aKey
, UserDataType
* pData NS_OUTPARAM
) const;
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
105 // nsInterfaceHashtable definitions
108 template<class KeyClass
,class Interface
>
110 nsInterfaceHashtable
<KeyClass
,Interface
>::Get
111 (KeyType aKey
, UserDataType
* pInterface
) const
113 typename nsBaseHashtable
<KeyClass
, nsCOMPtr
<Interface
>, Interface
*>::EntryType
* ent
=
120 *pInterface
= ent
->mData
;
122 NS_IF_ADDREF(*pInterface
);
128 // if the key doesn't exist, set *pInterface to null
129 // so that it is a valid XPCOM getter
131 *pInterface
= nsnull
;
136 template<class KeyClass
,class Interface
>
138 nsInterfaceHashtable
<KeyClass
,Interface
>::GetWeak
139 (KeyType aKey
, PRBool
* aFound
) const
141 typename nsBaseHashtable
<KeyClass
, nsCOMPtr
<Interface
>, Interface
*>::EntryType
* ent
=
152 // Key does not exist, return nsnull and set aFound to PR_FALSE
159 // nsInterfaceHashtableMT definitions
162 template<class KeyClass
,class Interface
>
164 nsInterfaceHashtableMT
<KeyClass
,Interface
>::Get
165 (KeyType aKey
, UserDataType
* pInterface
) const
167 PR_Lock(this->mLock
);
169 typename nsBaseHashtableMT
<KeyClass
, nsCOMPtr
<Interface
>, Interface
*>::EntryType
* ent
=
176 *pInterface
= ent
->mData
;
178 NS_IF_ADDREF(*pInterface
);
181 PR_Unlock(this->mLock
);
186 // if the key doesn't exist, set *pInterface to null
187 // so that it is a valid XPCOM getter
189 *pInterface
= nsnull
;
191 PR_Unlock(this->mLock
);
196 #endif // nsInterfaceHashtable_h__