Refactored CObjectManager to std::map

dev-mp
krzys-h 2014-12-20 19:09:53 +01:00
parent 58bc01c82c
commit c5b6faea05
4 changed files with 49 additions and 45 deletions

View File

@ -350,7 +350,7 @@ CObject::CObject()
m_botVar->SetUserPtr(this);
m_botVar->SetIdent(m_id);
CObjectManager::GetInstancePointer()->AddInstance(this);
CObjectManager::GetInstancePointer()->AddObject(this);
}
// Object's destructor.
@ -374,7 +374,7 @@ CObject::~CObject()
m_auto = nullptr;
CInstanceManager::GetInstancePointer()->DeleteInstance(CLASS_OBJECT, this);
CObjectManager::GetInstancePointer()->DeleteInstance(this);
CObjectManager::GetInstancePointer()->DeleteObject(this);
m_app = nullptr;
}

View File

@ -29,43 +29,45 @@ template<> CObjectManager* CSingleton<CObjectManager>::m_instance = nullptr;
CObjectManager::CObjectManager()
{
for (int i = 0; i < MAX_OBJECTS; i++)
{
m_table[i] = nullptr;
}
m_usedCount = 0;
}
CObjectManager::~CObjectManager()
{
}
bool CObjectManager::AddInstance(CObject* instance)
bool CObjectManager::AddObject(CObject* instance)
{
if (m_usedCount >= MAX_OBJECTS) return false;
assert(instance != nullptr);
assert(m_table[instance->GetID()] == nullptr);
m_table[instance->GetID()] = instance;
m_usedCount++;
return true;
}
bool CObjectManager::DeleteInstance(CObject* instance)
bool CObjectManager::DeleteObject(CObject* instance)
{
for (int i = 0; i < m_usedCount; i++)
assert(instance != nullptr);
for(auto it = m_table.begin(); it != m_table.end(); ++it)
{
if (m_table[i] == instance)
m_table[i] = nullptr;
if(it->second == instance)
{
m_table.erase(it);
return true;
}
}
return true;
return false;
}
CObject* CObjectManager::SearchInstance(int id)
CObject* CObjectManager::GetObjectById(int id)
{
if (id >= MAX_OBJECTS) return nullptr;
return m_table[id];
}
void CObjectManager::Flush()
{
m_table.clear();
}
CObject* CObjectManager::CreateObject(Math::Vector pos, float angle, ObjectType type,
float power, float zoom, float height,
bool trainer, bool toy, int option)
@ -361,11 +363,10 @@ CObject* CObjectManager::CreateObject(Math::Vector pos, float angle, ObjectType
return object;
}
void CObjectManager::Flush()
bool CObjectManager::DestroyObject(int id)
{
for (int i = 0; i < MAX_OBJECTS; i++)
{
m_table[i] = nullptr;
}
m_usedCount = 0;
}
CObject* obj = GetObjectById(id);
if(obj == nullptr) return false;
delete obj; // Destructor calls CObjectManager::DeleteObject
return true;
}

View File

@ -19,7 +19,7 @@
/**
* \file object/objman.h
* \brief Instance manager for objects
* \brief Object manager
*/
#pragma once
@ -28,11 +28,11 @@
#include "common/singleton.h"
const int MAX_OBJECTS = 500;
#include <map>
/**
* \class ObjectManager
* \brief Manager for objects
* \brief Manages CObject instances
*/
class CObjectManager : public CSingleton<CObjectManager>
{
@ -41,18 +41,21 @@ public:
virtual ~CObjectManager();
//! Registers new object
bool AddInstance(CObject* instance);
//! Deletes the registered object
bool DeleteInstance(CObject* instance);
//! Seeks for an object
CObject* SearchInstance(int id);
//! Creates an object
CObject* CreateObject(Math::Vector pos, float angle, ObjectType type, float power = -1.f, float zoom = 1.f, float height = 0.f, bool trainer = false, bool toy = false, int option = 0);
bool AddObject(CObject* instance);
//! Unregisters the object
bool DeleteObject(CObject* instance);
//! Finds object by id
CObject* GetObjectById(int id);
//! Removes all objects
void Flush();
//! Creates an object
CObject* CreateObject(Math::Vector pos, float angle, ObjectType type, float power = -1.f, float zoom = 1.f, float height = 0.f, bool trainer = false, bool toy = false, int option = 0);
//! Destroys an object
bool DestroyObject(int id);
protected:
CObject* m_table[MAX_OBJECTS];
int m_usedCount;
std::map<int, CObject*> m_table;
};

View File

@ -473,7 +473,7 @@ bool CScriptFunctions::rGetObjectById(CBotVar* var, CBotVar* result, int& except
rank = var->GetValInt();
pObj = static_cast<CObject*>(CObjectManager::GetInstancePointer()->SearchInstance(rank));
pObj = static_cast<CObject*>(CObjectManager::GetInstancePointer()->GetObjectById(rank));
if ( pObj == 0 )
{
result->SetPointer(0);
@ -558,7 +558,7 @@ bool CScriptFunctions::rBusy(CBotVar* thisclass, CBotVar* var, CBotVar* result,
classVars = classVars->GetNext(); // "load"
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* obj = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CObject* obj = CObjectManager::GetInstancePointer()->GetObjectById(rank);
CAuto* automat = obj->GetAuto();
if ( automat != nullptr )
@ -590,7 +590,7 @@ bool CScriptFunctions::rDestroy(CBotVar* thisclass, CBotVar* var, CBotVar* resul
classVars = classVars->GetNext(); // "load"
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* obj = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CObject* obj = CObjectManager::GetInstancePointer()->GetObjectById(rank);
CAuto* automat = obj->GetAuto();
if ( thisType == OBJECT_DESTROYER )
@ -667,7 +667,7 @@ bool CScriptFunctions::rFactory(CBotVar* thisclass, CBotVar* var, CBotVar* resul
classVars = classVars->GetNext(); // "load"
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* factory = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CObject* factory = CObjectManager::GetInstancePointer()->GetObjectById(rank);
if (factory == nullptr) {
exception = ERR_GENERIC;
result->SetValInt(ERR_GENERIC);
@ -847,7 +847,7 @@ bool CScriptFunctions::rResearch(CBotVar* thisclass, CBotVar* var, CBotVar* resu
classVars = classVars->GetNext(); // "load"
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* center = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CObject* center = CObjectManager::GetInstancePointer()->GetObjectById(rank);
CAuto* automat = center->GetAuto();
if ( thisType == OBJECT_RESEARCH ||
@ -927,7 +927,7 @@ bool CScriptFunctions::rTakeOff(CBotVar* thisclass, CBotVar* var, CBotVar* resul
classVars = classVars->GetNext(); // "load"
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* center = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CObject* center = CObjectManager::GetInstancePointer()->GetObjectById(rank);
CAuto* automat = center->GetAuto();
if ( thisType == OBJECT_BASE )
@ -998,7 +998,7 @@ bool CScriptFunctions::rDelete(CBotVar* var, CBotVar* result, int& exception, vo
}
}
pObj = static_cast<CObject*>(CObjectManager::GetInstancePointer()->SearchInstance(rank));
pObj = static_cast<CObject*>(CObjectManager::GetInstancePointer()->GetObjectById(rank));
if ( pObj == 0 )
{
return true;
@ -3452,7 +3452,7 @@ bool CScriptFunctions::rCameraFocus(CBotVar* var, CBotVar* result, int& exceptio
classVars = classVars->GetNext(); // "load"
classVars = classVars->GetNext(); // "id"
int rank = classVars->GetValInt();
CObject* object = CObjectManager::GetInstancePointer()->SearchInstance(rank);
CObject* object = CObjectManager::GetInstancePointer()->GetObjectById(rank);
script->m_main->SelectObject(object, false);