LCOV - code coverage report
Current view: directory - toolkit/components/places - nsMaybeWeakPtr.h (source / functions) Found Hit Coverage
Test: app.info Lines: 15 15 100.0 %
Date: 2012-06-02 Functions: 31 28 90.3 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       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 Places code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Google Inc.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2005
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Brian Ryner <bryner@brianryner.com> (original author)
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #ifndef nsMaybeWeakPtr_h_
      40                 : #define nsMaybeWeakPtr_h_
      41                 : 
      42                 : #include "nsCOMPtr.h"
      43                 : #include "nsWeakReference.h"
      44                 : #include "nsTArray.h"
      45                 : 
      46                 : // nsMaybeWeakPtr is a helper object to hold a strong-or-weak reference
      47                 : // to the template class.  It's pretty minimal, but sufficient.
      48                 : 
      49                 : class nsMaybeWeakPtr_base
      50           15796 : {
      51                 : protected:
      52                 :   // Returns an addref'd pointer to the requested interface
      53                 :   void* GetValueAs(const nsIID& iid) const;
      54                 : 
      55                 :   nsCOMPtr<nsISupports> mPtr;
      56                 : };
      57                 : 
      58                 : template<class T>
      59                 : class nsMaybeWeakPtr : private nsMaybeWeakPtr_base
      60            7898 : {
      61                 : public:
      62            2592 :   nsMaybeWeakPtr(nsISupports *ref) { mPtr = ref; }
      63            2407 :   nsMaybeWeakPtr(const nsCOMPtr<nsIWeakReference> &ref) { mPtr = ref; }
      64            2899 :   nsMaybeWeakPtr(const nsCOMPtr<T> &ref) { mPtr = ref; }
      65                 : 
      66            6418 :   bool operator==(const nsMaybeWeakPtr<T> &other) const {
      67            6418 :     return mPtr == other.mPtr;
      68                 :   }
      69                 : 
      70           14583 :   operator const nsCOMPtr<T>() const { return GetValue(); }
      71                 : 
      72                 : protected:
      73           14583 :   const nsCOMPtr<T> GetValue() const {
      74                 :     return nsCOMPtr<T>(dont_AddRef(static_cast<T*>
      75           14583 :                                               (GetValueAs(NS_GET_TEMPLATE_IID(T)))));
      76                 :   }
      77                 : };
      78                 : 
      79                 : // nsMaybeWeakPtrArray is an array of MaybeWeakPtr objects, that knows how to
      80                 : // grab a weak reference to a given object if requested.  It only allows a
      81                 : // given object to appear in the array once.
      82                 : 
      83                 : typedef nsTArray< nsMaybeWeakPtr<nsISupports> > isupports_array_type;
      84                 : nsresult NS_AppendWeakElementBase(isupports_array_type *aArray,
      85                 :                                   nsISupports *aElement, bool aWeak);
      86                 : nsresult NS_RemoveWeakElementBase(isupports_array_type *aArray,
      87                 :                                   nsISupports *aElement);
      88                 : 
      89                 : template<class T>
      90                 : class nsMaybeWeakPtrArray : public nsTArray< nsMaybeWeakPtr<T> >
      91            4532 : {
      92                 : public:
      93            1480 :   nsresult AppendWeakElement(T *aElement, bool aOwnsWeak)
      94                 :   {
      95                 :     return NS_AppendWeakElementBase(
      96            1480 :       reinterpret_cast<isupports_array_type*>(this), aElement, aOwnsWeak);
      97                 :   }
      98                 : 
      99            1211 :   nsresult RemoveWeakElement(T *aElement)
     100                 :   {
     101                 :     return NS_RemoveWeakElementBase(
     102            1211 :       reinterpret_cast<isupports_array_type*>(this), aElement);
     103                 :   }
     104                 : };
     105                 : 
     106                 : // Call a method on each element in the array, but only if the element is
     107                 : // non-null.
     108                 : 
     109                 : #define ENUMERATE_WEAKARRAY(array, type, method)                           \
     110                 :   for (PRUint32 array_idx = 0; array_idx < array.Length(); ++array_idx) {  \
     111                 :     const nsCOMPtr<type> &e = array.ElementAt(array_idx);                  \
     112                 :     if (e)                                                                 \
     113                 :       e->method;                                                           \
     114                 :   }
     115                 : 
     116                 : #endif

Generated by: LCOV version 1.7