Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/ds/nsDataHashtable.h
Line
Count
Source
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
33
6
  nsDataHashtable() {}
nsDataHashtable<nsRefPtrHashKey<nsIMemoryReporter>, bool>::nsDataHashtable()
Line
Count
Source
33
3
  nsDataHashtable() {}
nsDataHashtable<nsPtrHashKey<nsIMemoryReporter>, bool>::nsDataHashtable()
Line
Count
Source
33
3
  nsDataHashtable() {}
34
  explicit nsDataHashtable(uint32_t aInitLength)
35
    : BaseClass(aInitLength)
36
6
  {
37
6
  }
nsDataHashtable<nsIDPointerHashKey, nsFactoryEntry*>::nsDataHashtable(unsigned int)
Line
Count
Source
36
3
  {
37
3
  }
nsDataHashtable<nsCStringHashKey, nsFactoryEntry*>::nsDataHashtable(unsigned int)
Line
Count
Source
36
3
  {
37
3
  }
38
39
  /**
40
   * Retrieve a reference to the value for a key.
41
   *
42
   * @param aKey the key to retrieve.
43
   * @return a reference to the found value, or nullptr if no entry was found
44
   * with the given key.
45
   */
46
  DataType* GetValue(KeyType aKey)
47
  {
48
    if (EntryType* ent = this->GetEntry(aKey)) {
49
      return &ent->mData;
50
    }
51
    return nullptr;
52
  }
53
54
  /**
55
   * Retrieve the value for a key and remove the corresponding entry at
56
   * the same time.
57
   *
58
   * @param aKey the key to retrieve and remove
59
   * @return the found value, or Nothing if no entry was found with the
60
   *   given key.
61
   */
62
  mozilla::Maybe<DataType> GetAndRemove(KeyType aKey)
63
  {
64
    mozilla::Maybe<DataType> value;
65
    if (EntryType* ent = this->GetEntry(aKey)) {
66
      value.emplace(std::move(ent->mData));
67
      this->RemoveEntry(ent);
68
    }
69
    return value;
70
  }
71
};
72
73
#endif // nsDataHashtable_h__