circlebuffer.h

来自「MudMaster 2000 的C++源码」· C头文件 代码 · 共 109 行

H
109
字号
/************************************************************************************
	Copyright (c) 2000 Aaron O'Neil
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

		1) Redistributions of source code must retain the above copyright notice, 
				this list of conditions and the following disclaimer.
		2) Redistributions in binary form must reproduce the above copyright notice, 
				this list of conditions and the following disclaimer in the documentation
				and/or other materials provided with the distribution.
		3) Redistributions in binary form must reproduce the above copyright notice on
				program startup. Additional credits for program modification are acceptable
				but original copyright and credits must be visible at startup.
		4) You may charge a reasonable copying fee for any distribution of Mud Master. 
				You may charge any fee you choose for support of Mud Master. You may not 
				charge a fee for Mud Master itself.

  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************************/

class CCircleBuffer
{
public:
	CCircleBuffer();
	~CCircleBuffer();

	// Returns TRUE if successfully initialized.  This function can
	// be called more than one if you wish to redimension the
	// buffer.  All data in the buffer at the time of the redim will
	// be lost.
	virtual BOOL Init(int nNumElements, int nElementSize);

	// Add the data to the tail of the buffer. 
	virtual void Add(void *pData, int nDataSize);

	// Turns on data padding.
	void PadData(unsigned char ch)	{ m_bPad = TRUE; m_chPad = ch; }
	// Turns off data padding.
	void NoPad()				{ m_bPad = FALSE; }

	// Makes the buffer empty.
	void Empty()				{ m_nHead = m_nTail = 0; }

	BOOL IsEmpty()				{ return(m_nHead == m_nTail); }

	// Returns the data at the head of the buffer then removes
	// it from the buffer.
	virtual void* RemoveHead();

	virtual void* GetAt(int nIndex);

	// Gives you the data at the head of the buffer.  You need to 
	// call this before calling FindNext.  Will return NULL if the
	// buffer is empty.
	virtual void* FindFirst();
	virtual void* FindLast();

	// Will walk the buffer until the last element has been read.
	// Returns NULL when the end of the buffer has been reached.
	// Don't call this function without calling FindFirst for the 
	// first time.  Also don't call this function after data has
	// been added to the buffer without calling FindFirst again.
	virtual void* FindNext();
	virtual void* FindPrev();

	// Returns TRUE if the Find fucntion is at the top of the
	// buffer.  If this is TRUE the next call to FindPrevious 
	// will return NULL.  Also, if at the top, FindNext will
	// not function correctly.
	BOOL FindAtTop();
	BOOL FindAtBottom();

	int GetNumElements()			{ return(m_nNumElements); }
	int GetElementSize()			{ return(m_nElementSize); }

protected:
	void AdvanceTail();
	void AdvanceHead();
	
	int m_nHead;
	int m_nTail;

	// False by default.  When TRUE if the data addeded to the
	// buffer is shorter than m_nElementSize, the data is padded
	// on the right with m_chPad up to m_nElementSize.
	BOOL m_bPad;
	unsigned char m_chPad;

	int m_nFindIndex;

private:
	int m_nNumElements;
	int m_nElementSize;

	char *m_pBuf;
};

⌨️ 快捷键说明

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