/gecko_api/include/nsCategoryCache.h
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 37#ifndef nsCategoryCache_h_ 38#define nsCategoryCache_h_ 39 40#include "nsICategoryManager.h" 41#include "nsIObserver.h" 42#include "nsISimpleEnumerator.h" 43#include "nsISupportsPrimitives.h" 44 45#include "nsServiceManagerUtils.h" 46 47#include "nsAutoPtr.h" 48#include "nsCOMArray.h" 49#include "nsDataHashtable.h" 50 51#include "nsXPCOM.h" 52 53class NS_NO_VTABLE nsCategoryListener { 54 protected: 55 // no virtual destructor (people shouldn't delete through an 56 // nsCategoryListener pointer) 57 ~nsCategoryListener() {} 58 59 public: 60 virtual void EntryAdded(const nsCString& aValue) = 0; 61 virtual void EntryRemoved(const nsCString& aValue) = 0; 62 virtual void CategoryCleared() = 0; 63}; 64 65class NS_COM_GLUE nsCategoryObserver : public nsIObserver { 66 public: 67 nsCategoryObserver(const char* aCategory, 68 nsCategoryListener* aCategoryListener); 69 ~nsCategoryObserver(); 70 71 void ListenerDied(); 72 73 NS_DECL_ISUPPORTS 74 NS_DECL_NSIOBSERVER 75 private: 76 nsDataHashtable<nsCStringHashKey, nsCString> mHash; 77 nsCategoryListener* mListener; 78 nsCString mCategory; 79}; 80 81/** 82 * This is a helper class that caches services that are registered in a certain 83 * category. The intended usage is that a service stores a variable of type 84 * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface 85 * that these services should implement. The constructor of this class should 86 * then get the name of the category. 87 */ 88template<class T> 89class nsCategoryCache : protected nsCategoryListener { 90 public: 91 explicit nsCategoryCache(const char* aCategory); 92 ~nsCategoryCache() { if (mObserver) mObserver->ListenerDied(); } 93 94 const nsCOMArray<T>& GetEntries() { 95 // Lazy initialization, so that services in this category can't 96 // cause reentrant getService (bug 386376) 97 if (!mObserver) 98 mObserver = new nsCategoryObserver(mCategoryName.get(), this); 99 return mEntries; 100 } 101 protected: 102 virtual void EntryAdded(const nsCString& aValue); 103 virtual void EntryRemoved(const nsCString& aValue); 104 virtual void CategoryCleared(); 105 private: 106 friend class CategoryObserver; 107 108 // Not to be implemented 109 nsCategoryCache(const nsCategoryCache<T>&); 110 111 nsCString mCategoryName; 112 nsCOMArray<T> mEntries; 113 nsRefPtr<nsCategoryObserver> mObserver; 114}; 115 116// ----------------------------------- 117// Implementation 118 119template<class T> 120nsCategoryCache<T>::nsCategoryCache(const char* aCategory) 121: mCategoryName(aCategory) 122{ 123} 124 125template<class T> 126void nsCategoryCache<T>::EntryAdded(const nsCString& aValue) { 127 nsCOMPtr<T> catEntry = do_GetService(aValue.get()); 128 if (catEntry) 129 mEntries.AppendObject(catEntry); 130} 131 132template<class T> 133void nsCategoryCache<T>::EntryRemoved(const nsCString& aValue) { 134 nsCOMPtr<T> catEntry = do_GetService(aValue.get()); 135 if (catEntry) 136 mEntries.RemoveObject(catEntry); 137} 138 139template<class T> 140void nsCategoryCache<T>::CategoryCleared() { 141 mEntries.Clear(); 142} 143 144#endif