Refactor public classes list to std::set

dev-time-step
krzys-h 2015-12-31 16:30:54 +01:00
parent 48ab72d056
commit dcc29442bd
3 changed files with 30 additions and 60 deletions

View File

@ -42,14 +42,14 @@ namespace CBot
{ {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::m_ExClass = nullptr; std::set<CBotClass*> CBotClass::m_publicClasses{};
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotClass::CBotClass(const std::string& name, CBotClass::CBotClass(const std::string& name,
CBotClass* pPapa, CBotClass* parent,
bool bIntrinsic) bool bIntrinsic)
{ {
m_pParent = pPapa; m_pParent = parent;
m_name = name; m_name = name;
m_pVar = nullptr; m_pVar = nullptr;
m_pCalls = nullptr; m_pCalls = nullptr;
@ -59,28 +59,13 @@ CBotClass::CBotClass(const std::string& name,
m_bIntrinsic= bIntrinsic; m_bIntrinsic= bIntrinsic;
m_nbVar = m_pParent == nullptr ? 0 : m_pParent->m_nbVar; m_nbVar = m_pParent == nullptr ? 0 : m_pParent->m_nbVar;
m_lockCurrentCount = 0; m_publicClasses.insert(this);
m_lockProg.clear();
// is located alone in the list
if (m_ExClass) m_ExClass->m_ExPrev = this;
m_ExNext = m_ExClass;
m_ExPrev = nullptr;
m_ExClass = this;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotClass::~CBotClass() CBotClass::~CBotClass()
{ {
// removes the list of class m_publicClasses.erase(this);
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;
delete m_pVar; delete m_pVar;
delete m_pCalls; delete m_pCalls;
@ -96,12 +81,9 @@ CBotClass* CBotClass::Create(const std::string& name,
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotClass::Free() void CBotClass::ClearPublic()
{ {
while ( m_ExClass != nullptr ) m_publicClasses.clear();
{
delete m_ExClass;
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -159,9 +141,7 @@ void CBotClass::Unlock()
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotClass::FreeLock(CBotProgram* prog) void CBotClass::FreeLock(CBotProgram* prog)
{ {
CBotClass* pClass = m_ExClass; for (CBotClass* pClass : m_publicClasses)
while (pClass != nullptr)
{ {
if (pClass->m_lockProg.size() > 0 && prog == pClass->m_lockProg[0]) if (pClass->m_lockProg.size() > 0 && prog == pClass->m_lockProg[0])
{ {
@ -169,8 +149,6 @@ void CBotClass::FreeLock(CBotProgram* prog)
} }
pClass->m_lockProg.erase(std::remove(pClass->m_lockProg.begin(), pClass->m_lockProg.end(), prog)); pClass->m_lockProg.erase(std::remove(pClass->m_lockProg.begin(), pClass->m_lockProg.end(), prog));
pClass = pClass->m_ExNext;
} }
} }
@ -284,12 +262,9 @@ CBotClass* CBotClass::Find(CBotToken* &pToken)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::Find(const std::string& name) CBotClass* CBotClass::Find(const std::string& name)
{ {
CBotClass* p = m_ExClass; for (CBotClass* p : m_publicClasses)
while ( p != nullptr )
{ {
if ( p->GetName() == name ) return p; if ( p->GetName() == name ) return p;
p = p->m_ExNext;
} }
return nullptr; return nullptr;
@ -394,11 +369,9 @@ bool CBotClass::SaveStaticState(FILE* pf)
if (!WriteWord( pf, CBOTVERSION*2)) return false; if (!WriteWord( pf, CBOTVERSION*2)) return false;
// saves the state of static variables in classes // saves the state of static variables in classes
CBotClass* p = m_ExClass; for (CBotClass* p : m_publicClasses)
while ( p != nullptr )
{ {
if (!WriteWord( pf, 1)) return false; if (!WriteWord( pf, 1 )) return false;
// save the name of the class // save the name of the class
if (!WriteString( pf, p->GetName() )) return false; if (!WriteString( pf, p->GetName() )) return false;
@ -407,21 +380,20 @@ bool CBotClass::SaveStaticState(FILE* pf)
{ {
if ( pv->IsStatic() ) if ( pv->IsStatic() )
{ {
if (!WriteWord( pf, 1)) return false; if (!WriteWord( pf, 1 )) return false;
if (!WriteString( pf, pv->GetName() )) return false; if (!WriteString( pf, pv->GetName() )) return false;
if ( !pv->Save0State(pf)) return false; // common header if ( !pv->Save0State(pf) ) return false; // common header
if ( !pv->Save1State(pf) ) return false; // saves as the child class if ( !pv->Save1State(pf) ) return false; // saves as the child class
if ( !WriteWord( pf, 0)) return false; if ( !WriteWord( pf, 0 ) ) return false;
} }
pv = pv->GetNext(); pv = pv->GetNext();
} }
if (!WriteWord( pf, 0)) return false; if (!WriteWord( pf, 0 )) return false;
p = p->m_ExNext;
} }
if (!WriteWord( pf, 0)) return false; if (!WriteWord( pf, 0 )) return false;
return true; return true;
} }

View File

@ -25,6 +25,7 @@
#include <string> #include <string>
#include <deque> #include <deque>
#include <set>
namespace CBot namespace CBot
{ {
@ -111,11 +112,11 @@ public:
* \brief CBotClass Constructor. Once a class is created, it is known around * \brief CBotClass Constructor. Once a class is created, it is known around
* CBot intrinsic mode gives a class that is not managed by pointers. * CBot intrinsic mode gives a class that is not managed by pointers.
* \param name * \param name
* \param pParent * \param parent
* \param bIntrinsic * \param bIntrinsic
*/ */
CBotClass(const std::string& name, CBotClass(const std::string& name,
CBotClass* pParent, CBotClass* parent,
bool bIntrinsic = false); bool bIntrinsic = false);
/*! /*!
@ -318,7 +319,7 @@ public:
/*! /*!
* \brief Free * \brief Free
*/ */
static void Free(); static void ClearPublic();
/*! /*!
* \brief SaveStaticState * \brief SaveStaticState
@ -363,33 +364,30 @@ public:
bool CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken); bool CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken);
private: private:
//! List of classes existing at a given time. //! List of all public classes
static CBotClass* m_ExClass; static std::set<CBotClass*> m_publicClasses;
//! For this general list.
CBotClass* m_ExNext;
//! For this general list.
CBotClass* m_ExPrev;
//! Parent class. //! Parent class.
CBotClass* m_pParent; CBotClass* m_pParent;
//! Name of this class. //! Name of this class.
std::string m_name; std::string m_name;
//! Number of variables in the chain. //! Number of variables in the chain.
int m_nbVar; int m_nbVar;
//! Content of the class.
CBotVar* m_pVar;
//! Intrinsic class. //! Intrinsic class.
bool m_bIntrinsic; bool m_bIntrinsic;
//! List of methods defined in external. //! Linked list of all class fields
CBotVar* m_pVar;
//! Linked list of all class external calls
CBotCallMethode* m_pCalls; CBotCallMethode* m_pCalls;
//! Compiled list of methods. //! Linked list of all class methods
CBotFunction* m_pMethod; CBotFunction* m_pMethod;
void (*m_rMaj) ( CBotVar* pThis, void* pUser ); void (*m_rMaj) ( CBotVar* pThis, void* pUser );
friend class CBotVarClass; friend class CBotVarClass;
//! How many times the program currently holding the lock called Lock() //! How many times the program currently holding the lock called Lock()
int m_lockCurrentCount; int m_lockCurrentCount = 0;
//! Programs waiting for lock //! Programs waiting for lock
std::deque<CBotProgram*> m_lockProg; std::deque<CBotProgram*> m_lockProg{};
}; };
} // namespace CBot } // namespace CBot

View File

@ -406,7 +406,7 @@ void CBotProgram::Free()
{ {
CBotToken::ClearDefineNum(); CBotToken::ClearDefineNum();
m_externalCalls->Clear(); m_externalCalls->Clear();
CBotClass::Free(); CBotClass::ClearPublic();
} }
CBotExternalCallList* CBotProgram::GetExternalCalls() CBotExternalCallList* CBotProgram::GetExternalCalls()