Made CBotExternalCallList not static

dev-time-step
krzys-h 2015-12-24 12:36:09 +01:00
parent 8e01a208c1
commit ae544c71ae
9 changed files with 41 additions and 30 deletions

View File

@ -351,7 +351,7 @@ CBotTypResult CBotCStack::CompileCall(CBotToken* &p, CBotVar** ppVars, long& nId
nIdent = 0; nIdent = 0;
CBotTypResult val(-1); CBotTypResult val(-1);
val = CBotExternalCallList::CompileCall(p, ppVars, this); val = m_prog->GetExternalCalls()->CompileCall(p, ppVars, this);
if (val.GetType() < 0) if (val.GetType() < 0)
{ {
val = m_prog->GetFunctions()->CompileCall(p->GetString(), ppVars, nIdent); val = m_prog->GetFunctions()->CompileCall(p->GetString(), ppVars, nIdent);
@ -371,7 +371,7 @@ bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam)
{ {
std::string name = pToken->GetString(); std::string name = pToken->GetString();
if ( CBotExternalCallList::CheckCall(name) ) return true; if ( m_prog->GetExternalCalls()->CheckCall(name) ) return true;
CBotFunction* pp = m_prog->GetFunctions(); CBotFunction* pp = m_prog->GetFunctions();
while ( pp != nullptr ) while ( pp != nullptr )

View File

@ -495,12 +495,11 @@ bool CBotClass::RestoreStaticState(FILE* pf)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool CBotClass::CheckCall(CBotToken* &pToken, bool CBotClass::CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken)
CBotDefParam* pParam)
{ {
std::string name = pToken->GetString(); std::string name = pToken->GetString();
if ( CBotExternalCallList::CheckCall(name) ) return true; if ( program->GetExternalCalls()->CheckCall(name) ) return true;
CBotFunction* pp = m_pMethod; CBotFunction* pp = m_pMethod;
while ( pp != nullptr ) while ( pp != nullptr )

View File

@ -354,8 +354,7 @@ public:
* \param pParam * \param pParam
* \return * \return
*/ */
bool CheckCall(CBotToken* &pToken, bool CheckCall(CBotProgram* program, CBotDefParam* pParam, CBotToken*& pToken);
CBotDefParam* pParam);
private: private:
//! List of classes existing at a given time. //! List of classes existing at a given time.

View File

@ -26,10 +26,6 @@
#include "CBot/CBotVar/CBotVar.h" #include "CBot/CBotVar/CBotVar.h"
std::map<std::string, std::unique_ptr<CBotExternalCall>> CBotExternalCallList::m_list{};
void* CBotExternalCallList::m_user = nullptr;
void CBotExternalCallList::Clear() void CBotExternalCallList::Clear()
{ {
m_list.clear(); m_list.clear();

View File

@ -115,7 +115,7 @@ public:
* \param call Function to add * \param call Function to add
* \return true * \return true
*/ */
static bool AddFunction(const std::string& name, std::unique_ptr<CBotExternalCall> call); bool AddFunction(const std::string& name, std::unique_ptr<CBotExternalCall> call);
/** /**
* \brief Find and call compile function * \brief Find and call compile function
@ -127,14 +127,14 @@ public:
* \param pStack Compilation stack * \param pStack Compilation stack
* \return CBotTypResult representing the return type of the function (::CBotTypVar), or an error (::CBotError) * \return CBotTypResult representing the return type of the function (::CBotTypVar), or an error (::CBotError)
*/ */
static CBotTypResult CompileCall(CBotToken*& p, CBotVar** ppVars, CBotCStack* pStack); CBotTypResult CompileCall(CBotToken*& p, CBotVar** ppVars, CBotCStack* pStack);
/** /**
* \brief Check if function with given name has been defined * \brief Check if function with given name has been defined
* \param name Name to check * \param name Name to check
* \return true if function was defined * \return true if function was defined
*/ */
static bool CheckCall(const std::string& name); bool CheckCall(const std::string& name);
/** /**
* \brief Find and call runtime function * \brief Find and call runtime function
@ -147,7 +147,7 @@ public:
* \param rettype Return type of the function, as returned by CompileCall() * \param rettype Return type of the function, as returned by CompileCall()
* \return -1 if call failed (no such function), 0 if function requested interruption, 1 on success * \return -1 if call failed (no such function), 0 if function requested interruption, 1 on success
*/ */
static int DoCall(CBotToken* token, CBotVar** ppVars, CBotStack* pStack, const CBotTypResult& rettype); int DoCall(CBotToken* token, CBotVar** ppVars, CBotStack* pStack, const CBotTypResult& rettype);
/** /**
* \brief Restore execution status after loading saved state * \brief Restore execution status after loading saved state
@ -157,7 +157,7 @@ public:
* \param pStack Runtime stack * \param pStack Runtime stack
* \return false on failure (e.g. function doesn't exist) * \return false on failure (e.g. function doesn't exist)
*/ */
static bool RestoreCall(CBotToken* token, CBotVar** ppVar, CBotStack* pStack); bool RestoreCall(CBotToken* token, CBotVar** ppVar, CBotStack* pStack);
/** /**
* \brief Set user pointer to pass to compile functions * \brief Set user pointer to pass to compile functions
@ -166,14 +166,14 @@ public:
* *
* \param pUser User pointer * \param pUser User pointer
*/ */
static void SetUserPtr(void* pUser); void SetUserPtr(void* pUser);
/** /**
* \brief Reset the list of registered functions * \brief Reset the list of registered functions
*/ */
static void Clear(); void Clear();
private: private:
static std::map<std::string, std::unique_ptr<CBotExternalCall>> m_list; std::map<std::string, std::unique_ptr<CBotExternalCall>> m_list{};
static void* m_user; void* m_user = nullptr;
}; };

View File

@ -315,7 +315,7 @@ CBotFunction* CBotFunction::Compile1(CBotToken* &p, CBotCStack* pStack, CBotClas
{ {
// looks if the function exists elsewhere // looks if the function exists elsewhere
if (( pClass != nullptr || !pStack->CheckCall(pp, func->m_Param)) && if (( pClass != nullptr || !pStack->CheckCall(pp, func->m_Param)) &&
( pClass == nullptr || !pClass->CheckCall(pp, func->m_Param)) ) ( pClass == nullptr || !pClass->CheckCall(pStack->GetProgram(), func->m_Param, pp)) )
{ {
if (IsOfType(p, ID_OPBLK)) if (IsOfType(p, ID_OPBLK))
{ {

View File

@ -30,6 +30,8 @@
#include "CBot/stdlib/stdlib.h" #include "CBot/stdlib/stdlib.h"
CBotExternalCallList* CBotProgram::m_externalCalls = new CBotExternalCallList();
CBotProgram::CBotProgram() CBotProgram::CBotProgram()
{ {
} }
@ -77,7 +79,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
CBotToken* p = tokens.get()->GetNext(); // skips the first token (separator) CBotToken* p = tokens.get()->GetNext(); // skips the first token (separator)
pStack->SetProgram(this); // defined used routines pStack->SetProgram(this); // defined used routines
CBotExternalCallList::SetUserPtr(pUser); m_externalCalls->SetUserPtr(pUser);
// Step 2. Find all function and class definitions // Step 2. Find all function and class definitions
while ( pStack->IsOk() && p != nullptr && p->GetType() != 0) while ( pStack->IsOk() && p != nullptr && p->GetType() != 0)
@ -312,7 +314,7 @@ bool CBotProgram::AddFunction(const std::string& name,
bool rExec(CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser), bool rExec(CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser),
CBotTypResult rCompile(CBotVar*& pVar, void* pUser)) CBotTypResult rCompile(CBotVar*& pVar, void* pUser))
{ {
return CBotExternalCallList::AddFunction(name, std::unique_ptr<CBotExternalCall>(new CBotExternalCallDefault(rExec, rCompile))); return m_externalCalls->AddFunction(name, std::unique_ptr<CBotExternalCall>(new CBotExternalCallDefault(rExec, rCompile)));
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -435,6 +437,11 @@ void CBotProgram::Init()
void CBotProgram::Free() void CBotProgram::Free()
{ {
CBotToken::ClearDefineNum(); CBotToken::ClearDefineNum();
CBotExternalCallList::Clear(); m_externalCalls->Clear();
CBotClass::Free(); CBotClass::Free();
} }
CBotExternalCallList* CBotProgram::GetExternalCalls()
{
return m_externalCalls;
}

View File

@ -28,6 +28,7 @@ class CBotFunction;
class CBotClass; class CBotClass;
class CBotStack; class CBotStack;
class CBotVar; class CBotVar;
class CBotExternalCallList;
/** /**
* \brief Class that manages a CBot program. This is the main entry point into the CBot engine. * \brief Class that manages a CBot program. This is the main entry point into the CBot engine.
@ -335,7 +336,14 @@ public:
*/ */
bool m_bCompileClass; bool m_bCompileClass;
/**
* \brief Returns static list of all registered external calls
*/
static CBotExternalCallList* GetExternalCalls();
private: private:
//! All external calls
static CBotExternalCallList* m_externalCalls;
//! All user-defined functions //! All user-defined functions
CBotFunction* m_functions = nullptr; CBotFunction* m_functions = nullptr;
//! The entry point function //! The entry point function

View File

@ -744,7 +744,7 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo
// first looks by the identifier // first looks by the identifier
res = CBotExternalCallList::DoCall(nullptr, ppVar, this, rettype); res = m_prog->GetExternalCalls()->DoCall(nullptr, ppVar, this, rettype);
if (res.GetType() >= 0) return res.GetType(); if (res.GetType() >= 0) return res.GetType();
res = m_prog->GetFunctions()->DoCall(nIdent, "", ppVar, this, token ); res = m_prog->GetFunctions()->DoCall(nIdent, "", ppVar, this, token );
@ -753,7 +753,7 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo
// if not found (recompile?) seeks by name // if not found (recompile?) seeks by name
nIdent = 0; nIdent = 0;
res = CBotExternalCallList::DoCall(token, ppVar, this, rettype); res = m_prog->GetExternalCalls()->DoCall(token, ppVar, this, rettype);
if (res.GetType() >= 0) return res.GetType(); if (res.GetType() >= 0) return res.GetType();
res = m_prog->GetFunctions()->DoCall(nIdent, token->GetString(), ppVar, this, token ); res = m_prog->GetFunctions()->DoCall(nIdent, token->GetString(), ppVar, this, token );
@ -766,10 +766,12 @@ bool CBotStack::ExecuteCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBo
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar) void CBotStack::RestoreCall(long& nIdent, CBotToken* token, CBotVar** ppVar)
{ {
if ( m_next == nullptr ) return; if (m_next == nullptr) return;
if ( !CBotExternalCallList::RestoreCall(token, ppVar, this)) if (m_prog->GetExternalCalls()->RestoreCall(token, ppVar, this))
m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this ); return;
m_prog->GetFunctions()->RestoreCall(nIdent, token->GetString(), ppVar, this);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////