2005-08-11 23:42:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
#ifndef nsClassHashtable_h__
|
|
|
|
#define nsClassHashtable_h__
|
|
|
|
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2005-08-11 23:43:06 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* templated hashtable class maps keys to C++ object pointers.
|
|
|
|
* See nsBaseHashtable for complete declaration.
|
|
|
|
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
|
|
|
* for a complete specification.
|
|
|
|
* @param Class the class-type being wrapped
|
|
|
|
* @see nsInterfaceHashtable, nsClassHashtable
|
|
|
|
*/
|
|
|
|
template<class KeyClass,class T>
|
|
|
|
class nsClassHashtable :
|
2005-08-11 23:43:06 +04:00
|
|
|
public nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* >
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
2005-08-11 23:43:06 +04:00
|
|
|
typedef T* UserDataType;
|
2010-07-01 01:11:43 +04:00
|
|
|
typedef nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* > base_type;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
nsClassHashtable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
explicit nsClassHashtable(uint32_t aInitSize)
|
|
|
|
: nsBaseHashtable<KeyClass,nsAutoPtr<T>,T*>(aInitSize)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
2005-08-11 23:43:11 +04:00
|
|
|
* @copydoc nsBaseHashtable::Get
|
2012-07-30 18:20:58 +04:00
|
|
|
* @param pData if the key doesn't exist, pData will be set to nullptr.
|
2005-08-11 23:42:59 +04:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Get(KeyType aKey, UserDataType* pData) const;
|
2010-06-12 00:13:26 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
2013-10-11 00:41:39 +04:00
|
|
|
* @returns nullptr if the key is not present.
|
2010-06-12 00:13:26 +04:00
|
|
|
*/
|
|
|
|
UserDataType Get(KeyType aKey) const;
|
2010-07-17 01:10:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the entry for the given key from the hashtable and return it in
|
2013-10-11 00:41:39 +04:00
|
|
|
* aOut. If the key is not in the hashtable, aOut's pointer is set to
|
|
|
|
* nullptr.
|
2010-07-17 01:10:01 +04:00
|
|
|
*
|
|
|
|
* Normally, an entry is deleted when it's removed from an nsClassHashtable,
|
|
|
|
* but this function transfers ownership of the entry back to the caller
|
|
|
|
* through aOut -- the entry will be deleted when aOut goes out of scope.
|
|
|
|
*
|
|
|
|
* @param aKey the key to get and remove from the hashtable
|
|
|
|
*/
|
|
|
|
void RemoveAndForget(KeyType aKey, nsAutoPtr<T> &aOut);
|
2005-08-11 23:43:11 +04:00
|
|
|
};
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2005-08-11 23:43:01 +04:00
|
|
|
//
|
|
|
|
// nsClassHashtable definitions
|
|
|
|
//
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
template<class KeyClass,class T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-08-11 23:42:59 +04:00
|
|
|
nsClassHashtable<KeyClass,T>::Get(KeyType aKey, T** retVal) const
|
|
|
|
{
|
2010-07-01 01:11:43 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
if (ent)
|
|
|
|
{
|
|
|
|
if (retVal)
|
|
|
|
*retVal = ent->mData;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-08-11 23:43:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (retVal)
|
2012-07-30 18:20:58 +04:00
|
|
|
*retVal = nullptr;
|
2005-08-11 23:43:11 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-08-11 23:43:16 +04:00
|
|
|
}
|
2005-08-11 23:43:11 +04:00
|
|
|
|
2010-06-12 00:13:26 +04:00
|
|
|
template<class KeyClass,class T>
|
|
|
|
T*
|
|
|
|
nsClassHashtable<KeyClass,T>::Get(KeyType aKey) const
|
|
|
|
{
|
2010-07-18 12:24:23 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2010-06-12 00:13:26 +04:00
|
|
|
|
|
|
|
if (!ent)
|
2013-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2010-06-12 00:13:26 +04:00
|
|
|
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2010-07-17 01:10:01 +04:00
|
|
|
template<class KeyClass,class T>
|
|
|
|
void
|
|
|
|
nsClassHashtable<KeyClass,T>::RemoveAndForget(KeyType aKey, nsAutoPtr<T> &aOut)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
aOut = nullptr;
|
2010-07-17 01:10:01 +04:00
|
|
|
nsAutoPtr<T> ptr;
|
|
|
|
|
|
|
|
typename base_type::EntryType *ent = this->GetEntry(aKey);
|
|
|
|
if (!ent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Transfer ownership from ent->mData into aOut.
|
|
|
|
aOut = ent->mData;
|
|
|
|
|
|
|
|
this->Remove(aKey);
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#endif // nsClassHashtable_h__
|