/src/mozilla-central/xpcom/ds/nsStringEnumerator.h
Line | Count | Source (jump to first uncovered line) |
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 | | #ifndef nsStringEnumerator_h |
8 | | #define nsStringEnumerator_h |
9 | | |
10 | | #include "nsIStringEnumerator.h" |
11 | | #include "nsStringFwd.h" |
12 | | #include "nsTArrayForwardDeclare.h" |
13 | | |
14 | | class nsStringEnumeratorBase : public nsIStringEnumerator |
15 | | , public nsIUTF8StringEnumerator |
16 | | { |
17 | | public: |
18 | | NS_DECL_NSISTRINGENUMERATORBASE |
19 | | |
20 | | NS_IMETHOD GetNext(nsAString&) override; |
21 | | |
22 | | using nsIUTF8StringEnumerator::GetNext; |
23 | | |
24 | | protected: |
25 | 0 | virtual ~nsStringEnumeratorBase() = default; |
26 | | }; |
27 | | |
28 | | // nsIStringEnumerator/nsIUTF8StringEnumerator implementations |
29 | | // |
30 | | // Currently all implementations support both interfaces. The |
31 | | // constructors below provide the most common interface for the given |
32 | | // type (i.e. nsIStringEnumerator for char16_t* strings, and so |
33 | | // forth) but any resulting enumerators can be queried to the other |
34 | | // type. Internally, the enumerators will hold onto the type that was |
35 | | // passed in and do conversion if GetNext() for the other type of |
36 | | // string is called. |
37 | | |
38 | | // There are a few different types of enumerators: |
39 | | |
40 | | // |
41 | | // These enumerators hold a pointer to the array. Be careful |
42 | | // because modifying the array may confuse the iterator, especially if |
43 | | // you insert or remove elements in the middle of the array. |
44 | | // |
45 | | |
46 | | // The non-adopting enumerator requires that the array sticks around |
47 | | // at least as long as the enumerator does. These are for constant |
48 | | // string arrays that the enumerator does not own, this could be used |
49 | | // in VERY specialized cases such as when the provider KNOWS that the |
50 | | // string enumerator will be consumed immediately, or will at least |
51 | | // outlast the array. |
52 | | // For example: |
53 | | // |
54 | | // nsTArray<nsCString> array; |
55 | | // array.AppendCString("abc"); |
56 | | // array.AppendCString("def"); |
57 | | // NS_NewStringEnumerator(&enumerator, &array, true); |
58 | | // |
59 | | // // call some internal method which iterates the enumerator |
60 | | // InternalMethod(enumerator); |
61 | | // NS_RELEASE(enumerator); |
62 | | // |
63 | | MOZ_MUST_USE nsresult |
64 | | NS_NewStringEnumerator(nsIStringEnumerator** aResult, |
65 | | const nsTArray<nsString>* aArray, |
66 | | nsISupports* aOwner); |
67 | | MOZ_MUST_USE nsresult |
68 | | NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, |
69 | | const nsTArray<nsCString>* aArray); |
70 | | |
71 | | MOZ_MUST_USE nsresult |
72 | | NS_NewStringEnumerator(nsIStringEnumerator** aResult, |
73 | | const nsTArray<nsString>* aArray); |
74 | | |
75 | | // Adopting string enumerators assume ownership of the array and will |
76 | | // call |operator delete| on the array when the enumerator is destroyed |
77 | | // this is useful when the provider creates an array solely for the |
78 | | // purpose of creating the enumerator. |
79 | | // For example: |
80 | | // |
81 | | // nsTArray<nsCString>* array = new nsTArray<nsCString>; |
82 | | // array->AppendString("abcd"); |
83 | | // NS_NewAdoptingStringEnumerator(&result, array); |
84 | | MOZ_MUST_USE nsresult |
85 | | NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult, |
86 | | nsTArray<nsString>* aArray); |
87 | | |
88 | | MOZ_MUST_USE nsresult |
89 | | NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, |
90 | | nsTArray<nsCString>* aArray); |
91 | | |
92 | | |
93 | | // these versions take a refcounted "owner" which will be addreffed |
94 | | // when the enumerator is created, and destroyed when the enumerator |
95 | | // is released. This allows providers to give non-owning pointers to |
96 | | // ns*StringArray member variables without worrying about lifetime |
97 | | // issues |
98 | | // For example: |
99 | | // |
100 | | // nsresult MyClass::Enumerate(nsIUTF8StringEnumerator** aResult) { |
101 | | // mCategoryList->AppendString("abcd"); |
102 | | // return NS_NewStringEnumerator(aResult, mCategoryList, this); |
103 | | // } |
104 | | // |
105 | | MOZ_MUST_USE nsresult |
106 | | NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, |
107 | | const nsTArray<nsCString>* aArray, |
108 | | nsISupports* aOwner); |
109 | | |
110 | | #endif // defined nsStringEnumerator_h |