Back to index

lightning-sunbird  0.9+nobinonly
nsCOMPtr.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Scott Collins <scc@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 
00041 nsresult
00042 nsQueryInterface::operator()( const nsIID& aIID, void** answer ) const
00043        {
00044               nsresult status;
00045               if ( mRawPtr )
00046                      {
00047                             status = mRawPtr->QueryInterface(aIID, answer);
00048 #ifdef NSCAP_FEATURE_TEST_NONNULL_QUERY_SUCCEEDS
00049                             NS_WARN_IF_FALSE(NS_SUCCEEDED(status), "interface not found---were you expecting that?");
00050 #endif
00051                      }
00052               else
00053                      status = NS_ERROR_NULL_POINTER;
00054               
00055               return status;
00056        }
00057 
00058 nsresult
00059 nsQueryInterfaceWithError::operator()( const nsIID& aIID, void** answer ) const
00060        {
00061               nsresult status;
00062               if ( mRawPtr )
00063                      {
00064                             status = mRawPtr->QueryInterface(aIID, answer);
00065 #ifdef NSCAP_FEATURE_TEST_NONNULL_QUERY_SUCCEEDS
00066                             NS_WARN_IF_FALSE(NS_SUCCEEDED(status), "interface not found---were you expecting that?");
00067 #endif
00068                      }
00069               else
00070                      status = NS_ERROR_NULL_POINTER;
00071               
00072               if ( mErrorPtr )
00073                      *mErrorPtr = status;
00074               return status;
00075        }
00076 
00077 nsCOMPtr_base::~nsCOMPtr_base()
00078        {
00079          NSCAP_LOG_RELEASE(this, mRawPtr);
00080               if ( mRawPtr )
00081                      NSCAP_RELEASE(this, mRawPtr);
00082        }
00083 
00084 void
00085 nsCOMPtr_base::assign_with_AddRef( nsISupports* rawPtr )
00086        {
00087     if ( rawPtr )
00088        NSCAP_ADDREF(this, rawPtr);
00089     assign_assuming_AddRef(rawPtr);
00090        }
00091 
00092 void
00093 nsCOMPtr_base::assign_from_qi( const nsQueryInterface qi, const nsIID& iid )
00094        {
00095               nsISupports* newRawPtr;
00096               if ( NS_FAILED( qi(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00097                      newRawPtr = 0;
00098     assign_assuming_AddRef(newRawPtr);
00099        }
00100 
00101 void
00102 nsCOMPtr_base::assign_from_qi_with_error( const nsQueryInterfaceWithError& qi, const nsIID& iid )
00103        {
00104               nsISupports* newRawPtr;
00105               if ( NS_FAILED( qi(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00106                      newRawPtr = 0;
00107     assign_assuming_AddRef(newRawPtr);
00108        }
00109 
00110 void
00111 nsCOMPtr_base::assign_from_gs_cid( const nsGetServiceByCID gs, const nsIID& iid )
00112        {
00113               nsISupports* newRawPtr;
00114               if ( NS_FAILED( gs(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00115                      newRawPtr = 0;
00116     assign_assuming_AddRef(newRawPtr);
00117        }
00118 
00119 void
00120 nsCOMPtr_base::assign_from_gs_cid_with_error( const nsGetServiceByCIDWithError& gs, const nsIID& iid )
00121        {
00122               nsISupports* newRawPtr;
00123               if ( NS_FAILED( gs(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00124                      newRawPtr = 0;
00125     assign_assuming_AddRef(newRawPtr);
00126        }
00127 
00128 void
00129 nsCOMPtr_base::assign_from_gs_contractid( const nsGetServiceByContractID gs, const nsIID& iid )
00130        {
00131               nsISupports* newRawPtr;
00132               if ( NS_FAILED( gs(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00133                      newRawPtr = 0;
00134     assign_assuming_AddRef(newRawPtr);
00135        }
00136 
00137 void
00138 nsCOMPtr_base::assign_from_gs_contractid_with_error( const nsGetServiceByContractIDWithError& gs, const nsIID& iid )
00139        {
00140               nsISupports* newRawPtr;
00141               if ( NS_FAILED( gs(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00142                      newRawPtr = 0;
00143     assign_assuming_AddRef(newRawPtr);
00144        }
00145 
00146 void
00147 nsCOMPtr_base::assign_from_helper( const nsCOMPtr_helper& helper, const nsIID& iid )
00148        {
00149               nsISupports* newRawPtr;
00150               if ( NS_FAILED( helper(iid, NS_REINTERPRET_CAST(void**, &newRawPtr)) ) )
00151                      newRawPtr = 0;
00152     assign_assuming_AddRef(newRawPtr);
00153        }
00154 
00155 void**
00156 nsCOMPtr_base::begin_assignment()
00157   {
00158     assign_assuming_AddRef(0);
00159     return NS_REINTERPRET_CAST(void**, &mRawPtr);
00160   }