2 * Copyright 2016, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
6 #include "TargetHost.h"
8 #include <AutoLocker.h>
13 TargetHost::TargetHost(const BString
& name
)
24 TargetHost::~TargetHost()
26 while (!fTeams
.IsEmpty())
27 delete fTeams
.RemoveItemAt((int32
)0);
32 TargetHost::AddListener(Listener
* listener
)
34 AutoLocker
<TargetHost
> hostLocker(this);
35 fListeners
.Add(listener
);
40 TargetHost::RemoveListener(Listener
* listener
)
42 AutoLocker
<TargetHost
> hostLocker(this);
43 fListeners
.Remove(listener
);
48 TargetHost::CountTeams() const
50 return fTeams
.CountItems();
55 TargetHost::AddTeam(const team_info
& info
)
57 TeamInfo
* teamInfo
= new (std::nothrow
) TeamInfo(info
.team
, info
);
61 if (!fTeams
.BinaryInsert(teamInfo
, &_CompareTeams
))
64 _NotifyTeamAdded(teamInfo
);
70 TargetHost::RemoveTeam(team_id team
)
72 int32 index
= fTeams
.BinarySearchIndexByKey(team
,
77 _NotifyTeamRemoved(team
);
78 TeamInfo
* info
= fTeams
.RemoveItemAt(index
);
84 TargetHost::UpdateTeam(const team_info
& info
)
86 int32 index
= fTeams
.BinarySearchIndexByKey(info
.team
,
91 TeamInfo
* teamInfo
= fTeams
.ItemAt(index
);
92 teamInfo
->SetTo(info
.team
, info
);
93 _NotifyTeamRenamed(teamInfo
);
98 TargetHost::TeamInfoAt(int32 index
) const
100 return fTeams
.ItemAt(index
);
105 TargetHost::TeamInfoByID(team_id team
) const
107 return fTeams
.BinarySearchByKey(team
, &_FindTeamByKey
);
112 TargetHost::_CompareTeams(const TeamInfo
* a
, const TeamInfo
* b
)
114 return a
->TeamID() < b
->TeamID() ? -1 : 1;
119 TargetHost::_FindTeamByKey(const team_id
* id
, const TeamInfo
* info
)
121 if (*id
< info
->TeamID())
123 else if (*id
> info
->TeamID())
130 TargetHost::_NotifyTeamAdded(TeamInfo
* info
)
132 for (ListenerList::Iterator it
= fListeners
.GetIterator();
133 Listener
* listener
= it
.Next();) {
134 listener
->TeamAdded(info
);
140 TargetHost::_NotifyTeamRemoved(team_id team
)
142 for (ListenerList::Iterator it
= fListeners
.GetIterator();
143 Listener
* listener
= it
.Next();) {
144 listener
->TeamRemoved(team
);
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()
168 TargetHost::Listener::TeamAdded(TeamInfo
* info
)
174 TargetHost::Listener::TeamRemoved(team_id team
)
180 TargetHost::Listener::TeamRenamed(TeamInfo
* info
)