xref: /haiku/src/servers/registrar/AppInfoList.h (revision f7c507c3a6fbf3a44c59500543926a9088724968)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		AppInfoList.h
23 //	Author:			Ingo Weinhold (bonefish@users.sf.net)
24 //	Description:	A helper class for TRoster. A list of RosterAppInfos.
25 //------------------------------------------------------------------------------
26 
27 #ifndef APP_INFO_LIST_H
28 #define APP_INFO_LIST_H
29 
30 #include <List.h>
31 #include <OS.h>
32 
33 struct entry_ref;
34 
35 struct RosterAppInfo;
36 
37 // AppInfoList
38 class AppInfoList {
39 public:
40 	class Iterator;
41 
42 public:
43 	AppInfoList();
44 	virtual ~AppInfoList();
45 
46 	bool AddInfo(RosterAppInfo *info);
47 	bool RemoveInfo(RosterAppInfo *info);
48 	void MakeEmpty(bool deleteInfos = false);
49 
50 	RosterAppInfo *InfoFor(const char *signature) const;
51 	RosterAppInfo *InfoFor(team_id team) const;
52 	RosterAppInfo *InfoFor(const entry_ref *ref) const;
53 	RosterAppInfo *InfoForToken(uint32 token) const;
54 
55 	bool IsEmpty() const		{ return (CountInfos() == 0); };
56 	int32 CountInfos() const;
57 
58 	Iterator It();
59 
60 	void Sort(bool (*lessFunc)(const RosterAppInfo *, const RosterAppInfo *));
61 
62 private:
63 	RosterAppInfo *RemoveInfo(int32 index);
64 
65 	RosterAppInfo *InfoAt(int32 index) const;
66 
67 	int32 IndexOf(RosterAppInfo *info) const;
68 	int32 IndexOf(const char *signature) const;
69 	int32 IndexOf(team_id team) const;
70 	int32 IndexOf(const entry_ref *ref) const;
71 	int32 IndexOfToken(uint32 token) const;
72 
73 private:
74 	friend class Iterator;
75 
76 private:
77 	BList	fInfos;
78 };
79 
80 // AppInfoList::Iterator
81 class AppInfoList::Iterator {
82 public:
83 	inline Iterator(const Iterator &it)
84 		: fList(it.fList),
85 		  fIndex(it.fIndex),
86 		  fCount(it.fCount)
87 	{
88 	}
89 
90 	inline ~Iterator() {}
91 
92 	inline bool IsValid() const
93 	{
94 		return (fIndex >= 0 && fIndex < fCount);
95 	}
96 
97 	inline RosterAppInfo *Remove()
98 	{
99 		RosterAppInfo *info = fList->RemoveInfo(fIndex);
100 		if (info)
101 			fCount--;
102 		return info;
103 	}
104 
105 	inline Iterator &operator=(const Iterator &it)
106 	{
107 		fList = it.fList;
108 		fIndex = it.fIndex;
109 		fCount = it.fCount;
110 		return *this;
111 	}
112 
113 	inline Iterator &operator++()
114 	{
115 		fIndex++;
116 		return *this;
117 	}
118 
119 	inline Iterator operator++(int)
120 	{
121 		return Iterator(fList, fIndex + 1);
122 	}
123 
124 	inline Iterator &operator--()
125 	{
126 		fIndex--;
127 		return *this;
128 	}
129 
130 	inline Iterator operator--(int)
131 	{
132 		return Iterator(fList, fIndex - 1);
133 	}
134 
135 	inline bool operator==(const Iterator &it) const
136 	{
137 		return (fList == it.fList && fIndex == it.fIndex);
138 	}
139 
140 	inline bool operator!=(const Iterator &it) const
141 	{
142 		return !(*this == it);
143 	}
144 
145 	inline RosterAppInfo *operator*() const
146 	{
147 		return fList->InfoAt(fIndex);
148 	}
149 
150 private:
151 	friend class AppInfoList;
152 
153 private:
154 	inline Iterator(AppInfoList *list, int32 index = 0)
155 		: fList(list),
156 		  fIndex(index),
157 		  fCount(list->CountInfos())
158 	{
159 	}
160 
161 private:
162 	AppInfoList	*fList;
163 	int32		fIndex;
164 	int32		fCount;
165 };
166 
167 #endif	// APP_INFO_LIST_H
168