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

📄 opcdrvgrouplist.cpp

📁 基于Intellution开发包的开发的OPC服务器
💻 CPP
字号:
// OPCDrvGroupList.cpp
//
// This file contains the implementation of 
// the COPCDrv Group Object ITEM LIST management functions.
//
//  (c) COPYRIGHT 1996-1998, INTELLUTION INC.
//  ALL RIGHTS RESERVED
//
//
//	Functions defined in this module:
//
//			COPCDrvGroup::IsItemValid()
//			COPCDrvGroup::GetItemPtr()
//			COPCDrvGroup::GetNumItemHandles()
//			COPCDrvGroup::ItemAlloc()
//			COPCDrvGroup::ItemSet()
//			COPCDrvGroup::ItemFree()
//			COPCDrvGroup::FreeAllItems()
//			COPCDrvGroup::GetFirstItemPosition()
//			COPCDrvGroup::GetNextItem()
//
//
//
// Modification Log:
//	Vers	Date     By		Notes
//	----	-------- ---	-----
//	1.0		08/26/97 jra	Created
//	1.3		03/10/98 jra	Modified to be wizard generated and driver specific.
//
//

#define WIN32_LEAN_AND_MEAN

#define NOCOMM


#include <afxtempl.h>

#include "OPCDrv.h"


////////////////////////////////////////////////////////////////
// COPCDrvGroup::IsItemValid()
//
// Verifies that an Item is valid.
//
// Returns TRUE if the Item handle is found.
// Returns FALSE if it is not found.
//
////////////////////////////////////////////////////////////////
BOOL COPCDrvGroup::IsItemValid(OPCHANDLE OPCHandle)
{
	COPCDrvItem	*pItem;


	if (m_mapItem.Lookup(OPCHandle, pItem) == TRUE)
	{
		// Item handle found
		return TRUE;
	}

	return FALSE;
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::GetItemPtr()
//
// Accepts an OPCHANDLE and returns a pointer to the 
//	corresponding Item in the map.
//
// Returns a pointer to the Item.
// Returns NULL if the handle was not found in the map.
//
////////////////////////////////////////////////////////////////
COPCDrvItem* COPCDrvGroup::GetItemPtr(OPCHANDLE OPCHandle)
{
	COPCDrvItem	*pItem;


	if (m_mapItem.Lookup(OPCHandle, pItem) == TRUE)
	{
		// Item handle found
		return pItem;
	}

	return NULL;
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::ItemHandles()
//
// Return # item handles (for use in loops)
//
////////////////////////////////////////////////////////////////
int COPCDrvGroup::GetNumItemHandles(void)
{
	return (m_mapItem.GetCount());
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::ItemAlloc()
//
// Adds a new Item to the Map. Memory is allocated dynamically.
//
// Returns the new Item's handle and a pointer to it.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvGroup::ItemAlloc(OPCHANDLE		*OPCHandle, 
								COPCDrvItem		**ppItem)
{
	// Make sure that we have valid pointers
	if ((NULL == ppItem) ||
		(NULL == OPCHandle))
	{
		return E_INVALIDARG;
	}

	COPCDrvItem	*pNewItem = new COPCDrvItem (this);
	if (NULL == pNewItem)
	{
		*OPCHandle = 0;			// Invalid handle
		return E_OUTOFMEMORY;
	}

	// Return the handle and pointer to the caller
	*OPCHandle = (OPCHANDLE)pNewItem;
	*ppItem = pNewItem;
	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::ItemSet()
//
// Assign an Item to a specific handle.
//
// Returns E_INVALIDARG if an argument is invalid (NULL pointer, etc)
// Returns S_OK otherwise.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvGroup::ItemSet(COPCDrvItem	*pItem)
{
	// Make sure that we have a valid pointers
	//
	if (NULL == pItem)
	{
		return E_INVALIDARG;
	}

	// Save the OPC handle
	//
	pItem->m_hServerItemHandle = (OPCHANDLE)pItem;

	// Add item to map
	//
	Lock();
	m_mapItem.SetAt(pItem->m_hServerItemHandle, pItem);
	m_dwNumItems++;
	UnLock();

	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::ItemFree()
//
// This function will remove the Item key from the Item map,
// but the caller is expected to free the memory associated with
// the object (can be done by calling Release()). However, with the
// bForce set to TRUE, the function will free the memory
// regardless as to how many connections are referencing it. This
// is used when an Item is removed by force.
//
// Note: We don't perform a Lock()/UnLock() here because the 
//		 parent group is locked out by the caller.
//
// Returns E_INVALIDARG if an argument is invalid (NULL pointer, etc)
// Returns S_OK otherwise.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvGroup::ItemFree(OPCHANDLE		OPCHandle, 
							   BOOL				bForce)
{
	COPCDrvItem		*pTempItem;
	DWORD			dwCurrentCount;


	// Make sure the OPCHandle is valid
	if (0 == OPCHandle)
	{
		return E_INVALIDARG;
	}

	if (m_mapItem.Lookup(OPCHandle, pTempItem) != TRUE)
	{
		// Item key not found
		return E_INVALIDARG;
	}

	// Remove the key first, then release it
	//
	m_mapItem.RemoveKey(OPCHandle);
	dwCurrentCount = pTempItem->Release();

	if ((dwCurrentCount > 0) && (TRUE == bForce))
	{
		// The force flag is set, so delete it regardless
		//
		delete pTempItem;
	}

	m_dwNumItems--;
	return S_OK;
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::FreeAllItems()
//
// This function will remove all of the items in a group.
//
// Note: We don't perform a Lock()/UnLock() here because the 
//		 parent group is locked out by the caller.
//
// Returns E_FAIL if ItemFree() fails
// Returns S_OK otherwise.
//
////////////////////////////////////////////////////////////////
HRESULT COPCDrvGroup::FreeAllItems(void)
{
	POSITION	posItem		= m_mapItem.GetStartPosition();
	DWORD		dwNumItems	= GetNumItemHandles();
	OPCHANDLE	OPCHandle	= 0;
	COPCDrvItem	*pItem		= NULL;
	HRESULT		hrRet		= S_OK;


	if (NULL != posItem)
	{
		// Free each element in the map
		//
		for (DWORD j = 0; j < dwNumItems; j++)
		{
			m_mapItem.GetNextAssoc(posItem, OPCHandle, pItem);
			if (FAILED(ItemFree(OPCHandle, TRUE)))
			{
				hrRet = E_FAIL;
			}
		}
	}
	
	return hrRet;
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::GetFirstItemPosition()
//
// Returns a POSITION structure indicating where the first
// item is located in the map
//
////////////////////////////////////////////////////////////////
POSITION COPCDrvGroup::GetFirstItemPosition()
{
	return (m_mapItem.GetStartPosition());
}


////////////////////////////////////////////////////////////////
// COPCDrvGroup::GetNextItem()
//
// Gets the next Item in the map, relative to ItemPosition.
//
////////////////////////////////////////////////////////////////
void COPCDrvGroup::GetNextItem(POSITION			&ItemPosition, 
							   OPCHANDLE		&OPCHandle, 
							   COPCDrvItem		*&pItem)
{
	m_mapItem.GetNextAssoc(ItemPosition, OPCHandle, pItem);
}

⌨️ 快捷键说明

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