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

View File

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

View File

@ -19,7 +19,7 @@
/** /**
* \file object/objman.h * \file object/objman.h
* \brief Instance manager for objects * \brief Object manager
*/ */
#pragma once #pragma once
@ -28,11 +28,11 @@
#include "common/singleton.h" #include "common/singleton.h"
const int MAX_OBJECTS = 500; #include <map>
/** /**
* \class ObjectManager * \class ObjectManager
* \brief Manager for objects * \brief Manages CObject instances
*/ */
class CObjectManager : public CSingleton<CObjectManager> class CObjectManager : public CSingleton<CObjectManager>
{ {
@ -41,18 +41,21 @@ public:
virtual ~CObjectManager(); virtual ~CObjectManager();
//! Registers new object //! Registers new object
bool AddInstance(CObject* instance); bool AddObject(CObject* instance);
//! Deletes the registered object //! Unregisters the object
bool DeleteInstance(CObject* instance); bool DeleteObject(CObject* instance);
//! Seeks for an object //! Finds object by id
CObject* SearchInstance(int id); CObject* GetObjectById(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);
//! Removes all objects //! Removes all objects
void Flush(); 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: protected:
CObject* m_table[MAX_OBJECTS]; std::map<int, CObject*> m_table;
int m_usedCount;
}; };

View File

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