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 /**
8  * Class for representing record arguments.  Basically an array under the hood.
9  */
10 
11 #ifndef mozilla_dom_Record_h
12 #define mozilla_dom_Record_h
13 
14 #include "nsTHashtable.h"
15 #include "nsHashKeys.h"
16 #include "nsString.h"
17 #include "nsTArray.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/Move.h"
20 
21 namespace mozilla {
22 namespace dom {
23 
24 namespace binding_detail {
25 template <typename KeyType, typename ValueType>
26 class RecordEntry {
27  public:
RecordEntry()28   RecordEntry() {}
29 
30   // Move constructor so we can do Records of Records.
RecordEntry(RecordEntry<KeyType,ValueType> && aOther)31   RecordEntry(RecordEntry<KeyType, ValueType>&& aOther)
32       : mKey(Move(aOther.mKey)), mValue(Move(aOther.mValue)) {}
33 
34   KeyType mKey;
35   ValueType mValue;
36 };
37 
38 // Specialize for a JSObject* ValueType and initialize it on construction, so we
39 // don't need to worry about un-initialized JSObject* floating around.
40 template <typename KeyType>
41 class RecordEntry<KeyType, JSObject*> {
42  public:
RecordEntry()43   RecordEntry() : mValue(nullptr) {}
44 
45   // Move constructor so we can do Records of Records.
RecordEntry(RecordEntry<KeyType,JSObject * > && aOther)46   RecordEntry(RecordEntry<KeyType, JSObject*>&& aOther)
47       : mKey(std::move(aOther.mKey)), mValue(std::move(aOther.mValue)) {}
48 
49   KeyType mKey;
50   JSObject* mValue;
51 };
52 
53 }  // namespace binding_detail
54 
55 template <typename KeyType, typename ValueType>
56 class Record {
57  public:
58   typedef typename binding_detail::RecordEntry<KeyType, ValueType> EntryType;
59   typedef Record<KeyType, ValueType> SelfType;
60 
Record()61   Record() {}
62 
63   // Move constructor so we can do Record of Record.
Record(SelfType && aOther)64   Record(SelfType&& aOther) : mEntries(Move(aOther.mEntries)) {}
65 
Entries()66   const nsTArray<EntryType>& Entries() const { return mEntries; }
67 
Entries()68   nsTArray<EntryType>& Entries() { return mEntries; }
69 
70  private:
71   nsTArray<EntryType> mEntries;
72 };
73 
74 }  // namespace dom
75 }  // namespace mozilla
76 
77 template <typename K, typename V>
78 class nsDefaultComparator<mozilla::dom::binding_detail::RecordEntry<K, V>, K> {
79  public:
Equals(const mozilla::dom::binding_detail::RecordEntry<K,V> & aEntry,const K & aKey)80   bool Equals(const mozilla::dom::binding_detail::RecordEntry<K, V>& aEntry,
81               const K& aKey) const {
82     return aEntry.mKey == aKey;
83   }
84 };
85 
86 #endif  // mozilla_dom_Record_h
87