#include <algorithm>
#include <strings.h>
#include "AppInfoList.h"
#include "RosterAppInfo.h"
\class AppInfoList
\brief A list of RosterAppInfos.
Features adding/removing of RosterAppInfos and method for finding
infos by signature, team ID, entry_ref or token.
The method It() returns an iterator, an instance of the basic
AppInfoList::Iterator class.
*/
*/
AppInfoList::AppInfoList()
: fInfos()
{
}
The RosterAppInfos the list contains are deleted.
*/
AppInfoList::~AppInfoList()
{
MakeEmpty(true);
}
\param info The RosterAppInfo to be added
\return \c true on success, false if \a info is \c NULL or there's not
enough memory for this operation.
*/
bool
AppInfoList::AddInfo(RosterAppInfo *info)
{
bool result = false;
if (info)
result = fInfos.AddItem(info);
return result;
}
\param info The RosterAppInfo to be removed
\return \c true on success, false if \a info was not in the list.
*/
bool
AppInfoList::RemoveInfo(RosterAppInfo *info)
{
return fInfos.RemoveItem(info);
}
*/
void
AppInfoList::MakeEmpty(bool deleteInfos)
{
if (deleteInfos) {
for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++)
delete info;
}
fInfos.MakeEmpty();
}
If the list contains more than one RosterAppInfo with the given signature,
it is undefined, which one is returned.
\param signature The signature
\return A RosterAppInfo with the supplied signature, or \c NULL, if
\a signature is \c NULL or the list doesn't contain an info with
this signature.
*/
RosterAppInfo *
AppInfoList::InfoFor(const char *signature) const
{
return InfoAt(IndexOf(signature));
}
\param team The team ID
\return A RosterAppInfo with the supplied team ID, or \c NULL, if the list
doesn't contain an info with this team ID.
*/
RosterAppInfo *
AppInfoList::InfoFor(team_id team) const
{
return InfoAt(IndexOf(team));
}
If the list contains more than one RosterAppInfo with the given entry_ref,
it is undefined, which one is returned.
\param ref The entry_ref
\return A RosterAppInfo with the supplied entry_ref, or \c NULL, if
\a ref is \c NULL or the list doesn't contain an info with
this entry_ref.
*/
RosterAppInfo *
AppInfoList::InfoFor(const entry_ref *ref) const
{
return InfoAt(IndexOf(ref));
}
If the list contains more than one RosterAppInfo with the given token,
it is undefined, which one is returned.
\param token The token
\return A RosterAppInfo with the supplied token, or \c NULL, if the list
doesn't contain an info with the token.
*/
RosterAppInfo *
AppInfoList::InfoForToken(uint32 token) const
{
return InfoAt(IndexOfToken(token));
}
\return The number of RosterAppInfos this list contains.
*/
int32
AppInfoList::CountInfos() const
{
return fInfos.CountItems();
}
\return A list iterator.
*/
AppInfoList::Iterator
AppInfoList::It()
{
return Iterator(this, 0);
}
function.
\param lessFunc The compare function (less than) to be used.
*/
void
AppInfoList::Sort(
bool (*lessFunc)(const RosterAppInfo *, const RosterAppInfo *))
{
int32 count = CountInfos();
if (count > 1) {
RosterAppInfo **infos = (RosterAppInfo **)fInfos.Items();
std::sort(infos, infos + count, lessFunc);
}
}
\param index The index of the info to be removed
\return A pointer to the removed RosterAppInfo, or \c NULL, if the index
is out of range.
*/
RosterAppInfo *
AppInfoList::RemoveInfo(int32 index)
{
return (RosterAppInfo*)fInfos.RemoveItem(index);
}
\param index The index of the info to be returned
\return A pointer to the RosterAppInfo, or \c NULL, if the index
is out of range.
*/
RosterAppInfo *
AppInfoList::InfoAt(int32 index) const
{
return (RosterAppInfo*)fInfos.ItemAt(index);
}
\param info The RosterAppInfo in question
\return The index of the supplied info, or -1, if \a info is \c NULL or not
contained in the list.
*/
int32
AppInfoList::IndexOf(RosterAppInfo *info) const
{
return fInfos.IndexOf(info);
}
signature.
If the list contains more than one RosterAppInfo with the given signature,
it is undefined, which one is returned.
\param signature The signature
\return The index of the found RosterAppInfo, or -1, if \a signature is
\c NULL or the list doesn't contain an info with this signature.
*/
int32
AppInfoList::IndexOf(const char *signature) const
{
if (signature) {
for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
if (!strcasecmp(info->signature, signature))
return i;
}
}
return -1;
}
team ID.
\param team The team ID
\return The index of the found RosterAppInfo, or -1, if the list doesn't
contain an info with this team ID.
*/
int32
AppInfoList::IndexOf(team_id team) const
{
for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
if (info->team == team)
return i;
}
return -1;
}
entry_ref.
If the list contains more than one RosterAppInfo with the given entry_ref,
it is undefined, which one is returned.
\param ref The entry_ref
\return The index of the found RosterAppInfo, or -1, if \a ref is
\c NULL or the list doesn't contain an info with this entry_ref.
*/
int32
AppInfoList::IndexOf(const entry_ref *ref) const
{
if (ref) {
BEntry entry(ref, true);
entry_ref realRef;
if (entry.GetRef(&realRef) != B_OK)
realRef = *ref;
for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
if (info->ref == realRef)
return i;
}
}
return -1;
}
token.
\param token The token
\return The index of the found RosterAppInfo, or -1, if the list doesn't
contain an info with this token.
*/
int32
AppInfoList::IndexOfToken(uint32 token) const
{
for (int32 i = 0; RosterAppInfo *info = InfoAt(i); i++) {
if (info->token == token)
return i;
}
return -1;
}