Back to index

lightning-sunbird  0.9+nobinonly
xpctest_noisy.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   John Bandhauer <jband@netscape.com>
00026  *   Pierre Phaneuf <pp@ludusdesign.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either of the GNU General Public License Version 2 or later (the "GPL"),
00030  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 /* implement nsIXPCTestNoisy for testing. */
00043 
00044 #include "xpctest_private.h"
00045 
00046 class xpctestNoisy : public nsIXPCTestNoisy
00047 {
00048 public:
00049     NS_DECL_ISUPPORTS
00050     NS_DECL_NSIXPCTESTNOISY
00051 
00052     xpctestNoisy();
00053     virtual ~xpctestNoisy();
00054 private:
00055     static int sID;
00056     static int sCount;
00057     int        mID;
00058 };
00059 
00060 int xpctestNoisy::sID = 0;
00061 int xpctestNoisy::sCount = 0;
00062 
00063 
00064 NS_IMETHODIMP_(nsrefcnt) xpctestNoisy::AddRef(void)
00065 {
00066   NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
00067   ++mRefCnt;
00068   NS_LOG_ADDREF(this, mRefCnt, "xpctestNoisy", sizeof(*this));
00069   printf("Noisy %d - incremented refcount to %d\n", mID, mRefCnt.get());
00070   return mRefCnt;
00071 }
00072 
00073 NS_IMETHODIMP_(nsrefcnt) xpctestNoisy::Release(void)
00074 {
00075   NS_PRECONDITION(0 != mRefCnt, "dup release");
00076   --mRefCnt;
00077   printf("Noisy %d - decremented refcount to %d\n", mID, mRefCnt.get());
00078   NS_LOG_RELEASE(this, mRefCnt, "xpctestNoisy");
00079   if (mRefCnt == 0) {
00080     NS_DELETEXPCOM(this);
00081     return 0;
00082   }
00083   return mRefCnt;
00084 }
00085 
00086 NS_IMETHODIMP
00087 xpctestNoisy::QueryInterface(REFNSIID iid, void** result)
00088 {
00089     if (! result)
00090         return NS_ERROR_NULL_POINTER;
00091 
00092     if (iid.Equals(NS_GET_IID(nsIXPCTestNoisy)) ||
00093         iid.Equals(NS_GET_IID(nsISupports))) {
00094         *result = NS_STATIC_CAST(nsIXPCTestNoisy*, this);
00095         printf("Noisy %d - QueryInterface called and succeeding\n", mID);
00096         NS_ADDREF(this);
00097         return NS_OK;
00098     }
00099     else {
00100         *result = nsnull;
00101         printf("Noisy %d - QueryInterface for interface I don't do\n", mID);
00102         return NS_NOINTERFACE;
00103     }
00104 }
00105 
00106 xpctestNoisy::xpctestNoisy()
00107     : mID(++sID)
00108 {
00109     sCount++;
00110     printf("Noisy %d - Created, %d total\n", mID, sCount);
00111     NS_ADDREF_THIS();
00112 }
00113 
00114 xpctestNoisy::~xpctestNoisy()
00115 {
00116     sCount--;
00117     printf("Noisy %d - Destroyed, %d total\n", mID, sCount);
00118 }
00119 
00120 NS_IMETHODIMP xpctestNoisy::Squawk()
00121 {
00122     printf("Noisy %d - Squawk called\n", mID);
00123     return NS_OK;
00124 }
00125 
00126 /***************************************************************************/
00127 
00128 // static
00129 NS_IMETHODIMP
00130 xpctest::ConstructNoisy(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00131 {
00132     nsresult rv;
00133     NS_ASSERTION(aOuter == nsnull, "no aggregation");
00134     xpctestNoisy* obj = new xpctestNoisy();
00135 
00136     if(obj)
00137     {
00138         rv = obj->QueryInterface(aIID, aResult);
00139         NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
00140         NS_RELEASE(obj);
00141     }
00142     else
00143     {
00144         *aResult = nsnull;
00145         rv = NS_ERROR_OUT_OF_MEMORY;
00146     }
00147 
00148     return rv;
00149 }
00150 /***************************************************************************/
00151 
00152 
00153 
00154