2014-06-30 19:39:45 +04: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 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 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
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class KeyClass, class Interface>
|
|
|
|
class nsInterfaceHashtable
|
|
|
|
: public nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
|
|
|
typedef Interface* UserDataType;
|
2014-06-27 05:35:39 +04:00
|
|
|
typedef nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*> base_type;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsInterfaceHashtable() {}
|
2014-08-06 17:31:21 +04:00
|
|
|
explicit nsInterfaceHashtable(uint32_t aInitLength)
|
|
|
|
: nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>(aInitLength)
|
2013-09-02 12:41:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
/**
|
2005-08-11 23:43:11 +04:00
|
|
|
* @copydoc nsBaseHashtable::Get
|
2014-06-27 05:35:39 +04: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.
|
2005-08-11 23:42:59 +04:00
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
bool Get(KeyType aKey, UserDataType* aData) const;
|
2005-08-11 23:43:18 +04:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
|
|
|
*/
|
|
|
|
already_AddRefed<Interface> Get(KeyType aKey) const;
|
|
|
|
|
2005-08-11 23:43:18 +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:43:18 +04:00
|
|
|
*/
|
2012-07-30 18:20:58 +04:00
|
|
|
Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
|
2017-07-05 03:01:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the entry associated with aKey (if any), optionally _moving_ its
|
|
|
|
* current value into *aData, thereby avoiding calls to AddRef and Release.
|
|
|
|
* Return true if found.
|
|
|
|
* @param aKey the key to remove from the hashtable
|
|
|
|
* @param aData where to move the value (if non-null). If an entry is not
|
|
|
|
* found it will be set to nullptr.
|
|
|
|
* @return true if an entry for aKey was found (and removed)
|
|
|
|
*/
|
|
|
|
inline bool Remove(KeyType aKey, Interface** aData = nullptr);
|
2005-08-11 23:43:11 +04:00
|
|
|
};
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename K, typename T>
|
2013-04-03 20:49:17 +04:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField)
|
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<typename K, typename T>
|
2013-04-03 20:49:17 +04:00
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
2013-07-24 11:39:56 +04:00
|
|
|
const nsInterfaceHashtable<K, T>& aField,
|
2013-04-03 20:49:17 +04:00
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
2015-07-27 05:29:52 +03:00
|
|
|
for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
|
|
|
|
}
|
2013-04-03 20:49:17 +04:00
|
|
|
}
|
|
|
|
|
2005-08-11 23:43:01 +04:00
|
|
|
//
|
|
|
|
// nsInterfaceHashtable definitions
|
|
|
|
//
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class KeyClass, class Interface>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-06-27 05:35:39 +04:00
|
|
|
nsInterfaceHashtable<KeyClass, Interface>::Get(KeyType aKey,
|
|
|
|
UserDataType* aInterface) const
|
2005-08-11 23:43:01 +04:00
|
|
|
{
|
2010-07-01 01:11:43 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2005-08-11 23:43:01 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (ent) {
|
|
|
|
if (aInterface) {
|
|
|
|
*aInterface = ent->mData;
|
2005-08-11 23:43:01 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
NS_IF_ADDREF(*aInterface);
|
2005-08-11 23:43:01 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-08-11 23:43:11 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
// if the key doesn't exist, set *aInterface to null
|
2005-08-11 23:43:11 +04:00
|
|
|
// so that it is a valid XPCOM getter
|
2014-06-27 05:35:39 +04:00
|
|
|
if (aInterface) {
|
|
|
|
*aInterface = nullptr;
|
|
|
|
}
|
2005-08-11 23:43:11 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-08-11 23:43:11 +04:00
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
template<class KeyClass, class Interface>
|
|
|
|
already_AddRefed<Interface>
|
2014-06-27 05:35:39 +04:00
|
|
|
nsInterfaceHashtable<KeyClass, Interface>::Get(KeyType aKey) const
|
2010-06-10 22:11:11 +04:00
|
|
|
{
|
2010-07-18 12:24:23 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2014-06-27 05:35:39 +04:00
|
|
|
if (!ent) {
|
2013-06-14 07:57:47 +04:00
|
|
|
return nullptr;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-06-10 22:11:11 +04:00
|
|
|
|
2011-09-17 00:22:44 +04:00
|
|
|
nsCOMPtr<Interface> copy = ent->mData;
|
|
|
|
return copy.forget();
|
2010-06-10 22:11:11 +04:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class KeyClass, class Interface>
|
2005-08-11 23:43:18 +04:00
|
|
|
Interface*
|
2014-06-27 05:35:39 +04:00
|
|
|
nsInterfaceHashtable<KeyClass, Interface>::GetWeak(KeyType aKey,
|
|
|
|
bool* aFound) const
|
2005-08-11 23:43:18 +04:00
|
|
|
{
|
2010-07-01 01:11:43 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2005-08-11 23:43:18 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (ent) {
|
|
|
|
if (aFound) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFound = true;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2005-08-11 23:43:18 +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-06-27 05:35:39 +04:00
|
|
|
if (aFound) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFound = false;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2005-08-11 23:43:18 +04:00
|
|
|
}
|
2005-08-11 23:43:11 +04:00
|
|
|
|
2017-07-05 03:01:44 +03:00
|
|
|
template<class KeyClass, class Interface>
|
|
|
|
bool
|
|
|
|
nsInterfaceHashtable<KeyClass, Interface>::Remove(KeyType aKey,
|
|
|
|
Interface** aData)
|
|
|
|
{
|
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
|
|
|
|
|
|
|
if (ent) {
|
|
|
|
if (aData) {
|
|
|
|
ent->mData.forget(aData);
|
|
|
|
}
|
|
|
|
this->RemoveEntry(ent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData) {
|
|
|
|
*aData = nullptr;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#endif // nsInterfaceHashtable_h__
|