vfs: check userland buffers before reading them.
[haiku.git] / src / kits / debugger / model / TargetHost.cpp
blob42c90ed4a680477d9f05efddc636270545d7a5bd
1 /*
2 * Copyright 2016, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
6 #include "TargetHost.h"
8 #include <AutoLocker.h>
10 #include "TeamInfo.h"
13 TargetHost::TargetHost(const BString& name)
15 BReferenceable(),
16 fName(name),
17 fLock(),
18 fListeners(),
19 fTeams()
24 TargetHost::~TargetHost()
26 while (!fTeams.IsEmpty())
27 delete fTeams.RemoveItemAt((int32)0);
31 void
32 TargetHost::AddListener(Listener* listener)
34 AutoLocker<TargetHost> hostLocker(this);
35 fListeners.Add(listener);
39 void
40 TargetHost::RemoveListener(Listener* listener)
42 AutoLocker<TargetHost> hostLocker(this);
43 fListeners.Remove(listener);
47 int32
48 TargetHost::CountTeams() const
50 return fTeams.CountItems();
54 status_t
55 TargetHost::AddTeam(const team_info& info)
57 TeamInfo* teamInfo = new (std::nothrow) TeamInfo(info.team, info);
58 if (teamInfo == NULL)
59 return B_NO_MEMORY;
61 if (!fTeams.BinaryInsert(teamInfo, &_CompareTeams))
62 return B_NO_MEMORY;
64 _NotifyTeamAdded(teamInfo);
65 return B_OK;
69 void
70 TargetHost::RemoveTeam(team_id team)
72 int32 index = fTeams.BinarySearchIndexByKey(team,
73 &_FindTeamByKey);
74 if (index < 0)
75 return;
77 _NotifyTeamRemoved(team);
78 TeamInfo* info = fTeams.RemoveItemAt(index);
79 delete info;
83 void
84 TargetHost::UpdateTeam(const team_info& info)
86 int32 index = fTeams.BinarySearchIndexByKey(info.team,
87 &_FindTeamByKey);
88 if (index < 0)
89 return;
91 TeamInfo* teamInfo = fTeams.ItemAt(index);
92 teamInfo->SetTo(info.team, info);
93 _NotifyTeamRenamed(teamInfo);
97 TeamInfo*
98 TargetHost::TeamInfoAt(int32 index) const
100 return fTeams.ItemAt(index);
104 TeamInfo*
105 TargetHost::TeamInfoByID(team_id team) const
107 return fTeams.BinarySearchByKey(team, &_FindTeamByKey);
111 /*static*/ int
112 TargetHost::_CompareTeams(const TeamInfo* a, const TeamInfo* b)
114 return a->TeamID() < b->TeamID() ? -1 : 1;
118 /*static*/ int
119 TargetHost::_FindTeamByKey(const team_id* id, const TeamInfo* info)
121 if (*id < info->TeamID())
122 return -1;
123 else if (*id > info->TeamID())
124 return 1;
125 return 0;
129 void
130 TargetHost::_NotifyTeamAdded(TeamInfo* info)
132 for (ListenerList::Iterator it = fListeners.GetIterator();
133 Listener* listener = it.Next();) {
134 listener->TeamAdded(info);
139 void
140 TargetHost::_NotifyTeamRemoved(team_id team)
142 for (ListenerList::Iterator it = fListeners.GetIterator();
143 Listener* listener = it.Next();) {
144 listener->TeamRemoved(team);
149 void
150 TargetHost::_NotifyTeamRenamed(TeamInfo* info)
152 for (ListenerList::Iterator it = fListeners.GetIterator();
153 Listener* listener = it.Next();) {
154 listener->TeamRenamed(info);
159 // #pragma mark - TargetHost::Listener
162 TargetHost::Listener::~Listener()
167 void
168 TargetHost::Listener::TeamAdded(TeamInfo* info)
173 void
174 TargetHost::Listener::TeamRemoved(team_id team)
179 void
180 TargetHost::Listener::TeamRenamed(TeamInfo* info)