2014-10-14 13:11:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
|
|
|
|
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
|
|
|
|
|
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
|
|
|
|
|
*
|
|
|
|
|
* 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 http://gnu.org/licenses
|
|
|
|
|
*/
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/iman.h"
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
|
template<> CInstanceManager* CSingleton<CInstanceManager>::m_instance = nullptr;
|
2012-07-19 17:19:21 +00:00
|
|
|
|
|
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
|
CInstanceManager::CInstanceManager()
|
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
for (int i = 0; i < CLASS_MAX; i++)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
m_table[i].maxCount = 0;
|
|
|
|
|
m_table[i].usedCount = 0;
|
|
|
|
|
m_table[i].instances = nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CInstanceManager::~CInstanceManager()
|
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
Flush();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CInstanceManager::Flush()
|
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
for (int i = 0; i < CLASS_MAX; i++)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2014-08-09 20:45:07 +00:00
|
|
|
|
delete[] m_table[i].instances;
|
2012-09-09 15:51:10 +00:00
|
|
|
|
m_table[i].instances = nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
void CInstanceManager::Flush(ManagedClassType classType)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (classType < 0 || classType >= CLASS_MAX) return;
|
|
|
|
|
if (m_table[classType].instances == nullptr) return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
delete[] m_table[classType].instances;
|
|
|
|
|
m_table[classType].instances = nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
bool CInstanceManager::AddInstance(ManagedClassType classType, void* instance, int max)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (classType < 0 || classType >= CLASS_MAX) return false;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (m_table[classType].instances == nullptr)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
m_table[classType].instances = new void*[max];
|
|
|
|
|
m_table[classType].maxCount = max;
|
|
|
|
|
m_table[classType].usedCount = 0;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (m_table[classType].usedCount >= m_table[classType].maxCount) return false;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
int i = m_table[classType].usedCount++;
|
|
|
|
|
m_table[classType].instances[i] = instance;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
bool CInstanceManager::DeleteInstance(ManagedClassType classType, void* instance)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (classType < 0 || classType >= CLASS_MAX) return false;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
for (int i = 0; i < m_table[classType].usedCount; i++)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (m_table[classType].instances[i] == instance)
|
|
|
|
|
m_table[classType].instances[i] = nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Compress(classType);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
void* CInstanceManager::SearchInstance(ManagedClassType classType, int rank)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (classType < 0 || classType >= CLASS_MAX) return nullptr;
|
|
|
|
|
if (m_table[classType].instances == nullptr) return nullptr;
|
|
|
|
|
if (rank >= m_table[classType].usedCount) return nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
return m_table[classType].instances[rank];
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
void CInstanceManager::Compress(ManagedClassType classType)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (classType < 0 || classType >= CLASS_MAX) return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
|
int j = 0;
|
|
|
|
|
for (int i = 0; i < m_table[classType].usedCount; i++)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2012-09-09 15:51:10 +00:00
|
|
|
|
if (m_table[classType].instances[i] != nullptr)
|
|
|
|
|
m_table[classType].instances[j++] = m_table[classType].instances[i];
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
2012-09-09 15:51:10 +00:00
|
|
|
|
m_table[classType].usedCount = j;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
2013-05-26 15:47:54 +00:00
|
|
|
|
|