PageRenderTime 39ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/nsClassHashtable.h

http://firefox-mac-pdf.googlecode.com/
C Header | 149 lines | 59 code | 25 blank | 65 comment | 6 complexity | 2b8b6ffefc72fd3cd6ec87b395838403 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 nsClassHashtable_h__
  38. #define nsClassHashtable_h__
  39. #include "nsBaseHashtable.h"
  40. #include "nsHashKeys.h"
  41. #include "nsAutoPtr.h"
  42. /**
  43. * templated hashtable class maps keys to C++ object 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 Class the class-type being wrapped
  48. * @see nsInterfaceHashtable, nsClassHashtable
  49. */
  50. template<class KeyClass,class T>
  51. class nsClassHashtable :
  52. public nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* >
  53. {
  54. public:
  55. typedef typename KeyClass::KeyType KeyType;
  56. typedef T* UserDataType;
  57. /**
  58. * @copydoc nsBaseHashtable::Get
  59. * @param pData if the key doesn't exist, pData will be set to nsnull.
  60. */
  61. PRBool Get(KeyType aKey, UserDataType* pData) const;
  62. };
  63. /**
  64. * Thread-safe version of nsClassHashtable
  65. * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
  66. * for a complete specification.
  67. * @param Class the class-type being wrapped
  68. * @see nsInterfaceHashtable, nsClassHashtable
  69. */
  70. template<class KeyClass,class T>
  71. class nsClassHashtableMT :
  72. public nsBaseHashtableMT< KeyClass, nsAutoPtr<T>, T* >
  73. {
  74. public:
  75. typedef typename KeyClass::KeyType KeyType;
  76. typedef T* UserDataType;
  77. /**
  78. * @copydoc nsBaseHashtable::Get
  79. * @param pData if the key doesn't exist, pData will be set to nsnull.
  80. */
  81. PRBool Get(KeyType aKey, UserDataType* pData) const;
  82. };
  83. //
  84. // nsClassHashtable definitions
  85. //
  86. template<class KeyClass,class T>
  87. PRBool
  88. nsClassHashtable<KeyClass,T>::Get(KeyType aKey, T** retVal) const
  89. {
  90. typename nsBaseHashtable<KeyClass,nsAutoPtr<T>,T*>::EntryType* ent =
  91. GetEntry(aKey);
  92. if (ent)
  93. {
  94. if (retVal)
  95. *retVal = ent->mData;
  96. return PR_TRUE;
  97. }
  98. if (retVal)
  99. *retVal = nsnull;
  100. return PR_FALSE;
  101. }
  102. //
  103. // nsClassHashtableMT definitions
  104. //
  105. template<class KeyClass,class T>
  106. PRBool
  107. nsClassHashtableMT<KeyClass,T>::Get(KeyType aKey, T** retVal) const
  108. {
  109. PR_Lock(this->mLock);
  110. typename nsBaseHashtableMT<KeyClass,nsAutoPtr<T>,T*>::EntryType* ent =
  111. GetEntry(aKey);
  112. if (ent)
  113. {
  114. if (retVal)
  115. *retVal = ent->mData;
  116. PR_Unlock(this->mLock);
  117. return PR_TRUE;
  118. }
  119. if (retVal)
  120. *retVal = nsnull;
  121. PR_Unlock(this->mLock);
  122. return PR_FALSE;
  123. }
  124. #endif // nsClassHashtable_h__