home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / xpcom / tests / dynamic / TestDynamic.cpp < prev   
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.7 KB  |  144 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. #include <iostream.h>
  20. #include "pratom.h"
  21. #include "TestFactory.h"
  22. #include "nsCom.h"
  23. #include "nsISupports.h"
  24. #include "nsRepository.h"
  25.  
  26. NS_DEFINE_IID(kFactoryIID, NS_IFACTORY_IID);
  27. NS_DEFINE_CID(kTestLoadedFactoryCID, NS_TESTLOADEDFACTORY_CID);
  28. NS_DEFINE_IID(kTestClassIID, NS_ITESTCLASS_IID);
  29.  
  30. static PRInt32 g_InstanceCount = 0;
  31. static PRInt32 g_LockCount = 0;
  32.  
  33. /**
  34.  * ITestClass implementation
  35.  */
  36.  
  37. class TestDynamicClassImpl: public ITestClass {
  38.   NS_DECL_ISUPPORTS
  39.  
  40.   TestDynamicClassImpl() {
  41.     NS_INIT_REFCNT();
  42.     PR_AtomicIncrement(&g_InstanceCount);
  43.   }
  44.  
  45.   ~TestDynamicClassImpl() {
  46.     PR_AtomicDecrement(&g_InstanceCount);
  47.   }
  48.  
  49.   void Test();
  50. };
  51.  
  52. NS_IMPL_ISUPPORTS(TestDynamicClassImpl, kTestClassIID);
  53.  
  54. void TestDynamicClassImpl::Test() {
  55.   cout << "hello, dynamic world!\n";
  56. }
  57.  
  58. /**
  59.  * TestFactory implementation
  60.  */
  61.  
  62. class TestDynamicFactory: public nsIFactory {
  63.   NS_DECL_ISUPPORTS
  64.   
  65.   TestDynamicFactory() {
  66.     NS_INIT_REFCNT();
  67.     PR_AtomicIncrement(&g_InstanceCount);
  68.   }
  69.  
  70.   ~TestDynamicFactory() {
  71.     PR_AtomicDecrement(&g_InstanceCount);
  72.   }
  73.  
  74.   NS_IMETHOD CreateInstance(nsISupports *aDelegate,
  75.                             const nsIID &aIID,
  76.                             void **aResult);
  77.  
  78.   NS_IMETHOD_(void) LockFactory(PRBool aLock) {
  79.     if (aLock) {
  80.       PR_AtomicIncrement(&g_LockCount);
  81.     } else {
  82.       PR_AtomicDecrement(&g_LockCount);
  83.     }
  84.   };
  85. };
  86.  
  87. NS_IMPL_ISUPPORTS(TestDynamicFactory, kFactoryIID);
  88.  
  89. nsresult TestDynamicFactory::CreateInstance(nsISupports *aDelegate,
  90.                                             const nsIID &aIID,
  91.                                             void **aResult)
  92. {
  93.   if (aDelegate != NULL) {
  94.     return NS_ERROR_NO_AGGREGATION;
  95.   }
  96.  
  97.   TestDynamicClassImpl *t = new TestDynamicClassImpl();
  98.   
  99.   if (t == NULL) {
  100.     return NS_ERROR_OUT_OF_MEMORY;
  101.   }
  102.   
  103.   nsresult res = t->QueryInterface(aIID, aResult);
  104.   
  105.   if (NS_FAILED(res)) {
  106.     *aResult = NULL;
  107.     delete t;
  108.   }
  109.  
  110.   return res;
  111. }
  112.  
  113. extern "C" NS_EXPORT nsresult NSGetFactory(const nsCID &aCID,
  114.                                            nsIFactory **aFactory) {
  115.   if (aFactory == NULL) {
  116.     return NS_ERROR_NULL_POINTER;
  117.   }
  118.   if (aCID.Equals(kTestLoadedFactoryCID)) {
  119.     TestDynamicFactory *factory = new TestDynamicFactory();
  120.     nsresult res = factory->QueryInterface(kFactoryIID, (void **) aFactory);
  121.     if (NS_FAILED(res)) {
  122.       *aFactory = NULL;
  123.       delete factory;
  124.     }
  125.     return res;
  126.   }
  127.   return NS_NOINTERFACE;
  128. }
  129.  
  130. extern "C" NS_EXPORT PRBool NSCanUnload() {
  131.   return PRBool(g_InstanceCount == 0 && g_LockCount == 0);
  132. }
  133.  
  134. extern "C" NS_EXPORT nsresult NSRegisterSelf(const char *path)
  135. {
  136.   return NSRepository::RegisterFactory(kTestLoadedFactoryCID, path, 
  137.                                        PR_TRUE, PR_TRUE);
  138. }
  139.  
  140. extern "C" NS_EXPORT nsresult NSUnregisterSelf(const char *path)
  141. {
  142.   return NSRepository::UnregisterFactory(kTestLoadedFactoryCID, path);
  143. }
  144.