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