xref: /haiku/src/kits/debugger/model/TargetHost.cpp (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
1*fce4895dSRene Gollent /*
2*fce4895dSRene Gollent  * Copyright 2016, Rene Gollent, rene@gollent.com.
3*fce4895dSRene Gollent  * Distributed under the terms of the MIT License.
4*fce4895dSRene Gollent  */
5*fce4895dSRene Gollent 
6*fce4895dSRene Gollent #include "TargetHost.h"
7*fce4895dSRene Gollent 
8*fce4895dSRene Gollent #include <AutoLocker.h>
9*fce4895dSRene Gollent 
10*fce4895dSRene Gollent #include "TeamInfo.h"
11*fce4895dSRene Gollent 
12*fce4895dSRene Gollent 
TargetHost(const BString & name)13*fce4895dSRene Gollent TargetHost::TargetHost(const BString& name)
14*fce4895dSRene Gollent 	:
15*fce4895dSRene Gollent 	BReferenceable(),
16*fce4895dSRene Gollent 	fName(name),
17*fce4895dSRene Gollent 	fLock(),
18*fce4895dSRene Gollent 	fListeners(),
19*fce4895dSRene Gollent 	fTeams()
20*fce4895dSRene Gollent {
21*fce4895dSRene Gollent }
22*fce4895dSRene Gollent 
23*fce4895dSRene Gollent 
~TargetHost()24*fce4895dSRene Gollent TargetHost::~TargetHost()
25*fce4895dSRene Gollent {
26*fce4895dSRene Gollent 	while (!fTeams.IsEmpty())
27*fce4895dSRene Gollent 		delete fTeams.RemoveItemAt((int32)0);
28*fce4895dSRene Gollent }
29*fce4895dSRene Gollent 
30*fce4895dSRene Gollent 
31*fce4895dSRene Gollent void
AddListener(Listener * listener)32*fce4895dSRene Gollent TargetHost::AddListener(Listener* listener)
33*fce4895dSRene Gollent {
34*fce4895dSRene Gollent 	AutoLocker<TargetHost> hostLocker(this);
35*fce4895dSRene Gollent 	fListeners.Add(listener);
36*fce4895dSRene Gollent }
37*fce4895dSRene Gollent 
38*fce4895dSRene Gollent 
39*fce4895dSRene Gollent void
RemoveListener(Listener * listener)40*fce4895dSRene Gollent TargetHost::RemoveListener(Listener* listener)
41*fce4895dSRene Gollent {
42*fce4895dSRene Gollent 	AutoLocker<TargetHost> hostLocker(this);
43*fce4895dSRene Gollent 	fListeners.Remove(listener);
44*fce4895dSRene Gollent }
45*fce4895dSRene Gollent 
46*fce4895dSRene Gollent 
47*fce4895dSRene Gollent int32
CountTeams() const48*fce4895dSRene Gollent TargetHost::CountTeams() const
49*fce4895dSRene Gollent {
50*fce4895dSRene Gollent 	return fTeams.CountItems();
51*fce4895dSRene Gollent }
52*fce4895dSRene Gollent 
53*fce4895dSRene Gollent 
54*fce4895dSRene Gollent status_t
AddTeam(const team_info & info)55*fce4895dSRene Gollent TargetHost::AddTeam(const team_info& info)
56*fce4895dSRene Gollent {
57*fce4895dSRene Gollent 	TeamInfo* teamInfo = new (std::nothrow) TeamInfo(info.team, info);
58*fce4895dSRene Gollent 	if (teamInfo == NULL)
59*fce4895dSRene Gollent 		return B_NO_MEMORY;
60*fce4895dSRene Gollent 
61*fce4895dSRene Gollent 	if (!fTeams.BinaryInsert(teamInfo, &_CompareTeams))
62*fce4895dSRene Gollent 		return B_NO_MEMORY;
63*fce4895dSRene Gollent 
64*fce4895dSRene Gollent 	_NotifyTeamAdded(teamInfo);
65*fce4895dSRene Gollent 	return B_OK;
66*fce4895dSRene Gollent }
67*fce4895dSRene Gollent 
68*fce4895dSRene Gollent 
69*fce4895dSRene Gollent void
RemoveTeam(team_id team)70*fce4895dSRene Gollent TargetHost::RemoveTeam(team_id team)
71*fce4895dSRene Gollent {
72*fce4895dSRene Gollent 	int32 index = fTeams.BinarySearchIndexByKey(team,
73*fce4895dSRene Gollent 		&_FindTeamByKey);
74*fce4895dSRene Gollent 	if (index < 0)
75*fce4895dSRene Gollent 		return;
76*fce4895dSRene Gollent 
77*fce4895dSRene Gollent 	_NotifyTeamRemoved(team);
78*fce4895dSRene Gollent 	TeamInfo* info = fTeams.RemoveItemAt(index);
79*fce4895dSRene Gollent 	delete info;
80*fce4895dSRene Gollent }
81*fce4895dSRene Gollent 
82*fce4895dSRene Gollent 
83*fce4895dSRene Gollent void
UpdateTeam(const team_info & info)84*fce4895dSRene Gollent TargetHost::UpdateTeam(const team_info& info)
85*fce4895dSRene Gollent {
86*fce4895dSRene Gollent 	int32 index = fTeams.BinarySearchIndexByKey(info.team,
87*fce4895dSRene Gollent 		&_FindTeamByKey);
88*fce4895dSRene Gollent 	if (index < 0)
89*fce4895dSRene Gollent 		return;
90*fce4895dSRene Gollent 
91*fce4895dSRene Gollent 	TeamInfo* teamInfo = fTeams.ItemAt(index);
92*fce4895dSRene Gollent 	teamInfo->SetTo(info.team, info);
93*fce4895dSRene Gollent 	_NotifyTeamRenamed(teamInfo);
94*fce4895dSRene Gollent }
95*fce4895dSRene Gollent 
96*fce4895dSRene Gollent 
97*fce4895dSRene Gollent TeamInfo*
TeamInfoAt(int32 index) const98*fce4895dSRene Gollent TargetHost::TeamInfoAt(int32 index) const
99*fce4895dSRene Gollent {
100*fce4895dSRene Gollent 	return fTeams.ItemAt(index);
101*fce4895dSRene Gollent }
102*fce4895dSRene Gollent 
103*fce4895dSRene Gollent 
104*fce4895dSRene Gollent TeamInfo*
TeamInfoByID(team_id team) const105*fce4895dSRene Gollent TargetHost::TeamInfoByID(team_id team) const
106*fce4895dSRene Gollent {
107*fce4895dSRene Gollent 	return fTeams.BinarySearchByKey(team, &_FindTeamByKey);
108*fce4895dSRene Gollent }
109*fce4895dSRene Gollent 
110*fce4895dSRene Gollent 
111*fce4895dSRene Gollent /*static*/ int
_CompareTeams(const TeamInfo * a,const TeamInfo * b)112*fce4895dSRene Gollent TargetHost::_CompareTeams(const TeamInfo* a, const TeamInfo* b)
113*fce4895dSRene Gollent {
114*fce4895dSRene Gollent 	return a->TeamID() < b->TeamID() ? -1 : 1;
115*fce4895dSRene Gollent }
116*fce4895dSRene Gollent 
117*fce4895dSRene Gollent 
118*fce4895dSRene Gollent /*static*/ int
_FindTeamByKey(const team_id * id,const TeamInfo * info)119*fce4895dSRene Gollent TargetHost::_FindTeamByKey(const team_id* id, const TeamInfo* info)
120*fce4895dSRene Gollent {
121*fce4895dSRene Gollent 	if (*id < info->TeamID())
122*fce4895dSRene Gollent 		return -1;
123*fce4895dSRene Gollent 	else if (*id > info->TeamID())
124*fce4895dSRene Gollent 		return 1;
125*fce4895dSRene Gollent 	return 0;
126*fce4895dSRene Gollent }
127*fce4895dSRene Gollent 
128*fce4895dSRene Gollent 
129*fce4895dSRene Gollent void
_NotifyTeamAdded(TeamInfo * info)130*fce4895dSRene Gollent TargetHost::_NotifyTeamAdded(TeamInfo* info)
131*fce4895dSRene Gollent {
132*fce4895dSRene Gollent 	for (ListenerList::Iterator it = fListeners.GetIterator();
133*fce4895dSRene Gollent 			Listener* listener = it.Next();) {
134*fce4895dSRene Gollent 		listener->TeamAdded(info);
135*fce4895dSRene Gollent 	}
136*fce4895dSRene Gollent }
137*fce4895dSRene Gollent 
138*fce4895dSRene Gollent 
139*fce4895dSRene Gollent void
_NotifyTeamRemoved(team_id team)140*fce4895dSRene Gollent TargetHost::_NotifyTeamRemoved(team_id team)
141*fce4895dSRene Gollent {
142*fce4895dSRene Gollent 	for (ListenerList::Iterator it = fListeners.GetIterator();
143*fce4895dSRene Gollent 			Listener* listener = it.Next();) {
144*fce4895dSRene Gollent 		listener->TeamRemoved(team);
145*fce4895dSRene Gollent 	}
146*fce4895dSRene Gollent }
147*fce4895dSRene Gollent 
148*fce4895dSRene Gollent 
149*fce4895dSRene Gollent void
_NotifyTeamRenamed(TeamInfo * info)150*fce4895dSRene Gollent TargetHost::_NotifyTeamRenamed(TeamInfo* info)
151*fce4895dSRene Gollent {
152*fce4895dSRene Gollent 	for (ListenerList::Iterator it = fListeners.GetIterator();
153*fce4895dSRene Gollent 			Listener* listener = it.Next();) {
154*fce4895dSRene Gollent 		listener->TeamRenamed(info);
155*fce4895dSRene Gollent 	}
156*fce4895dSRene Gollent }
157*fce4895dSRene Gollent 
158*fce4895dSRene Gollent 
159*fce4895dSRene Gollent // #pragma mark - TargetHost::Listener
160*fce4895dSRene Gollent 
161*fce4895dSRene Gollent 
~Listener()162*fce4895dSRene Gollent TargetHost::Listener::~Listener()
163*fce4895dSRene Gollent {
164*fce4895dSRene Gollent }
165*fce4895dSRene Gollent 
166*fce4895dSRene Gollent 
167*fce4895dSRene Gollent void
TeamAdded(TeamInfo * info)168*fce4895dSRene Gollent TargetHost::Listener::TeamAdded(TeamInfo* info)
169*fce4895dSRene Gollent {
170*fce4895dSRene Gollent }
171*fce4895dSRene Gollent 
172*fce4895dSRene Gollent 
173*fce4895dSRene Gollent void
TeamRemoved(team_id team)174*fce4895dSRene Gollent TargetHost::Listener::TeamRemoved(team_id team)
175*fce4895dSRene Gollent {
176*fce4895dSRene Gollent }
177*fce4895dSRene Gollent 
178*fce4895dSRene Gollent 
179*fce4895dSRene Gollent void
TeamRenamed(TeamInfo * info)180*fce4895dSRene Gollent TargetHost::Listener::TeamRenamed(TeamInfo* info)
181*fce4895dSRene Gollent {
182*fce4895dSRene Gollent }
183