home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / wincom / dllcom / src / dllobj.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.8 KB  |  149 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #include "dllcom.h"
  21.  
  22. //  ALL COM IMPLEMENTED OBJECTS MUST DERIVE FROM THIS
  23. //      CLASS (or do what it does).
  24.  
  25. //  Perform simple minimal maintenance such that
  26. //      the remainder of the library can perform
  27. //      more powerful house keeping.
  28. //  All we do here is inform the instance data
  29. //      (CComDll) that there is yet another
  30. //      object (more instance data) alive and
  31. //      well.  This way, the CComDll class won't
  32. //      remove itself from memory until there are
  33. //      no more objects hanging around.
  34.  
  35. CComClass::CComClass(IUnknown *pAggregate)
  36. {
  37.     m_ulCount = 0;
  38.     m_IUnknown.m_pObject = this;
  39.     m_pAggregate = NULL;
  40.     m_pAggregate = pAggregate;
  41.  
  42.     //  Need to add a reference to ourselves.
  43.     //  Use IUnknown to avoid aggregate.
  44.     m_IUnknown.AddRef();
  45. }
  46.  
  47. CComClass::~CComClass()
  48. {
  49.     DLL_ASSERT(m_ulCount == 0);
  50.     m_IUnknown.m_pObject = NULL;
  51.     m_pAggregate = NULL;
  52. }
  53.  
  54. HRESULT CComClass::ObjectQueryInterface(REFIID iid, void **ppObj, BOOL bIUnknown)
  55. {
  56.     //  Determine if we should aggregate the call.
  57.     if(m_pAggregate && !bIUnknown)  {
  58.         return(m_pAggregate->QueryInterface(iid, ppObj));
  59.     }
  60.     else    {
  61.         return(CustomQueryInterface(iid, ppObj));
  62.     }
  63. }
  64.  
  65. HRESULT CComClass::CustomQueryInterface(REFIID iid, void **ppObj)
  66. {
  67.     HRESULT hRetval = ResultFromScode(E_NOINTERFACE);
  68.     *ppObj = NULL;
  69.  
  70.     //  This is virtual.
  71.     //  Just check to see if we should return our IUnknown
  72.     //      implementation.
  73.     if(IsEqualIID(iid, IID_IUnknown))  {
  74.         m_IUnknown.AddRef();
  75.         *ppObj = (void *)&m_IUnknown;
  76.         hRetval = ResultFromScode(S_OK);
  77.     }
  78.  
  79.     return(hRetval);
  80. }
  81.  
  82. DWORD CComClass::ObjectAddRef(BOOL bIUnknown)
  83. {
  84.     DWORD dwRetval;
  85.  
  86.     if(m_pAggregate && !bIUnknown)    {
  87.         dwRetval = m_pAggregate->AddRef();
  88.     }
  89.     else    {
  90.         dwRetval = CustomAddRef();
  91.     }
  92.  
  93.     return(dwRetval);
  94. }
  95.  
  96. DWORD CComClass::CustomAddRef()
  97. {
  98.     m_ulCount++;
  99.     return(m_ulCount);
  100. }
  101.  
  102. DWORD CComClass::ObjectRelease(BOOL bIUnknown)
  103. {
  104.     DWORD dwRetval;
  105.  
  106.     if(m_pAggregate && !bIUnknown)    {
  107.         dwRetval = m_pAggregate->Release();
  108.     }
  109.     else    {
  110.         dwRetval = CustomRelease();
  111.     }
  112.  
  113.     return(dwRetval);
  114. }
  115.  
  116. DWORD CComClass::CustomRelease()
  117. {
  118.     DWORD dwRetval;
  119.     
  120.     m_ulCount--;
  121.     dwRetval = m_ulCount;
  122.  
  123.     if(!dwRetval)   {
  124.         delete this;
  125.     }
  126.  
  127.     return(dwRetval);
  128. }
  129.  
  130. STDMETHODIMP CComClass::CImplUnknown::QueryInterface(REFIID iid, void **ppObj)
  131. {
  132.     //  Avoid aggregation in actual IUnknown implementation.
  133.     HRESULT hRetval = m_pObject->ObjectQueryInterface(iid, ppObj, TRUE);
  134.     return(hRetval);
  135. }
  136.  
  137. STDMETHODIMP_(DWORD) CComClass::CImplUnknown::AddRef()
  138. {
  139.     //  Avoid aggregation in actual IUnknown implementation.
  140.     DWORD dwRetval = m_pObject->ObjectAddRef(TRUE);
  141.     return(dwRetval);
  142. }
  143.  
  144. STDMETHODIMP_(DWORD) CComClass::CImplUnknown::Release()
  145. {
  146.     //  Avoid aggregation in actual IUnknown implementation.
  147.     DWORD dwRetval = m_pObject->ObjectRelease(TRUE);
  148.     return(dwRetval);
  149. }