/*
*********************************************************************
*
*	The Essence of OLE
*
*	Source code sample
*
*	Copyright (c) 1996, Rolling Thunder Computing, Ipswich MA
*
*	e-mail: essence@rollthunder.com
*	CompuServe: 73540,3430
*	Web: http://www.rollthunder.com
*
*	File Name: CCLASSF.CPP
*
*	Purpose: Class factory
*
*********************************************************************
*/

#include <windows.h>
#include "cclassf.h"
#include "cfiletextdata.h"


extern HWND hMainWnd ;
extern int LockCount, ObjectCount ;
extern CFileTextData *pFileDataObj ;

CClassFactory::CClassFactory (void)
{
    m_RefCount = 1 ;
    return;
}

CClassFactory::~CClassFactory(void)
{
    return;
}



STDMETHODIMP CClassFactory::QueryInterface(REFIID riid, LPVOID *ppv)
{
    if (IsEqualIID(riid, IID_IUnknown)
        || IsEqualIID(riid, IID_IClassFactory))
	{
        *ppv = (LPVOID)this;
		AddRef( ) ;
		return NOERROR;
	}
	else
	{
    	*ppv = NULL ;
    	return  E_NOINTERFACE ;
	}

}


STDMETHODIMP_(ULONG) CClassFactory::AddRef(void)
{
    return ++m_RefCount;
}


STDMETHODIMP_(ULONG) CClassFactory::Release(void)
{
	m_RefCount -- ;

    if (m_RefCount == 0)
	{
        delete this;
		return 0 ;
	}

    return m_RefCount ;
}



					  	
STDMETHODIMP CClassFactory::CreateInstance(LPUNKNOWN pUnkOuter, 
	REFIID riid, LPVOID* ppvObj)
{
	    
/*
Check for aggregation request.  We don't do it.
*/
	if (pUnkOuter != NULL)
	{
		*ppvObj = NULL ;
		return  CLASS_E_NOAGGREGATION ;
	}

/*
Create a new data object.  Query interface as requested.  If found,
go happily on our way.
*/
	pFileDataObj = new CFileTextData ( ) ;

   	if (pFileDataObj->QueryInterface (riid, ppvObj) == NOERROR)
	{
		pFileDataObj->Release ( ) ;
		return NOERROR ;
	}
	else
	{
		return E_NOINTERFACE ;
	}
}


STDMETHODIMP CClassFactory::LockServer(BOOL bLock)
{
	return E_NOTIMPL ;
}
