[colobot] 220/377: Refactored CBotVarClass instance list to std::set

Didier Raboud odyx at moszumanska.debian.org
Wed Mar 30 13:34:18 UTC 2016


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

odyx pushed a commit to branch debian/master
in repository colobot.

commit a70381e1c80862261c9186ca0e221781d5f09ce9
Author: krzys-h <krzys_h at interia.pl>
Date:   Thu Dec 31 16:54:13 2015 +0100

    Refactored CBotVarClass instance list to std::set
---
 src/CBot/CBotClass.cpp            | 47 ++++++++++++++++++++++-----------------
 src/CBot/CBotClass.h              | 25 +++++++++++----------
 src/CBot/CBotStack.h              |  6 ++---
 src/CBot/CBotVar/CBotVarClass.cpp | 38 ++++++-------------------------
 src/CBot/CBotVar/CBotVarClass.h   | 10 ++++-----
 src/script/scriptfunc.cpp         |  2 +-
 6 files changed, 54 insertions(+), 74 deletions(-)

diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp
index 81143fa..a3ee74d 100644
--- a/src/CBot/CBotClass.cpp
+++ b/src/CBot/CBotClass.cpp
@@ -49,15 +49,15 @@ CBotClass::CBotClass(const std::string& name,
                      CBotClass* parent,
                      bool bIntrinsic)
 {
-    m_pParent   = parent;
+    m_parent    = parent;
     m_name      = name;
     m_pVar      = nullptr;
     m_pCalls    = nullptr;
     m_pMethod   = nullptr;
-    m_rMaj      = nullptr;
+    m_rUpdate   = nullptr;
     m_IsDef     = true;
     m_bIntrinsic= bIntrinsic;
-    m_nbVar     = m_pParent == nullptr ? 0 : m_pParent->m_nbVar;
+    m_nbVar     = m_parent == nullptr ? 0 : m_parent->m_nbVar;
 
     m_publicClasses.insert(this);
 }
@@ -99,7 +99,7 @@ void CBotClass::Purge()
     m_pMethod   = nullptr;
     m_IsDef     = false;
 
-    m_nbVar     = m_pParent == nullptr ? 0 : m_pParent->m_nbVar;
+    m_nbVar     = m_parent == nullptr ? 0 : m_parent->m_nbVar;
 
     m_next->Purge();
     m_next = nullptr;          // no longer belongs to this chain
@@ -198,7 +198,7 @@ std::string  CBotClass::GetName()
 CBotClass*  CBotClass::GetParent()
 {
     if ( this == nullptr ) return nullptr;
-    return m_pParent;
+    return m_parent;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -208,7 +208,7 @@ bool  CBotClass::IsChildOf(CBotClass* pClass)
     while ( p != nullptr )
     {
         if ( p == pClass ) return true;
-        p = p->m_pParent;
+        p = p->m_parent;
     }
     return false;
 }
@@ -229,7 +229,7 @@ CBotVar* CBotClass::GetItem(const std::string& name)
         if ( p->GetName() == name ) return p;
         p = p->GetNext();
     }
-    if ( m_pParent != nullptr ) return m_pParent->GetItem(name);
+    if (m_parent != nullptr ) return m_parent->GetItem(name);
     return nullptr;
 }
 
@@ -243,7 +243,7 @@ CBotVar* CBotClass::GetItemRef(int nIdent)
         if ( p->GetUniqNum() == nIdent ) return p;
         p = p->GetNext();
     }
-    if ( m_pParent != nullptr ) return m_pParent->GetItemRef(nIdent);
+    if (m_parent != nullptr ) return m_parent->GetItemRef(nIdent);
     return nullptr;
 }
 
@@ -301,9 +301,9 @@ bool CBotClass::AddFunction(const std::string& name,
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) )
+bool CBotClass::SetUpdateFunc(void rUpdate(CBotVar* thisVar, void* user))
 {
-    m_rMaj = rMaj;
+    m_rUpdate = rUpdate;
     return true;
 }
 
@@ -324,8 +324,8 @@ CBotTypResult CBotClass::CompileMethode(const std::string& name,
     // find the methods declared by user
 
     r = m_pMethod->CompileCall(name, ppParams, nIdent);
-    if ( r.Eq(CBotErrUndefCall) && m_pParent != nullptr )
-        return m_pParent->m_pMethod->CompileCall(name, ppParams, nIdent);
+    if ( r.Eq(CBotErrUndefCall) && m_parent != nullptr )
+        return m_parent->m_pMethod->CompileCall(name, ppParams, nIdent);
     return r;
 }
 
@@ -344,11 +344,11 @@ bool CBotClass::ExecuteMethode(long& nIdent,
     ret = m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, this);
     if (ret >= 0) return ret;
 
-    if (m_pParent != nullptr)
+    if (m_parent != nullptr)
     {
-        ret = m_pParent->m_pCalls->DoCall(name, pThis, ppParams, pResult, pStack, pToken);
+        ret = m_parent->m_pCalls->DoCall(name, pThis, ppParams, pResult, pStack, pToken);
         if (ret >= 0) return ret;
-        ret = m_pParent->m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, m_pParent);
+        ret = m_parent->m_pMethod->DoCall(nIdent, name, pThis, ppParams, pStack, pToken, m_parent);
     }
     return ret;
 }
@@ -493,7 +493,7 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack)
             }
         }
         CBotClass* classe = (pOld == nullptr) ? new CBotClass(name, pPapa) : pOld;
-        classe->Purge();                            // emptythe old definitions
+        classe->Purge();                            // empty the old definitions // TODO: Doesn't this remove all classes of the current program?
         classe->m_IsDef = false;                    // current definition
 
         if ( !IsOfType( p, ID_OPBLK) )
@@ -615,11 +615,11 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
                     pThis->SetUniqNum(-2);
                     pile->AddVar(pThis);
 
-                    if ( m_pParent )
+                    if (m_parent)
                     {
                         // makes "super" known
                         CBotToken TokenSuper(std::string("super"), std::string());
-                        CBotVar* pThis = CBotVar::Create(TokenSuper, CBotTypResult( CBotTypClass, m_pParent ) );
+                        CBotVar* pThis = CBotVar::Create(TokenSuper, CBotTypResult(CBotTypClass, m_parent) );
                         pThis->SetUniqNum(-3);
                         pile->AddVar(pThis);
                     }
@@ -641,7 +641,7 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
                             pile->AddVar(pcopy);
                             pv = pv->GetNext();
                         }
-                        my = my->m_pParent;
+                        my = my->m_parent;
                     }
 
                     // compiles a method
@@ -745,13 +745,13 @@ CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack)
                 pStack->SetError( CBotErrNotClass, p );
                 return nullptr;
             }
-            pOld->m_pParent = pPapa;
+            pOld->m_parent = pPapa;
         }
         else
         {
             if (pOld != nullptr)
             {
-                pOld->m_pParent = nullptr;
+                pOld->m_parent = nullptr;
             }
         }
         IsOfType( p, ID_OPBLK); // necessarily
@@ -768,4 +768,9 @@ CBotClass* CBotClass::Compile(CBotToken* &p, CBotCStack* pStack)
     return nullptr;
 }
 
+void CBotClass::Update(CBotVar* var, void* user)
+{
+    m_rUpdate(var, user);
+}
+
 } // namespace CBot
diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h
index 2ed9c28..56b6902 100644
--- a/src/CBot/CBotClass.h
+++ b/src/CBot/CBotClass.h
@@ -105,9 +105,6 @@ class CBotCStack;
 class CBotClass : public CBotLinkedList<CBotClass>
 {
 public:
-    //! Mark if is set or not
-    bool m_IsDef;
-
     /*!
      * \brief CBotClass Constructor. Once a class is created, it is known around
      * CBot intrinsic mode gives a class that is not managed by pointers.
@@ -149,12 +146,12 @@ public:
                      CBotTypResult rCompile(CBotVar* pThis, CBotVar*& pVar));
 
     /*!
-     * \brief AddUpdateFunc Defines routine to be called to update the elements
+     * \brief SetUpdateFunc Defines routine to be called to update the elements
      * of the class.
      * \param rMaj
      * \return
      */
-    bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) );
+    bool SetUpdateFunc(void rUpdate(CBotVar* thisVar, void* user));
     //
 
     /*!
@@ -363,17 +360,22 @@ public:
      */
     bool CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken);
 
+    void Update(CBotVar* var, void* user);
+
 private:
     //! List of all public classes
     static std::set<CBotClass*> m_publicClasses;
 
-    //! Parent class.
-    CBotClass* m_pParent;
-    //! Name of this class.
+
+    //! true if this class is fully compiled, false if only precompiled
+    bool m_IsDef;
+    //! Name of this class
     std::string m_name;
-    //! Number of variables in the chain.
+    //! Parent class
+    CBotClass* m_parent;
+    //! Number of variables in the chain
     int m_nbVar;
-    //! Intrinsic class.
+    //! Intrinsic class
     bool m_bIntrinsic;
     //! Linked list of all class fields
     CBotVar* m_pVar;
@@ -381,8 +383,7 @@ private:
     CBotCallMethode* m_pCalls;
     //! Linked list of all class methods
     CBotFunction* m_pMethod;
-    void (*m_rMaj) ( CBotVar* pThis, void* pUser );
-    friend class CBotVarClass;
+    void (*m_rUpdate)(CBotVar* thisVar, void* user);
 
     //! How many times the program currently holding the lock called Lock()
     int m_lockCurrentCount = 0;
diff --git a/src/CBot/CBotStack.h b/src/CBot/CBotStack.h
index 4546644..261663e 100644
--- a/src/CBot/CBotStack.h
+++ b/src/CBot/CBotStack.h
@@ -162,7 +162,7 @@ public:
     /**
      * \brief Fetch a variable by its token
      * \param pToken Token upon which search is performed
-     * \param bUpdate true to automatically call update function for classes, see CBotClass::AddUpdateFunc()
+     * \param bUpdate true to automatically call update function for classes, see CBotClass::SetUpdateFunc()
      * \return Found variable, nullptr if not found
      */
     CBotVar* FindVar(CBotToken*& pToken, bool bUpdate);
@@ -185,7 +185,7 @@ public:
      * This is faster than comparing names
      *
      * \param ident Unique identifier of a variable
-     * \param bUpdate true to automatically call update function for classes, see CBotClass::AddUpdateFunc()
+     * \param bUpdate true to automatically call update function for classes, see CBotClass::SetUpdateFunc()
      * \return Found variable, nullptr if not found
      */
     CBotVar* FindVar(long ident, bool bUpdate);
@@ -194,7 +194,7 @@ public:
      * \brief Find variable by its token and returns a copy of it
      *
      * \param pToken Token upon which search is performed
-     * \param bUpdate true to automatically call update function for classes, see CBotClass::AddUpdateFunc()
+     * \param bUpdate true to automatically call update function for classes, see CBotClass::SetUpdateFunc()
      * \return Found variable, nullptr if not found
      */
     CBotVar*        CopyVar(CBotToken& pToken, bool bUpdate = false);
diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp
index 46c88f6..efd6412 100644
--- a/src/CBot/CBotVar/CBotVarClass.cpp
+++ b/src/CBot/CBotVar/CBotVarClass.cpp
@@ -33,7 +33,7 @@ namespace CBot
 {
 
 ////////////////////////////////////////////////////////////////////////////////
-CBotVarClass* CBotVarClass::m_ExClass = nullptr;
+std::set<CBotVarClass*> CBotVarClass::m_instances{};
 
 ////////////////////////////////////////////////////////////////////////////////
 CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type)
@@ -66,10 +66,7 @@ CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type)
     m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum();
 
     // add to the list
-    if (m_ExClass != nullptr) m_ExClass->m_ExPrev = this;
-    m_ExNext  = m_ExClass;
-    m_ExPrev  = nullptr;
-    m_ExClass = this;
+    m_instances.insert(this);
 
     CBotClass* pClass = type.GetClass();
     CBotClass* pClass2 = pClass->GetParent();
@@ -79,7 +76,7 @@ CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type)
         m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(),pClass2) ); //, nIdent);
     }
 
-    SetClass( pClass ); //, nIdent );
+    SetClass( pClass );
 
 }
 
@@ -92,17 +89,8 @@ CBotVarClass::~CBotVarClass( )
     if ( m_pParent ) delete m_pParent;
     m_pParent = nullptr;
 
-    // frees the indirect object if necessary
-//    if ( m_Indirect != nullptr )
-//        m_Indirect->DecrementUse();
-
     // removes the class list
-    if ( m_ExPrev ) m_ExPrev->m_ExNext = m_ExNext;
-    else m_ExClass = m_ExNext;
-
-    if ( m_ExNext ) m_ExNext->m_ExPrev = m_ExPrev;
-    m_ExPrev = nullptr;
-    m_ExNext = nullptr;
+    m_instances.erase(this);
 
     delete    m_pVar;
 }
@@ -242,20 +230,13 @@ CBotClass* CBotVarClass::GetClass()
 ////////////////////////////////////////////////////////////////////////////////
 void CBotVarClass::Maj(void* pUser)
 {
-/*    if (!bContinu && m_pMyThis != nullptr)
-        m_pMyThis->Maj(pUser, true);*/
-
-    // an update routine exist?
-
-    if ( m_pClass->m_rMaj == nullptr ) return;
-
     // retrieves the user pointer according to the class
     // or according to the parameter passed to CBotProgram::Run()
 
     if ( m_pUserPtr != nullptr) pUser = m_pUserPtr;
     if ( pUser == OBJECTDELETED ||
          pUser == OBJECTCREATED ) return;
-    m_pClass->m_rMaj( this, pUser );
+    m_pClass->Update(this, pUser);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -328,8 +309,6 @@ CBotVar* CBotVarClass::GetItemList()
 ////////////////////////////////////////////////////////////////////////////////
 std::string CBotVarClass::GetValString()
 {
-//    if ( m_Indirect != nullptr) return m_Indirect->GetValString();
-
     std::string    res;
 
     if ( m_pClass != nullptr )                        // not used for an array
@@ -440,12 +419,9 @@ CBotVarClass* CBotVarClass::GetPointer()
 ////////////////////////////////////////////////////////////////////////////////
 CBotVarClass* CBotVarClass::Find(long id)
 {
-    CBotVarClass*    p = m_ExClass;
-
-    while ( p != nullptr )
+    for (CBotVarClass* p : m_instances)
     {
-        if ( p->m_ItemIdent == id ) return p;
-        p = p->m_ExNext;
+        if (p->m_ItemIdent == id) return p;
     }
 
     return nullptr;
diff --git a/src/CBot/CBotVar/CBotVarClass.h b/src/CBot/CBotVar/CBotVarClass.h
index 7749170..d00610e 100644
--- a/src/CBot/CBotVar/CBotVarClass.h
+++ b/src/CBot/CBotVar/CBotVarClass.h
@@ -21,6 +21,8 @@
 
 #include "CBot/CBotVar/CBotVar.h"
 
+#include <set>
+
 namespace CBot
 {
 
@@ -93,12 +95,8 @@ public:
     void ConstructorSet() override;
 
 private:
-    //! Doubly linked list of all class instances - first
-    static CBotVarClass* m_ExClass;
-    //! Doubly linked list of all class instances - next
-    CBotVarClass* m_ExNext;
-    //! Doubly linked list of all class instances - previous
-    CBotVarClass* m_ExPrev;
+    //! List of all class instances - first
+    static std::set<CBotVarClass*> m_instances;
     //! Class definition
     CBotClass* m_pClass;
     //! Parent class instance
diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp
index 7b92430..4e16d40 100644
--- a/src/script/scriptfunc.cpp
+++ b/src/script/scriptfunc.cpp
@@ -3427,7 +3427,7 @@ CBotVar* CScriptFunctions::CreateObjectVar(CObject* obj)
     CBotClass* bc = CBotClass::Find("object");
     if ( bc != nullptr )
     {
-        bc->AddUpdateFunc(CScriptFunctions::uObject);
+        bc->SetUpdateFunc(CScriptFunctions::uObject);
     }
 
     CBotVar* botVar = CBotVar::Create("", CBotTypResult(CBotTypClass, "object"));

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



More information about the Pkg-games-commits mailing list