/*
Moonfall Copyright (C) 2008 Alex Yatskov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
template
RefCount::RefCount(int history) :
m_history(history)
{
}
template
boost::shared_ptr RefCount::FindClient(Token id) const
{
typename ClientMap::const_iterator iter = m_map.find(id);
return iter == m_map.end() ? boost::shared_ptr() : iter->second.lock();
}
template
void RefCount::AddClient(Token id, const boost::shared_ptr& client)
{
if (FindClient(id))
{
m_map[id] = client;
}
else
{
m_map.insert(std::make_pair(id, boost::weak_ptr(client)));
}
CacheClient(client);
}
template
void RefCount::RemoveClient(Token id)
{
typename ClientMap::iterator iter = m_map.find(id);
if (iter != m_map.end())
{
m_map.erase(iter);
}
}
template
void RefCount::ClearClients()
{
m_map.clear();
m_list.clear();
}
template
void RefCount::CacheClient(const boost::shared_ptr& client)
{
if (m_history == 0)
{
return;
}
typename ClientList::iterator iter = find(m_list.begin(), m_list.end(), client);
if (iter == m_list.end())
{
m_list.push_back(client);
if (m_list.size() > static_cast(m_history))
{
m_list.erase(m_list.begin());
}
}
else
{
m_list.erase(iter);
m_list.push_back(client);
}
}