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