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

📄 memory.h

📁 C51访问西门子MC55做的透传模块, 支持远程注册和配置, 仅支持TCP协议, 其他请参考MC55说明书.
💻 H
字号:
//////////////////////////////////////////////////
//用法  CMemory<类型>  变量
//#include "Memory.h"
/////////////////////////////////////////////////

#ifndef _CLASS_CMemory_H_
#define _CLASS_CMemory_H_
#include		<malloc.h>
#include		<stdlib.h>
#include    <stdio.h>
#include    <string.h>
#include    <conio.h>
template<class TYPE> class CMemory
{
public:
	CMemory();
	~CMemory();
	TYPE & operator[](const long int nIndex);
	TYPE *GetAddr(const long int nIndex) ;
	long int GetSize();
	long int SetSize(const long int uSize);
	void Free();
protected:
	long int m_nMaxSize;		//数组元素的个数
	TYPE huge* m_pData;
};

template<class TYPE> CMemory<TYPE>::CMemory()
{
	m_pData=NULL;
	m_nMaxSize=0;
}
template<class TYPE>  CMemory<TYPE>::~CMemory()
{
	Free();
}
template<class TYPE>  void CMemory<TYPE>::Free()
{
	if(m_pData) farfree(m_pData);
	m_pData=NULL;
}
template<class TYPE>  long int CMemory<TYPE>::SetSize(const long int uSize)
{
	if(m_pData) farfree(m_pData);
	m_nMaxSize=uSize;
	m_pData=(TYPE huge*)farmalloc(uSize*sizeof(TYPE));
	if(!m_pData)
	{
		m_nMaxSize=0L;
		cprintf("\r\nMemory  Not Enough!!!!!");
	}
	return m_nMaxSize;
}
template<class TYPE>  long int CMemory<TYPE>::GetSize()
{
	return m_nMaxSize;
}
template<class TYPE>  TYPE *CMemory<TYPE>::GetAddr(const long int nIndex)
{
	if((nIndex<0)||(nIndex>m_nMaxSize))
	{
		cprintf("\r\nnIndex=%ld,m_nMaxSize=%ld,Error!!!",nIndex,m_nMaxSize);
		return (TYPE *)(m_pData+m_nMaxSize);
	}
	return (TYPE *)(m_pData+nIndex);
}
template<class TYPE>  TYPE & CMemory<TYPE>::operator[](const long int nIndex)
{
	if((nIndex<0)||(nIndex>m_nMaxSize))
	{
		cprintf("\r\nnIndex=%ld,m_nMaxSize=%ld,Error!!!",nIndex,m_nMaxSize);
		return *(TYPE *)(m_pData+m_nMaxSize);
	}
	return *(TYPE *)(m_pData+nIndex);
}
#endif

⌨️ 快捷键说明

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