1/*
2
3TList.h
4
5Copyright (c) 2002 OpenBeOS.
6
7Author:
8	Michael Pfeiffer
9
10Permission is hereby granted, free of charge, to any person obtaining a copy of
11this software and associated documentation files (the "Software"), to deal in
12the Software without restriction, including without limitation the rights to
13use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14of the Software, and to permit persons to whom the Software is furnished to do
15so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27
28*/
29
30/*
31TODO: Move this header file into a "common" directory!
32This class is an minor extended version of the one found in
33current/src/add-ons/print/drivers/pdf/source
34*/
35
36#ifndef _TLIST_H
37#define _TLIST_H
38
39#include <List.h>
40
41template <class T>
42class TList {
43private:
44	BList fList;
45	bool  fOwnsItems;
46
47	typedef int (*sort_func)(const void*, const void*);
48
49public:
50	TList(bool ownsItems = true) : fOwnsItems(ownsItems) { }
51	virtual ~TList();
52	void     MakeEmpty();
53	int32    CountItems() const;
54	T*       ItemAt(int32 index) const;
55	void     AddItem(T* p);
56	T*       Items();
57	void     SortItems(int (*comp)(const T**, const T**));
58
59};
60
61// TList
62template<class T>
63TList<T>::~TList() {
64	MakeEmpty();
65}
66
67
68template<class T>
69void TList<T>::MakeEmpty() {
70	const int32 n = CountItems();
71	if (fOwnsItems) {
72		for (int i = 0; i < n; i++) {
73			delete ItemAt(i);
74		}
75	}
76	fList.MakeEmpty();
77}
78
79
80template<class T>
81int32 TList<T>::CountItems() const {
82	return fList.CountItems();
83}
84
85
86template<class T>
87T* TList<T>::ItemAt(int32 index) const {
88	return (T*)fList.ItemAt(index);
89}
90
91
92template<class T>
93void TList<T>::AddItem(T* p) {
94	fList.AddItem(p);
95}
96
97
98template<class T>
99T* TList<T>::Items() {
100	return (T*)fList.Items();
101}
102
103
104template<class T>
105void TList<T>::SortItems(int (*comp)(const T**, const T**)) {
106	sort_func sort = (sort_func)comp;
107	fList.SortItems(sort);
108}
109
110#endif
111