2014-06-30 08:39:45 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsRefPtrHashtable_h__
|
|
|
|
#define nsRefPtrHashtable_h__
|
|
|
|
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* templated hashtable class maps keys to reference pointers.
|
|
|
|
* See nsBaseHashtable for complete declaration.
|
|
|
|
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
|
|
|
* for a complete specification.
|
|
|
|
* @param RefPtr the reference-type being wrapped
|
|
|
|
* @see nsDataHashtable, nsClassHashtable
|
|
|
|
*/
|
|
|
|
template<class KeyClass, class RefPtr>
|
2014-06-26 18:35:39 -07:00
|
|
|
class nsRefPtrHashtable
|
|
|
|
: public nsBaseHashtable<KeyClass, nsRefPtr<RefPtr>, RefPtr*>
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
|
|
|
typedef RefPtr* UserDataType;
|
2014-06-26 18:35:39 -07:00
|
|
|
typedef nsBaseHashtable<KeyClass, nsRefPtr<RefPtr>, RefPtr*> base_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
nsRefPtrHashtable() {}
|
2013-09-02 01:41:57 -07:00
|
|
|
explicit nsRefPtrHashtable(uint32_t aInitSize)
|
2014-06-26 18:35:39 -07:00
|
|
|
: nsBaseHashtable<KeyClass, nsRefPtr<RefPtr>, RefPtr*>(aInitSize)
|
2013-09-02 01:41:57 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
2014-06-26 18:35:39 -07:00
|
|
|
* @param aData This is an XPCOM getter, so aData is already_addrefed.
|
|
|
|
* If the key doesn't exist, aData will be set to nullptr.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2014-06-26 18:35:39 -07:00
|
|
|
bool Get(KeyType aKey, UserDataType* aData) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a weak reference to the hashtable entry.
|
2012-07-30 07:20:58 -07:00
|
|
|
* @param aFound If not nullptr, will be set to true if the entry is found,
|
2011-10-17 07:59:28 -07:00
|
|
|
* to false otherwise.
|
2012-07-30 07:20:58 -07:00
|
|
|
* @return The entry, or nullptr if not found. Do not release this pointer!
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-07-30 07:20:58 -07:00
|
|
|
RefPtr* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
|
2014-02-24 17:16:10 -08:00
|
|
|
|
2014-05-09 09:49:52 -07:00
|
|
|
// Overload Put, rather than overriding it.
|
|
|
|
using base_type::Put;
|
|
|
|
|
|
|
|
void Put(KeyType aKey, already_AddRefed<RefPtr> aData);
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
MOZ_WARN_UNUSED_RESULT bool Put(KeyType aKey, already_AddRefed<RefPtr> aData,
|
|
|
|
const mozilla::fallible_t&);
|
2014-05-09 09:49:52 -07:00
|
|
|
|
2014-02-24 17:16:10 -08:00
|
|
|
// Overload Remove, rather than overriding it.
|
|
|
|
using base_type::Remove;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the data for the associated key, swapping the current value into
|
|
|
|
* pData, thereby avoiding calls to AddRef and Release.
|
|
|
|
* @param aKey the key to remove from the hashtable
|
2014-06-26 18:35:39 -07:00
|
|
|
* @param aData This is an XPCOM getter, so aData is already_addrefed.
|
|
|
|
* If the key doesn't exist, aData will be set to nullptr. Must be non-null.
|
2014-02-24 17:16:10 -08:00
|
|
|
*/
|
2014-06-26 18:35:39 -07:00
|
|
|
bool Remove(KeyType aKey, UserDataType* aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
template<typename K, typename T>
|
2013-04-03 09:49:17 -07:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsRefPtrHashtable<K, T>& aField)
|
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
template<typename K, typename T>
|
2013-04-03 09:49:17 -07:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsRefPtrHashtable<K, T>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags);
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc<typename K::KeyType, T*>,
|
2013-04-03 09:49:17 -07:00
|
|
|
&userData);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
// nsRefPtrHashtable definitions
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class KeyClass, class RefPtr>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-06-26 18:35:39 -07:00
|
|
|
nsRefPtrHashtable<KeyClass, RefPtr>::Get(KeyType aKey,
|
|
|
|
UserDataType* aRefPtr) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-30 14:11:43 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-24 17:16:10 -08:00
|
|
|
if (ent) {
|
2014-06-26 18:35:39 -07:00
|
|
|
if (aRefPtr) {
|
|
|
|
*aRefPtr = ent->mData;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
NS_IF_ADDREF(*aRefPtr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// if the key doesn't exist, set *aRefPtr to null
|
2007-03-22 10:30:00 -07:00
|
|
|
// so that it is a valid XPCOM getter
|
2014-06-26 18:35:39 -07:00
|
|
|
if (aRefPtr) {
|
|
|
|
*aRefPtr = nullptr;
|
2014-02-24 17:16:10 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class KeyClass, class RefPtr>
|
|
|
|
RefPtr*
|
2014-06-26 18:35:39 -07:00
|
|
|
nsRefPtrHashtable<KeyClass, RefPtr>::GetWeak(KeyType aKey, bool* aFound) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-06-30 14:11:43 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-24 17:16:10 -08:00
|
|
|
if (ent) {
|
|
|
|
if (aFound) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aFound = true;
|
2014-02-24 17:16:10 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
// Key does not exist, return nullptr and set aFound to false
|
2014-02-24 17:16:10 -08:00
|
|
|
if (aFound) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aFound = false;
|
2014-02-24 17:16:10 -08:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-05-09 09:49:52 -07:00
|
|
|
template<class KeyClass, class RefPtr>
|
|
|
|
void
|
2014-06-26 18:35:39 -07:00
|
|
|
nsRefPtrHashtable<KeyClass, RefPtr>::Put(KeyType aKey,
|
|
|
|
already_AddRefed<RefPtr> aData)
|
2014-05-09 09:49:52 -07:00
|
|
|
{
|
|
|
|
if (!Put(aKey, mozilla::Move(aData), mozilla::fallible_t())) {
|
|
|
|
NS_ABORT_OOM(this->mTable.entrySize * this->mTable.entryCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class KeyClass, class RefPtr>
|
|
|
|
bool
|
2014-06-26 18:35:39 -07:00
|
|
|
nsRefPtrHashtable<KeyClass, RefPtr>::Put(KeyType aKey,
|
|
|
|
already_AddRefed<RefPtr> aData,
|
|
|
|
const mozilla::fallible_t&)
|
2014-05-09 09:49:52 -07:00
|
|
|
{
|
|
|
|
typename base_type::EntryType* ent = this->PutEntry(aKey);
|
|
|
|
|
|
|
|
if (!ent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ent->mData = aData;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-24 17:16:10 -08:00
|
|
|
template<class KeyClass, class RefPtr>
|
|
|
|
bool
|
2014-06-26 18:35:39 -07:00
|
|
|
nsRefPtrHashtable<KeyClass, RefPtr>::Remove(KeyType aKey,
|
|
|
|
UserDataType* aRefPtr)
|
2014-02-24 17:16:10 -08:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
MOZ_ASSERT(aRefPtr);
|
2014-02-24 17:16:10 -08:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
|
|
|
|
|
|
|
if (ent) {
|
2014-06-26 18:35:39 -07:00
|
|
|
ent->mData.forget(aRefPtr);
|
2014-02-24 17:16:10 -08:00
|
|
|
this->Remove(aKey);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
// If the key doesn't exist, set *aRefPtr to null
|
2014-02-24 17:16:10 -08:00
|
|
|
// so that it is a valid XPCOM getter.
|
2014-06-26 18:35:39 -07:00
|
|
|
*aRefPtr = nullptr;
|
2014-02-24 17:16:10 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif // nsRefPtrHashtable_h__
|