michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIStringEnumerator.h" michael@0: #include "nsStringFwd.h" michael@0: #include "nsTArrayForwardDeclare.h" michael@0: michael@0: // nsIStringEnumerator/nsIUTF8StringEnumerator implementations michael@0: // michael@0: // Currently all implementations support both interfaces. The michael@0: // constructors below provide the most common interface for the given michael@0: // type (i.e. nsIStringEnumerator for char16_t* strings, and so michael@0: // forth) but any resulting enumerators can be queried to the other michael@0: // type. Internally, the enumerators will hold onto the type that was michael@0: // passed in and do conversion if GetNext() for the other type of michael@0: // string is called. michael@0: michael@0: // There are a few different types of enumerators: michael@0: michael@0: // michael@0: // These enumerators hold a pointer to the array. Be careful michael@0: // because modifying the array may confuse the iterator, especially if michael@0: // you insert or remove elements in the middle of the array. michael@0: // michael@0: michael@0: // The non-adopting enumerator requires that the array sticks around michael@0: // at least as long as the enumerator does. These are for constant michael@0: // string arrays that the enumerator does not own, this could be used michael@0: // in VERY specialized cases such as when the provider KNOWS that the michael@0: // string enumerator will be consumed immediately, or will at least michael@0: // outlast the array. michael@0: // For example: michael@0: // michael@0: // nsTArray array; michael@0: // array.AppendCString("abc"); michael@0: // array.AppendCString("def"); michael@0: // NS_NewStringEnumerator(&enumerator, &array, true); michael@0: // michael@0: // // call some internal method which iterates the enumerator michael@0: // InternalMethod(enumerator); michael@0: // NS_RELEASE(enumerator); michael@0: // michael@0: nsresult michael@0: NS_NewStringEnumerator(nsIStringEnumerator** aResult, michael@0: const nsTArray* aArray, michael@0: nsISupports* aOwner); michael@0: nsresult michael@0: NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, michael@0: const nsTArray* aArray); michael@0: michael@0: nsresult michael@0: NS_NewStringEnumerator(nsIStringEnumerator** aResult, michael@0: const nsTArray* aArray); michael@0: michael@0: // Adopting string enumerators assume ownership of the array and will michael@0: // call |operator delete| on the array when the enumerator is destroyed michael@0: // this is useful when the provider creates an array solely for the michael@0: // purpose of creating the enumerator. michael@0: // For example: michael@0: // michael@0: // nsTArray* array = new nsTArray; michael@0: // array->AppendString("abcd"); michael@0: // NS_NewAdoptingStringEnumerator(&result, array); michael@0: nsresult michael@0: NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult, michael@0: nsTArray* aArray); michael@0: michael@0: nsresult michael@0: NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, michael@0: nsTArray* aArray); michael@0: michael@0: michael@0: // these versions take a refcounted "owner" which will be addreffed michael@0: // when the enumerator is created, and destroyed when the enumerator michael@0: // is released. This allows providers to give non-owning pointers to michael@0: // ns*StringArray member variables without worrying about lifetime michael@0: // issues michael@0: // For example: michael@0: // michael@0: // nsresult MyClass::Enumerate(nsIUTF8StringEnumerator** aResult) { michael@0: // mCategoryList->AppendString("abcd"); michael@0: // return NS_NewStringEnumerator(aResult, mCategoryList, this); michael@0: // } michael@0: // michael@0: nsresult michael@0: NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult, michael@0: const nsTArray* aArray, michael@0: nsISupports* aOwner);