mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-10 03:45:46 +00:00
cleaned up CreateInstance to not use delete on interface pointers. Modernized implementation using NS_IMPL_ISUPPORTS1.
This commit is contained in:
parent
1c534dce1f
commit
ac8b456d1c
@ -34,82 +34,34 @@ static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
|
||||
|
||||
class nsViewFactory : public nsIFactory
|
||||
{
|
||||
public:
|
||||
// nsISupports methods
|
||||
NS_IMETHOD QueryInterface(const nsIID &aIID,
|
||||
void **aResult);
|
||||
NS_IMETHOD_(nsrefcnt) AddRef(void);
|
||||
NS_IMETHOD_(nsrefcnt) Release(void);
|
||||
|
||||
// nsIFactory methods
|
||||
NS_IMETHOD CreateInstance(nsISupports *aOuter,
|
||||
const nsIID &aIID,
|
||||
void **aResult);
|
||||
|
||||
NS_IMETHOD LockFactory(PRBool aLock);
|
||||
|
||||
public:
|
||||
nsViewFactory(const nsCID &aClass);
|
||||
|
||||
protected:
|
||||
virtual ~nsViewFactory();
|
||||
|
||||
NS_DECL_ISUPPORTS
|
||||
|
||||
NS_DECL_NSIFACTORY
|
||||
|
||||
private:
|
||||
nsrefcnt mRefCnt;
|
||||
nsCID mClassID;
|
||||
};
|
||||
|
||||
nsViewFactory::nsViewFactory(const nsCID &aClass)
|
||||
{
|
||||
mRefCnt = 0;
|
||||
mClassID = aClass;
|
||||
NS_INIT_ISUPPORTS();
|
||||
mClassID = aClass;
|
||||
}
|
||||
|
||||
nsViewFactory::~nsViewFactory()
|
||||
{
|
||||
NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
|
||||
NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction");
|
||||
}
|
||||
|
||||
nsresult nsViewFactory::QueryInterface(const nsIID &aIID,
|
||||
void **aResult)
|
||||
{
|
||||
if (aResult == NULL) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
}
|
||||
|
||||
// Always NULL result, in case of failure
|
||||
*aResult = NULL;
|
||||
|
||||
if (aIID.Equals(kISupportsIID)) {
|
||||
*aResult = (void *)(nsISupports*)this;
|
||||
} else if (aIID.Equals(kIFactoryIID)) {
|
||||
*aResult = (void *)(nsIFactory*)this;
|
||||
}
|
||||
|
||||
if (*aResult == NULL) {
|
||||
return NS_NOINTERFACE;
|
||||
}
|
||||
|
||||
AddRef(); // Increase reference count for caller
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsrefcnt nsViewFactory::AddRef()
|
||||
{
|
||||
return ++mRefCnt;
|
||||
}
|
||||
|
||||
nsrefcnt nsViewFactory::Release()
|
||||
{
|
||||
if (--mRefCnt == 0) {
|
||||
delete this;
|
||||
return 0; // Don't access mRefCnt after deleting!
|
||||
}
|
||||
return mRefCnt;
|
||||
}
|
||||
NS_IMPL_ISUPPORTS1(nsViewFactory, nsIFactory)
|
||||
|
||||
nsresult nsViewFactory::CreateInstance(nsISupports *aOuter,
|
||||
const nsIID &aIID,
|
||||
void **aResult)
|
||||
const nsIID &aIID,
|
||||
void **aResult)
|
||||
{
|
||||
if (aResult == NULL) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
@ -120,25 +72,21 @@ nsresult nsViewFactory::CreateInstance(nsISupports *aOuter,
|
||||
nsISupports *inst = nsnull;
|
||||
|
||||
if (mClassID.Equals(kCViewManager)) {
|
||||
inst = (nsISupports *)new nsViewManager();
|
||||
}
|
||||
else if (mClassID.Equals(kCView)) {
|
||||
inst = (nsISupports *)new nsView();
|
||||
}
|
||||
else if (mClassID.Equals(kCScrollingView)) {
|
||||
inst = (nsISupports *)(nsView*)new nsScrollingView();
|
||||
inst = (nsISupports*) new nsViewManager();
|
||||
} else if (mClassID.Equals(kCView)) {
|
||||
inst = (nsISupports*) new nsView();
|
||||
} else if (mClassID.Equals(kCScrollingView)) {
|
||||
inst = (nsISupports*) (nsView*) new nsScrollingView();
|
||||
}
|
||||
|
||||
if (inst == NULL) {
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
}
|
||||
|
||||
// add a reference count,
|
||||
NS_ADDREF(inst);
|
||||
nsresult res = inst->QueryInterface(aIID, aResult);
|
||||
|
||||
if (res != NS_OK) {
|
||||
// We didn't get the right interface, so clean up
|
||||
delete inst;
|
||||
}
|
||||
NS_RELEASE(inst);
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -150,21 +98,12 @@ nsresult nsViewFactory::LockFactory(PRBool aLock)
|
||||
}
|
||||
|
||||
// return the proper factory to the caller
|
||||
#if defined(XP_MAC) && defined(MAC_STATIC)
|
||||
extern "C" NS_VIEW nsresult
|
||||
NSGetFactory_VIEW_DLL(nsISupports* serviceMgr,
|
||||
const nsCID &aClass,
|
||||
const char *aClassName,
|
||||
const char *aProgID,
|
||||
nsIFactory **aFactory)
|
||||
#else
|
||||
extern "C" NS_VIEW nsresult
|
||||
NSGetFactory(nsISupports* serviceMgr,
|
||||
const nsCID &aClass,
|
||||
const char *aClassName,
|
||||
const char *aProgID,
|
||||
nsIFactory **aFactory)
|
||||
#endif
|
||||
{
|
||||
if (nsnull == aFactory) {
|
||||
return NS_ERROR_NULL_POINTER;
|
||||
|
Loading…
Reference in New Issue
Block a user