1 /*---------------------------------------------------------------------------*
2   Project:  My template library
3   File:     mytl.h
4 
5   (C)2005 HUDSON SOFT
6 
7   $Header: /home/cvsroot/SDK/include/win32/mytl.h,v 1.2 2006/03/09 12:28:40 yasuh-to Exp $
8 
9   $NoKeywords: $
10  *---------------------------------------------------------------------------*/
11 
12 #ifndef	__MYTL_H__
13 #define	__MYTL_H__
14 
15 #include <assert.h>
16 #include <deque>
17 
18 using namespace std;
19 
20 ///////////////////////////////////////////////////////////////////////////////
21 //
22 // TMyList
23 //
24 
25 template <class T>
26 class TMYList : public deque<T>
27 {
28 public:
29 	typedef BOOL	(* COMPARE)(T pItem1, LPVOID pData);
30 
31 	// Get the number of lists (to control warnings)
GetCount()32 	int	GetCount() { return (int)size(); }
33 
34 	// Get the list item corresponding to the index
GetItem(int nIndex)35 	T	GetItem(int nIndex)
36 	{
37 		assert((nIndex >= 0) && (nIndex < GetCount()));
38 		return at(nIndex);
39 	}
40 
41 	// Add an item to the end of the list
AddTail(T pItem)42 	int	AddTail(T pItem)
43 	{
44 		push_back(pItem);
45 		return GetCount() - 1;
46 	}
47 
48 	// Get the index for the specified item
IndexOf(T pItem)49 	int	IndexOf(T pItem)
50 	{
51 		for (int i=0; i<GetCount(); i++)
52 		{
53 			if ( at(i) == pItem ) return i;
54 		}
55 		return -1;
56 	}
57 
58 	// Get the index for the item set to the specified data
IndexOf(LPVOID pData,COMPARE fncCompare)59 	int	IndexOf(LPVOID pData, COMPARE fncCompare)
60 	{
61 		assert(fncCompare != NULL);
62 		for (int i=0; i<GetCount(); i++)
63 		{
64 			if ( fncCompare(at(i), pData) ) return i;
65 		}
66 		return -1;
67 	}
68 
69 	// Delete the list item corresponding to the index
70 	void	Remove(int nIndex, BOOL bDelItem=TRUE)
71 	{
72 		assert((nIndex >= 0) && (nIndex < GetCount()));
73 		if ( bDelItem ) delete at(nIndex);
74 		erase(begin() + nIndex);
75 	}
76 
77 	// Delete the specified item
78 	void	Remove(T pItem, BOOL bDelItem=TRUE)
79 	{
80 		int nIndex = IndexOf(pItem);
81 		assert(nIndex != -1);
82 		if ( bDelItem ) delete at(nIndex);
83 		erase(begin() + nIndex);
84 	}
85 
86 	// Clear the list
87 	void	Clear(BOOL bDelItem=TRUE)
88 	{
89 		if ( bDelItem )
90 		{
91 			for (int i=0; i<GetCount(); i++) delete at(i);
92 		}
93 		clear();
94 	}
95 };
96 
97 #endif	// __MYTL_H__
98