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 nsClassHashtable_h__
|
|
|
|
#define nsClassHashtable_h__
|
|
|
|
|
2014-05-07 15:05:37 -07:00
|
|
|
#include "mozilla/Move.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-26 18:35:39 -07:00
|
|
|
template<class KeyClass, class T>
|
|
|
|
class nsClassHashtable
|
|
|
|
: public nsBaseHashtable<KeyClass, nsAutoPtr<T>, T*>
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename KeyClass::KeyType KeyType;
|
|
|
|
typedef T* UserDataType;
|
2014-06-26 18:35:39 -07:00
|
|
|
typedef nsBaseHashtable<KeyClass, nsAutoPtr<T>, T*> base_type;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
nsClassHashtable() {}
|
2014-08-06 06:31:21 -07:00
|
|
|
explicit nsClassHashtable(uint32_t aInitLength)
|
|
|
|
: nsBaseHashtable<KeyClass, nsAutoPtr<T>, T*>(aInitLength)
|
2013-09-02 01:41:57 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:13:36 -07:00
|
|
|
/**
|
|
|
|
* Looks up aKey in the hash table. If it doesn't exist a new object of
|
|
|
|
* KeyClass will be created (using its default constructor) and then
|
|
|
|
* returned.
|
|
|
|
*/
|
|
|
|
UserDataType LookupOrAdd(KeyType aKey);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
2014-06-26 18:35:39 -07:00
|
|
|
* @param aData if the key doesn't exist, pData 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;
|
2010-06-11 13:13:26 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @copydoc nsBaseHashtable::Get
|
2013-10-10 13:41:39 -07:00
|
|
|
* @returns nullptr if the key is not present.
|
2010-06-11 13:13:26 -07:00
|
|
|
*/
|
|
|
|
UserDataType Get(KeyType aKey) const;
|
2010-07-16 14:10:01 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the entry for the given key from the hashtable and return it in
|
2013-10-10 13:41:39 -07:00
|
|
|
* aOut. If the key is not in the hashtable, aOut's pointer is set to
|
|
|
|
* nullptr.
|
2010-07-16 14:10:01 -07:00
|
|
|
*
|
|
|
|
* Normally, an entry is deleted when it's removed from an nsClassHashtable,
|
|
|
|
* but this function transfers ownership of the entry back to the caller
|
|
|
|
* through aOut -- the entry will be deleted when aOut goes out of scope.
|
|
|
|
*
|
|
|
|
* @param aKey the key to get and remove from the hashtable
|
|
|
|
*/
|
2014-06-26 18:35:39 -07:00
|
|
|
void RemoveAndForget(KeyType aKey, nsAutoPtr<T>& aOut);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsClassHashtable definitions
|
|
|
|
//
|
|
|
|
|
2014-08-11 11:13:36 -07:00
|
|
|
template<class KeyClass, class T>
|
|
|
|
T*
|
|
|
|
nsClassHashtable<KeyClass, T>::LookupOrAdd(KeyType aKey)
|
|
|
|
{
|
|
|
|
typename base_type::EntryType* ent = this->PutEntry(aKey);
|
|
|
|
if (!ent->mData) {
|
|
|
|
ent->mData = new T();
|
|
|
|
}
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
template<class KeyClass, class T>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-06-26 18:35:39 -07:00
|
|
|
nsClassHashtable<KeyClass, T>::Get(KeyType aKey, T** aRetVal) 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-06-26 18:35:39 -07:00
|
|
|
if (ent) {
|
|
|
|
if (aRetVal) {
|
|
|
|
*aRetVal = ent->mData;
|
|
|
|
}
|
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 (aRetVal) {
|
|
|
|
*aRetVal = 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
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
template<class KeyClass, class T>
|
2010-06-11 13:13:26 -07:00
|
|
|
T*
|
2014-06-26 18:35:39 -07:00
|
|
|
nsClassHashtable<KeyClass, T>::Get(KeyType aKey) const
|
2010-06-11 13:13:26 -07:00
|
|
|
{
|
2010-07-18 01:24:23 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
2014-06-26 18:35:39 -07:00
|
|
|
if (!ent) {
|
2013-10-10 13:41:39 -07:00
|
|
|
return nullptr;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2010-06-11 13:13:26 -07:00
|
|
|
|
|
|
|
return ent->mData;
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
template<class KeyClass, class T>
|
2010-07-16 14:10:01 -07:00
|
|
|
void
|
2014-06-26 18:35:39 -07:00
|
|
|
nsClassHashtable<KeyClass, T>::RemoveAndForget(KeyType aKey, nsAutoPtr<T>& aOut)
|
2010-07-16 14:10:01 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
aOut = nullptr;
|
2010-07-16 14:10:01 -07:00
|
|
|
nsAutoPtr<T> ptr;
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
typename base_type::EntryType* ent = this->GetEntry(aKey);
|
|
|
|
if (!ent) {
|
2010-07-16 14:10:01 -07:00
|
|
|
return;
|
2014-06-26 18:35:39 -07:00
|
|
|
}
|
2010-07-16 14:10:01 -07:00
|
|
|
|
|
|
|
// Transfer ownership from ent->mData into aOut.
|
2014-05-07 15:05:37 -07:00
|
|
|
aOut = mozilla::Move(ent->mData);
|
2010-07-16 14:10:01 -07:00
|
|
|
|
|
|
|
this->Remove(aKey);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif // nsClassHashtable_h__
|