Skip to content

Commit

Permalink
Make public functions no longer shared between teams
Browse files Browse the repository at this point in the history
  • Loading branch information
hexagonrecursion committed Aug 13, 2024
1 parent 68f3391 commit 91645a1
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 61 deletions.
2 changes: 1 addition & 1 deletion CBot/src/CBot/CBotCStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ bool CBotCStack::CheckCall(CBotToken* &pToken, CBotDefParam* pParam, const std::
}
}

for (CBotFunction* pp : CBotFunction::m_publicFunctions)
for (CBotFunction* pp : GetProgram()->GetPublicFunctions())
{
if ( name == pp->GetName() )
{
Expand Down
56 changes: 25 additions & 31 deletions CBot/src/CBot/CBotInstr/CBotFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,13 @@ CBotFunction::CBotFunction(CBotProgram& prog):
m_bSynchro = false;
}

////////////////////////////////////////////////////////////////////////////////
std::set<CBotFunction*> CBotFunction::m_publicFunctions{};

////////////////////////////////////////////////////////////////////////////////
CBotFunction::~CBotFunction()
{
delete m_param; // empty parameter list
delete m_block; // the instruction block

// remove public list if there is
if (m_bPublic)
{
m_publicFunctions.erase(this);
}
m_prog.RemovePublic(this);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -503,6 +496,7 @@ void CBotFunction::RestoreState(CBotVar** ppVars, CBotStack* &pj, CBotVar* pInst
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotFunction::CompileCall(const std::string &name, CBotVar** ppVars, long &nIdent, CBotProgram* program)
{
assert(program);
CBotTypResult type;
if (!FindLocalOrPublic(program->GetFunctions(), nIdent, name, ppVars, type, program))
{
Expand All @@ -516,6 +510,7 @@ CBotTypResult CBotFunction::CompileCall(const std::string &name, CBotVar** ppVar
CBotFunction* CBotFunction::FindLocalOrPublic(const std::list<CBotFunction*>& localFunctionList, long &nIdent, const std::string &name,
CBotVar** ppVars, CBotTypResult &TypeOrError, CBotProgram* baseProg)
{
assert(baseProg);
TypeOrError.SetType(CBotErrUndefCall); // no routine of the name

if ( nIdent )
Expand All @@ -530,7 +525,7 @@ CBotFunction* CBotFunction::FindLocalOrPublic(const std::list<CBotFunction*>& lo
}

// search the list of public functions
for (CBotFunction* pt : m_publicFunctions)
for (CBotFunction* pt : baseProg->GetPublicFunctions())
{
if (pt->m_nFuncIdent == nIdent)
{
Expand All @@ -546,14 +541,14 @@ CBotFunction* CBotFunction::FindLocalOrPublic(const std::list<CBotFunction*>& lo

CBotFunction::SearchList(localFunctionList, name, ppVars, TypeOrError, funcMap);

CBotFunction::SearchPublic(name, ppVars, TypeOrError, funcMap);
CBotFunction::SearchPublic(name, ppVars, TypeOrError, funcMap, nullptr, baseProg);

if (baseProg != nullptr && baseProg->m_thisVar != nullptr)
if (baseProg->m_thisVar != nullptr)
{
// find object:: functions
CBotClass* pClass = baseProg->m_thisVar->GetClass();
CBotFunction::SearchList(localFunctionList, name, ppVars, TypeOrError, funcMap, pClass);
CBotFunction::SearchPublic(name, ppVars, TypeOrError, funcMap, pClass);
CBotFunction::SearchPublic(name, ppVars, TypeOrError, funcMap, pClass, baseProg);
}

return CBotFunction::BestFunction(funcMap, nIdent, TypeOrError);
Expand Down Expand Up @@ -637,10 +632,11 @@ void CBotFunction::SearchList(const std::list<CBotFunction*>& functionList,

////////////////////////////////////////////////////////////////////////////////
void CBotFunction::SearchPublic(const std::string& name, CBotVar** ppVars, CBotTypResult& TypeOrError,
std::map<CBotFunction*, int>& funcMap, CBotClass* pClass)
std::map<CBotFunction*, int>& funcMap, CBotClass* pClass, CBotProgram* program)
{
assert(program);
{
for (CBotFunction* pt : m_publicFunctions)
for (CBotFunction* pt : program->GetPublicFunctions())
{
if ( pt->m_token.GetString() == name )
{
Expand Down Expand Up @@ -750,6 +746,7 @@ int CBotFunction::DoCall(CBotProgram* program, const std::list<CBotFunction*>& l
CBotTypResult type;
CBotFunction* pt = nullptr;
CBotProgram* baseProg = pStack->GetProgram(true);
assert(baseProg);

pt = FindLocalOrPublic(localFunctionList, nIdent, name, ppVars, type, baseProg);

Expand Down Expand Up @@ -841,6 +838,7 @@ void CBotFunction::RestoreCall(const std::list<CBotFunction*>& localFunctionList
CBotStack* pStk1;
CBotStack* pStk3;
CBotProgram* baseProg = pStack->GetProgram(true);
assert(baseProg);

pt = FindLocalOrPublic(localFunctionList, nIdent, name, ppVars, type, baseProg);

Expand Down Expand Up @@ -973,19 +971,19 @@ CBotFunction* CBotFunction::FindMethod(long& nIdent, const std::string& name,
return pt;
}
}
}

// search the list of public functions
if (!skipPublic)
{
for (CBotFunction* pt : m_publicFunctions)
// search the list of public functions
if (!skipPublic)
{
if (pt->m_nFuncIdent == nIdent)
for (CBotFunction* pt : program->GetPublicFunctions())
{
// check if the method is inherited, break in case there is an override
if ( pt->GetClassName() != pClass->GetName() ) break;
TypeOrError = pt->m_retTyp;
return pt;
if (pt->m_nFuncIdent == nIdent)
{
// check if the method is inherited, break in case there is an override
if ( pt->GetClassName() != pClass->GetName() ) break;
TypeOrError = pt->m_retTyp;
return pt;
}
}
}
}
Expand All @@ -1000,9 +998,10 @@ CBotFunction* CBotFunction::FindMethod(long& nIdent, const std::string& name,

// search the current program for methods
if (program != nullptr)
{
CBotFunction::SearchList(program->GetFunctions(), name, ppVars, TypeOrError, funcMap, pClass);

CBotFunction::SearchPublic(name, ppVars, TypeOrError, funcMap, pClass);
CBotFunction::SearchPublic(name, ppVars, TypeOrError, funcMap, pClass, program);
}

return CBotFunction::BestFunction(funcMap, nIdent, TypeOrError);
}
Expand Down Expand Up @@ -1211,11 +1210,6 @@ const std::string& CBotFunction::GetClassName() const
}

////////////////////////////////////////////////////////////////////////////////
void CBotFunction::AddPublic(CBotFunction* func)
{
m_publicFunctions.insert(func);
}

bool CBotFunction::HasReturn()
{
if (m_block != nullptr) return m_block->HasReturn();
Expand Down
31 changes: 11 additions & 20 deletions CBot/src/CBot/CBotInstr/CBotFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,6 @@ class CBotFunction : public CBotInstr
const std::string& name, CBotVar** ppVars, CBotTypResult& TypeOrError,
std::map<CBotFunction*, int>& funcMap, CBotClass* pClass = nullptr);

/*!
* \brief Find all public functions that match the name and arguments.
* \param name Name of the function to find.
* \param ppVars Arguments to compare with parameters.
* \param TypeOrError Contains a CBotError when no useable function has been found.
* \param funcMap Container for suitable functions and their signature values.
* \param pClass Pointer to class when searching for methods.
*/
static void SearchPublic(const std::string& name, CBotVar** ppVars, CBotTypResult& TypeOrError,
std::map<CBotFunction*, int>& funcMap, CBotClass* pClass = nullptr);

/*!
* \brief Find the function with the lowest signature value. If there is more
* than one of the same signature value, TypeOrError is set to CBotErrAmbiguousCall.
Expand Down Expand Up @@ -253,12 +242,6 @@ class CBotFunction : public CBotInstr
*/
bool CheckParam(CBotDefParam* pParam);

/*!
* \brief AddPublic
* \param pfunc
*/
static void AddPublic(CBotFunction* pfunc);

/*!
* \brief GetName
* \return
Expand Down Expand Up @@ -325,6 +308,17 @@ class CBotFunction : public CBotInstr
virtual std::map<std::string, CBotInstr*> GetDebugLinks() override;

private:
/*!
* \brief Find all public functions that match the name and arguments.
* \param name Name of the function to find.
* \param ppVars Arguments to compare with parameters.
* \param TypeOrError Contains a CBotError when no useable function has been found.
* \param funcMap Container for suitable functions and their signature values.
* \param pClass Pointer to class when searching for methods.
*/
static void SearchPublic(const std::string& name, CBotVar** ppVars, CBotTypResult& TypeOrError,
std::map<CBotFunction*, int>& funcMap, CBotClass* pClass, CBotProgram* program);

friend class CBotDebug;
long m_nFuncIdent;
//! Synchronized method.
Expand Down Expand Up @@ -358,9 +352,6 @@ class CBotFunction : public CBotInstr
CBotToken m_openblk;
CBotToken m_closeblk;

//! List of public functions
static std::set<CBotFunction*> m_publicFunctions;

friend class CBotProgram;
friend class CBotClass;
friend class CBotCStack;
Expand Down
18 changes: 15 additions & 3 deletions CBot/src/CBot/CBotProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "CBot/CBotCStack.h"
#include "CBot/CBotClass.h"
#include "CBot/CBotUtils.h"
#include "CBot/CBotProgramGroup.h"

#include "CBot/CBotInstr/CBotFunction.h"

Expand All @@ -36,8 +37,8 @@ namespace CBot

std::unique_ptr<CBotExternalCallList> CBotProgram::m_externalCalls;

CBotProgram::CBotProgram(CBotVar* thisVar)
: m_thisVar(thisVar)
CBotProgram::CBotProgram(CBotVar* thisVar, CBotProgramGroup& group)
: m_thisVar(thisVar), m_group(group)
{
}

Expand Down Expand Up @@ -126,7 +127,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
{
CBotFunction::Compile(p, pStack.get(), *next);
if ((*next)->IsExtern()) externFunctions.push_back((*next)->GetName()/* + next->GetParams()*/);
if ((*next)->IsPublic()) CBotFunction::AddPublic(*next);
if ((*next)->IsPublic()) m_group.AddPublic(*next);
++next;
}
}
Expand Down Expand Up @@ -417,4 +418,15 @@ const std::unique_ptr<CBotExternalCallList>& CBotProgram::GetExternalCalls()
return m_externalCalls;
}


const std::set<CBotFunction*>& CBotProgram::GetPublicFunctions() const
{
return m_group.GetPublicFunctions();
}

void CBotProgram::RemovePublic(CBotFunction* func)
{
m_group.RemovePublic(func);
}

} // namespace CBot
14 changes: 12 additions & 2 deletions CBot/src/CBot/CBotProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <memory>
#include <string>
#include <vector>
#include <set>

namespace CBot
{
Expand All @@ -35,6 +36,7 @@ class CBotStack;
class CBotTypResult;
class CBotVar;
class CBotExternalCallList;
class CBotProgramGroup;

/**
* \brief Class that manages a CBot program. This is the main entry point into the CBot engine.
Expand Down Expand Up @@ -326,8 +328,14 @@ class CBotProgram
* \brief Constructor
* \param thisVar Variable to pass to the program as "this"
*/
CBotProgram(CBotVar* thisVar);
private:
CBotProgram(CBotVar* thisVar, CBotProgramGroup& group);

/**
* \brief Returns all public functions in the program group that contains this program
*/
const std::set<CBotFunction*>& GetPublicFunctions() const;

void RemovePublic(CBotFunction* func);

//! All external calls
static std::unique_ptr<CBotExternalCallList> m_externalCalls;
Expand All @@ -344,10 +352,12 @@ class CBotProgram
friend class CBotFunction;
friend class CBotDebug;
friend class CBotProgramGroup;
friend class CBotCStack;

CBotError m_error = CBotNoErr;
int m_errorStart = 0;
int m_errorEnd = 0;
CBotProgramGroup& m_group;
};

} // namespace CBot
17 changes: 16 additions & 1 deletion CBot/src/CBot/CBotProgramGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,22 @@ namespace CBot

std::unique_ptr<CBotProgram> CBotProgramGroup::AddProgram(CBotVar* thisVar)
{
return std::unique_ptr<CBotProgram>(new CBotProgram(thisVar));
return std::unique_ptr<CBotProgram>(new CBotProgram(thisVar, *this));
}

const std::set<CBotFunction*>& CBotProgramGroup::GetPublicFunctions() const
{
return m_publicFunctions;
}

void CBotProgramGroup::AddPublic(CBotFunction* func)
{
m_publicFunctions.insert(func);
}

void CBotProgramGroup::RemovePublic(CBotFunction* func)
{
m_publicFunctions.erase(func);
}

} // namespace CBot
Expand Down
10 changes: 10 additions & 0 deletions CBot/src/CBot/CBotProgramGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,27 @@

#include <memory>
#include <string>
#include <set>

namespace CBot
{

class CBotProgram;
class CBotVar;
class CBotFunction;

class CBotProgramGroup
{
public:
std::unique_ptr<CBotProgram> AddProgram(CBotVar* thisVar);

private:
const std::set<CBotFunction*>& GetPublicFunctions() const;
void AddPublic(CBotFunction* func);
void RemovePublic(CBotFunction* func);
friend class CBotProgram;

std::set<CBotFunction*> m_publicFunctions;
};

} // namespace CBot
5 changes: 2 additions & 3 deletions colobot-base/src/level/robotmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ class CRobotMain : public CSingleton<CRobotMain>
Gfx::CLightManager* m_lightMan = nullptr;
CSoundInterface* m_sound = nullptr;
CInput* m_input = nullptr;
CBot::CBotProgramGroup m_refereeGroup; // CBotProgramGroup must outlive CObjectManager
std::map<int, CBot::CBotProgramGroup> m_progGroups; // CBotProgramGroup must outlive CObjectManager
std::unique_ptr<CObjectManager> m_objMan;
std::unique_ptr<CMainMovie> m_movie;
std::unique_ptr<CPauseManager> m_pause;
Expand Down Expand Up @@ -772,7 +774,4 @@ class CRobotMain : public CSingleton<CRobotMain>

//! Vector of available viewpoints
std::vector<Viewpoint> m_viewpoints;

CBot::CBotProgramGroup m_refereeGroup;
std::map<int, CBot::CBotProgramGroup> m_progGroups;
};

0 comments on commit 91645a1

Please sign in to comment.