📄 gqueue.h
字号:
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#ifndef __GQUEUE_H__#define __GQUEUE_H__#include <string.h>#include "GMacros.h"class GQueueChunk{public: GQueueChunk* m_pNext; int m_nSize; unsigned char* m_pData; bool m_bDelete; GQueueChunk(int nSize, const unsigned char* pStaticData) { m_pNext = NULL; m_nSize = nSize; if(pStaticData) { m_pData = (unsigned char*)pStaticData; m_bDelete = false; } else { m_pData = new unsigned char[nSize]; m_bDelete = true; } } ~GQueueChunk() { GAssert(!m_pNext, "Still linked into the list!"); if(m_bDelete) delete(m_pData); }#ifndef NO_TEST_CODE static void Test();#endif // NO_TEST_CODE};class GQueue{protected: int m_nDefaultChunkSize; int m_nInPos; int m_nOutPos; int m_nDataSize; GQueueChunk* m_pFirstChunk; GQueueChunk* m_pLastChunk; GQueueChunk* m_pExtra; GQueueChunk* m_pExtraStatic;public: GQueue(int nChunkSize = 2048); ~GQueue(); void Flush(); void PushStatic(const unsigned char* pBuf, int nBufSize); inline void Push(const unsigned char* pData, int nSize) { GQueueChunk* pCurrentChunk = m_pLastChunk; if(!pCurrentChunk || pCurrentChunk->m_nSize - m_nInPos < nSize) pCurrentChunk = MakeNewChunk(nSize); memcpy(&pCurrentChunk->m_pData[m_nInPos], pData, nSize); m_nInPos += nSize; m_nDataSize += nSize; } inline void Push(const wchar_t wc) { Push((const unsigned char*)&wc, sizeof(wchar_t)); } inline void Push(const char c) { Push((const unsigned char*)&c, sizeof(char)); } inline void Push(const int n) { Push((const unsigned char*)&n, sizeof(int)); } inline void Push(const void* pointer) { Push((const unsigned char*)&pointer, sizeof(void*)); } inline void Push(const unsigned int n) { Push((const unsigned char*)&n, sizeof(unsigned int)); } inline void Push(const unsigned char uc) { Push((const unsigned char*)&uc, sizeof(unsigned char)); } inline void Push(const float f) { Push((const unsigned char*)&f, sizeof(float)); } inline void Push(const double d) { Push((const unsigned char*)&d, sizeof(double)); } inline void Push(const char* szString) { Push((const unsigned char*)szString, strlen(szString)); } inline void Push(const wchar_t* wszString, int nLen) { Push((const unsigned char*)wszString, sizeof(wchar_t) * nLen); } bool Pop(unsigned char* pData, int nSize) { int nPart; while(true) { if(!m_pFirstChunk) { GAssert(m_nDataSize == 0, "inconsistent size"); return false; } nPart = MIN(nSize, m_pFirstChunk->m_nSize - m_nOutPos); memcpy(pData, &m_pFirstChunk->m_pData[m_nOutPos], nPart); m_nDataSize -= nPart; m_nOutPos += nPart; nSize -= nPart; if(nSize <= 0) return true; pData += nPart; DropFirstChunk(); } } inline bool Pop(wchar_t* pwc) { return Pop((unsigned char*)pwc, sizeof(wchar_t)); } inline bool Pop(char* pc) { return Pop((unsigned char*)pc, sizeof(char)); } inline bool Pop(int* pn) { return Pop((unsigned char*)pn, sizeof(int)); } inline bool Pop(void** pointer) { return Pop((unsigned char*)pointer, sizeof(void*)); } inline bool Pop(unsigned int* pui) { return Pop((unsigned char*)pui, sizeof(unsigned int)); } inline bool Pop(unsigned char* puc) { return Pop(puc, sizeof(unsigned char)); } inline bool Pop(float* pf) { return Pop((unsigned char*)pf, sizeof(float)); } inline bool Pop(double* pd) { return Pop((unsigned char*)pd, sizeof(double)); } int GetSize() { return m_nDataSize; } char* DumpToString(); // note: you must delete the string this returns // This puts data into an arbitrary position in the queue. It inserts // '\0' as padding if necessary, and it overwrites any data already // existing at that location. Behavior is undefined if you poke data // past the end of the queue. void Poke(int nIndex, const unsigned char* pBuf, int nLen);#ifndef NO_TEST_CODE static void Test();#endif // NO_TEST_CODEprotected: void Link(GQueueChunk* pChunk); GQueueChunk* UnlinkFirst(); GQueueChunk* MakeNewChunk(int nMinSize); void DropFirstChunk();};#endif // __GQUEUE_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -