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/. */
7 #ifndef mozilla_SimpleEnumerator_h
8 #define mozilla_SimpleEnumerator_h
11 #include "nsISimpleEnumerator.h"
16 * A wrapper class around nsISimpleEnumerator to support ranged iteration. This
17 * requires every element in the enumeration to implement the same interface, T.
18 * If any element does not implement this interface, the enumeration ends at
19 * that element, and triggers an assertion in debug builds.
21 * Typical usage looks something like:
23 * for (auto& docShell : SimpleEnumerator<nsIDocShell>(docShellEnum)) {
24 * docShell.LoadURI(...);
29 class SimpleEnumerator final
{
31 explicit SimpleEnumerator(nsISimpleEnumerator
* aEnum
) : mEnum(aEnum
) {}
35 explicit Entry(T
* aPtr
) : mPtr(aPtr
) {}
37 explicit Entry(nsISimpleEnumerator
& aEnum
) : mEnum(&aEnum
) { ++*this; }
39 const nsCOMPtr
<T
>& operator*() {
46 nsCOMPtr
<nsISupports
> next
;
47 if (NS_SUCCEEDED(mEnum
->GetNext(getter_AddRefs(next
)))) {
48 mPtr
= do_QueryInterface(next
);
56 bool operator!=(const Entry
& aOther
) const { return mPtr
!= aOther
.mPtr
; }
60 nsCOMPtr
<nsISimpleEnumerator
> mEnum
;
63 Entry
begin() { return Entry(*mEnum
); }
65 Entry
end() { return Entry(nullptr); }
68 nsCOMPtr
<nsISimpleEnumerator
> mEnum
;
71 } // namespace mozilla
73 #endif // mozilla_SimpleEnumerator_h