一个对大小固定的内存数据进行操作的简单类
#include "stdafx.h"#include <windows.h>// 固定内存大小队列管理class CFixedQueue{public:enum FuncResult{ErrNull,// 没有错误ErrInit,// 初始化错误ErrParam,// 参数错误ErrEmpty,// 空的不能进行出队操作ErrFull,// 满了不能进行入队};public :CFixedQueue();CFixedQueue(LPVOID lpData, int nElemSize, int nMaxNum);BOOL Attach(LPVOID lpData, int nElemSize, int nMaxNum);// 对内存进行挂钩FuncResult Push(LPVOID lpElem);// 压入数据FuncResult Pop(LPVOID lpData);// 弹出数据int GetElemTotalNum() const;// 获取当前数据条数int GetMaxNum() const;// 获取数据最大存储条数BOOL IsFull() const;// 数据是否已经存满BOOL IsEmpty() const;// 数据是否为空BOOL IsValid() const;// 数据空间是否有效void ClearAll();// 清空protected:void Init();LPBYTE m_pData;// 数据起始指针int m_nMaxNum;// 最大个数int m_nBeginIndex;// 起始偏移int m_nCurNum;// 当前成员个数int m_nElemSize;// 每个成员大小BOOL m_fgInit;// 有没有初始化好};/************************************************************************//* *//************************************************************************/CFixedQueue::CFixedQueue(LPVOID lpData, int nElemSize, int nMaxNum){Attach(lpData, nElemSize, nMaxNum);}CFixedQueue::CFixedQueue(){Init();}void CFixedQueue::Init(){m_pData = NULL;m_nElemSize = 0;m_nMaxNum = 0;m_nBeginIndex = 0;m_nCurNum = 0;m_fgInit = FALSE;}BOOL CFixedQueue::Attach(LPVOID lpData, int nElemSize, int nMaxNum){Init();if (lpData != NULL && nElemSize > 0 && nMaxNum > 0){m_pData = (LPBYTE)lpData;m_nElemSize = nElemSize;m_nMaxNum = nMaxNum;m_fgInit = TRUE;return TRUE;}return FALSE;}CFixedQueue::FuncResult CFixedQueue::Push(LPVOID lpElem){if (NULL == lpElem){return ErrParam;}if (!IsValid()){return ErrInit;}if (IsFull()){return ErrFull;}int nTempIndex = m_nBeginIndex + m_nCurNum;if (nTempIndex >= m_nMaxNum){nTempIndex -= m_nMaxNum;}memcpy(m_pData+nTempIndex*m_nElemSize, lpElem, m_nElemSize);m_nCurNum++;return ErrNull;}CFixedQueue::FuncResult CFixedQueue::Pop(LPVOID lpData){if (NULL == lpData){return ErrParam;}if (!IsValid()){return ErrInit;}if (IsEmpty()){return ErrEmpty;}memcpy(lpData, m_pData+m_nBeginIndex*m_nElemSize, m_nElemSize);if (1 == m_nCurNum){ClearAll();}else{m_nCurNum--;m_nBeginIndex++;if (m_nBeginIndex >= m_nMaxNum){m_nBeginIndex = 0;}}return ErrNull;}int CFixedQueue::GetElemTotalNum() const{return m_nCurNum;}int CFixedQueue::GetMaxNum() const{return m_nMaxNum;}BOOL CFixedQueue::IsFull() const{return (m_fgInit && m_nMaxNum <= m_nCurNum);}BOOL CFixedQueue::IsEmpty() const{return (m_nCurNum == 0);}BOOL CFixedQueue::IsValid() const{return m_fgInit;}void CFixedQueue::ClearAll(){m_nBeginIndex = 0;m_nCurNum = 0;}int _tmain(int argc, _TCHAR* argv[]){int nData[5];CFixedQueue fixQueue(nData, sizeof(nData[0]), 5);int nTmp;nTmp = 1;fixQueue.Push(&nTmp);nTmp = 2;fixQueue.Push(&nTmp);nTmp = 3;fixQueue.Push(&nTmp);nTmp = 4;fixQueue.Push(&nTmp);nTmp = 5;fixQueue.Push(&nTmp);if (CFixedQueue::ErrFull == fixQueue.Push(&nTmp)){printf("Error\n");}fixQueue.Pop(&nTmp);fixQueue.Pop(&nTmp);fixQueue.Pop(&nTmp);fixQueue.Pop(&nTmp);nTmp = 10;fixQueue.Push(&nTmp);nTmp = 11;fixQueue.Push(&nTmp);nTmp = 12;fixQueue.Push(&nTmp);nTmp = 13;fixQueue.Push(&nTmp);nTmp = 14;fixQueue.Push(&nTmp);nTmp = 15;fixQueue.Push(&nTmp);nTmp = 16;fixQueue.Push(&nTmp);fixQueue.Pop(&nTmp);//nTmp = 6;//fixQueue.Push(&nTmp);int i;int n = fixQueue.GetElemTotalNum();for (i=0; i<n; i++){fixQueue.Pop(&nTmp);printf("%d\n", nTmp);}return 0;}