PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/nsInterfaceHashtable.h

http://firefox-mac-pdf.googlecode.com/
C Header | 196 lines | 85 code | 31 blank | 80 comment | 9 complexity | 948e9fccdd428ec052d4042a31a83e8c MD5 | raw file
  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. #ifndef nsInterfaceHashtable_h__
  38. #define nsInterfaceHashtable_h__
  39. #include "nsBaseHashtable.h"
  40. #include "nsHashKeys.h"
  41. #include "nsCOMPtr.h"
  42. /**
  43. * templated hashtable class maps keys to interface pointers.
  44. * See nsBaseHashtable for complete declaration.
  45. * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
  46. * for a complete specification.
  47. * @param Interface the interface-type being wrapped
  48. * @see nsDataHashtable, nsClassHashtable
  49. */
  50. template<class KeyClass,class Interface>
  51. class nsInterfaceHashtable :
  52. public nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* >
  53. {
  54. public:
  55. typedef typename KeyClass::KeyType KeyType;
  56. typedef Interface* UserDataType;
  57. /**
  58. * @copydoc nsBaseHashtable::Get
  59. * @param pData This is an XPCOM getter, so pData is already_addrefed.
  60. * If the key doesn't exist, pData will be set to nsnull.
  61. */
  62. PRBool Get(KeyType aKey, UserDataType* pData) const;
  63. /**
  64. * Gets a weak reference to the hashtable entry.
  65. * @param aFound If not nsnull, will be set to PR_TRUE if the entry is found,
  66. * to PR_FALSE otherwise.
  67. * @return The entry, or nsnull if not found. Do not release this pointer!
  68. */
  69. Interface* GetWeak(KeyType aKey, PRBool* aFound = nsnull) const;
  70. };
  71. /**
  72. * Thread-safe version of nsInterfaceHashtable
  73. * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
  74. * for a complete specification.
  75. * @param Interface the interface-type being wrapped
  76. */
  77. template<class KeyClass,class Interface>
  78. class nsInterfaceHashtableMT :
  79. public nsBaseHashtableMT< KeyClass, nsCOMPtr<Interface> , Interface* >
  80. {
  81. public:
  82. typedef typename KeyClass::KeyType KeyType;
  83. typedef Interface* UserDataType;
  84. /**
  85. * @copydoc nsBaseHashtable::Get
  86. * @param pData This is an XPCOM getter, so pData is already_addrefed.
  87. * If the key doesn't exist, pData will be set to nsnull.
  88. */
  89. PRBool Get(KeyType aKey, UserDataType* pData) const;
  90. // GetWeak does not make sense on a multi-threaded hashtable, where another
  91. // thread may remove the entry (and hence release it) as soon as GetWeak
  92. // returns
  93. };
  94. //
  95. // nsInterfaceHashtable definitions
  96. //
  97. template<class KeyClass,class Interface>
  98. PRBool
  99. nsInterfaceHashtable<KeyClass,Interface>::Get
  100. (KeyType aKey, UserDataType* pInterface) const
  101. {
  102. typename nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
  103. GetEntry(aKey);
  104. if (ent)
  105. {
  106. if (pInterface)
  107. {
  108. *pInterface = ent->mData;
  109. NS_IF_ADDREF(*pInterface);
  110. }
  111. return PR_TRUE;
  112. }
  113. // if the key doesn't exist, set *pInterface to null
  114. // so that it is a valid XPCOM getter
  115. if (pInterface)
  116. *pInterface = nsnull;
  117. return PR_FALSE;
  118. }
  119. template<class KeyClass,class Interface>
  120. Interface*
  121. nsInterfaceHashtable<KeyClass,Interface>::GetWeak
  122. (KeyType aKey, PRBool* aFound) const
  123. {
  124. typename nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
  125. GetEntry(aKey);
  126. if (ent)
  127. {
  128. if (aFound)
  129. *aFound = PR_TRUE;
  130. return ent->mData;
  131. }
  132. // Key does not exist, return nsnull and set aFound to PR_FALSE
  133. if (aFound)
  134. *aFound = PR_FALSE;
  135. return nsnull;
  136. }
  137. //
  138. // nsInterfaceHashtableMT definitions
  139. //
  140. template<class KeyClass,class Interface>
  141. PRBool
  142. nsInterfaceHashtableMT<KeyClass,Interface>::Get
  143. (KeyType aKey, UserDataType* pInterface) const
  144. {
  145. PR_Lock(this->mLock);
  146. typename nsBaseHashtableMT<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
  147. GetEntry(aKey);
  148. if (ent)
  149. {
  150. if (pInterface)
  151. {
  152. *pInterface = ent->mData;
  153. NS_IF_ADDREF(*pInterface);
  154. }
  155. PR_Unlock(this->mLock);
  156. return PR_TRUE;
  157. }
  158. // if the key doesn't exist, set *pInterface to null
  159. // so that it is a valid XPCOM getter
  160. if (pInterface)
  161. *pInterface = nsnull;
  162. PR_Unlock(this->mLock);
  163. return PR_FALSE;
  164. }
  165. #endif // nsInterfaceHashtable_h__