LCOV - code coverage report
Current view: directory - objdir/dist/include/mozilla - ClearOnShutdown.h (source / functions) Found Hit Coverage
Test: app.info Lines: 18 18 100.0 %
Date: 2012-06-02 Functions: 17 5 29.4 %

       1                 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2                 : /* vim: set sw=2 ts=8 et ft=cpp : */
       3                 : /* ***** BEGIN LICENSE BLOCK *****
       4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       5                 :  *
       6                 :  * The contents of this file are subject to the Mozilla Public License Version
       7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       8                 :  * the License. You may obtain a copy of the License at:
       9                 :  * http://www.mozilla.org/MPL/
      10                 :  *
      11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      13                 :  * for the specific language governing rights and limitations under the
      14                 :  * License.
      15                 :  *
      16                 :  * The Original Code is Mozilla Code.
      17                 :  *
      18                 :  * The Initial Developer of the Original Code is
      19                 :  *   The Mozilla Foundation
      20                 :  * Portions created by the Initial Developer are Copyright (C) 2011
      21                 :  * the Initial Developer. All Rights Reserved.
      22                 :  *
      23                 :  * Contributor(s):
      24                 :  *   Justin Lebar <justin.lebar@gmail.com>
      25                 :  *
      26                 :  * Alternatively, the contents of this file may be used under the terms of
      27                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      28                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      29                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      30                 :  * of those above. If you wish to allow use of your version of this file only
      31                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      32                 :  * use your version of this file under the terms of the MPL, indicate your
      33                 :  * decision by deleting the provisions above and replace them with the notice
      34                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      35                 :  * the provisions above, a recipient may use your version of this file under
      36                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      37                 :  *
      38                 :  * ***** END LICENSE BLOCK ***** */
      39                 : 
      40                 : #ifndef mozilla_ClearOnShutdown_h
      41                 : #define mozilla_ClearOnShutdown_h
      42                 : 
      43                 : #include "mozilla/LinkedList.h"
      44                 : 
      45                 : /*
      46                 :  * This header exports one public method in the mozilla namespace:
      47                 :  *
      48                 :  *   template<class SmartPtr>
      49                 :  *   void ClearOnShutdown(SmartPtr *aPtr)
      50                 :  *
      51                 :  * This function takes a pointer to a smart pointer (i.e., nsCOMPtr<T>*,
      52                 :  * nsRefPtr<T>*, or nsAutoPtr<T>*) and nulls the smart pointer on shutdown.
      53                 :  *
      54                 :  * This is useful if you have a global smart pointer object which you don't
      55                 :  * want to "leak" on shutdown.
      56                 :  *
      57                 :  * There is no way to undo a call to ClearOnShutdown, so you can call it only
      58                 :  * on smart pointers which you know will live until the program shuts down.
      59                 :  */
      60                 : 
      61                 : namespace mozilla {
      62                 : namespace ClearOnShutdown_Internal {
      63                 : 
      64                 : class ShutdownObserver : public LinkedListElement<ShutdownObserver>
      65               4 : {
      66                 : public:
      67                 :   virtual void Shutdown() = 0;
      68                 : };
      69                 : 
      70                 : template<class SmartPtr>
      71                 : class PointerClearer : public ShutdownObserver
      72                 : {
      73                 : public:
      74               4 :   PointerClearer(SmartPtr *aPtr)
      75               4 :     : mPtr(aPtr)
      76               4 :   {}
      77                 : 
      78               4 :   virtual void Shutdown()
      79                 :   {
      80               4 :     if (mPtr) {
      81               4 :       *mPtr = NULL;
      82                 :     }
      83               4 :   }
      84                 : 
      85                 : private:
      86                 :   SmartPtr *mPtr;
      87                 : };
      88                 : 
      89                 : extern bool sHasShutDown;
      90                 : extern LinkedList<ShutdownObserver> sShutdownObservers;
      91                 : 
      92                 : } // namespace ClearOnShutdown_Internal
      93                 : 
      94                 : template<class SmartPtr>
      95               4 : inline void ClearOnShutdown(SmartPtr *aPtr)
      96                 : {
      97                 :   using namespace ClearOnShutdown_Internal;
      98                 : 
      99               4 :   MOZ_ASSERT(!sHasShutDown);
     100               4 :   ShutdownObserver *observer = new PointerClearer<SmartPtr>(aPtr);
     101               4 :   sShutdownObservers.insertBack(observer);
     102               4 : }
     103                 : 
     104                 : // Called when XPCOM is shutting down, after all shutdown notifications have
     105                 : // been sent and after all threads' event loops have been purged.
     106            1419 : inline void KillClearOnShutdown()
     107                 : {
     108                 :   using namespace ClearOnShutdown_Internal;
     109                 : 
     110                 :   ShutdownObserver *observer;
     111            2842 :   while ((observer = sShutdownObservers.popFirst())) {
     112               4 :     observer->Shutdown();
     113                 :     delete observer;
     114                 :   }
     115                 : 
     116            1419 :   sHasShutDown = true;
     117            1419 : }
     118                 : 
     119                 : } // namespace mozilla
     120                 : 
     121                 : #endif

Generated by: LCOV version 1.7