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


extern long g_ObjectCount ;

CTimeData *g_pTimeDataObj = NULL ;

CClassFactory::CClassFactory (void)
{
    m_RefCount = 1 ;
	InterlockedIncrement (&g_ObjectCount) ;
	m_CreatingBothThreadingModels = FALSE ;
    return;
}


CClassFactory::~CClassFactory(void)
{
	InterlockedDecrement (&g_ObjectCount) ;
    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 ;
	}

/*
This class factory provides connections to a single object. If this
is the first request, create the object. In its constructor,
pass it a flag so it knows whether or not it supports both threading
models and should therefore create a free threaded marshaler. 
*/
	if (g_pTimeDataObj == NULL)
	{
		 g_pTimeDataObj  = new CTimeData (m_CreatingBothThreadingModels) ;
	}

/*
Call QueryInterface( ) on the one and only data object on behalf of COM.
*/
   	if (g_pTimeDataObj->QueryInterface (riid, ppvObj) == S_OK)
	{
		return S_OK ;
	}
	else
	{
		return E_NOINTERFACE ;
	}
}


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