bug 200709 remove dynamic linking of nsTHashtable and family. r=jkeiser sr=alecf

This commit is contained in:
bsmedberg%covad.net 2003-04-10 13:27:16 +00:00
Родитель c1a4389e74
Коммит 2fe15728d7
10 изменённых файлов: 407 добавлений и 624 удалений

Просмотреть файл

@ -84,9 +84,8 @@
#ifdef DEBUG
#include "pure.h"
#endif
#include "nsInterfaceHashtable.h"
#include "nsDataHashtable.h"
#include "nsHashKeys.h"
#include "nsTHashtable.h"
#include "pldhash.h"
#include "nsVariant.h"
#include "nsEscape.h"
@ -111,6 +110,8 @@ void XXXNeverCalled()
NS_NewInputStreamReadyEvent(NULL, NULL, NULL);
NS_NewOutputStreamReadyEvent(NULL, NULL, NULL);
NS_AsyncCopy(NULL, NULL, PR_TRUE, PR_TRUE, 0, 0, NULL);
PL_DHashStubEnumRemove(nsnull, nsnull, nsnull, nsnull);
nsIDHashKey::HashKey(nsID());
nsFixedSizeAllocator a;
nsRecyclingAllocator recyclingAllocator(2);
a.Init(0, 0, 0, 0, 0);
@ -141,8 +142,6 @@ void XXXNeverCalled()
nsCWeakReferent(0);
NS_GetWeakReference(NULL);
nsCOMPtr<nsISupports> dummyFoo(do_GetInterface(nsnull));
nsInterfaceHashtable<nsStringHashKey,nsISupports>();
nsDataHashtable<nsUint32HashKey,PRInt32>();
NS_NewByteArrayInputStream(NULL, NULL, 0);
NS_NewStorageStream(0,0, nsnull);
nsString foo;

Просмотреть файл

@ -40,7 +40,8 @@
#include "nsTHashtable.h"
#include NEW_H
typedef struct PRRWLock PRRWLock;
#include "prrwlock.h"
#include "nsDebug.h"
template<class KeyClass,class DataType,class UserDataType>
class nsBaseHashtable; // forward declaration
@ -183,8 +184,180 @@ protected:
void *arg);
};
#ifndef HAVE_CPP_EXTERN_INSTANTIATION
#include "nsBaseHashtableImpl.h"
#endif
//
// nsBaseHashtableET definitions
//
template<class KeyClass,class DataType>
nsBaseHashtableET<KeyClass,DataType>::nsBaseHashtableET(KeyTypePointer aKey) :
KeyClass(aKey)
{ }
template<class KeyClass,class DataType>
nsBaseHashtableET<KeyClass,DataType>::nsBaseHashtableET
(const nsBaseHashtableET<KeyClass,DataType>& toCopy) :
KeyClass(toCopy),
mData(toCopy.mData)
{ }
template<class KeyClass,class DataType>
nsBaseHashtableET<KeyClass,DataType>::~nsBaseHashtableET()
{ }
//
// nsBaseHashtable definitions
//
template<class KeyClass,class DataType,class UserDataType>
nsBaseHashtable<KeyClass,DataType,UserDataType>::nsBaseHashtable()
{ }
template<class KeyClass,class DataType,class UserDataType>
nsBaseHashtable<KeyClass,DataType,UserDataType>::~nsBaseHashtable()
{
if (mLock)
PR_DestroyRWLock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
PRBool
nsBaseHashtable<KeyClass,DataType,UserDataType>::Init(PRUint32 initSize,
PRBool threadSafe)
{
if (!nsTHashtable<EntryType>::Init(initSize))
return PR_FALSE;
if (!threadSafe)
{
mLock = nsnull;
return PR_TRUE;
}
mLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "nsBaseHashtable");
NS_WARN_IF_FALSE(mLock, "Error creating lock during nsBaseHashtable::Init()");
if (!mLock)
return PR_FALSE;
return PR_TRUE;
}
template<class KeyClass,class DataType,class UserDataType>
PRBool
nsBaseHashtable<KeyClass,DataType,UserDataType>::Get(KeyType aKey,
UserDataType* pData) const
{
if (mLock)
PR_RWLock_Rlock(mLock);
EntryType* ent = GetEntry(KeyClass::KeyToPointer(aKey));
if (ent)
{
if (pData)
*pData = ent->mData;
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_TRUE;
}
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_FALSE;
}
template<class KeyClass,class DataType,class UserDataType>
PRBool
nsBaseHashtable<KeyClass,DataType,UserDataType>::Put(KeyType aKey,
UserDataType aData)
{
if (mLock)
PR_RWLock_Wlock(mLock);
EntryType* ent = PutEntry(KeyClass::KeyToPointer(aKey));
if (!ent)
{
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_FALSE;
}
ent->mData = aData;
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_TRUE;
}
template<class KeyClass,class DataType,class UserDataType>
void
nsBaseHashtable<KeyClass,DataType,UserDataType>::Remove(KeyType aKey)
{
if (mLock)
PR_RWLock_Wlock(mLock);
RemoveEntry(KeyClass::KeyToPointer(aKey));
if (mLock)
PR_RWLock_Unlock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
void
nsBaseHashtable<KeyClass,DataType,UserDataType>::EnumerateRead
(EnumReadFunction fEnumCall, void* userArg) const
{
NS_ASSERTION(mTable.entrySize,
"nsBaseHashtable was not initialized properly.");
if (mLock)
PR_RWLock_Rlock(mLock);
s_EnumReadArgs enumData = { fEnumCall, userArg };
PL_DHashTableEnumerate(NS_CONST_CAST(PLDHashTable*,&mTable),
s_EnumStub,
&enumData);
if (mLock)
PR_RWLock_Unlock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
void nsBaseHashtable<KeyClass,DataType,UserDataType>::Clear()
{
if (mLock)
PR_RWLock_Wlock(mLock);
nsTHashtable<EntryType>::Clear();
if (mLock)
PR_RWLock_Unlock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
PLDHashOperator
nsBaseHashtable<KeyClass,DataType,UserDataType>::s_EnumReadStub
(PLDHashTable *table, PLDHashEntryHdr *hdr, PRUint32 number, void* arg)
{
EntryType* ent = NS_STATIC_CAST(EntryType*, hdr);
s_EnumReadArgs* eargs = (s_EnumReadArgs*) arg;
PLDHashOperator res = (eargs->func)(ent->GetKey(), ent->mData, eargs->userArg);
NS_ASSERTION( !(res & PL_DHASH_REMOVE ),
"PL_DHASH_REMOVE return during const enumeration; ignoring.");
if (res & PL_DHASH_STOP)
return PL_DHASH_STOP;
return PL_DHASH_NEXT;
}
#endif // nsBaseHashtable_h__

Просмотреть файл

@ -1,224 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is C++ hashtable templates.
*
* The Initial Developer of the Original Code is
* Benjamin Smedberg.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsBaseHashtableImpl_h__
#define nsBaseHashtableImpl_h__
#ifndef nsBaseHashtable_h__
#include "nsBaseHashtable.h"
#endif
#ifndef nsTHashtableImpl_h__
#include "nsTHashtableImpl.h"
#endif
#include "prrwlock.h"
#include "nsDebug.h"
/* nsBaseHashtableET definitions */
template<class KeyClass,class DataType>
nsBaseHashtableET<KeyClass,DataType>::nsBaseHashtableET(KeyTypePointer aKey) :
KeyClass(aKey)
{ }
template<class KeyClass,class DataType>
nsBaseHashtableET<KeyClass,DataType>::nsBaseHashtableET
(const nsBaseHashtableET<KeyClass,DataType>& toCopy) :
KeyClass(toCopy),
mData(toCopy.mData)
{ }
template<class KeyClass,class DataType>
nsBaseHashtableET<KeyClass,DataType>::~nsBaseHashtableET()
{ }
/* nsBaseHashtable definitions */
template<class KeyClass,class DataType,class UserDataType>
nsBaseHashtable<KeyClass,DataType,UserDataType>::nsBaseHashtable()
{ }
template<class KeyClass,class DataType,class UserDataType>
nsBaseHashtable<KeyClass,DataType,UserDataType>::~nsBaseHashtable()
{
if (mLock)
PR_DestroyRWLock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
PRBool
nsBaseHashtable<KeyClass,DataType,UserDataType>::Init(PRUint32 initSize,
PRBool threadSafe)
{
if (!nsTHashtable<EntryType>::Init(initSize))
return PR_FALSE;
if (!threadSafe)
{
mLock = nsnull;
return PR_TRUE;
}
mLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "nsBaseHashtable");
NS_WARN_IF_FALSE(mLock, "Error creating lock during nsBaseHashtable::Init()");
if (!mLock)
return PR_FALSE;
return PR_TRUE;
}
template<class KeyClass,class DataType,class UserDataType>
PRBool
nsBaseHashtable<KeyClass,DataType,UserDataType>::Get(KeyType aKey,
UserDataType* pData) const
{
if (mLock)
PR_RWLock_Rlock(mLock);
EntryType* ent = GetEntry(KeyClass::KeyToPointer(aKey));
if (ent)
{
if (pData)
*pData = ent->mData;
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_TRUE;
}
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_FALSE;
}
template<class KeyClass,class DataType,class UserDataType>
PRBool
nsBaseHashtable<KeyClass,DataType,UserDataType>::Put(KeyType aKey,
UserDataType aData)
{
if (mLock)
PR_RWLock_Wlock(mLock);
EntryType* ent = PutEntry(KeyClass::KeyToPointer(aKey));
if (!ent)
{
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_FALSE;
}
ent->mData = aData;
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_TRUE;
}
template<class KeyClass,class DataType,class UserDataType>
void
nsBaseHashtable<KeyClass,DataType,UserDataType>::Remove(KeyType aKey)
{
if (mLock)
PR_RWLock_Wlock(mLock);
RemoveEntry(KeyClass::KeyToPointer(aKey));
if (mLock)
PR_RWLock_Unlock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
void
nsBaseHashtable<KeyClass,DataType,UserDataType>::EnumerateRead
(EnumReadFunction fEnumCall, void* userArg) const
{
NS_ASSERTION(mTable.entrySize,
"nsBaseHashtable was not initialized properly.");
if (mLock)
PR_RWLock_Rlock(mLock);
s_EnumReadArgs enumData = { fEnumCall, userArg };
PL_DHashTableEnumerate(NS_CONST_CAST(PLDHashTable*,&mTable),
s_EnumStub,
&enumData);
if (mLock)
PR_RWLock_Unlock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
void nsBaseHashtable<KeyClass,DataType,UserDataType>::Clear()
{
if (mLock)
PR_RWLock_Wlock(mLock);
nsTHashtable<EntryType>::Clear();
if (mLock)
PR_RWLock_Unlock(mLock);
}
template<class KeyClass,class DataType,class UserDataType>
PLDHashOperator
nsBaseHashtable<KeyClass,DataType,UserDataType>::s_EnumReadStub
(PLDHashTable *table, PLDHashEntryHdr *hdr, PRUint32 number, void* arg)
{
EntryType* ent = NS_STATIC_CAST(EntryType*, hdr);
s_EnumReadArgs* eargs = (s_EnumReadArgs*) arg;
PLDHashOperator res = (eargs->func)(ent->GetKey(), ent->mData, eargs->userArg);
NS_ASSERTION( !(res & PL_DHASH_REMOVE ),
"PL_DHASH_REMOVE return during const enumeration; ignoring.");
if (res & PL_DHASH_STOP)
return PL_DHASH_STOP;
return PL_DHASH_NEXT;
}
#endif // nsBaseHashtableImpl_h__

Просмотреть файл

@ -74,14 +74,9 @@ public:
PRBool Get(KeyType aKey, UserDataType* pData) const;
};
/*
* this class doesn't have any predefined instances; implementations are
* included here
*/
#ifndef nsTHashtableImpl_h__
#include "nsTHashtableImpl.h"
#endif
//
// nsClassHashtable definitions
//
template<class KeyClass,class T>
PRBool

Просмотреть файл

@ -54,25 +54,4 @@ class nsDataHashtable :
public nsBaseHashtable<KeyClass,DataType,DataType>
{ };
/**
* declare shared-linkage for common usages of nsDataHashtable.
*
* To avoid including this code in multiple translation units, common usages
* are predeclared with external linkage. To add to this list, include the
* declarations here, definitions in nsTHashtable.cpp and dlldeps.cpp . This
* only works if the configure-test HAVE_CPP_EXTERN_INSTANTIATION succeeds.
*/
#ifndef HAVE_CPP_EXTERN_INSTANTIATION
#include "nsBaseHashtableImpl.h"
#else
#ifndef NO_THASHTABLE_EXTERN_DECL
extern template class NS_COM nsBaseHashtableET<nsUint32HashKey,PRInt32>;
extern template class NS_COM nsTHashtable< nsBaseHashtableET<nsUint32HashKey,PRInt32> >;
extern template class NS_COM nsBaseHashtable<nsUint32HashKey,PRInt32,PRInt32>;
extern template class NS_COM nsDataHashtable<nsUint32HashKey,PRInt32>;
#else
#include "nsBaseHashtableImpl.h"
#endif
#endif
#endif // nsDataHashtable_h__

Просмотреть файл

@ -75,25 +75,46 @@ public:
PRBool Get(KeyType aKey, UserDataType* pData) const;
};
/**
* declare shared-linkage for common usages of nsDataHashtable.
*
* To avoid including this code in multiple translation units, common usages
* are predeclared with external linkage. To add to this list, include the
* declarations here, definitions in nsTHashtable.cpp and dlldeps.cpp . This
* only works if the configure-test HAVE_CPP_EXTERN_INSTANTIATION succeeds.
*/
#ifndef HAVE_CPP_EXTERN_INSTANTIATION
#include "nsInterfaceHashtableImpl.h"
#else
#ifndef NO_THASHTABLE_EXTERN_DECL
extern template class NS_COM nsBaseHashtableET<nsStringHashKey, nsCOMPtr<nsISupports> >;
extern template class NS_COM nsTHashtable< nsBaseHashtableET<nsStringHashKey,nsCOMPtr<nsISupports> > >;
extern template class NS_COM nsBaseHashtable<nsStringHashKey, nsCOMPtr<nsISupports>, nsISupports*>;
extern template class NS_COM nsInterfaceHashtable<nsStringHashKey, nsISupports>;
#else
#include "nsInterfaceHashtableImpl.h"
#endif
#endif
//
// nsInterfaceHashtable definitions
//
template<class KeyClass,class Interface>
PRBool
nsInterfaceHashtable<KeyClass,Interface>::Get
(KeyType aKey, UserDataType* pInterface) const
{
if (mLock)
PR_RWLock_Rlock(mLock);
typename nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
GetEntry(KeyClass::KeyToPointer(aKey));
if (ent)
{
if (pInterface)
{
*pInterface = ent->mData;
NS_IF_ADDREF(*pInterface);
}
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_TRUE;
}
// if the key doesn't exist, set *pInterface to null
// so that it is a valid XPCOM getter
if (pInterface)
*pInterface = nsnull;
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_FALSE;
}
#endif // nsInterfaceHashtable_h__

Просмотреть файл

@ -1,88 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is C++ hashtable templates.
*
* The Initial Developer of the Original Code is
* Benjamin Smedberg.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsInterfaceHashtableImpl_h__
#define nsInterfaceHashtableImpl_h__
#ifndef nsInterfaceHashtable_h__
#include "nsInterfaceHashtable.h"
#endif
#ifndef nsBaseHashtableImpl_h__
#include "nsBaseHashtableImpl.h"
#endif
/* nsInterfaceHashtable definitions */
template<class KeyClass,class Interface>
PRBool
nsInterfaceHashtable<KeyClass,Interface>::Get
(KeyType aKey, UserDataType* pInterface) const
{
if (mLock)
PR_RWLock_Rlock(mLock);
typename nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>::EntryType* ent =
GetEntry(KeyClass::KeyToPointer(aKey));
if (ent)
{
if (pInterface)
{
*pInterface = ent->mData;
NS_IF_ADDREF(*pInterface);
}
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_TRUE;
}
// if the key doesn't exist, set *pInterface to null
// so that it is a valid XPCOM getter
if (pInterface)
*pInterface = nsnull;
if (mLock)
PR_RWLock_Unlock(mLock);
return PR_FALSE;
}
#endif // nsInterfaceHashtableImpl_h__

Просмотреть файл

@ -35,16 +35,11 @@
*
* ***** END LICENSE BLOCK ***** */
#define NO_THASHTABLE_EXTERN_DECL
#include "nsTHashtable.h"
#include "nsTHashtableImpl.h"
#include "nsInterfaceHashtable.h"
#include "nsInterfaceHashtableImpl.h"
#include "nsDataHashtable.h"
#include "nsHashKeys.h"
PLDHashOperator PL_DHashStubEnumRemove(PLDHashTable *table,
PR_IMPLEMENT(PLDHashOperator)
PL_DHashStubEnumRemove(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 ordinal,
void *userarg)
@ -65,18 +60,3 @@ PRUint32 nsIDHashKey::HashKey(const nsID* id)
return h;
}
// explicit instantiation of templates
#ifdef HAVE_CPP_EXTERN_INSTANTIATION
/* instantiate common nsInterfaceHashtable uses */
template class NS_EXPORT nsBaseHashtableET<nsStringHashKey,nsCOMPtr<nsISupports> >;
template class NS_EXPORT nsTHashtable< nsBaseHashtableET<nsStringHashKey,nsCOMPtr<nsISupports> > >;
template class NS_EXPORT nsBaseHashtable<nsStringHashKey,nsCOMPtr<nsISupports>,nsISupports*>;
template class NS_EXPORT nsInterfaceHashtable<nsStringHashKey,nsISupports>;
/* instantiate common nsDataHashtable uses */
template class NS_EXPORT nsBaseHashtableET<nsUint32HashKey,PRInt32>;
template class NS_EXPORT nsTHashtable< nsBaseHashtableET<nsUint32HashKey,PRInt32> >;
template class NS_EXPORT nsBaseHashtable<nsUint32HashKey,PRInt32,PRInt32>;
template class NS_EXPORT nsDataHashtable<nsUint32HashKey,PRInt32>;
#endif // HAVE_CPP_EXTERN_INSTANTIATION

Просмотреть файл

@ -40,10 +40,7 @@
#include "nscore.h"
#include "pldhash.h"
#ifdef _MSC_VER
#pragma warning( disable: 4231 ) // 'extern template' nonstandard C++ extension
#endif
#include "nsDebug.h"
/**
* a base class for templated hashtables.
@ -225,17 +222,186 @@ protected:
};
// helper function for Reset()
PLDHashOperator PL_DHashStubEnumRemove(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 ordinal,
void *userArg);
PR_EXTERN(PLDHashOperator)
PL_DHashStubEnumRemove(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 ordinal,
void *userArg);
/**
* if we can't declare external linkage for templates, we need to include the
* implementation header.
*/
#ifndef HAVE_CPP_EXTERN_INSTANTIATION
#include "nsTHashtableImpl.h"
#endif
//
// template definitions
//
template<class EntryType>
nsTHashtable<EntryType>::nsTHashtable()
{
// entrySize is our "I'm initialized" indicator
mTable.entrySize = 0;
}
template<class EntryType>
nsTHashtable<EntryType>::~nsTHashtable()
{
if (mTable.entrySize)
PL_DHashTableFinish(&mTable);
}
template<class EntryType>
PRBool
nsTHashtable<EntryType>::Init(PRUint32 initSize)
{
if (mTable.entrySize)
{
NS_ERROR("nsTHashtable::Init() should not be called twice.");
return PR_FALSE;
}
if (!PL_DHashTableInit(&mTable, &sOps, nsnull, sizeof(EntryType), initSize))
{
// if failed, reset "flag"
mTable.entrySize = 0;
return PR_FALSE;
}
return PR_TRUE;
}
template<class EntryType>
EntryType*
nsTHashtable<EntryType>::GetEntry(KeyTypePointer aKey) const
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
return NS_REINTERPRET_CAST(EntryType*,
PL_DHashTableOperate(
NS_CONST_CAST(PLDHashTable*,&mTable),
aKey,
PL_DHASH_LOOKUP));
}
template<class EntryType>
EntryType*
nsTHashtable<EntryType>::PutEntry(KeyTypePointer aKey)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
return (EntryType*) PL_DHashTableOperate(&mTable, aKey, PL_DHASH_ADD);
}
template<class EntryType>
void
nsTHashtable<EntryType>::RemoveEntry(KeyTypePointer aKey)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
PL_DHashTableOperate(&mTable, aKey, PL_DHASH_REMOVE);
return;
}
template<class EntryType>
PRUint32
nsTHashtable<EntryType>::EnumerateEntries(Enumerator enumFunc, void* userArg)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
s_EnumArgs args = { enumFunc, userArg };
return PL_DHashTableEnumerate(&mTable, s_EnumStub, &args);
}
template<class EntryType>
void
nsTHashtable<EntryType>::Clear()
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
PL_DHashTableEnumerate(&mTable, PL_DHashStubEnumRemove, nsnull);
}
// static definitions
template<class EntryType>
PLDHashTableOps
nsTHashtable<EntryType>::sOps =
{
::PL_DHashAllocTable,
::PL_DHashFreeTable,
s_GetKey,
s_HashKey,
s_MatchEntry,
EntryType::AllowMemMove() ? ::PL_DHashMoveEntryStub : s_CopyEntry,
s_ClearEntry,
::PL_DHashFinalizeStub,
s_InitEntry
};
template<class EntryType>
const void*
nsTHashtable<EntryType>::s_GetKey(PLDHashTable *table,
PLDHashEntryHdr *entry)
{
return ((EntryType*) entry)->GetKeyPointer();
}
template<class EntryType>
PLDHashNumber
nsTHashtable<EntryType>::s_HashKey(PLDHashTable *table,
const void *key)
{
return EntryType::HashKey(NS_REINTERPRET_CAST(const KeyTypePointer, key));
}
template<class EntryType>
PRBool
nsTHashtable<EntryType>::s_MatchEntry(PLDHashTable *table,
const PLDHashEntryHdr *entry,
const void *key)
{
return ((const EntryType*) entry)->KeyEquals(
NS_REINTERPRET_CAST(const KeyTypePointer, key));
}
template<class EntryType>
void
nsTHashtable<EntryType>::s_CopyEntry(PLDHashTable *table,
const PLDHashEntryHdr *from,
PLDHashEntryHdr *to)
{
new(to) EntryType(* NS_REINTERPRET_CAST(const EntryType*,from));
NS_CONST_CAST(EntryType*,NS_REINTERPRET_CAST(const EntryType*,from))->~EntryType();
}
template<class EntryType>
void
nsTHashtable<EntryType>::s_ClearEntry(PLDHashTable *table,
PLDHashEntryHdr *entry)
{
NS_REINTERPRET_CAST(EntryType*,entry)->~EntryType();
}
template<class EntryType>
void
nsTHashtable<EntryType>::s_InitEntry(PLDHashTable *table,
PLDHashEntryHdr *entry,
const void *key)
{
new(entry) EntryType(NS_REINTERPRET_CAST(KeyTypePointer,key));
}
template<class EntryType>
PLDHashOperator
nsTHashtable<EntryType>::s_EnumStub(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 number,
void *arg)
{
// dereferences the function-pointer to the user's enumeration function
return (* NS_REINTERPRET_CAST(s_EnumArgs*,arg)->userFunc)(
NS_REINTERPRET_CAST(EntryType*,entry),
NS_REINTERPRET_CAST(s_EnumArgs*,arg)->userArg);
}
#endif // nsTHashtable_h__

Просмотреть файл

@ -1,218 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is C++ hashtable templates.
*
* The Initial Developer of the Original Code is
* Benjamin Smedberg.
* Portions created by the Initial Developer are Copyright (C) 2002
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsTHashtableImpl_h__
#define nsTHashtableImpl_h__
#ifndef nsTHashtable_h__
#include "nsTHashtable.h"
#endif
#include "nsDebug.h"
template<class EntryType>
nsTHashtable<EntryType>::nsTHashtable()
{
// entrySize is our "I'm initialized" indicator
mTable.entrySize = 0;
}
template<class EntryType>
nsTHashtable<EntryType>::~nsTHashtable()
{
if (mTable.entrySize)
PL_DHashTableFinish(&mTable);
}
template<class EntryType>
PRBool
nsTHashtable<EntryType>::Init(PRUint32 initSize)
{
if (mTable.entrySize)
{
NS_ERROR("nsTHashtable::Init() should not be called twice.");
return PR_FALSE;
}
if (!PL_DHashTableInit(&mTable, &sOps, nsnull, sizeof(EntryType), initSize))
{
// if failed, reset "flag"
mTable.entrySize = 0;
return PR_FALSE;
}
return PR_TRUE;
}
template<class EntryType>
EntryType*
nsTHashtable<EntryType>::GetEntry(KeyTypePointer aKey) const
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
return NS_REINTERPRET_CAST(EntryType*,
PL_DHashTableOperate(
NS_CONST_CAST(PLDHashTable*,&mTable),
aKey,
PL_DHASH_LOOKUP));
}
template<class EntryType>
EntryType*
nsTHashtable<EntryType>::PutEntry(KeyTypePointer aKey)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
return (EntryType*) PL_DHashTableOperate(&mTable, aKey, PL_DHASH_ADD);
}
template<class EntryType>
void
nsTHashtable<EntryType>::RemoveEntry(KeyTypePointer aKey)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
PL_DHashTableOperate(&mTable, aKey, PL_DHASH_REMOVE);
return;
}
template<class EntryType>
PRUint32
nsTHashtable<EntryType>::EnumerateEntries(Enumerator enumFunc, void* userArg)
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
s_EnumArgs args = { enumFunc, userArg };
return PL_DHashTableEnumerate(&mTable, s_EnumStub, &args);
}
template<class EntryType>
void
nsTHashtable<EntryType>::Clear()
{
NS_ASSERTION(mTable.entrySize, "nsTHashtable was not initialized properly.");
PL_DHashTableEnumerate(&mTable, PL_DHashStubEnumRemove, nsnull);
}
// static definitions
template<class EntryType>
PLDHashTableOps
nsTHashtable<EntryType>::sOps =
{
::PL_DHashAllocTable,
::PL_DHashFreeTable,
s_GetKey,
s_HashKey,
s_MatchEntry,
EntryType::AllowMemMove() ? ::PL_DHashMoveEntryStub : s_CopyEntry,
s_ClearEntry,
::PL_DHashFinalizeStub,
s_InitEntry
};
template<class EntryType>
const void*
nsTHashtable<EntryType>::s_GetKey(PLDHashTable *table,
PLDHashEntryHdr *entry)
{
return ((EntryType*) entry)->GetKeyPointer();
}
template<class EntryType>
PLDHashNumber
nsTHashtable<EntryType>::s_HashKey(PLDHashTable *table,
const void *key)
{
return EntryType::HashKey(NS_REINTERPRET_CAST(const KeyTypePointer, key));
}
template<class EntryType>
PRBool
nsTHashtable<EntryType>::s_MatchEntry(PLDHashTable *table,
const PLDHashEntryHdr *entry,
const void *key)
{
return ((const EntryType*) entry)->KeyEquals(
NS_REINTERPRET_CAST(const KeyTypePointer, key));
}
template<class EntryType>
void
nsTHashtable<EntryType>::s_CopyEntry(PLDHashTable *table,
const PLDHashEntryHdr *from,
PLDHashEntryHdr *to)
{
new(to) EntryType(* NS_REINTERPRET_CAST(const EntryType*,from));
NS_CONST_CAST(EntryType*,NS_REINTERPRET_CAST(const EntryType*,from))->~EntryType();
}
template<class EntryType>
void
nsTHashtable<EntryType>::s_ClearEntry(PLDHashTable *table,
PLDHashEntryHdr *entry)
{
NS_REINTERPRET_CAST(EntryType*,entry)->~EntryType();
}
template<class EntryType>
void
nsTHashtable<EntryType>::s_InitEntry(PLDHashTable *table,
PLDHashEntryHdr *entry,
const void *key)
{
new(entry) EntryType(NS_REINTERPRET_CAST(KeyTypePointer,key));
}
template<class EntryType>
PLDHashOperator
nsTHashtable<EntryType>::s_EnumStub(PLDHashTable *table,
PLDHashEntryHdr *entry,
PRUint32 number,
void *arg)
{
// dereferences the function-pointer to the user's enumeration function
return (* NS_REINTERPRET_CAST(s_EnumArgs*,arg)->userFunc)(
NS_REINTERPRET_CAST(EntryType*,entry),
NS_REINTERPRET_CAST(s_EnumArgs*,arg)->userArg);
}
#endif // nsTHashtableImpl_h__