xpcom/ds/nsStringEnumerator.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/nsStringEnumerator.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,90 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsIStringEnumerator.h"
    1.10 +#include "nsStringFwd.h"
    1.11 +#include "nsTArrayForwardDeclare.h"
    1.12 +
    1.13 +// nsIStringEnumerator/nsIUTF8StringEnumerator implementations
    1.14 +//
    1.15 +// Currently all implementations support both interfaces. The
    1.16 +// constructors below provide the most common interface for the given
    1.17 +// type (i.e. nsIStringEnumerator for char16_t* strings, and so
    1.18 +// forth) but any resulting enumerators can be queried to the other
    1.19 +// type. Internally, the enumerators will hold onto the type that was
    1.20 +// passed in and do conversion if GetNext() for the other type of
    1.21 +// string is called.
    1.22 +
    1.23 +// There are a few different types of enumerators:
    1.24 +
    1.25 +//
    1.26 +// These enumerators hold a pointer to the array. Be careful
    1.27 +// because modifying the array may confuse the iterator, especially if
    1.28 +// you insert or remove elements in the middle of the array.
    1.29 +//
    1.30 +
    1.31 +// The non-adopting enumerator requires that the array sticks around
    1.32 +// at least as long as the enumerator does. These are for constant
    1.33 +// string arrays that the enumerator does not own, this could be used
    1.34 +// in VERY specialized cases such as when the provider KNOWS that the
    1.35 +// string enumerator will be consumed immediately, or will at least
    1.36 +// outlast the array.
    1.37 +// For example:
    1.38 +//
    1.39 +// nsTArray<nsCString> array;
    1.40 +// array.AppendCString("abc");
    1.41 +// array.AppendCString("def");
    1.42 +// NS_NewStringEnumerator(&enumerator, &array, true);
    1.43 +//
    1.44 +// // call some internal method which iterates the enumerator
    1.45 +// InternalMethod(enumerator);
    1.46 +// NS_RELEASE(enumerator);
    1.47 +//
    1.48 +nsresult
    1.49 +NS_NewStringEnumerator(nsIStringEnumerator** aResult,
    1.50 +                       const nsTArray<nsString>* aArray,
    1.51 +                       nsISupports* aOwner);
    1.52 +nsresult
    1.53 +NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
    1.54 +                           const nsTArray<nsCString>* aArray);
    1.55 +
    1.56 +nsresult
    1.57 +NS_NewStringEnumerator(nsIStringEnumerator** aResult,
    1.58 +                       const nsTArray<nsString>* aArray);
    1.59 +
    1.60 +// Adopting string enumerators assume ownership of the array and will
    1.61 +// call |operator delete| on the array when the enumerator is destroyed
    1.62 +// this is useful when the provider creates an array solely for the
    1.63 +// purpose of creating the enumerator.
    1.64 +// For example:
    1.65 +//
    1.66 +// nsTArray<nsCString>* array = new nsTArray<nsCString>;
    1.67 +// array->AppendString("abcd");
    1.68 +// NS_NewAdoptingStringEnumerator(&result, array);
    1.69 +nsresult
    1.70 +NS_NewAdoptingStringEnumerator(nsIStringEnumerator** aResult,
    1.71 +                               nsTArray<nsString>* aArray);
    1.72 +
    1.73 +nsresult
    1.74 +NS_NewAdoptingUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
    1.75 +                                   nsTArray<nsCString>* aArray);
    1.76 +
    1.77 +
    1.78 +// these versions take a refcounted "owner" which will be addreffed
    1.79 +// when the enumerator is created, and destroyed when the enumerator
    1.80 +// is released. This allows providers to give non-owning pointers to
    1.81 +// ns*StringArray member variables without worrying about lifetime
    1.82 +// issues
    1.83 +// For example:
    1.84 +//
    1.85 +// nsresult MyClass::Enumerate(nsIUTF8StringEnumerator** aResult) {
    1.86 +//     mCategoryList->AppendString("abcd");
    1.87 +//     return NS_NewStringEnumerator(aResult, mCategoryList, this);
    1.88 +// }
    1.89 +//
    1.90 +nsresult
    1.91 +NS_NewUTF8StringEnumerator(nsIUTF8StringEnumerator** aResult,
    1.92 +                           const nsTArray<nsCString>* aArray,
    1.93 +                           nsISupports* aOwner);

mercurial