LCOV - code coverage report
Current view: directory - objdir/xpcom/build - nsEnumeratorUtils.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 104 21 20.2 %
Date: 2012-06-02 Functions: 30 11 36.7 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Benjamin Smedberg <benjamin@smedbergs.us>
      24                 :  * 
      25                 :  * Code moved from nsEmptyEnumerator.cpp:
      26                 :  *   L. David Baron <dbaron@dbaron.org>
      27                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
      28                 :  *
      29                 :  * Alternatively, the contents of this file may be used under the terms of
      30                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      31                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      32                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      33                 :  * of those above. If you wish to allow use of your version of this file only
      34                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      35                 :  * use your version of this file under the terms of the MPL, indicate your
      36                 :  * decision by deleting the provisions above and replace them with the notice
      37                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      38                 :  * the provisions above, a recipient may use your version of this file under
      39                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      40                 :  *
      41                 :  * ***** END LICENSE BLOCK ***** */
      42                 : 
      43                 : #include "mozilla/Attributes.h"
      44                 : 
      45                 : #include "nsEnumeratorUtils.h"
      46                 : 
      47                 : #include "nsISimpleEnumerator.h"
      48                 : #include "nsIStringEnumerator.h"
      49                 : 
      50                 : #include "nsCOMPtr.h"
      51                 : 
      52                 : class EmptyEnumeratorImpl : public nsISimpleEnumerator,
      53                 :                             public nsIUTF8StringEnumerator,
      54                 :                             public nsIStringEnumerator
      55                 : {
      56                 : public:
      57            1464 :     EmptyEnumeratorImpl() {}
      58                 :     // nsISupports interface
      59                 :     NS_DECL_ISUPPORTS_INHERITED  // not really inherited, but no mRefCnt
      60                 : 
      61                 :     // nsISimpleEnumerator
      62                 :     NS_DECL_NSISIMPLEENUMERATOR
      63                 :     NS_DECL_NSIUTF8STRINGENUMERATOR
      64                 :     // can't use NS_DECL_NSISTRINGENUMERATOR because they share the
      65                 :     // HasMore() signature
      66                 :     NS_IMETHOD GetNext(nsAString& aResult);
      67                 : 
      68            5504 :     static EmptyEnumeratorImpl* GetInstance() {
      69            5504 :         return const_cast<EmptyEnumeratorImpl*>(&kInstance);
      70                 :     }
      71                 : 
      72                 : private:
      73                 :     static const EmptyEnumeratorImpl kInstance;
      74                 : };
      75                 : 
      76                 : // nsISupports interface
      77            9029 : NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::AddRef(void)
      78                 : {
      79            9029 :     return 2;
      80                 : }
      81                 : 
      82           14533 : NS_IMETHODIMP_(nsrefcnt) EmptyEnumeratorImpl::Release(void)
      83                 : {
      84           14533 :     return 1;
      85                 : }
      86                 : 
      87            9111 : NS_IMPL_QUERY_INTERFACE3(EmptyEnumeratorImpl, nsISimpleEnumerator,
      88                 :                          nsIUTF8StringEnumerator, nsIStringEnumerator)
      89                 : 
      90                 : // nsISimpleEnumerator interface
      91             757 : NS_IMETHODIMP EmptyEnumeratorImpl::HasMoreElements(bool* aResult)
      92                 : {
      93             757 :     *aResult = false;
      94             757 :     return NS_OK;
      95                 : }
      96                 : 
      97            3519 : NS_IMETHODIMP EmptyEnumeratorImpl::HasMore(bool* aResult)
      98                 : {
      99            3519 :     *aResult = false;
     100            3519 :     return NS_OK;
     101                 : }
     102                 : 
     103            1228 : NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsISupports** aResult)
     104                 : {
     105            1228 :     return NS_ERROR_UNEXPECTED;
     106                 : }
     107                 : 
     108               0 : NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsACString& aResult)
     109                 : {
     110               0 :     return NS_ERROR_UNEXPECTED;
     111                 : }
     112                 : 
     113               0 : NS_IMETHODIMP EmptyEnumeratorImpl::GetNext(nsAString& aResult)
     114                 : {
     115               0 :     return NS_ERROR_UNEXPECTED;
     116                 : }
     117                 : 
     118            1464 : const EmptyEnumeratorImpl EmptyEnumeratorImpl::kInstance;
     119                 : 
     120                 : nsresult
     121            5504 : NS_NewEmptyEnumerator(nsISimpleEnumerator** aResult)
     122                 : {
     123            5504 :     *aResult = EmptyEnumeratorImpl::GetInstance();
     124            5504 :     return NS_OK;
     125                 : }
     126                 : 
     127                 : ////////////////////////////////////////////////////////////////////////////////
     128                 : 
     129                 : class nsSingletonEnumerator MOZ_FINAL : public nsISimpleEnumerator
     130                 : {
     131                 : public:
     132                 :     NS_DECL_ISUPPORTS
     133                 : 
     134                 :     // nsISimpleEnumerator methods
     135                 :     NS_IMETHOD HasMoreElements(bool* aResult);
     136                 :     NS_IMETHOD GetNext(nsISupports** aResult);
     137                 : 
     138                 :     nsSingletonEnumerator(nsISupports* aValue);
     139                 : 
     140                 : private:
     141                 :     ~nsSingletonEnumerator();
     142                 : 
     143                 : protected:
     144                 :     nsISupports* mValue;
     145                 :     bool mConsumed;
     146                 : };
     147                 : 
     148               0 : nsSingletonEnumerator::nsSingletonEnumerator(nsISupports* aValue)
     149               0 :     : mValue(aValue)
     150                 : {
     151               0 :     NS_IF_ADDREF(mValue);
     152               0 :     mConsumed = (mValue ? false : true);
     153               0 : }
     154                 : 
     155               0 : nsSingletonEnumerator::~nsSingletonEnumerator()
     156                 : {
     157               0 :     NS_IF_RELEASE(mValue);
     158               0 : }
     159                 : 
     160               0 : NS_IMPL_ISUPPORTS1(nsSingletonEnumerator, nsISimpleEnumerator)
     161                 : 
     162                 : NS_IMETHODIMP
     163               0 : nsSingletonEnumerator::HasMoreElements(bool* aResult)
     164                 : {
     165               0 :     NS_PRECONDITION(aResult != 0, "null ptr");
     166               0 :     if (! aResult)
     167               0 :         return NS_ERROR_NULL_POINTER;
     168                 : 
     169               0 :     *aResult = !mConsumed;
     170               0 :     return NS_OK;
     171                 : }
     172                 : 
     173                 : 
     174                 : NS_IMETHODIMP
     175               0 : nsSingletonEnumerator::GetNext(nsISupports** aResult)
     176                 : {
     177               0 :     NS_PRECONDITION(aResult != 0, "null ptr");
     178               0 :     if (! aResult)
     179               0 :         return NS_ERROR_NULL_POINTER;
     180                 : 
     181               0 :     if (mConsumed)
     182               0 :         return NS_ERROR_UNEXPECTED;
     183                 : 
     184               0 :     mConsumed = true;
     185                 : 
     186               0 :     *aResult = mValue;
     187               0 :     NS_ADDREF(*aResult);
     188               0 :     return NS_OK;
     189                 : }
     190                 : 
     191                 : nsresult
     192               0 : NS_NewSingletonEnumerator(nsISimpleEnumerator* *result,
     193                 :                           nsISupports* singleton)
     194                 : {
     195               0 :     nsSingletonEnumerator* enumer = new nsSingletonEnumerator(singleton);
     196               0 :     if (enumer == nsnull)
     197               0 :         return NS_ERROR_OUT_OF_MEMORY;
     198               0 :     *result = enumer; 
     199               0 :     NS_ADDREF(*result);
     200               0 :     return NS_OK;
     201                 : }
     202                 : 
     203                 : ////////////////////////////////////////////////////////////////////////////////
     204                 : 
     205                 : class nsUnionEnumerator : public nsISimpleEnumerator
     206                 : {
     207                 : public:
     208                 :     NS_DECL_ISUPPORTS
     209                 : 
     210                 :     // nsISimpleEnumerator methods
     211                 :     NS_IMETHOD HasMoreElements(bool* aResult);
     212                 :     NS_IMETHOD GetNext(nsISupports** aResult);
     213                 : 
     214                 :     nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
     215                 :                       nsISimpleEnumerator* secondEnumerator);
     216                 : 
     217                 : private:
     218                 :     ~nsUnionEnumerator();
     219                 : 
     220                 : protected:
     221                 :     nsCOMPtr<nsISimpleEnumerator> mFirstEnumerator, mSecondEnumerator;
     222                 :     bool mConsumed;
     223                 :     bool mAtSecond;
     224                 : };
     225                 : 
     226               0 : nsUnionEnumerator::nsUnionEnumerator(nsISimpleEnumerator* firstEnumerator,
     227                 :                                      nsISimpleEnumerator* secondEnumerator)
     228                 :     : mFirstEnumerator(firstEnumerator),
     229                 :       mSecondEnumerator(secondEnumerator),
     230               0 :       mConsumed(false), mAtSecond(false)
     231                 : {
     232               0 : }
     233                 : 
     234               0 : nsUnionEnumerator::~nsUnionEnumerator()
     235                 : {
     236               0 : }
     237                 : 
     238               0 : NS_IMPL_ISUPPORTS1(nsUnionEnumerator, nsISimpleEnumerator)
     239                 : 
     240                 : NS_IMETHODIMP
     241               0 : nsUnionEnumerator::HasMoreElements(bool* aResult)
     242                 : {
     243               0 :     NS_PRECONDITION(aResult != 0, "null ptr");
     244               0 :     if (! aResult)
     245               0 :         return NS_ERROR_NULL_POINTER;
     246                 : 
     247                 :     nsresult rv;
     248                 : 
     249               0 :     if (mConsumed) {
     250               0 :         *aResult = false;
     251               0 :         return NS_OK;
     252                 :     }
     253                 : 
     254               0 :     if (! mAtSecond) {
     255               0 :         rv = mFirstEnumerator->HasMoreElements(aResult);
     256               0 :         if (NS_FAILED(rv)) return rv;
     257                 : 
     258               0 :         if (*aResult)
     259               0 :             return NS_OK;
     260                 : 
     261               0 :         mAtSecond = true;
     262                 :     }
     263                 : 
     264               0 :     rv = mSecondEnumerator->HasMoreElements(aResult);
     265               0 :     if (NS_FAILED(rv)) return rv;
     266                 : 
     267               0 :     if (*aResult)
     268               0 :         return NS_OK;
     269                 : 
     270               0 :     *aResult = false;
     271               0 :     mConsumed = true;
     272               0 :     return NS_OK;
     273                 : }
     274                 : 
     275                 : NS_IMETHODIMP
     276               0 : nsUnionEnumerator::GetNext(nsISupports** aResult)
     277                 : {
     278               0 :     NS_PRECONDITION(aResult != 0, "null ptr");
     279               0 :     if (! aResult)
     280               0 :         return NS_ERROR_NULL_POINTER;
     281                 : 
     282               0 :     if (mConsumed)
     283               0 :         return NS_ERROR_UNEXPECTED;
     284                 : 
     285               0 :     if (! mAtSecond)
     286               0 :         return mFirstEnumerator->GetNext(aResult);
     287                 : 
     288               0 :     return mSecondEnumerator->GetNext(aResult);
     289                 : }
     290                 : 
     291                 : nsresult
     292               0 : NS_NewUnionEnumerator(nsISimpleEnumerator* *result,
     293                 :                       nsISimpleEnumerator* firstEnumerator,
     294                 :                       nsISimpleEnumerator* secondEnumerator)
     295                 : {
     296               0 :     *result = nsnull;
     297               0 :     if (! firstEnumerator) {
     298               0 :         *result = secondEnumerator;
     299               0 :     } else if (! secondEnumerator) {
     300               0 :         *result = firstEnumerator;
     301                 :     } else {
     302               0 :         nsUnionEnumerator* enumer = new nsUnionEnumerator(firstEnumerator, secondEnumerator);
     303               0 :         if (enumer == nsnull)
     304               0 :             return NS_ERROR_OUT_OF_MEMORY;
     305               0 :         *result = enumer; 
     306                 :     }
     307               0 :     NS_ADDREF(*result);
     308               0 :     return NS_OK;
     309            4392 : }

Generated by: LCOV version 1.7