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