pjs/xpcom/tests/dynamic/TestDynamic.cpp

154 строки
4.1 KiB
C++
Исходник Обычный вид История

1998-03-28 05:44:41 +03:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include <iostream.h>
#include "pratom.h"
#include "TestFactory.h"
#include "nsCom.h"
#include "nsISupports.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
1998-03-28 05:44:41 +03:00
NS_DEFINE_IID(kFactoryIID, NS_IFACTORY_IID);
NS_DEFINE_CID(kTestLoadedFactoryCID, NS_TESTLOADEDFACTORY_CID);
NS_DEFINE_IID(kTestClassIID, NS_ITESTCLASS_IID);
/**
* ITestClass implementation
*/
class TestDynamicClassImpl: public ITestClass {
NS_DECL_ISUPPORTS
TestDynamicClassImpl() {
NS_INIT_REFCNT();
}
void Test();
};
NS_IMPL_ISUPPORTS(TestDynamicClassImpl, kTestClassIID);
void TestDynamicClassImpl::Test() {
cout << "hello, dynamic world!\n";
}
/**
* TestFactory implementation
*/
1998-05-23 01:56:30 +04:00
static PRInt32 g_FactoryCount = 0;
static PRInt32 g_LockCount = 0;
1998-03-28 05:44:41 +03:00
class TestDynamicFactory: public nsIFactory {
NS_DECL_ISUPPORTS
TestDynamicFactory() {
NS_INIT_REFCNT();
1998-05-23 01:56:30 +04:00
PR_AtomicIncrement(&g_FactoryCount);
1998-03-28 05:44:41 +03:00
}
~TestDynamicFactory() {
1998-05-23 01:56:30 +04:00
PR_AtomicDecrement(&g_FactoryCount);
1998-03-28 05:44:41 +03:00
}
NS_IMETHOD CreateInstance(nsISupports *aDelegate,
const nsIID &aIID,
void **aResult);
1998-05-23 01:56:30 +04:00
NS_IMETHOD LockFactory(PRBool aLock) {
1998-03-28 05:44:41 +03:00
if (aLock) {
PR_AtomicIncrement(&g_LockCount);
} else {
PR_AtomicDecrement(&g_LockCount);
}
1998-05-23 01:56:30 +04:00
return NS_OK;
1998-03-28 05:44:41 +03:00
};
};
NS_IMPL_ISUPPORTS(TestDynamicFactory, kFactoryIID);
nsresult TestDynamicFactory::CreateInstance(nsISupports *aDelegate,
const nsIID &aIID,
void **aResult)
{
if (aDelegate != NULL) {
return NS_ERROR_NO_AGGREGATION;
}
TestDynamicClassImpl *t = new TestDynamicClassImpl();
if (t == NULL) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult res = t->QueryInterface(aIID, aResult);
if (NS_FAILED(res)) {
*aResult = NULL;
delete t;
}
return res;
}
extern "C" NS_EXPORT nsresult NSGetFactory(nsISupports* aServMgr,
const nsCID &aClass,
const char *aClassName,
const char *aProgID,
nsIFactory **aFactory)
{
1998-03-28 05:44:41 +03:00
if (aFactory == NULL) {
return NS_ERROR_NULL_POINTER;
}
if (aClass.Equals(kTestLoadedFactoryCID)) {
1998-03-28 05:44:41 +03:00
TestDynamicFactory *factory = new TestDynamicFactory();
nsresult res = factory->QueryInterface(kFactoryIID, (void **) aFactory);
if (NS_FAILED(res)) {
*aFactory = NULL;
delete factory;
}
return res;
}
return NS_NOINTERFACE;
}
extern "C" NS_EXPORT PRBool NSCanUnload(nsISupports* aServMgr) {
1998-05-23 01:56:30 +04:00
return PRBool(g_FactoryCount == 0 && g_LockCount == 0);
1998-03-28 05:44:41 +03:00
}
extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* aServMgr , const char *path)
1998-03-28 05:44:41 +03:00
{
nsresult rv;
nsService<nsIComponentManager> compMgr(aServMgr, kComponentManagerCID, &rv);
if (NS_FAILED(rv)) return rv;
return compMgr->RegisterComponent(kTestLoadedFactoryCID, NULL, NULL, path,
PR_TRUE, PR_TRUE);
1998-03-28 05:44:41 +03:00
}
extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, const char *path)
1998-03-28 05:44:41 +03:00
{
nsresult rv;
nsService<nsIComponentManager> compMgr(aServMgr, kComponentManagerCID, &rv);
if (NS_FAILED(rv)) return rv;
return compMgr->UnregisterFactory(kTestLoadedFactoryCID, path);
1998-03-28 05:44:41 +03:00
}