出自ProgWiki
用途
- List模板,打消 MFC / ATL / STL 的 List 差異。
程式碼
//File: ListT.h
//Description: template CListT from
// CList of MFC
// CAtlList of ATL
// std::list of STL
#ifndef __LISTT_H__
#define __LISTT_H__
#if _MSC_VER > 1000
#pragma once
#endif
//-------------------------------------------------------------------------------
// TODO: CListT is use MFC
//-------------------------------------------------------------------------------
#if !defined(CListT) && defined(_AFX) && defined(_Use_MFC)
#include <afxtempl.h>
template <typename TYPE>
class CListT : public CList<TYPE>
{
public:
//TODO: (MFC) typedef
typedef POSITION iterator;
typedef POSITION const_iterator;
//TODO: (MFC) IteratorValid?
bool IteratorValid(const iterator &iter) const
{
return iter != NULL;
}
};
#endif //for MFC
//-------------------------------------------------------------------------------
// TODO: CListT is use ATL
//-------------------------------------------------------------------------------
#if !defined(CListT) && defined(ATL) && defined(_Use_ATL)
#include <atlcoll.h>
template <typename TYPE>
class CListT : public CAtlList<TYPE>
{
//TODO: (ATL) typedef
typedef POSITION iterator;
typedef POSITION const_iterator;
//TODO: (ATL) IteratorValid?
bool IteratorValid(const iterator &iter) const
{
return iter != NULL;
}
}
#endif //for ATL
//-------------------------------------------------------------------------------
// TODO: CListT is use STL
//-------------------------------------------------------------------------------
#if !defined(CListT)
#include <assert.h>
#include <list>
template <typename TYPE>
class CListT : private std::list<TYPE>
{
public:
//TODO: (STL) typedef
typedef std::list<TYPE>::iterator iterator;
typedef std::list<TYPE>::const_iterator const_iterator;
//TODO: (STL) Status
int GetCount() const
{
return size();
}
int GetSize() const
{
return size();
}
bool IsEmpty() const
{
return empty();
}
//TODO: (STL) Add / Set
void AddTail(const TYPE& x)
{
push_back(x);
}
void AddHead(const TYPE& x)
{
push_front(x);
}
void SetAt(const iterator& pos, const TYPE& x)
{
*pos = x;
}
void SetAt(const_iterator& pos, const TYPE& x)
{
*pos = x;
}
//DOTO: (STL) Insertion
iterator InsertBefore(iterator pos, const TYPE& x)
{
assert(IteratorValid(pos));
iterator posSave = pos;
if (IteratorValid(--pos))
{
return insert(pos, x)
}
else
{
push_front(x);
--posSave;
assert(IteratorValid(posSave));
return posSave;
}
}
iterator InsertAfter(iterator pos, const TYPE& x)
{
assert(IteratorValid(pos));
iterator posSave = pos;
if (IteratorValid(++pos))
{
return insert(pos, x)
}
else
{
push_back(x);
++posSave;
assert(IteratorValid(posSave));
return posSave;
}
}
//TODO: (STL) Remove
void RemoveHead()
{
pop_front();
}
void RemoveTail()
{
pop_back();
}
void RemoveAll()
{
clear();
}
void RemoveAt(iterator& pos)
{
pos = erase(pos);
}
//TODO: (STL) Get
TYPE& GetHead()
{
return front();
}
TYPE GetHead() const
{
return front();
}
TYPE& GetTail()
{
return back();
}
TYPE GetTail() const
{
return back();
}
TYPE& GetNext(iterator& pos)
{
return *pos++;
}
TYPE GetNext(const_iterator& pos) const
{
return *pos++;
}
TYPE& GetPrev(iterator& pos)
{
return *pos--;
}
TYPE GetPrev(iterator& pos) const
{
return *pos--;
}
TYPE& GetAt(const iterator& pos)
{
return *pos;
}
TYPE GetAt(const_iterator& pos) const
{
return *pos;
}
//TODO: (STL) GetPos
iterator GetHeadPosition()
{
return begin();
}
const_iterator GetHeadPosition() const
{
return begin();
}
iterator GetTailPosition()
{
return back();
}
const_iterator GetTailPosition() const
{
return back();
}
//TODO: (STL) IteratorValid?
bool IteratorValid(const_iterator &iter) const
{
return (iter != end());
}
bool IteratorValid(iterator &iter)
{
return (iter != end());
}
//TODO: (STL) Searching
iterator Find(const TYPE& x)
{
iterator pos = GetHeadPosition();
while (IteratorValid(pos))
{
const TYPE& y = GetAt(pos);
if (x == y)
{
return pos;
}
pos++;
}
return end();
}
iterator Find(const TYPE& x, iterator& startAfter)
{
iterator pos = startAfter;
while (IteratorValid(pos))
{
const TYPE& y = GetAt(pos);
if (x == y)
{
return pos;
}
pos++;
}
return end();
}
iterator FindIndex(int iIndex)
{
iterator iter = begin();
int t = 0;
while (t != iIndex)
{
iter++;
t++;
}
return iter;
}
const_iterator FindIndex(int iIndex) const
{
const_iterator iter = begin();
int t = 0;
while (t != iIndex)
{
iter++;
t++;
}
return iter;
}
};
#endif // for STL
#endif // __LISTT_H__ (whole file)