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 nsDataHashtable_h__
|
|
|
|
#define nsDataHashtable_h__
|
|
|
|
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsBaseHashtable.h"
|
2016-08-23 03:31:26 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* templated hashtable class maps keys to simple datatypes.
|
|
|
|
* See nsBaseHashtable for complete declaration
|
|
|
|
* @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
|
|
|
|
* for a complete specification.
|
|
|
|
* @param DataType the simple datatype being wrapped
|
|
|
|
* @see nsInterfaceHashtable, nsClassHashtable
|
|
|
|
*/
|
2014-06-27 05:35:39 +04:00
|
|
|
template <class KeyClass, class DataType>
|
|
|
|
class nsDataHashtable : public nsBaseHashtable<KeyClass, DataType, DataType> {
|
2016-08-23 03:31:26 +03:00
|
|
|
private:
|
|
|
|
typedef nsBaseHashtable<KeyClass, DataType, DataType> BaseClass;
|
|
|
|
|
2013-09-02 12:41:57 +04:00
|
|
|
public:
|
2016-08-23 03:31:26 +03:00
|
|
|
using typename BaseClass::EntryType;
|
|
|
|
using typename BaseClass::KeyType;
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
nsDataHashtable() {}
|
2014-08-06 17:31:21 +04:00
|
|
|
explicit nsDataHashtable(uint32_t aInitLength) : BaseClass(aInitLength) {}
|
2016-08-23 03:31:26 +03:00
|
|
|
|
2017-10-02 16:24:59 +03:00
|
|
|
/**
|
|
|
|
* Retrieve a reference to the value for a key.
|
|
|
|
*
|
|
|
|
* @param aKey the key to retrieve.
|
|
|
|
* @return a reference to the found value, or nullptr if no entry was found
|
|
|
|
* with the given key.
|
|
|
|
*/
|
|
|
|
DataType* GetValue(KeyType aKey) {
|
|
|
|
if (EntryType* ent = this->GetEntry(aKey)) {
|
|
|
|
return &ent->mData;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:31:26 +03:00
|
|
|
/**
|
|
|
|
* Retrieve the value for a key and remove the corresponding entry at
|
|
|
|
* the same time.
|
|
|
|
*
|
|
|
|
* @param aKey the key to retrieve and remove
|
|
|
|
* @return the found value, or Nothing if no entry was found with the
|
|
|
|
* given key.
|
|
|
|
*/
|
|
|
|
mozilla::Maybe<DataType> GetAndRemove(KeyType aKey) {
|
|
|
|
mozilla::Maybe<DataType> value;
|
|
|
|
if (EntryType* ent = this->GetEntry(aKey)) {
|
2018-05-30 22:15:35 +03:00
|
|
|
value.emplace(std::move(ent->mData));
|
2016-08-23 03:31:26 +03:00
|
|
|
this->RemoveEntry(ent);
|
|
|
|
}
|
|
|
|
return value;
|
2013-09-02 12:41:57 +04:00
|
|
|
}
|
|
|
|
};
|
2005-08-11 23:42:59 +04:00
|
|
|
|
|
|
|
#endif // nsDataHashtable_h__
|