1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Pierre Phaneuf <[email protected]>
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
27 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #include <stdio.h>
|
---|
40 | #include "TestFactory.h"
|
---|
41 | #include "nsISupports.h"
|
---|
42 | #include "nsIComponentManager.h"
|
---|
43 | #include "nsIComponentRegistrar.h"
|
---|
44 | #include "nsIServiceManager.h"
|
---|
45 |
|
---|
46 | NS_DEFINE_CID(kTestFactoryCID, NS_TESTFACTORY_CID);
|
---|
47 | NS_DEFINE_CID(kTestLoadedFactoryCID, NS_TESTLOADEDFACTORY_CID);
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * ITestClass implementation
|
---|
52 | */
|
---|
53 |
|
---|
54 | class TestClassImpl: public ITestClass {
|
---|
55 | NS_DECL_ISUPPORTS
|
---|
56 | public:
|
---|
57 | TestClassImpl() {
|
---|
58 | }
|
---|
59 |
|
---|
60 | void Test();
|
---|
61 | };
|
---|
62 |
|
---|
63 | NS_IMPL_ISUPPORTS1(TestClassImpl, ITestClass)
|
---|
64 |
|
---|
65 | void TestClassImpl::Test() {
|
---|
66 | printf("hello, world!\n");
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * TestFactory implementation
|
---|
71 | */
|
---|
72 |
|
---|
73 | class TestFactory: public nsIFactory {
|
---|
74 | NS_DECL_ISUPPORTS
|
---|
75 |
|
---|
76 | public:
|
---|
77 | TestFactory() {
|
---|
78 | }
|
---|
79 |
|
---|
80 | NS_IMETHOD CreateInstance(nsISupports *aDelegate,
|
---|
81 | const nsIID &aIID,
|
---|
82 | void **aResult);
|
---|
83 |
|
---|
84 | NS_IMETHOD LockFactory(PRBool aLock) { return NS_OK; }
|
---|
85 | };
|
---|
86 |
|
---|
87 | NS_IMPL_ISUPPORTS1(TestFactory, nsIFactory)
|
---|
88 |
|
---|
89 | nsresult TestFactory::CreateInstance(nsISupports *aDelegate,
|
---|
90 | const nsIID &aIID,
|
---|
91 | void **aResult) {
|
---|
92 | if (aDelegate != NULL) {
|
---|
93 | return NS_ERROR_NO_AGGREGATION;
|
---|
94 | }
|
---|
95 |
|
---|
96 | TestClassImpl *t = new TestClassImpl();
|
---|
97 |
|
---|
98 | if (t == NULL) {
|
---|
99 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
100 | }
|
---|
101 |
|
---|
102 | nsresult res = t->QueryInterface(aIID, aResult);
|
---|
103 |
|
---|
104 | if (NS_FAILED(res)) {
|
---|
105 | *aResult = NULL;
|
---|
106 | delete t;
|
---|
107 | }
|
---|
108 |
|
---|
109 | return res;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | int main(int argc, char **argv) {
|
---|
114 | nsresult rv;
|
---|
115 |
|
---|
116 | {
|
---|
117 | nsCOMPtr<nsIServiceManager> servMan;
|
---|
118 | rv = NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
---|
119 | if (NS_FAILED(rv)) return -1;
|
---|
120 | nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
---|
121 | NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
---|
122 | if (registrar)
|
---|
123 | registrar->RegisterFactory(kTestFactoryCID,
|
---|
124 | nsnull,
|
---|
125 | nsnull,
|
---|
126 | new TestFactory());
|
---|
127 |
|
---|
128 | ITestClass *t = NULL;
|
---|
129 | nsComponentManager::CreateInstance(kTestFactoryCID,
|
---|
130 | NULL,
|
---|
131 | NS_GET_IID(ITestClass),
|
---|
132 | (void **) &t);
|
---|
133 |
|
---|
134 | if (t != NULL) {
|
---|
135 | t->Test();
|
---|
136 | t->Release();
|
---|
137 | } else {
|
---|
138 | printf("CreateInstance failed\n");
|
---|
139 | }
|
---|
140 |
|
---|
141 | t = NULL;
|
---|
142 |
|
---|
143 | nsComponentManager::CreateInstance(kTestLoadedFactoryCID,
|
---|
144 | NULL,
|
---|
145 | NS_GET_IID(ITestClass),
|
---|
146 | (void **) &t);
|
---|
147 |
|
---|
148 | if (t != NULL) {
|
---|
149 | t->Test();
|
---|
150 | t->Release();
|
---|
151 | } else {
|
---|
152 | printf("Dynamic CreateInstance failed\n");
|
---|
153 | }
|
---|
154 | } // this scopes the nsCOMPtrs
|
---|
155 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
---|
156 | rv = NS_ShutdownXPCOM(nsnull);
|
---|
157 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
---|
158 | return 0;
|
---|
159 | }
|
---|
160 |
|
---|