2005-08-11 23:42:51 +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:51 +04: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>
|
|
|
|
class nsRefPtrHashtable :
|
|
|
|
public nsBaseHashtable< KeyClass, nsRefPtr<RefPtr> , RefPtr* >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
|
|
|
typedef RefPtr* UserDataType;
|
2010-07-01 01:11:43 +04:00
|
|
|
typedef nsBaseHashtable< KeyClass, nsRefPtr<RefPtr> , RefPtr* > base_type;
|
2005-08-11 23:42:51 +04:00
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
nsRefPtrHashtable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
explicit nsRefPtrHashtable(uint32_t aInitSize)
|
|
|
|
: nsBaseHashtable<KeyClass,nsRefPtr<RefPtr>,RefPtr*>(aInitSize)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:51 +04:00
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
|
|
|
* @param pData This is an XPCOM getter, so pData is already_addrefed.
|
2012-07-30 18:20:58 +04:00
|
|
|
* If the key doesn't exist, pData will be set to nullptr.
|
2005-08-11 23:42:51 +04:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool Get(KeyType aKey, UserDataType* pData) const;
|
2005-08-11 23:42:51 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a weak reference to the hashtable entry.
|
2012-07-30 18:20:58 +04:00
|
|
|
* @param aFound If not nullptr, will be set to true if the entry is found,
|
2011-10-17 18:59:28 +04:00
|
|
|
* to false otherwise.
|
2012-07-30 18:20:58 +04:00
|
|
|
* @return The entry, or nullptr if not found. Do not release this pointer!
|
2005-08-11 23:42:51 +04:00
|
|
|
*/
|
2012-07-30 18:20:58 +04:00
|
|
|
RefPtr* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
|
2005-08-11 23:42:51 +04:00
|
|
|
};
|
|
|
|
|
2013-04-03 20:49:17 +04:00
|
|
|
template <typename K, typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsRefPtrHashtable<K, T>& aField)
|
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K, typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsRefPtrHashtable<K, T>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags);
|
|
|
|
|
|
|
|
aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc<typename K::KeyType,T*>,
|
|
|
|
&userData);
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:51 +04:00
|
|
|
//
|
|
|
|
// nsRefPtrHashtable definitions
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class KeyClass, class RefPtr>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-08-11 23:42:51 +04:00
|
|
|
nsRefPtrHashtable<KeyClass,RefPtr>::Get
|
|
|
|
(KeyType aKey, UserDataType* pRefPtr) const
|
|
|
|
{
|
2010-07-01 01:11:43 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2005-08-11 23:42:51 +04:00
|
|
|
|
2014-02-25 05:16:10 +04:00
|
|
|
if (ent) {
|
|
|
|
if (pRefPtr) {
|
2005-08-11 23:42:51 +04:00
|
|
|
*pRefPtr = ent->mData;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*pRefPtr);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-08-11 23:42:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the key doesn't exist, set *pRefPtr to null
|
|
|
|
// so that it is a valid XPCOM getter
|
2014-02-25 05:16:10 +04:00
|
|
|
if (pRefPtr) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*pRefPtr = nullptr;
|
2014-02-25 05:16:10 +04:00
|
|
|
}
|
2005-08-11 23:42:51 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-08-11 23:42:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class KeyClass, class RefPtr>
|
|
|
|
RefPtr*
|
|
|
|
nsRefPtrHashtable<KeyClass,RefPtr>::GetWeak
|
2011-09-29 10:19:26 +04:00
|
|
|
(KeyType aKey, bool* aFound) const
|
2005-08-11 23:42:51 +04:00
|
|
|
{
|
2010-07-01 01:11:43 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2005-08-11 23:42:51 +04:00
|
|
|
|
2014-02-25 05:16:10 +04:00
|
|
|
if (ent) {
|
|
|
|
if (aFound) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFound = true;
|
2014-02-25 05:16:10 +04:00
|
|
|
}
|
2005-08-11 23:42:51 +04:00
|
|
|
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
// Key does not exist, return nullptr and set aFound to false
|
2014-02-25 05:16:10 +04:00
|
|
|
if (aFound) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFound = false;
|
2014-02-25 05:16:10 +04:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2005-08-11 23:42:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // nsRefPtrHashtable_h__
|