⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ghswatch.cpp

📁 VC开发环境下
💻 CPP
字号:
// GHSwatch.cpp : Implementation of CGHSwatch

#include "stdafx.h"
#include "GHSwatch.h"
#include ".\ghswatch.h"
#include "GHRow.h"
#include "GHSwatchCursor.h"
#include "IndexSystem_i.c"
// CGHSwatch


STDMETHODIMP CGHSwatch::get_Name(BSTR* pVal)
{
	return m_Name.CopyTo(pVal);
	return S_OK;
}

STDMETHODIMP CGHSwatch::put_Name(BSTR newVal)
{
	HRESULT hr=m_Name.AssignBSTR(newVal);

	if (FAILED(hr))
		return hr;

	m_bRequiresSave=TRUE;
	return S_OK;
}

STDMETHODIMP CGHSwatch::get_CreatedDate(DATE* pVal)
{
	* pVal=m_CreatedDate;
	return S_OK;
}


STDMETHODIMP CGHSwatch::get_Description(BSTR* pVal)
{
	
	return m_Description.CopyTo(pVal);
}

STDMETHODIMP CGHSwatch::put_Description(BSTR newVal)
{
	
	HRESULT hr=m_Description.AssignBSTR(newVal);
	if(FAILED(hr))
		return hr;

	m_bRequiresSave=TRUE;
	return S_OK;
}


STDMETHODIMP CGHSwatch::get_IndexSystem(IGHIndexs** pVal)
{
	
	if (pVal==NULL) 
		return E_POINTER;

	return m_cpIndexs.CopyTo(pVal);
}

STDMETHODIMP CGHSwatch::put_IndexSystem(IGHIndexs* newVal)
{
	
	m_cpIndexs=newVal;
	return S_OK;
}

STDMETHODIMP CGHSwatch::get_FieldCount(LONG* pVal)
{
	HRESULT hr= m_cpIndexs->get_Count(pVal);
	*pVal+=1;
	return hr;
}

STDMETHODIMP CGHSwatch::get_RowCount(LONG* pVal)
{	
	*pVal=(LONG)m_listSwatch.size();
	return S_OK;
}

STDMETHODIMP CGHSwatch::CreateRow(IGHRow** row)
{
	
	if(row==NULL)
		return E_POINTER;

	ATLASSERT(m_cpIndexs!=NULL);
    
	HRESULT hRes;
	LONG lCount;
	hRes=get_FieldCount(&lCount);

	CComObject<CGHRow> *pGHRow;	
	hRes=CComObject<CGHRow>::CreateInstance(&pGHRow);
	if (hRes==S_OK) 
		hRes=pGHRow->CreateNewSwatchData(lCount+1);

	if(hRes==S_OK)
		hRes=pGHRow->QueryInterface(IID_IGHRow,(void**)row);
	

	return hRes;
}

STDMETHODIMP CGHSwatch::Search(VARIANT_BOOL Recycling, IGHSwatchCursor** cursor)
{
	

	if (cursor==NULL) 
		return E_POINTER;

	HRESULT hr;
	CComObject<CGHSwatchCursor> *pGHSCursor;
	hr=CComObject<CGHSwatchCursor>::CreateInstance(&pGHSCursor);
	if (hr==S_OK) 
		hr=pGHSCursor->Initial((CComObject<CGHSwatch>*)this,TRUE,Recycling==VARIANT_TRUE?TRUE:FALSE);
	
	if (hr==S_OK) 
		hr=pGHSCursor->QueryInterface(IID_IGHSwatchCursor,(void**)cursor);

	return hr;
}

STDMETHODIMP CGHSwatch::Update(VARIANT_BOOL Recycling, IGHSwatchCursor** cursor)
{
	if (cursor==NULL) 
		return E_POINTER;

	HRESULT hr;
	CComObject<CGHSwatchCursor> *pGHSCursor;
	hr=CComObject<CGHSwatchCursor>::CreateInstance(&pGHSCursor);
	if (hr==S_OK) 
		hr=pGHSCursor->Initial((CComObject<CGHSwatch>*)this,FALSE,Recycling==VARIANT_TRUE?TRUE:FALSE);
	
	if(hr==S_OK)
		hr=pGHSCursor->QueryInterface(IID_IGHSwatchCursor,(void**)cursor);

	m_bRequiresSave=TRUE;
	return hr;
}

STDMETHODIMP CGHSwatch::FindIndex(BSTR IndexName,LONG *lIndex)
{
	
	LONG lCount;
	HRESULT hr;
	hr=m_cpIndexs->get_Count(&lCount);

	CComBSTR bstrName;
	for (LONG i=0;i<lCount;i++) 
	{
		bstrName.Empty();
		CComPtr<IGHIndex> cpIndex;
		hr=m_cpIndexs->get_Item(i,&cpIndex);
		if (FAILED(hr)) 
			return hr;

		hr=cpIndex->get_Name(&bstrName);
		if (FAILED(hr)) 
			return hr;

		if (bstrName==IndexName)
			break;

	}

	if (i<lCount) 
	{
		*lIndex=i;
		return S_OK;
	}
	else
	{
		*lIndex=-1;
		return E_UNEXPECTED;
	}
}

STDMETHODIMP CGHSwatch::AddIndex(IGHIndex* pVal)
{
	m_bRequiresSave=TRUE;
	return m_cpIndexs->Add(pVal);

}

STDMETHODIMP CGHSwatch::DeleteIndex(IGHIndex* pVal)
{
	m_bRequiresSave=TRUE;
	return m_cpIndexs->Delete(pVal);
}

HRESULT CGHSwatch::IPersistStreamInit_Load(LPSTREAM pStm, ATL_PROPMAP_ENTRY* pMap)
{
	HRESULT hr;
	hr=IPersistStreamInitImpl<CGHSwatch>::IPersistStreamInit_Load(pStm,pMap);

	if (FAILED(hr)) 
		return hr;

	//Read IndexSystem's FilePath
	CComBSTR bstrFilePath;
	hr=bstrFilePath.ReadFromStream(pStm);
	if (FAILED(hr)) 
		return hr;

	CComPtr<IGHIndexsWorkspace> cpIndexsWorkspace;
	hr=cpIndexsWorkspace.CoCreateInstance(CLSID_IndexsWorkspace);
	if(FAILED(hr))
		return hr;

	if (m_cpIndexs) 
	{
		m_cpIndexs.Release();
		m_cpIndexs.p=NULL;
	}
	hr=cpIndexsWorkspace->Open(bstrFilePath,&m_cpIndexs);
	if(FAILED(hr))
		return hr;

	//Read list's size
	unsigned int listSize;
	hr=pStm->Read(&listSize,sizeof(unsigned int),NULL);
	if (FAILED(hr)) 
		return hr;

	//Read CSwathData's size
	LONG lCount;
	hr=pStm->Read(&lCount,sizeof(LONG),NULL);
	if (FAILED(hr)) 
		return hr;

	//Read Value
	FLOAT fValue;
	for (unsigned int i=0;i<listSize;i++)
	{
		CSwatchData *pSwatchData=new CSwatchData(lCount);
		for (LONG j=0;j<lCount;j++)
		{
			hr=pStm->Read(&fValue,sizeof(FLOAT),NULL);
			pSwatchData->set_Value(j,fValue);
		}

		m_listSwatch.push_back(pSwatchData);
	}
	
	m_bRequiresSave=FALSE;
	return hr;
}

HRESULT CGHSwatch::IPersistStreamInit_Save(LPSTREAM pStm, BOOL fClearDirty, ATL_PROPMAP_ENTRY* pMap)
{
	HRESULT hr;
	hr=IPersistStreamInitImpl<CGHSwatch>::IPersistStreamInit_Save(pStm,fClearDirty,pMap);
	if (FAILED(hr)) 
		return hr;

	//Write IndexsSystem's FilePath
	CComBSTR bstrFilePath;
	hr=m_cpIndexs->get_FilePath(&bstrFilePath);
	bstrFilePath.WriteToStream(pStm);

	//Write list's size
	unsigned int listSize=(unsigned int)m_listSwatch.size();
	hr=pStm->Write(&listSize,sizeof(unsigned int),NULL);
	if (FAILED(hr)) 
		return hr;
	
	//Write CSwatchData's size (is equally IIndexs's count) 
	LONG lCount;
	hr=get_FieldCount(&lCount);
	hr=pStm->Write(&lCount,sizeof(lCount),NULL);
	if (FAILED(hr)) 
		return hr;

	//Write value
	FLOAT fValue;
	for (std::list<CSwatchData*>::iterator pos=m_listSwatch.begin();pos!=m_listSwatch.end();++pos) 
	{
		for (LONG i=0;i<lCount;i++) 
		{
			fValue=(*pos)->get_Value(i);
			hr=pStm->Write(&fValue,sizeof(FLOAT),NULL);
		}
	}

	m_bRequiresSave=FALSE;

	return hr;

}

STDMETHODIMP CGHSwatch::GetSizeMax(ULARGE_INTEGER* pcbSize)
{
	HRESULT hr;

	hr=IPersistStreamInitImpl<CGHSwatch>::GetSizeMax(pcbSize);
	if(FAILED(hr))
		return hr;

	LONG lCount;
	hr=m_cpIndexs->get_Count(&lCount);

	pcbSize->QuadPart+=m_listSwatch.size()*lCount*sizeof(FLOAT);

	pcbSize->QuadPart+=sizeof(unsigned int);  //m_listSwatch's size
	pcbSize->QuadPart+=sizeof(LONG);          //m_cpIndexs's Count
	return S_OK;
}

STDMETHODIMP CGHSwatch::get_FilePath(BSTR* pVal)
{
	// TODO: Add your implementation code here

	return m_FilePath.CopyTo(pVal);
}

STDMETHODIMP CGHSwatch::put_FilePath(BSTR newVal)
{
	// TODO: Add your implementation code here

	return m_FilePath.AssignBSTR(newVal);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -