1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsDataHashtable_h__
8 #define nsDataHashtable_h__
9 
10 #include "nsHashKeys.h"
11 #include "nsBaseHashtable.h"
12 #include "mozilla/Maybe.h"
13 
14 /**
15  * templated hashtable class maps keys to simple datatypes.
16  * See nsBaseHashtable for complete declaration
17  * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
18  *   for a complete specification.
19  * @param DataType the simple datatype being wrapped
20  * @see nsInterfaceHashtable, nsClassHashtable
21  */
22 template<class KeyClass, class DataType>
23 class nsDataHashtable
24   : public nsBaseHashtable<KeyClass, DataType, DataType>
25 {
26 private:
27   typedef nsBaseHashtable<KeyClass, DataType, DataType> BaseClass;
28 
29 public:
30   using typename BaseClass::KeyType;
31   using typename BaseClass::EntryType;
32 
nsDataHashtable()33   nsDataHashtable() {}
nsDataHashtable(uint32_t aInitLength)34   explicit nsDataHashtable(uint32_t aInitLength)
35     : BaseClass(aInitLength)
36   {
37   }
38 
39   /**
40    * Retrieve the value for a key and remove the corresponding entry at
41    * the same time.
42    *
43    * @param aKey the key to retrieve and remove
44    * @return the found value, or Nothing if no entry was found with the
45    *   given key.
46    */
GetAndRemove(KeyType aKey)47   mozilla::Maybe<DataType> GetAndRemove(KeyType aKey)
48   {
49     mozilla::Maybe<DataType> value;
50     if (EntryType* ent = this->GetEntry(aKey)) {
51       value.emplace(mozilla::Move(ent->mData));
52       this->RemoveEntry(ent);
53     }
54     return value;
55   }
56 };
57 
58 #endif // nsDataHashtable_h__
59