This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch upstream/latest
in repository colobot.

commit c5b6faea05a3507fe3e08ad6110cd1473e1483cf
Author: krzys-h <krzy...@interia.pl>
Date:   Sat Dec 20 19:09:53 2014 +0100

    Refactored CObjectManager to std::map
---
 src/object/object.cpp     |  4 ++--
 src/object/objman.cpp     | 47 ++++++++++++++++++++++++-----------------------
 src/object/objman.h       | 27 +++++++++++++++------------
 src/script/scriptfunc.cpp | 16 ++++++++--------
 4 files changed, 49 insertions(+), 45 deletions(-)

diff --git a/src/object/object.cpp b/src/object/object.cpp
index 7d4c92d..7bdb60c 100644
--- a/src/object/object.cpp
+++ b/src/object/object.cpp
@@ -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;
 }
diff --git a/src/object/objman.cpp b/src/object/objman.cpp
index 25f907a..040b4fb 100644
--- a/src/object/objman.cpp
+++ b/src/object/objman.cpp
@@ -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;
+}
\ No newline at end of file
diff --git a/src/object/objman.h b/src/object/objman.h
index 137cbc8..0197c9b 100644
--- a/src/object/objman.h
+++ b/src/object/objman.h
@@ -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;
 };
 
diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp
index b447796..f5ccf86 100644
--- a/src/script/scriptfunc.cpp
+++ b/src/script/scriptfunc.cpp
@@ -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);
     

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-games/colobot.git

_______________________________________________
Pkg-games-commits mailing list
Pkg-games-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-games-commits

Reply via email to