xref: /haiku/src/servers/registrar/AppInfoList.h (revision f2ced752a08ff5d2618826bcd3ae3976c9f3e92e)
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 class entry_ref;
34 
35 class 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();
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 	int32 CountInfos() const;
56 
57 	Iterator It();
58 
59 private:
60 	RosterAppInfo *RemoveInfo(int32 index);
61 
62 	RosterAppInfo *InfoAt(int32 index) const;
63 
64 	int32 IndexOf(RosterAppInfo *info) const;
65 	int32 IndexOf(const char *signature) const;
66 	int32 IndexOf(team_id team) const;
67 	int32 IndexOf(const entry_ref *ref) const;
68 	int32 IndexOfToken(uint32 token) const;
69 
70 private:
71 	friend class Iterator;
72 
73 private:
74 	BList	fInfos;
75 };
76 
77 // AppInfoList::Iterator
78 class AppInfoList::Iterator {
79 public:
80 	inline Iterator(const Iterator &it)
81 		: fList(it.fList),
82 		  fIndex(it.fIndex),
83 		  fCount(it.fCount)
84 	{
85 	}
86 
87 	inline ~Iterator() {}
88 
89 	inline bool IsValid() const
90 	{
91 		return (fIndex >= 0 && fIndex < fCount);
92 	}
93 
94 	inline RosterAppInfo *Remove()
95 	{
96 		RosterAppInfo *info = fList->RemoveInfo(fIndex);
97 		if (info)
98 			fCount--;
99 		return info;
100 	}
101 
102 	inline Iterator &operator=(const Iterator &it)
103 	{
104 		fList = it.fList;
105 		fIndex = it.fIndex;
106 		fCount = it.fCount;
107 		return *this;
108 	}
109 
110 	inline Iterator &operator++()
111 	{
112 		fIndex++;
113 		return *this;
114 	}
115 
116 	inline Iterator operator++(int)
117 	{
118 		return Iterator(fList, fIndex + 1);
119 	}
120 
121 	inline Iterator &operator--()
122 	{
123 		fIndex--;
124 		return *this;
125 	}
126 
127 	inline Iterator operator--(int)
128 	{
129 		return Iterator(fList, fIndex - 1);
130 	}
131 
132 	inline bool operator==(const Iterator &it) const
133 	{
134 		return (fList == it.fList && fIndex == it.fIndex);
135 	}
136 
137 	inline bool operator!=(const Iterator &it) const
138 	{
139 		return !(*this == it);
140 	}
141 
142 	inline RosterAppInfo *operator*() const
143 	{
144 		return fList->InfoAt(fIndex);
145 	}
146 
147 private:
148 	friend class AppInfoList;
149 
150 private:
151 	inline Iterator(AppInfoList *list, int32 index = 0)
152 		: fList(list),
153 		  fIndex(index),
154 		  fCount(list->CountInfos())
155 	{
156 	}
157 
158 private:
159 	AppInfoList	*fList;
160 	int32		fIndex;
161 	int32		fCount;
162 };
163 
164 #endif	// APP_INFO_LIST_H
165