PageRenderTime 52ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/gecko_api/include/nsCategoryCache.h

http://firefox-mac-pdf.googlecode.com/
C Header | 144 lines | 75 code | 20 blank | 49 comment | 4 complexity | 817d9a66c61251150e30a74caa64e57f MD5 | raw file
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is a cache for services in a category.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Christian Biesinger <cbiesinger@web.de>.
  18. * Portions created by the Initial Developer are Copyright (C) 2005
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. #ifndef nsCategoryCache_h_
  37. #define nsCategoryCache_h_
  38. #include "nsICategoryManager.h"
  39. #include "nsIObserver.h"
  40. #include "nsISimpleEnumerator.h"
  41. #include "nsISupportsPrimitives.h"
  42. #include "nsServiceManagerUtils.h"
  43. #include "nsAutoPtr.h"
  44. #include "nsCOMArray.h"
  45. #include "nsDataHashtable.h"
  46. #include "nsXPCOM.h"
  47. class NS_NO_VTABLE nsCategoryListener {
  48. protected:
  49. // no virtual destructor (people shouldn't delete through an
  50. // nsCategoryListener pointer)
  51. ~nsCategoryListener() {}
  52. public:
  53. virtual void EntryAdded(const nsCString& aValue) = 0;
  54. virtual void EntryRemoved(const nsCString& aValue) = 0;
  55. virtual void CategoryCleared() = 0;
  56. };
  57. class NS_COM_GLUE nsCategoryObserver : public nsIObserver {
  58. public:
  59. nsCategoryObserver(const char* aCategory,
  60. nsCategoryListener* aCategoryListener);
  61. ~nsCategoryObserver();
  62. void ListenerDied();
  63. NS_DECL_ISUPPORTS
  64. NS_DECL_NSIOBSERVER
  65. private:
  66. nsDataHashtable<nsCStringHashKey, nsCString> mHash;
  67. nsCategoryListener* mListener;
  68. nsCString mCategory;
  69. };
  70. /**
  71. * This is a helper class that caches services that are registered in a certain
  72. * category. The intended usage is that a service stores a variable of type
  73. * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
  74. * that these services should implement. The constructor of this class should
  75. * then get the name of the category.
  76. */
  77. template<class T>
  78. class nsCategoryCache : protected nsCategoryListener {
  79. public:
  80. explicit nsCategoryCache(const char* aCategory);
  81. ~nsCategoryCache() { if (mObserver) mObserver->ListenerDied(); }
  82. const nsCOMArray<T>& GetEntries() {
  83. // Lazy initialization, so that services in this category can't
  84. // cause reentrant getService (bug 386376)
  85. if (!mObserver)
  86. mObserver = new nsCategoryObserver(mCategoryName.get(), this);
  87. return mEntries;
  88. }
  89. protected:
  90. virtual void EntryAdded(const nsCString& aValue);
  91. virtual void EntryRemoved(const nsCString& aValue);
  92. virtual void CategoryCleared();
  93. private:
  94. friend class CategoryObserver;
  95. // Not to be implemented
  96. nsCategoryCache(const nsCategoryCache<T>&);
  97. nsCString mCategoryName;
  98. nsCOMArray<T> mEntries;
  99. nsRefPtr<nsCategoryObserver> mObserver;
  100. };
  101. // -----------------------------------
  102. // Implementation
  103. template<class T>
  104. nsCategoryCache<T>::nsCategoryCache(const char* aCategory)
  105. : mCategoryName(aCategory)
  106. {
  107. }
  108. template<class T>
  109. void nsCategoryCache<T>::EntryAdded(const nsCString& aValue) {
  110. nsCOMPtr<T> catEntry = do_GetService(aValue.get());
  111. if (catEntry)
  112. mEntries.AppendObject(catEntry);
  113. }
  114. template<class T>
  115. void nsCategoryCache<T>::EntryRemoved(const nsCString& aValue) {
  116. nsCOMPtr<T> catEntry = do_GetService(aValue.get());
  117. if (catEntry)
  118. mEntries.RemoveObject(catEntry);
  119. }
  120. template<class T>
  121. void nsCategoryCache<T>::CategoryCleared() {
  122. mEntries.Clear();
  123. }
  124. #endif