implemented wrapper funtions to make GetObserverList private.

This commit is contained in:
neeti%netscape.com 1999-04-20 14:50:47 +00:00
parent 485a627711
commit 7bdbb69f8b
8 changed files with 234 additions and 70 deletions

View File

@ -38,7 +38,9 @@ class nsIObserverService : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IOBSERVERSERVICE_IID; return iid; }
NS_IMETHOD GetObserverList(nsString* aTopic, nsIObserverList** anObserverList) = 0;
NS_IMETHOD AddObserver(nsIObserver** anObserver, nsString* aTopic) = 0;
NS_IMETHOD RemoveObserver(nsIObserver** anObserver, nsString* aTopic) = 0;
NS_IMETHOD EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic) = 0;
};
extern NS_BASE nsresult NS_NewObserverService(nsIObserverService** anObserverService);

View File

@ -18,12 +18,8 @@
#define NS_IMPL_IDS
#include "pratom.h"
//#include "nsIFactory.h"
//#include "nsIServiceManager.h"
#include "nsRepository.h"
#include "nsIObserverList.h"
#include "nsObserverList.h"
#include "nsHashtable.h"
#include "nsString.h"
#include "nsAutoLock.h"
@ -73,6 +69,7 @@ nsObserverList::~nsObserverList(void)
nsresult nsObserverList::AddObserver(nsIObserver** anObserver)
{
nsresult rv;
PRBool inserted;
NS_AUTOLOCK(mLock);
@ -87,14 +84,17 @@ nsresult nsObserverList::AddObserver(nsIObserver** anObserver)
}
if(*anObserver) {
mObserverList->AppendElement(*anObserver);
inserted = mObserverList->AppendElement(*anObserver);
return inserted ? NS_OK : NS_ERROR_FAILURE;
}
return NS_OK;
return NS_ERROR_FAILURE;
}
nsresult nsObserverList::RemoveObserver(nsIObserver** anObserver)
{
PRBool removed;
NS_AUTOLOCK(mLock);
if (anObserver == NULL)
@ -103,10 +103,11 @@ nsresult nsObserverList::RemoveObserver(nsIObserver** anObserver)
}
if(*anObserver) {
mObserverList->RemoveElement(*anObserver);
removed = mObserverList->RemoveElement(*anObserver);
return removed ? NS_OK : NS_ERROR_FAILURE;
}
return NS_OK;
return NS_ERROR_FAILURE;
}

View File

@ -26,7 +26,6 @@
#include "nsIObserverList.h"
#include "nsObserverList.h"
#include "nsHashtable.h"
#include "nsVoidArray.h"
#include "nsString.h"
static NS_DEFINE_IID(kIObserverServiceIID, NS_IOBSERVERSERVICE_IID);
@ -37,9 +36,13 @@ static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
class nsObserverService : public nsIObserverService {
public:
static nsresult GetObserverService(nsIObserverService** anObserverService);
NS_IMETHOD GetObserverList(nsString* aTopic, nsIObserverList** anObserverList);
static nsresult GetObserverService(nsIObserverService** anObserverService);
NS_IMETHOD AddObserver(nsIObserver** anObserver, nsString* aTopic);
NS_IMETHOD RemoveObserver(nsIObserver** anObserver, nsString* aTopic);
NS_IMETHOD EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic);
nsObserverService();
~nsObserverService(void);
@ -48,6 +51,8 @@ public:
private:
NS_IMETHOD GetObserverList(nsIObserverList** anObserverList, nsString* aTopic);
nsHashtable *mObserverTopicTable;
@ -95,7 +100,7 @@ nsresult nsObserverService::GetObserverService(nsIObserverService** anObserverSe
return NS_OK;
}
nsresult nsObserverService::GetObserverList(nsString* aTopic, nsIObserverList** anObserverList)
nsresult nsObserverService::GetObserverList(nsIObserverList** anObserverList, nsString* aTopic)
{
if (anObserverList == NULL)
{
@ -142,6 +147,80 @@ nsresult nsObserverService::GetObserverList(nsString* aTopic, nsIObserverList**
return NS_OK;
}
nsresult nsObserverService::AddObserver(nsIObserver** anObserver, nsString* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
if (anObserver == NULL)
{
return NS_ERROR_NULL_POINTER;
}
if (aTopic == NULL)
{
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->AddObserver(anObserver);
}
return NS_ERROR_FAILURE;
}
nsresult nsObserverService::RemoveObserver(nsIObserver** anObserver, nsString* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
if (anObserver == NULL)
{
return NS_ERROR_NULL_POINTER;
}
if (aTopic == NULL)
{
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->RemoveObserver(anObserver);
}
return NS_ERROR_FAILURE;
}
nsresult nsObserverService::EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
if (anEnumerator == NULL)
{
return NS_ERROR_NULL_POINTER;
}
if (aTopic == NULL)
{
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->EnumerateObserverList(anEnumerator);
}
return NS_ERROR_FAILURE;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -2,7 +2,6 @@
#include "nsISupports.h"
#include "nsRepository.h"
#include "nsIObserverService.h"
#include "nsIObserverList.h"
#include "nsIObserver.h"
#include "nsString.h"
@ -21,10 +20,10 @@ int main(int argc, char *argv[])
nsIObserverService *anObserverService = NULL;
nsresult rv;
/* nsComponentManager::RegisterComponent(kObserverServiceCID,
nsComponentManager::RegisterComponent(kObserverServiceCID,
"ObserverService",
NS_OBSERVERSERVICE_PROGID,
BASE_DLL,PR_TRUE, PR_TRUE);*/
BASE_DLL,PR_FALSE, PR_FALSE);
nsresult res = nsRepository::CreateInstance(kObserverServiceCID,
NULL,
@ -34,9 +33,8 @@ int main(int argc, char *argv[])
if (res == NS_OK) {
nsString aTopic("tagobserver");
nsString aTopic("htmlparser");
nsIObserverList* anObserverList;
nsIObserver *anObserver;
nsIObserver *aObserver = nsnull;
nsIObserver *bObserver = nsnull;
@ -47,26 +45,22 @@ int main(int argc, char *argv[])
(void **) &anObserver);
rv = anObserverService->GetObserverList(&aTopic, &anObserverList);
if (NS_FAILED(rv)) return rv;
NS_NewObserver(&aObserver);
rv = NS_NewObserver(&aObserver);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
anObserverList->AddObserver(&aObserver);
}
NS_NewObserver(&bObserver);
if (anObserverList) {
anObserverList->AddObserver(&bObserver);
}
rv = anObserverService->AddObserver(&aObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
rv = NS_NewObserver(&bObserver);
if (NS_FAILED(rv)) return rv;
rv = anObserverService->AddObserver(&bObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
nsIEnumerator* e;
rv = anObserverList->EnumerateObserverList(&e);
rv = anObserverService->EnumerateObserverList(&e, &aTopic);
if (NS_FAILED(rv)) return rv;
nsISupports *inst;
@ -75,9 +69,15 @@ int main(int argc, char *argv[])
if (NS_SUCCEEDED(rv)) {
rv = inst->QueryInterface(nsIObserver::GetIID(), (void**)&anObserver);
}
anObserver->Notify(nsnull);
rv = anObserver->Notify(nsnull);
}
rv = anObserverService->RemoveObserver(&aObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
rv = anObserverService->RemoveObserver(&bObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
}

View File

@ -38,7 +38,9 @@ class nsIObserverService : public nsISupports {
public:
static const nsIID& GetIID() { static nsIID iid = NS_IOBSERVERSERVICE_IID; return iid; }
NS_IMETHOD GetObserverList(nsString* aTopic, nsIObserverList** anObserverList) = 0;
NS_IMETHOD AddObserver(nsIObserver** anObserver, nsString* aTopic) = 0;
NS_IMETHOD RemoveObserver(nsIObserver** anObserver, nsString* aTopic) = 0;
NS_IMETHOD EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic) = 0;
};
extern NS_BASE nsresult NS_NewObserverService(nsIObserverService** anObserverService);

View File

@ -18,12 +18,8 @@
#define NS_IMPL_IDS
#include "pratom.h"
//#include "nsIFactory.h"
//#include "nsIServiceManager.h"
#include "nsRepository.h"
#include "nsIObserverList.h"
#include "nsObserverList.h"
#include "nsHashtable.h"
#include "nsString.h"
#include "nsAutoLock.h"
@ -73,6 +69,7 @@ nsObserverList::~nsObserverList(void)
nsresult nsObserverList::AddObserver(nsIObserver** anObserver)
{
nsresult rv;
PRBool inserted;
NS_AUTOLOCK(mLock);
@ -87,14 +84,17 @@ nsresult nsObserverList::AddObserver(nsIObserver** anObserver)
}
if(*anObserver) {
mObserverList->AppendElement(*anObserver);
inserted = mObserverList->AppendElement(*anObserver);
return inserted ? NS_OK : NS_ERROR_FAILURE;
}
return NS_OK;
return NS_ERROR_FAILURE;
}
nsresult nsObserverList::RemoveObserver(nsIObserver** anObserver)
{
PRBool removed;
NS_AUTOLOCK(mLock);
if (anObserver == NULL)
@ -103,10 +103,11 @@ nsresult nsObserverList::RemoveObserver(nsIObserver** anObserver)
}
if(*anObserver) {
mObserverList->RemoveElement(*anObserver);
removed = mObserverList->RemoveElement(*anObserver);
return removed ? NS_OK : NS_ERROR_FAILURE;
}
return NS_OK;
return NS_ERROR_FAILURE;
}

View File

@ -26,7 +26,6 @@
#include "nsIObserverList.h"
#include "nsObserverList.h"
#include "nsHashtable.h"
#include "nsVoidArray.h"
#include "nsString.h"
static NS_DEFINE_IID(kIObserverServiceIID, NS_IOBSERVERSERVICE_IID);
@ -37,9 +36,13 @@ static NS_DEFINE_IID(kObserverServiceCID, NS_OBSERVERSERVICE_CID);
class nsObserverService : public nsIObserverService {
public:
static nsresult GetObserverService(nsIObserverService** anObserverService);
NS_IMETHOD GetObserverList(nsString* aTopic, nsIObserverList** anObserverList);
static nsresult GetObserverService(nsIObserverService** anObserverService);
NS_IMETHOD AddObserver(nsIObserver** anObserver, nsString* aTopic);
NS_IMETHOD RemoveObserver(nsIObserver** anObserver, nsString* aTopic);
NS_IMETHOD EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic);
nsObserverService();
~nsObserverService(void);
@ -48,6 +51,8 @@ public:
private:
NS_IMETHOD GetObserverList(nsIObserverList** anObserverList, nsString* aTopic);
nsHashtable *mObserverTopicTable;
@ -95,7 +100,7 @@ nsresult nsObserverService::GetObserverService(nsIObserverService** anObserverSe
return NS_OK;
}
nsresult nsObserverService::GetObserverList(nsString* aTopic, nsIObserverList** anObserverList)
nsresult nsObserverService::GetObserverList(nsIObserverList** anObserverList, nsString* aTopic)
{
if (anObserverList == NULL)
{
@ -142,6 +147,80 @@ nsresult nsObserverService::GetObserverList(nsString* aTopic, nsIObserverList**
return NS_OK;
}
nsresult nsObserverService::AddObserver(nsIObserver** anObserver, nsString* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
if (anObserver == NULL)
{
return NS_ERROR_NULL_POINTER;
}
if (aTopic == NULL)
{
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->AddObserver(anObserver);
}
return NS_ERROR_FAILURE;
}
nsresult nsObserverService::RemoveObserver(nsIObserver** anObserver, nsString* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
if (anObserver == NULL)
{
return NS_ERROR_NULL_POINTER;
}
if (aTopic == NULL)
{
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->RemoveObserver(anObserver);
}
return NS_ERROR_FAILURE;
}
nsresult nsObserverService::EnumerateObserverList(nsIEnumerator** anEnumerator, nsString* aTopic)
{
nsIObserverList* anObserverList;
nsresult rv;
if (anEnumerator == NULL)
{
return NS_ERROR_NULL_POINTER;
}
if (aTopic == NULL)
{
return NS_ERROR_NULL_POINTER;
}
rv = GetObserverList(&anObserverList, aTopic);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
return anObserverList->EnumerateObserverList(anEnumerator);
}
return NS_ERROR_FAILURE;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -2,7 +2,6 @@
#include "nsISupports.h"
#include "nsRepository.h"
#include "nsIObserverService.h"
#include "nsIObserverList.h"
#include "nsIObserver.h"
#include "nsString.h"
@ -21,10 +20,10 @@ int main(int argc, char *argv[])
nsIObserverService *anObserverService = NULL;
nsresult rv;
/* nsComponentManager::RegisterComponent(kObserverServiceCID,
nsComponentManager::RegisterComponent(kObserverServiceCID,
"ObserverService",
NS_OBSERVERSERVICE_PROGID,
BASE_DLL,PR_TRUE, PR_TRUE);*/
BASE_DLL,PR_FALSE, PR_FALSE);
nsresult res = nsRepository::CreateInstance(kObserverServiceCID,
NULL,
@ -34,9 +33,8 @@ int main(int argc, char *argv[])
if (res == NS_OK) {
nsString aTopic("tagobserver");
nsString aTopic("htmlparser");
nsIObserverList* anObserverList;
nsIObserver *anObserver;
nsIObserver *aObserver = nsnull;
nsIObserver *bObserver = nsnull;
@ -47,26 +45,22 @@ int main(int argc, char *argv[])
(void **) &anObserver);
rv = anObserverService->GetObserverList(&aTopic, &anObserverList);
if (NS_FAILED(rv)) return rv;
NS_NewObserver(&aObserver);
rv = NS_NewObserver(&aObserver);
if (NS_FAILED(rv)) return rv;
if (anObserverList) {
anObserverList->AddObserver(&aObserver);
}
NS_NewObserver(&bObserver);
if (anObserverList) {
anObserverList->AddObserver(&bObserver);
}
rv = anObserverService->AddObserver(&aObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
rv = NS_NewObserver(&bObserver);
if (NS_FAILED(rv)) return rv;
rv = anObserverService->AddObserver(&bObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
nsIEnumerator* e;
rv = anObserverList->EnumerateObserverList(&e);
rv = anObserverService->EnumerateObserverList(&e, &aTopic);
if (NS_FAILED(rv)) return rv;
nsISupports *inst;
@ -75,9 +69,15 @@ int main(int argc, char *argv[])
if (NS_SUCCEEDED(rv)) {
rv = inst->QueryInterface(nsIObserver::GetIID(), (void**)&anObserver);
}
anObserver->Notify(nsnull);
rv = anObserver->Notify(nsnull);
}
rv = anObserverService->RemoveObserver(&aObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
rv = anObserverService->RemoveObserver(&bObserver, &aTopic);
if (NS_FAILED(rv)) return rv;
}