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 nsClassHashtable_h__
|
|
|
|
#define nsClassHashtable_h__
|
|
|
|
|
2014-05-08 02:05:37 +04:00
|
|
|
#include "mozilla/Move.h"
|
2005-08-11 23:42:59 +04:00
|
|
|
#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
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class KeyClass, class T>
|
|
|
|
class nsClassHashtable
|
|
|
|
: 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;
|
2014-06-27 05:35:39 +04:00
|
|
|
typedef nsBaseHashtable<KeyClass, nsAutoPtr<T>, T*> base_type;
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2016-07-22 04:47:02 +03:00
|
|
|
using base_type::IsEmpty;
|
2017-07-05 03:01:44 +03:00
|
|
|
using base_type::Remove;
|
2016-07-22 04:47:02 +03:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsClassHashtable() {}
|
2014-08-06 17:31:21 +04:00
|
|
|
explicit nsClassHashtable(uint32_t aInitLength)
|
|
|
|
: nsBaseHashtable<KeyClass, nsAutoPtr<T>, T*>(aInitLength)
|
2013-09-02 12:41:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-11 22:13:36 +04:00
|
|
|
/**
|
|
|
|
* Looks up aKey in the hash table. If it doesn't exist a new object of
|
2016-07-29 20:49:19 +03:00
|
|
|
* KeyClass will be created (using the arguments provided) and then returned.
|
2014-08-11 22:13:36 +04:00
|
|
|
*/
|
2016-07-29 20:49:19 +03:00
|
|
|
template<typename... Args>
|
|
|
|
UserDataType LookupOrAdd(KeyType aKey, Args&&... aConstructionArgs);
|
2014-08-11 22:13:36 +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 if the key doesn't exist, pData 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;
|
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;
|
2005-08-11 23:43:11 +04:00
|
|
|
};
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2017-06-20 12:09:50 +03:00
|
|
|
template<typename K, typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionUnlink(nsClassHashtable<K, T>& aField)
|
|
|
|
{
|
|
|
|
aField.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename K, typename T>
|
|
|
|
inline void
|
|
|
|
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
|
|
|
|
const nsClassHashtable<K, T>& aField,
|
|
|
|
const char* aName,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
{
|
|
|
|
for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
ImplCycleCollectionTraverse(aCallback, *iter.UserData(), aName, aFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:43:01 +04:00
|
|
|
//
|
|
|
|
// nsClassHashtable definitions
|
|
|
|
//
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-08-11 22:13:36 +04:00
|
|
|
template<class KeyClass, class T>
|
2016-07-29 20:49:19 +03:00
|
|
|
template<typename... Args>
|
2014-08-11 22:13:36 +04:00
|
|
|
T*
|
2016-07-29 20:49:19 +03:00
|
|
|
nsClassHashtable<KeyClass, T>::LookupOrAdd(KeyType aKey,
|
|
|
|
Args&&... aConstructionArgs)
|
2014-08-11 22:13:36 +04:00
|
|
|
{
|
2017-06-14 02:03:38 +03:00
|
|
|
auto count = this->Count();
|
2014-08-11 22:13:36 +04:00
|
|
|
typename base_type::EntryType* ent = this->PutEntry(aKey);
|
2017-06-14 02:03:38 +03:00
|
|
|
if (count != this->Count()) {
|
2016-07-29 20:49:19 +03:00
|
|
|
ent->mData = new T(mozilla::Forward<Args>(aConstructionArgs)...);
|
2014-08-11 22:13:36 +04:00
|
|
|
}
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class KeyClass, class T>
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-06-27 05:35:39 +04:00
|
|
|
nsClassHashtable<KeyClass, T>::Get(KeyType aKey, T** aRetVal) const
|
2005-08-11 23:42:59 +04:00
|
|
|
{
|
2010-07-01 01:11:43 +04:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2005-08-11 23:42:59 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
if (ent) {
|
|
|
|
if (aRetVal) {
|
|
|
|
*aRetVal = ent->mData;
|
|
|
|
}
|
2005-08-11 23:42:59 +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 (aRetVal) {
|
|
|
|
*aRetVal = 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
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
template<class KeyClass, class T>
|
2010-06-12 00:13:26 +04:00
|
|
|
T*
|
2014-06-27 05:35:39 +04:00
|
|
|
nsClassHashtable<KeyClass, T>::Get(KeyType aKey) const
|
2010-06-12 00:13:26 +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-10-11 00:41:39 +04:00
|
|
|
return nullptr;
|
2014-06-27 05:35:39 +04:00
|
|
|
}
|
2010-06-12 00:13:26 +04:00
|
|
|
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2005-08-11 23:42:59 +04:00
|
|
|
#endif // nsClassHashtable_h__
|