Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef mozilla_dom_DOMStringList_h |
michael@0 | 7 | #define mozilla_dom_DOMStringList_h |
michael@0 | 8 | |
michael@0 | 9 | #include "nsISupports.h" |
michael@0 | 10 | #include "nsTArray.h" |
michael@0 | 11 | #include "nsWrapperCache.h" |
michael@0 | 12 | #include "nsString.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace dom { |
michael@0 | 16 | |
michael@0 | 17 | class DOMStringList : public nsISupports, |
michael@0 | 18 | public nsWrapperCache |
michael@0 | 19 | { |
michael@0 | 20 | public: |
michael@0 | 21 | DOMStringList() |
michael@0 | 22 | { |
michael@0 | 23 | SetIsDOMBinding(); |
michael@0 | 24 | } |
michael@0 | 25 | virtual ~DOMStringList(); |
michael@0 | 26 | |
michael@0 | 27 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 28 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMStringList) |
michael@0 | 29 | |
michael@0 | 30 | virtual JSObject* WrapObject(JSContext* aCx); |
michael@0 | 31 | nsISupports* GetParentObject() |
michael@0 | 32 | { |
michael@0 | 33 | return nullptr; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult) |
michael@0 | 37 | { |
michael@0 | 38 | EnsureFresh(); |
michael@0 | 39 | if (aIndex < mNames.Length()) { |
michael@0 | 40 | aFound = true; |
michael@0 | 41 | aResult = mNames[aIndex]; |
michael@0 | 42 | } else { |
michael@0 | 43 | aFound = false; |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | void Item(uint32_t aIndex, nsAString& aResult) |
michael@0 | 48 | { |
michael@0 | 49 | EnsureFresh(); |
michael@0 | 50 | if (aIndex < mNames.Length()) { |
michael@0 | 51 | aResult = mNames[aIndex]; |
michael@0 | 52 | } else { |
michael@0 | 53 | aResult.SetIsVoid(true); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | uint32_t Length() |
michael@0 | 58 | { |
michael@0 | 59 | EnsureFresh(); |
michael@0 | 60 | return mNames.Length(); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | bool Contains(const nsAString& aString) |
michael@0 | 64 | { |
michael@0 | 65 | EnsureFresh(); |
michael@0 | 66 | return mNames.Contains(aString); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | bool Add(const nsAString& aName) |
michael@0 | 70 | { |
michael@0 | 71 | // XXXbz mNames should really be a fallible array; otherwise this |
michael@0 | 72 | // return value is meaningless. |
michael@0 | 73 | return mNames.AppendElement(aName) != nullptr; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | void Clear() |
michael@0 | 77 | { |
michael@0 | 78 | mNames.Clear(); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | nsTArray<nsString>& StringArray() |
michael@0 | 82 | { |
michael@0 | 83 | return mNames; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | void CopyList(nsTArray<nsString>& aNames) |
michael@0 | 87 | { |
michael@0 | 88 | aNames = mNames; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | protected: |
michael@0 | 92 | // A method that subclasses can override to modify mNames as needed |
michael@0 | 93 | // before we index into it or return its length or whatnot. |
michael@0 | 94 | virtual void EnsureFresh() |
michael@0 | 95 | { |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | // XXXbz we really want this to be a fallible array, but we end up passing it |
michael@0 | 99 | // to consumers who declare themselves as taking and nsTArray. :( |
michael@0 | 100 | nsTArray<nsString> mNames; |
michael@0 | 101 | }; |
michael@0 | 102 | |
michael@0 | 103 | } // namespace dom |
michael@0 | 104 | } // namespace mozilla |
michael@0 | 105 | |
michael@0 | 106 | #endif /* mozilla_dom_DOMStringList_h */ |