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 nsStringEnumerator_h
8 #define nsStringEnumerator_h
10 #include "nsIStringEnumerator.h"
11 #include "nsStringFwd.h"
12 #include "nsTArrayForwardDeclare.h"
14 class nsStringEnumeratorBase
: public nsIStringEnumerator
,
15 public nsIUTF8StringEnumerator
{
17 NS_DECL_NSISTRINGENUMERATORBASE
19 NS_IMETHOD
GetNext(nsAString
&) override
;
21 using nsIUTF8StringEnumerator::GetNext
;
24 virtual ~nsStringEnumeratorBase() = default;
27 // nsIStringEnumerator/nsIUTF8StringEnumerator implementations
29 // Currently all implementations support both interfaces. The
30 // constructors below provide the most common interface for the given
31 // type (i.e. nsIStringEnumerator for char16_t* strings, and so
32 // forth) but any resulting enumerators can be queried to the other
33 // type. Internally, the enumerators will hold onto the type that was
34 // passed in and do conversion if GetNext() for the other type of
37 // There are a few different types of enumerators:
40 // These enumerators hold a pointer to the array. Be careful
41 // because modifying the array may confuse the iterator, especially if
42 // you insert or remove elements in the middle of the array.
45 // The non-adopting enumerator requires that the array sticks around
46 // at least as long as the enumerator does. These are for constant
47 // string arrays that the enumerator does not own, this could be used
48 // in VERY specialized cases such as when the provider KNOWS that the
49 // string enumerator will be consumed immediately, or will at least
53 // nsTArray<nsCString> array;
54 // array.AppendCString("abc");
55 // array.AppendCString("def");
56 // NS_NewStringEnumerator(&enumerator, &array, true);
58 // // call some internal method which iterates the enumerator
59 // InternalMethod(enumerator);
60 // NS_RELEASE(enumerator);
62 [[nodiscard
]] nsresult
NS_NewStringEnumerator(nsIStringEnumerator
** aResult
,
63 const nsTArray
<nsString
>* aArray
,
65 [[nodiscard
]] nsresult
NS_NewUTF8StringEnumerator(
66 nsIUTF8StringEnumerator
** aResult
, const nsTArray
<nsCString
>* aArray
);
68 [[nodiscard
]] nsresult
NS_NewStringEnumerator(nsIStringEnumerator
** aResult
,
69 const nsTArray
<nsString
>* aArray
);
71 // Adopting string enumerators assume ownership of the array and will
72 // call |operator delete| on the array when the enumerator is destroyed
73 // this is useful when the provider creates an array solely for the
74 // purpose of creating the enumerator.
77 // nsTArray<nsCString>* array = new nsTArray<nsCString>;
78 // array->AppendString("abcd");
79 // NS_NewAdoptingStringEnumerator(&result, array);
80 [[nodiscard
]] nsresult
NS_NewAdoptingStringEnumerator(
81 nsIStringEnumerator
** aResult
, nsTArray
<nsString
>* aArray
);
83 [[nodiscard
]] nsresult
NS_NewAdoptingUTF8StringEnumerator(
84 nsIUTF8StringEnumerator
** aResult
, nsTArray
<nsCString
>* aArray
);
86 // these versions take a refcounted "owner" which will be addreffed
87 // when the enumerator is created, and destroyed when the enumerator
88 // is released. This allows providers to give non-owning pointers to
89 // ns*StringArray member variables without worrying about lifetime
93 // nsresult MyClass::Enumerate(nsIUTF8StringEnumerator** aResult) {
94 // mCategoryList->AppendString("abcd");
95 // return NS_NewStringEnumerator(aResult, mCategoryList, this);
98 [[nodiscard
]] nsresult
NS_NewUTF8StringEnumerator(
99 nsIUTF8StringEnumerator
** aResult
, const nsTArray
<nsCString
>* aArray
,
100 nsISupports
* aOwner
);
102 #endif // defined nsStringEnumerator_h