1 // QueueT.h: interface for the CQueueT class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #if !defined(AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_)
  6 #define AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_
  7 
  8 #include "CritcalS.h"
  9 
 10 #if _MSC_VER > 1000
 11 #pragma once
 12 #endif // _MSC_VER > 1000
 13 
 14 // 循环队列: 模板类
 15 
 16 // 要求类型 TYPE 支持默认构造函数、赋值构造函数
 17 #define MAX_CMP(x,y) x>=y?x:y
 18 #define MIN_CMP(x,y) x<=y?x:y
 19 const int QUEUET_SIZE = 1024;   //队列大小
 20 template<class TYPE>
 21 class CQueueT  
 22 {
 23 public:
 24     CQueueT();
 25     virtual ~CQueueT();
 26 
 27     int Read(TYPE *pBuf, int readBytes);
 28     int GetfreeSize();
 29     int GetdataSize();
 30     int Write(const TYPE *pBuf, int writeBytes);
 31 private:
 32     void next(int &index);
 33     TYPE pop();
 34     void push(TYPE data);
 35 
 36     TYPE m_charArray[QUEUET_SIZE];
 37     int m_indexH;  // 对列头  第一个数据位的索引值
 38     int m_indexT;  // 队列尾  第一个空闲位的索引值
 39 
 40     CCritcalS m_critcal;   // 临界段
 41 };
 42 
 43 /////////////////////////////////////////////////////////////////////////////////
 44 template<class TYPE>
 45 CQueueT<TYPE>::CQueueT()
 46 {
 47 
 48 }
 49 
 50 template<class TYPE>
 51 CQueueT<TYPE>::~CQueueT()
 52 {
 53 
 54 }
 55 
 56 template<class TYPE>
 57 int CQueueT<TYPE>::Write(const TYPE *pBuf, int writeBytes)
 58 {    
 59     m_critcal.Lock();
 60      
 61     int sum = this->GetfreeSize();
 62     int    tSize = MIN_CMP(writeBytes,sum);
 63     for(int i=0;i<tSize;i++)
 64     {
 65         this->push(pBuf[i]);
 66     }
 67 
 68     m_critcal.Free();
 69 
 70     return tSize;
 71 }
 72 
 73 template<class TYPE>
 74 int CQueueT<TYPE>::Read(TYPE *pBuf, int readBytes)
 75 {
 76     m_critcal.Lock();
 77     
 78     int sum = this->GetdataSize();
 79     int tSize = MIN_CMP(readBytes,sum);
 80     for(int i=0;i<tSize;i++)
 81     {
 82         pBuf[i] = this->pop();
 83     }
 84     m_critcal.Free();
 85 
 86     return tSize;
 87 }
 88 
 89 // 队尾入队,由外层函数做队满判断
 90 template<class TYPE>
 91 void CQueueT<TYPE>::push(TYPE data)
 92 {
 93     this->m_charArray[this->m_indexT] = data;  // 
 94     next(this->m_indexT);
 95 }
 96 
 97 // 对头出队,有外层函数作队空判断
 98 template<class TYPE>
 99 TYPE CQueueT<TYPE>::pop()
100 {
101     TYPE res =     this->m_charArray[this->m_indexH];
102     next(this->m_indexH);
103 
104     return res;
105 }
106 
107 // 获得队列数据容量
108 template<class TYPE>
109 int CQueueT<TYPE>::GetdataSize()
110 {
111     if(m_indexT>=m_indexH)
112     {
113         return (m_indexT - m_indexH);
114     }else
115     {
116         return (m_indexT - m_indexH + QUEUET_SIZE);
117     }
118 }
119 
120 // 获得队列空闲容量
121 template<class TYPE>
122 int CQueueT<TYPE>::GetfreeSize()
123 {
124     return (QUEUESIZE-this->GetdataSize() - 1);
125 }
126 
127 // 索引下滑计算
128 template<class TYPE>
129 void CQueueT<TYPE>::next(int &index)
130 {
131     index = (index+1)% QUEUET_SIZE;
132 }
133 
134 #endif // !defined(AFX_QUEUET_H__BB664F37_5738_4439_8220_A54436AE6339__INCLUDED_)
CQueueT

相关文章:

  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
猜你喜欢
  • 2021-11-26
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案