Backed out 7 changesets (bug 1839993) for causing build bustages on DecoderTemplate...
[gecko.git] / dom / media / platforms / SimpleMap.h
blobc26bff1e9afd94ce05e5b204267d68f94cf782b8
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_SimpleMap_h
6 #define mozilla_SimpleMap_h
8 #include "mozilla/Mutex.h"
9 #include "nsTArray.h"
11 #include <utility>
13 namespace mozilla {
15 template <typename T>
16 class SimpleMap {
17 public:
18 typedef std::pair<int64_t, T> Element;
20 SimpleMap() : mMutex("SimpleMap") {}
22 // Insert Key and Value pair at the end of our map.
23 void Insert(int64_t aKey, const T& aValue) {
24 MutexAutoLock lock(mMutex);
25 mMap.AppendElement(std::make_pair(aKey, aValue));
27 // Sets aValue matching aKey and remove it from the map if found.
28 // The element returned is the first one found.
29 // Returns true if found, false otherwise.
30 bool Find(int64_t aKey, T& aValue) {
31 MutexAutoLock lock(mMutex);
32 for (uint32_t i = 0; i < mMap.Length(); i++) {
33 Element& element = mMap[i];
34 if (element.first == aKey) {
35 aValue = element.second;
36 mMap.RemoveElementAt(i);
37 return true;
40 return false;
42 // Remove all elements of the map.
43 void Clear() {
44 MutexAutoLock lock(mMutex);
45 mMap.Clear();
48 private:
49 Mutex mMutex MOZ_UNANNOTATED; // To protect mMap.
50 AutoTArray<Element, 16> mMap;
53 } // namespace mozilla
55 #endif // mozilla_SimpleMap_h