出自ProgWiki
用途
- Queue模板,打消 MFC / ATL / STL 在 Queue的差異。
程式碼
//File QueueT.h
//Description: template QueueT from
// CList of MFC
// CAtlList of ATL
// std::list of STL
#ifndef __QUEUET_H__
#define __QUEUET_H__
#if _MSC_VER > 1000
#pragma once
#endif
//-------------------------------------------------------------------------------
// TODO: CQueueT is use MFC
//-------------------------------------------------------------------------------
#if !defined(CQueueT) && defined(_AFX) && defined(_Use_MFC)
#include <afxtempl.h>
template <typename TYPE>
class CQueueT : private CList<TYPE>
{
public:
//TODO: (MFC) typedef
typedef POSITION iterator;
typedef POSITION const_iterator;
//TODO: (MFC) Status
int GetCount() const
{
return CList<TYPE>::GetCount();
}
int GetSize() const
{
return CList<TYPE>::GetCount();
}
bool IsEmpty() const
{
return CList<TYPE>::IsEmpty();
}
//TODO: (MFC) Operators
void RemoveAll()
{
CList<TYPE>::RemoveAll();
}
void AddToEnd(const TYPE& x)
{
CList<TYPE>::AddTail(x);
}
TYPE GetFromFront() const
{
return IsEmpty() ? NULL : CList<TYPE>::RemoveHead();
}
//TODO: (MFC) IteratorValid?
bool IteratorValid(const iterator &iter) const
{
return iter != NULL;
}
};
#endif
//-------------------------------------------------------------------------------
// TODO: CQueueT is use ATL
//-------------------------------------------------------------------------------
#if !defined(CQueueT) && defined(ATL) && defined(_Use_ATL)
#include <atlcoll.h>
template <typename TYPE>
class CQueueT : private CAtlList<TYPE>
{
public:
//TODO: (ATL) typedef
typedef POSITION iterator;
typedef POSITION const_iterator;
//TODO: (ATL) Status
int GetCount() const
{
return CAtlList<TYPE>::GetCount();
}
int GetSize() const
{
return CAtlList<TYPE>::GetCount();
}
bool IsEmpty() const
{
return CAtlList<TYPE>::IsEmpty();
}
//TODO: (ATL) Operators
void RemoveAll()
{
CAtlList<TYPE>::RemoveAll();
}
void AddToEnd(const TYPE& x)
{
CAtlList<TYPE>::AddTail(x);
}
TYPE GetFromFront() const
{
return IsEmpty() ? NULL : CAtlList<TYPE>::RemoveHead();
}
//TODO: (ATL) IteratorValid?
bool IteratorValid(const iterator &iter) const
{
return iter != NULL;
}
};
#endif
//-------------------------------------------------------------------------------
// TODO: CQueueT is use STL
//-------------------------------------------------------------------------------
#if !defined(CQueueT)
//#include <queue>
#include <list>
template <typename TYPE>
class CQueueT : private std::list<TYPE>
{
public:
//TODO: (STL) typedef
typedef std::list<TYPE>::iterator iterator;
typedef std::list<TYPE>::const_iterator const_iterator;
protected:
public:
//TODO: (STL) Attributes
int GetCount() const
{
return size();
}
int GetSize() const
{
return size();
}
bool IsEmpty() const
{
return empty();
}
//TODO: (STL) Operators
void RemoveAll()
{
clear();
}
void AddToEnd(const TYPE& x)
{
push_back(x);
}
TYPE GetFromFront() const
{
if (IsEmpty())
return end();
TYPE ret = std::list<TYPE>::front();
std::list<TYPE>::pop_front();
return ret;
}
//TODO: (STL) IteratorValid?
bool IteratorValid(const_iterator &iter) const
{
return (iter != end());
}
bool IteratorValid(iterator &iter)
{
return (iter != end());
}
};
#endif
#endif // __QUEUET_H__ (whole file)