nsDataHashtable.h 1.6 KB

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