gecko-dev/xpcom/components/nsGenericFactory.cpp

111 lines
3.1 KiB
C++
Raw Normal View History

1999-02-25 04:41:02 +00:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* 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 "nsGenericFactory.h"
1999-02-28 19:04:47 +00:00
nsGenericFactory::nsGenericFactory(ConstructorProcPtr constructor)
: mConstructor(constructor), mDestructor(NULL)
1999-02-25 04:41:02 +00:00
{
1999-02-26 04:08:26 +00:00
NS_INIT_ISUPPORTS();
1999-02-25 04:41:02 +00:00
}
1999-02-28 19:04:47 +00:00
nsGenericFactory::~nsGenericFactory()
{
if (mDestructor != NULL)
(*mDestructor) ();
}
1999-02-25 04:41:02 +00:00
1999-02-26 04:08:26 +00:00
NS_METHOD nsGenericFactory::QueryInterface(const nsIID& aIID, void** aInstancePtr)
{
if (NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
1999-05-26 01:38:36 +00:00
if (aIID.Equals(nsIGenericFactory::GetIID()) ||
aIID.Equals(nsIFactory::GetIID()) ||
aIID.Equals(nsISupports::GetIID())) {
*aInstancePtr = (nsIGenericFactory*) this;
NS_ADDREF_THIS();
1999-02-26 04:08:26 +00:00
return NS_OK;
}
return NS_NOINTERFACE;
}
1999-02-25 04:41:02 +00:00
1999-02-26 04:08:26 +00:00
NS_IMPL_ADDREF(nsGenericFactory)
NS_IMPL_RELEASE(nsGenericFactory)
1999-02-25 04:41:02 +00:00
NS_IMETHODIMP nsGenericFactory::CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
1999-02-26 04:08:26 +00:00
return mConstructor(aOuter, aIID, aResult);
1999-02-25 04:41:02 +00:00
}
NS_IMETHODIMP nsGenericFactory::LockFactory(PRBool aLock)
{
return NS_OK;
}
1999-02-26 04:08:26 +00:00
NS_IMETHODIMP nsGenericFactory::SetConstructor(ConstructorProcPtr constructor)
{
mConstructor = constructor;
return NS_OK;
}
1999-02-28 19:04:47 +00:00
NS_IMETHODIMP nsGenericFactory::SetDestructor(DestructorProcPtr destructor)
{
mDestructor = destructor;
return NS_OK;
}
1999-02-26 04:08:26 +00:00
NS_METHOD nsGenericFactory::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
{
// sorry, aggregation not spoken here.
nsresult res = NS_ERROR_NO_AGGREGATION;
if (outer == NULL) {
nsGenericFactory* factory = new nsGenericFactory;
if (factory != NULL) {
res = factory->QueryInterface(aIID, aInstancePtr);
if (res != NS_OK)
delete factory;
} else {
res = NS_ERROR_OUT_OF_MEMORY;
}
}
return res;
}
1999-05-26 01:38:36 +00:00
1999-06-03 21:51:14 +00:00
NS_COM nsresult
1999-05-26 01:38:36 +00:00
NS_NewGenericFactory(nsIGenericFactory* *result,
nsIGenericFactory::ConstructorProcPtr constructor,
nsIGenericFactory::DestructorProcPtr destructor)
{
nsresult rv;
nsIGenericFactory* fact;
rv = nsGenericFactory::Create(NULL, nsIGenericFactory::GetIID(), (void**)&fact);
if (NS_FAILED(rv)) return rv;
rv = fact->SetConstructor(constructor);
if (NS_FAILED(rv)) goto error;
rv = fact->SetDestructor(destructor);
if (NS_FAILED(rv)) goto error;
*result = fact;
return rv;
error:
NS_RELEASE(fact);
return rv;
}