Chat-O-Matic/libs/libsupport/KeyMap.h
Jaidyn Ann 48d0b7bc96 Create Conversation class, use it instead of Contact for chats
This is a commit with it's foot in a lot of places, but:

The Conversation class was created as the abstraction of chats: All
ImMessages that are relevant to a conversation get routed through it,
meta-data on chats is stored in it (even if right now that's basically
limited to the user list and ID).

Server was given more methods to help accessing contacts―
ContactById(BString) and AddContact(Contact*). This better allows
Conversations to add and fetch Contacts as necessary. Right now, all
users in chats are treated as Contacts, so in the future creating an
independent userlist for Server (fUserMap?) would be useful.

Server also now stores all Conversations (fChatMap) and has some
convenience methods like for Contacts: Conversations(),
ConversationById(BString), and AddConversation(Conversation*).

CayaRenderView has been changed to not store user nicks, and will use
the appropriate nick of any arbitrarily-numbered user.

Users also have a map of all Conversations they are a part of
(fChatMap).

The Remove* methods of KeyMap now return the removed item.
2021-05-24 01:47:21 -05:00

130 lines
2.2 KiB
C++

/*
* Copyright 2009-2010, Andrea Anzani. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _KEY_MAP_H
#define _KEY_MAP_H
#include <map>
#include "List.h"
template<class KEY, class TYPE>
class KeyMap {
public:
uint32 CountItems() const;
void AddItem(KEY k, TYPE t);
TYPE ValueFor(KEY, bool* found = NULL) const;
TYPE RemoveItemAt(int32 position);
TYPE RemoveItemFor(KEY);
KEY KeyAt(uint32 position) const;
TYPE ValueAt(uint32 position) const;
List<TYPE> Values() const;
private:
std::map<KEY, TYPE> fMap;
typedef typename std::map<KEY, TYPE>::iterator fIter;
typedef typename std::map<KEY, TYPE>::const_iterator fConstIter;
};
template<class KEY, class TYPE>
inline uint32
KeyMap<KEY, TYPE>::CountItems() const
{
return fMap.size();
}
template<class KEY, class TYPE>
inline void
KeyMap<KEY, TYPE>::AddItem(KEY k, TYPE t)
{
fMap[k] = t;
}
template<class KEY, class TYPE>
inline TYPE
KeyMap<KEY, TYPE>::ValueFor(KEY k, bool* found) const
{
fConstIter i = fMap.find(k);
if (found) {
if (i == fMap.end())
*found = false;
else
*found = true;
}
if (i == fMap.end())
return NULL;
return i->second;
}
template<class KEY, class TYPE>
inline TYPE
KeyMap<KEY, TYPE>::RemoveItemAt(int32 position)
{
TYPE value = ValueAt(position);
fIter i = fMap.begin();
std::advance(i, position);
fMap.erase(i->first);
return value;
}
template<class KEY, class TYPE>
inline TYPE
KeyMap<KEY, TYPE>::RemoveItemFor(KEY k)
{
TYPE value = ValueFor(k);
fMap.erase(k);
return value;
}
template<class KEY, class TYPE>
inline KEY
KeyMap<KEY, TYPE>::KeyAt(uint32 position) const
{
fIter i = fMap.begin();
std::advance(i, position);
if (i == fMap.end())
return NULL;
return i->first;
}
template<class KEY, class TYPE>
inline TYPE
KeyMap<KEY, TYPE>::ValueAt(uint32 position) const
{
fConstIter i = fMap.begin();
std::advance(i, position);
if (i == fMap.end())
return NULL;
return i->second;
}
template<class KEY, class TYPE>
inline List<TYPE>
KeyMap<KEY, TYPE>::Values() const
{
List<TYPE> list;
for (fIter i = fMap.begin(); i != fMap.end(); ++i)
list.AddItem(i->second);
return list;
}
#endif // _KEY_MAP_H