2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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 nsInterfaceHashtable_h__
|
|
|
|
#define nsInterfaceHashtable_h__
|
|
|
|
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* templated hashtable class maps keys to interface pointers.
|
|
|
|
* See nsBaseHashtable for complete declaration.
|
|
|
|
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
|
|
|
* for a complete specification.
|
|
|
|
* @param Interface the interface-type being wrapped
|
|
|
|
* @see nsDataHashtable, nsClassHashtable
|
|
|
|
*/
|
|
|
|
template<class KeyClass,class Interface>
|
|
|
|
class nsInterfaceHashtable :
|
|
|
|
public nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
|
|
|
typedef Interface* UserDataType;
|
2010-06-30 14:11:43 -07:00
|
|
|
typedef nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* >
|
|
|
|
base_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
|
|
|
* @param pData This is an XPCOM getter, so pData is already_addrefed.
|
2012-07-30 07:20:58 -07:00
|
|
|
* If the key doesn't exist, pData will be set to nullptr.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-07-06 13:14:07 -07:00
|
|
|
bool Get(KeyType aKey, UserDataType* pData) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
|
|
|
*/
|
|
|
|
already_AddRefed<Interface> Get(KeyType aKey) 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
|
|
|
Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-04-03 09:49:17 -07:00
|
|
|
template <typename K, typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField)
|
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename K, typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
nsInterfaceHashtable<K, T>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags);
|
|
|
|
|
|
|
|
aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc<typename K::KeyType,T*>,
|
|
|
|
&userData);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Thread-safe version of nsInterfaceHashtable
|
|
|
|
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
|
|
|
* for a complete specification.
|
|
|
|
* @param Interface the interface-type being wrapped
|
|
|
|
*/
|
|
|
|
template<class KeyClass,class Interface>
|
|
|
|
class nsInterfaceHashtableMT :
|
|
|
|
public nsBaseHashtableMT< KeyClass, nsCOMPtr<Interface> , Interface* >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
|
|
|
typedef Interface* UserDataType;
|
2010-06-30 14:11:43 -07:00
|
|
|
typedef nsBaseHashtableMT< KeyClass, nsCOMPtr<Interface> , Interface* >
|
|
|
|
base_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
|
|
|
* @param pData This is an XPCOM getter, so pData is already_addrefed.
|
2012-07-30 07:20:58 -07:00
|
|
|
* If the key doesn't exist, pData will be set to nullptr.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-07-06 13:14:07 -07:00
|
|
|
bool Get(KeyType aKey, UserDataType* pData) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// GetWeak does not make sense on a multi-threaded hashtable, where another
|
|
|
|
// thread may remove the entry (and hence release it) as soon as GetWeak
|
|
|
|
// returns
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsInterfaceHashtable definitions
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class KeyClass,class Interface>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsInterfaceHashtable<KeyClass,Interface>::Get
|
|
|
|
(KeyType aKey, UserDataType* pInterface) const
|
|
|
|
{
|
2010-06-30 14:11:43 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (ent)
|
|
|
|
{
|
|
|
|
if (pInterface)
|
|
|
|
{
|
|
|
|
*pInterface = ent->mData;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*pInterface);
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the key doesn't exist, set *pInterface to null
|
|
|
|
// so that it is a valid XPCOM getter
|
|
|
|
if (pInterface)
|
2012-07-30 07:20:58 -07:00
|
|
|
*pInterface = nullptr;
|
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
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
template<class KeyClass, class Interface>
|
|
|
|
already_AddRefed<Interface>
|
|
|
|
nsInterfaceHashtable<KeyClass,Interface>::Get(KeyType aKey) const
|
|
|
|
{
|
2010-07-18 01:24:23 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2010-06-10 11:11:11 -07:00
|
|
|
if (!ent)
|
|
|
|
return NULL;
|
|
|
|
|
2011-09-16 13:22:44 -07:00
|
|
|
nsCOMPtr<Interface> copy = ent->mData;
|
|
|
|
return copy.forget();
|
2010-06-10 11:11:11 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
template<class KeyClass,class Interface>
|
|
|
|
Interface*
|
|
|
|
nsInterfaceHashtable<KeyClass,Interface>::GetWeak
|
2011-09-28 23:19:26 -07:00
|
|
|
(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
|
|
|
|
|
|
|
if (ent)
|
|
|
|
{
|
|
|
|
if (aFound)
|
2011-10-17 07:59:28 -07:00
|
|
|
*aFound = true;
|
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
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aFound)
|
2011-10-17 07:59:28 -07:00
|
|
|
*aFound = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsInterfaceHashtableMT definitions
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class KeyClass,class Interface>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsInterfaceHashtableMT<KeyClass,Interface>::Get
|
|
|
|
(KeyType aKey, UserDataType* pInterface) const
|
|
|
|
{
|
|
|
|
PR_Lock(this->mLock);
|
|
|
|
|
2010-06-30 14:11:43 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (ent)
|
|
|
|
{
|
|
|
|
if (pInterface)
|
|
|
|
{
|
|
|
|
*pInterface = ent->mData;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*pInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_Unlock(this->mLock);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the key doesn't exist, set *pInterface to null
|
|
|
|
// so that it is a valid XPCOM getter
|
|
|
|
if (pInterface)
|
2012-07-30 07:20:58 -07:00
|
|
|
*pInterface = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PR_Unlock(this->mLock);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // nsInterfaceHashtable_h__
|