vfs: check userland buffers before reading them.
[haiku.git] / src / servers / registrar / AppInfoList.h
blob61fa5015f8df7ab604fb6892f12a7f0e03db36d0
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:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
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.
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 //------------------------------------------------------------------------------
27 #ifndef APP_INFO_LIST_H
28 #define APP_INFO_LIST_H
30 #include <List.h>
31 #include <OS.h>
33 class entry_ref;
35 class RosterAppInfo;
37 // AppInfoList
38 class AppInfoList {
39 public:
40 class Iterator;
42 public:
43 AppInfoList();
44 virtual ~AppInfoList();
46 bool AddInfo(RosterAppInfo *info);
47 bool RemoveInfo(RosterAppInfo *info);
48 void MakeEmpty(bool deleteInfos = false);
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;
55 bool IsEmpty() const { return (CountInfos() == 0); };
56 int32 CountInfos() const;
58 Iterator It();
60 void Sort(bool (*lessFunc)(const RosterAppInfo *, const RosterAppInfo *));
62 private:
63 RosterAppInfo *RemoveInfo(int32 index);
65 RosterAppInfo *InfoAt(int32 index) const;
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;
73 private:
74 friend class Iterator;
76 private:
77 BList fInfos;
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)
90 inline ~Iterator() {}
92 inline bool IsValid() const
94 return (fIndex >= 0 && fIndex < fCount);
97 inline RosterAppInfo *Remove()
99 RosterAppInfo *info = fList->RemoveInfo(fIndex);
100 if (info)
101 fCount--;
102 return info;
105 inline Iterator &operator=(const Iterator &it)
107 fList = it.fList;
108 fIndex = it.fIndex;
109 fCount = it.fCount;
110 return *this;
113 inline Iterator &operator++()
115 fIndex++;
116 return *this;
119 inline Iterator operator++(int)
121 return Iterator(fList, fIndex + 1);
124 inline Iterator &operator--()
126 fIndex--;
127 return *this;
130 inline Iterator operator--(int)
132 return Iterator(fList, fIndex - 1);
135 inline bool operator==(const Iterator &it) const
137 return (fList == it.fList && fIndex == it.fIndex);
140 inline bool operator!=(const Iterator &it) const
142 return !(*this == it);
145 inline RosterAppInfo *operator*() const
147 return fList->InfoAt(fIndex);
150 private:
151 friend class AppInfoList;
153 private:
154 inline Iterator(AppInfoList *list, int32 index = 0)
155 : fList(list),
156 fIndex(index),
157 fCount(list->CountInfos())
161 private:
162 AppInfoList *fList;
163 int32 fIndex;
164 int32 fCount;
167 #endif // APP_INFO_LIST_H