Back to index

lightning-sunbird  0.9+nobinonly
TestFactory.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  *   Pierre Phaneuf <pp@ludusdesign.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 <stdio.h>
00040 #include "TestFactory.h"
00041 #include "nsISupports.h"
00042 #include "nsIComponentManager.h"
00043 #include "nsIComponentRegistrar.h"
00044 #include "nsIServiceManager.h"
00045 
00046 NS_DEFINE_CID(kTestFactoryCID, NS_TESTFACTORY_CID);
00047 NS_DEFINE_CID(kTestLoadedFactoryCID, NS_TESTLOADEDFACTORY_CID);
00048 
00049 
00054 class TestClassImpl: public ITestClass {
00055   NS_DECL_ISUPPORTS
00056 public:
00057   TestClassImpl() {
00058   }
00059 
00060   void Test();
00061 };
00062 
00063 NS_IMPL_ISUPPORTS1(TestClassImpl, ITestClass)
00064 
00065 void TestClassImpl::Test() {
00066   printf("hello, world!\n");
00067 }
00068 
00073 class TestFactory: public nsIFactory {
00074   NS_DECL_ISUPPORTS
00075   
00076 public:
00077   TestFactory() {
00078   }
00079 
00080   NS_IMETHOD CreateInstance(nsISupports *aDelegate,
00081                             const nsIID &aIID,
00082                             void **aResult);
00083 
00084   NS_IMETHOD LockFactory(PRBool aLock) { return NS_OK; }
00085 };
00086 
00087 NS_IMPL_ISUPPORTS1(TestFactory, nsIFactory)
00088 
00089 nsresult TestFactory::CreateInstance(nsISupports *aDelegate,
00090                                      const nsIID &aIID,
00091                                      void **aResult) {
00092   if (aDelegate != NULL) {
00093     return NS_ERROR_NO_AGGREGATION;
00094   }
00095 
00096   TestClassImpl *t = new TestClassImpl();
00097   
00098   if (t == NULL) {
00099     return NS_ERROR_OUT_OF_MEMORY;
00100   }
00101   
00102   nsresult res = t->QueryInterface(aIID, aResult);
00103 
00104   if (NS_FAILED(res)) {
00105     *aResult = NULL;
00106     delete t;
00107   }
00108 
00109   return res;
00110 }
00111 
00112 
00113 int main(int argc, char **argv) {
00114   nsresult rv;
00115 
00116   {
00117     nsCOMPtr<nsIServiceManager> servMan;
00118     rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00119     if (NS_FAILED(rv)) return -1;
00120     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00121     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00122     if (registrar)
00123       registrar->RegisterFactory(kTestFactoryCID,
00124                                  nsnull,
00125                                  nsnull,
00126                                  new TestFactory());
00127 
00128     ITestClass *t = NULL;
00129     CallCreateInstance(kTestFactoryCID, &t);
00130 
00131     if (t != NULL) {
00132       t->Test();
00133       t->Release();
00134     } else {
00135       printf("CreateInstance failed\n");
00136     }
00137 
00138     t = NULL;
00139 
00140     CallCreateInstance(kTestLoadedFactoryCID, &t);
00141 
00142     if (t != NULL) {
00143       t->Test();
00144       t->Release();
00145     } else {
00146       printf("Dynamic CreateInstance failed\n");
00147     }
00148   } // this scopes the nsCOMPtrs
00149   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00150   rv = NS_ShutdownXPCOM(nsnull);
00151   NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00152   return 0;
00153 }
00154