/gecko_api/include/nsClassHashtable.h
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 38#ifndef nsClassHashtable_h__ 39#define nsClassHashtable_h__ 40 41#include "nsBaseHashtable.h" 42#include "nsHashKeys.h" 43#include "nsAutoPtr.h" 44 45/** 46 * templated hashtable class maps keys to C++ object 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 Class the class-type being wrapped 51 * @see nsInterfaceHashtable, nsClassHashtable 52 */ 53template<class KeyClass,class T> 54class nsClassHashtable : 55 public nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* > 56{ 57public: 58 typedef typename KeyClass::KeyType KeyType; 59 typedef T* UserDataType; 60 61 /** 62 * @copydoc nsBaseHashtable::Get 63 * @param pData if the key doesn't exist, pData will be set to nsnull. 64 */ 65 PRBool Get(KeyType aKey, UserDataType* pData) const; 66}; 67 68 69/** 70 * Thread-safe version of nsClassHashtable 71 * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h 72 * for a complete specification. 73 * @param Class the class-type being wrapped 74 * @see nsInterfaceHashtable, nsClassHashtable 75 */ 76template<class KeyClass,class T> 77class nsClassHashtableMT : 78 public nsBaseHashtableMT< KeyClass, nsAutoPtr<T>, T* > 79{ 80public: 81 typedef typename KeyClass::KeyType KeyType; 82 typedef T* UserDataType; 83 84 /** 85 * @copydoc nsBaseHashtable::Get 86 * @param pData if the key doesn't exist, pData will be set to nsnull. 87 */ 88 PRBool Get(KeyType aKey, UserDataType* pData) const; 89}; 90 91 92// 93// nsClassHashtable definitions 94// 95 96template<class KeyClass,class T> 97PRBool 98nsClassHashtable<KeyClass,T>::Get(KeyType aKey, T** retVal) const 99{ 100 typename nsBaseHashtable<KeyClass,nsAutoPtr<T>,T*>::EntryType* ent = 101 GetEntry(aKey); 102 103 if (ent) 104 { 105 if (retVal) 106 *retVal = ent->mData; 107 108 return PR_TRUE; 109 } 110 111 if (retVal) 112 *retVal = nsnull; 113 114 return PR_FALSE; 115} 116 117 118// 119// nsClassHashtableMT definitions 120// 121 122template<class KeyClass,class T> 123PRBool 124nsClassHashtableMT<KeyClass,T>::Get(KeyType aKey, T** retVal) const 125{ 126 PR_Lock(this->mLock); 127 128 typename nsBaseHashtableMT<KeyClass,nsAutoPtr<T>,T*>::EntryType* ent = 129 GetEntry(aKey); 130 131 if (ent) 132 { 133 if (retVal) 134 *retVal = ent->mData; 135 136 PR_Unlock(this->mLock); 137 138 return PR_TRUE; 139 } 140 141 if (retVal) 142 *retVal = nsnull; 143 144 PR_Unlock(this->mLock); 145 146 return PR_FALSE; 147} 148 149#endif // nsClassHashtable_h__