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


extern int LockCount, ObjectCount ;
extern CTimeData *lpFirstObj ;

CClassFactory::CClassFactory (void)
{
    m_RefCount = 1 ;
	AddRef( ) ;   
    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 ;
}







/*
 * CClassFactory::CreateInstance
 *
 * Purpose:
 *  Instantiates a Figure object that supports embedding.
 *
 * Parameters:
 *  pUnkOuter       LPUNKNOWN to the controlling IUnknown if we are
 *                  being used in an aggregation.
 *  riid            REFIID identifying the interface the caller
 *                  desires to have for the new object.
 *  ppvObj          LPLPVOID in which to store the desired
 *                  interface pointer for the new object.
 *
 * Return Value:
 *  HRESULT         NOERROR if successful, otherwise contains
 *                  E_NOINTERFACE if we cannot support the requested
 *                  interface.
 */
					  	
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.
*/

	CTimeData * pTD = new CTimeData ( ) ;

   	if (pTD->QueryInterface (riid, ppvObj) == NOERROR)
	{
		pTD->Release ( ) ;
		ObjectCount ++ ;

		if (!lpFirstObj)
		{
			lpFirstObj = pTD ;
		}

		return NOERROR ;
	}
	else
	{
		delete pTD ;
		return E_NOINTERFACE ;
	}
}


/*
 * CClassFactory::LockServer
 *
 * Purpose:
 *  Increments or decrements the lock count of the serving
 *  IClassFactory object.  When the number of locks goes to
 *  zero and the number of objects is zero, we shut down the
 *  application.
 *
 * Parameters:
 *  fLock           BOOL specifying whether to increment or
 *                  decrement the lock count.
 *
 * Return Value:
 *  HRESULT         NOERROR always.
 */

STDMETHODIMP CClassFactory::LockServer(BOOL bLock)
{
	MessageBox (NULL, "LockServer", "", 0) ;

	if (bLock == TRUE)
	{
		LockCount ++ ;
	}
	else
	{
		LockCount -- ;
	}
	return NOERROR ;
}
