From 2519825104750e45c2de2f84ff3324cc1d00fb51 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Mon, 21 Dec 2015 23:07:40 +0100 Subject: [PATCH] CBotVar::ProtectionLevel enum --- src/CBot/CBotClass.cpp | 10 +++++----- src/CBot/CBotClass.h | 6 +++--- src/CBot/CBotDefines.h | 13 ------------- src/CBot/CBotInstr/CBotExprVar.cpp | 2 +- src/CBot/CBotInstr/CBotExprVar.h | 4 +++- src/CBot/CBotInstr/CBotLeftExpr.cpp | 4 ++-- src/CBot/CBotInstr/CBotParExpr.cpp | 4 ++-- src/CBot/CBotStack.cpp | 4 ++-- src/CBot/CBotVar/CBotVar.cpp | 10 +++++----- src/CBot/CBotVar/CBotVar.h | 16 +++++++++++---- src/CBot/CBotVar/CBotVarBoolean.cpp | 2 +- src/CBot/CBotVar/CBotVarClass.cpp | 2 +- src/CBot/CBotVar/CBotVarFloat.cpp | 2 +- src/CBot/CBotVar/CBotVarInt.cpp | 2 +- src/CBot/CBotVar/CBotVarString.cpp | 2 +- src/CBot/stdlib/FileFunctions.cpp | 2 +- src/script/scriptfunc.cpp | 30 ++++++++++++++--------------- 17 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 34a3b333..c2a6e5b6 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -211,7 +211,7 @@ void CBotClass::FreeLock(CBotProgram* p) //////////////////////////////////////////////////////////////////////////////// bool CBotClass::AddItem(std::string name, CBotTypResult type, - int mPrivate) + CBotVar::ProtectionLevel mPrivate) { CBotClass* pClass = type.GetClass(); @@ -589,7 +589,7 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) { bool bStatic = false; - int mProtect = PR_PUBLIC; + CBotVar::ProtectionLevel mProtect = CBotVar::ProtectionLevel::Public; bool bSynchro = false; while (IsOfType(p, ID_SEP)) ; @@ -600,9 +600,9 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond) CBotToken* pBase = p; if ( IsOfType(p, ID_STATIC) ) bStatic = true; - if ( IsOfType(p, ID_PUBLIC) ) mProtect = PR_PUBLIC; - if ( IsOfType(p, ID_PRIVATE) ) mProtect = PR_PRIVATE; - if ( IsOfType(p, ID_PROTECTED) ) mProtect = PR_PROTECT; + if ( IsOfType(p, ID_PUBLIC) ) mProtect = CBotVar::ProtectionLevel::Public; + if ( IsOfType(p, ID_PRIVATE) ) mProtect = CBotVar::ProtectionLevel::Private; + if ( IsOfType(p, ID_PROTECTED) ) mProtect = CBotVar::ProtectionLevel::Protected; if ( IsOfType(p, ID_STATIC) ) bStatic = true; // CBotClass* pClass = nullptr; diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index 849a066f..b3f1f87c 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -21,8 +21,8 @@ // Modules inlcude #include "CBot/CBotDefines.h" - #include "CBot/CBotTypResult.h" +#include "CBot/CBotVar/CBotVar.h" // Local include @@ -30,7 +30,6 @@ #include // Forward declaration -class CBotVar; class CBotClass; class CBotCallMethode; class CBotFunction; @@ -107,7 +106,8 @@ public: * \param mPrivate * \return */ - bool AddItem(std::string name, CBotTypResult type, int mPrivate = PR_PUBLIC); + bool AddItem(std::string name, CBotTypResult type, + CBotVar::ProtectionLevel mPrivate = CBotVar::ProtectionLevel::Public); /*! * \brief AddItem Adds an item by passing the pointer to an instance of a diff --git a/src/CBot/CBotDefines.h b/src/CBot/CBotDefines.h index f2ede5ab..01f7e1b5 100644 --- a/src/CBot/CBotDefines.h +++ b/src/CBot/CBotDefines.h @@ -19,13 +19,6 @@ #pragma once -// Modules inlcude - -// Local include - -// Global include - - #define STACKMEM 1 /// \def preserve memory for the execution stack #define MAXSTACK 990 /// \def stack size reserved @@ -33,12 +26,6 @@ #define MAXARRAYSIZE 9999 -// variable type SetPrivate / IsPrivate -#define PR_PUBLIC 0 // public variable -#define PR_READ 1 // read only -#define PR_PROTECT 2 // protected (inheritance) -#define PR_PRIVATE 3 // strictly private - //! Define the current CBot version #define CBOTVERSION 104 diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp index 57bee4e3..9740521a 100644 --- a/src/CBot/CBotInstr/CBotExprVar.cpp +++ b/src/CBot/CBotInstr/CBotExprVar.cpp @@ -46,7 +46,7 @@ CBotExprVar::~CBotExprVar() } //////////////////////////////////////////////////////////////////////////////// -CBotInstr* CBotExprVar::Compile(CBotToken* &p, CBotCStack* pStack, int privat) +CBotInstr* CBotExprVar::Compile(CBotToken*& p, CBotCStack* pStack, CBotVar::ProtectionLevel privat) { // CBotToken* pDebut = p; CBotCStack* pStk = pStack->TokenStack(); diff --git a/src/CBot/CBotInstr/CBotExprVar.h b/src/CBot/CBotInstr/CBotExprVar.h index ff5cf17d..23063136 100644 --- a/src/CBot/CBotInstr/CBotExprVar.h +++ b/src/CBot/CBotInstr/CBotExprVar.h @@ -23,6 +23,7 @@ #include "CBot/CBotDefines.h" #include "CBot/CBotInstr/CBotInstr.h" +#include "CBot/CBotVar/CBotVar.h" // Local include @@ -54,7 +55,8 @@ public: * \param privat * \return */ - static CBotInstr* Compile(CBotToken* &p, CBotCStack* pStack, int privat=PR_PROTECT); + static CBotInstr* Compile(CBotToken*& p, CBotCStack* pStack, + CBotVar::ProtectionLevel privat = CBotVar::ProtectionLevel::Protected); /*! * \brief CompileMethode diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp index 56f124f1..aef6bbe8 100644 --- a/src/CBot/CBotInstr/CBotLeftExpr.cpp +++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp @@ -67,7 +67,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) inst->m_nIdent = var->GetUniqNum(); if (inst->m_nIdent > 0 && inst->m_nIdent < 9000) { - if ( var->IsPrivate(PR_READ) && + if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) && !pStk->GetBotCall()->m_bCompileClass) { pStk->SetError(CBotErrPrivate, p); @@ -131,7 +131,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack) var = var->GetItem(p->GetString()); // get item correspondent if (var != nullptr) { - if ( var->IsPrivate(PR_READ) && + if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) && !pStk->GetBotCall()->m_bCompileClass) { pStk->SetError(CBotErrPrivate, pp); diff --git a/src/CBot/CBotInstr/CBotParExpr.cpp b/src/CBot/CBotInstr/CBotParExpr.cpp index 25d530d7..897e1b04 100644 --- a/src/CBot/CBotInstr/CBotParExpr.cpp +++ b/src/CBot/CBotInstr/CBotParExpr.cpp @@ -102,7 +102,7 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) // recompile the variable for read-only delete inst; p = pvar; - inst = CBotExprVar::Compile(p, pStk, PR_READ); + inst = CBotExprVar::Compile(p, pStk, CBotVar::ProtectionLevel::ReadOnly); p = p->GetNext(); CBotPostIncExpr* i = new CBotPostIncExpr(); @@ -122,7 +122,7 @@ CBotInstr* CBotParExpr::Compile(CBotToken* &p, CBotCStack* pStack) if (p->GetType() == TokenTypVar) { - if (nullptr != (i->m_Instr = CBotExprVar::Compile(p, pStk, PR_READ))) + if (nullptr != (i->m_Instr = CBotExprVar::Compile(p, pStk, CBotVar::ProtectionLevel::ReadOnly))) { if (pStk->GetType() >= CBotTypBoolean) { diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 4b13df58..cb4ecc8b 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -943,7 +943,7 @@ bool CBotStack::RestoreState(FILE* pf, CBotStack* &pStack) //////////////////////////////////////////////////////////////////////////////// bool CBotVar::Save0State(FILE* pf) { - if (!WriteWord(pf, 100+m_mPrivate))return false; // private variable? + if (!WriteWord(pf, 100+static_cast(m_mPrivate)))return false; // private variable? if (!WriteWord(pf, m_bStatic))return false; // static variable? if (!WriteWord(pf, m_type.GetType()))return false; // saves the type (always non-zero) if (!WriteWord(pf, static_cast(m_binit))) return false; // variable defined? @@ -1106,7 +1106,7 @@ bool CBotVar::RestoreState(FILE* pf, CBotVar* &pVar) pNew->m_binit = initType; // pNew->SetInit(wi); pNew->SetStatic(st); - pNew->SetPrivate(prv-100); + pNew->SetPrivate(static_cast(prv-100)); pPrev = pNew; } return true; diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index aeab11db..cc86620d 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -58,7 +58,7 @@ CBotVar::CBotVar( ) m_binit = InitType::UNDEF; m_ident = 0; m_bStatic = false; - m_mPrivate = 0; + m_mPrivate = ProtectionLevel::Public; } //////////////////////////////////////////////////////////////////////////////// @@ -509,7 +509,7 @@ void CBotVar::SetStatic(bool bStatic) } //////////////////////////////////////////////////////////////////////////////// -void CBotVar::SetPrivate(int mPrivate) +void CBotVar::SetPrivate(ProtectionLevel mPrivate) { m_mPrivate = mPrivate; } @@ -521,13 +521,13 @@ bool CBotVar::IsStatic() } //////////////////////////////////////////////////////////////////////////////// -bool CBotVar::IsPrivate(int mode) +bool CBotVar::IsPrivate(ProtectionLevel mode) { - return m_mPrivate >= mode; + return static_cast(m_mPrivate) >= static_cast(mode); } //////////////////////////////////////////////////////////////////////////////// -int CBotVar::GetPrivate() +CBotVar::ProtectionLevel CBotVar::GetPrivate() { return m_mPrivate; } diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index 8acd9cf5..39e3b6c9 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -219,24 +219,32 @@ public: */ bool IsStatic(); + enum class ProtectionLevel + { + Public = 0, //!< public variable + ReadOnly = 1, //!< read only (can't be set from CBot, only from the engine) + Protected = 2, //!< protected (inheritance) + Private = 3 //!< private + }; + /*! * \brief SetPrivate * \param mPrivate */ - void SetPrivate(int mPrivate); + void SetPrivate(ProtectionLevel mPrivate); /*! * \brief IsPrivate * \param mode * \return */ - bool IsPrivate(int mode = PR_PROTECT); + bool IsPrivate(ProtectionLevel mode = ProtectionLevel::Protected); /*! * \brief GetPrivate * \return */ - int GetPrivate(); + ProtectionLevel GetPrivate(); /*! * \brief ConstructorSet @@ -590,7 +598,7 @@ protected: //! Static element (in class). bool m_bStatic; //! Element public, protected or private. - int m_mPrivate; + ProtectionLevel m_mPrivate; //! Expression for the original content. CBotInstr* m_InitExpr; //! List of limits for a table. diff --git a/src/CBot/CBotVar/CBotVarBoolean.cpp b/src/CBot/CBotVar/CBotVarBoolean.cpp index 10d33bd8..a06292f4 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.cpp +++ b/src/CBot/CBotVar/CBotVarBoolean.cpp @@ -41,7 +41,7 @@ CBotVarBoolean::CBotVarBoolean( const CBotToken* name ) m_type = CBotTypBoolean; m_binit = InitType::UNDEF; m_bStatic = false; - m_mPrivate = 0; + m_mPrivate = ProtectionLevel::Public; m_val = 0; } diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index 0dcef3ea..4caa4c97 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -61,7 +61,7 @@ CBotVarClass::CBotVarClass( const CBotToken* name, const CBotTypResult& type) m_pParent = nullptr; m_binit = InitType::UNDEF; m_bStatic = false; - m_mPrivate = 0; + m_mPrivate = ProtectionLevel::Public; m_bConstructor = false; m_CptUse = 0; m_ItemIdent = type.Eq(CBotTypIntrinsic) ? 0 : CBotVar::NextUniqNum(); diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp index 886d9730..0d7688ad 100644 --- a/src/CBot/CBotVar/CBotVarFloat.cpp +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -43,7 +43,7 @@ CBotVarFloat::CBotVarFloat( const CBotToken* name ) m_type = CBotTypFloat; m_binit = InitType::UNDEF; m_bStatic = false; - m_mPrivate = 0; + m_mPrivate = ProtectionLevel::Public; m_val = 0; } diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp index 614a959f..44373868 100644 --- a/src/CBot/CBotVar/CBotVarInt.cpp +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -42,7 +42,7 @@ CBotVarInt::CBotVarInt( const CBotToken* name ) m_type = CBotTypInt; m_binit = InitType::UNDEF; m_bStatic = false; - m_mPrivate = 0; + m_mPrivate = ProtectionLevel::Public; m_val = 0; } diff --git a/src/CBot/CBotVar/CBotVarString.cpp b/src/CBot/CBotVar/CBotVarString.cpp index 5cf3d47e..931c6102 100644 --- a/src/CBot/CBotVar/CBotVarString.cpp +++ b/src/CBot/CBotVar/CBotVarString.cpp @@ -41,7 +41,7 @@ CBotVarString::CBotVarString( const CBotToken* name ) m_type = CBotTypString; m_binit = InitType::UNDEF; m_bStatic = false; - m_mPrivate = 0; + m_mPrivate = ProtectionLevel::Public; m_val.clear(); } diff --git a/src/CBot/stdlib/FileFunctions.cpp b/src/CBot/stdlib/FileFunctions.cpp index 26836a90..c913d24c 100644 --- a/src/CBot/stdlib/FileFunctions.cpp +++ b/src/CBot/stdlib/FileFunctions.cpp @@ -348,7 +348,7 @@ void InitFileFunctions() // adds the component ".filename" bc->AddItem("filename", CBotTypString); // adds the component ".handle" - bc->AddItem("handle", CBotTypInt, PR_PRIVATE); + bc->AddItem("handle", CBotTypInt, CBotVar::ProtectionLevel::Private); // define a constructor and a destructor bc->AddFunction("file", rfconstruct, cfconstruct); diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp index 93e02419..b730bf29 100644 --- a/src/script/scriptfunc.cpp +++ b/src/script/scriptfunc.cpp @@ -3183,21 +3183,21 @@ void CScriptFunctions::Init() // Adds the class Object. bc = CBotClass::Create("object", nullptr); - bc->AddItem("category", CBotTypResult(CBotTypInt), PR_READ); - bc->AddItem("position", CBotTypResult(CBotTypClass, "point"), PR_READ); - bc->AddItem("orientation", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("pitch", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("roll", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("energyLevel", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("shieldLevel", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("temperature", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("altitude", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("lifeTime", CBotTypResult(CBotTypFloat), PR_READ); - bc->AddItem("energyCell", CBotTypResult(CBotTypPointer, "object"), PR_READ); - bc->AddItem("load", CBotTypResult(CBotTypPointer, "object"), PR_READ); - bc->AddItem("id", CBotTypResult(CBotTypInt), PR_READ); - bc->AddItem("team", CBotTypResult(CBotTypInt), PR_READ); - bc->AddItem("velocity", CBotTypResult(CBotTypClass, "point"), PR_READ); + bc->AddItem("category", CBotTypResult(CBotTypInt), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("position", CBotTypResult(CBotTypClass, "point"), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("orientation", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("pitch", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("roll", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("energyLevel", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("shieldLevel", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("temperature", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("altitude", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("lifeTime", CBotTypResult(CBotTypFloat), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("energyCell", CBotTypResult(CBotTypPointer, "object"), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("load", CBotTypResult(CBotTypPointer, "object"), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("id", CBotTypResult(CBotTypInt), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("team", CBotTypResult(CBotTypInt), CBotVar::ProtectionLevel::ReadOnly); + bc->AddItem("velocity", CBotTypResult(CBotTypClass, "point"), CBotVar::ProtectionLevel::ReadOnly); bc->AddFunction("busy", rBusy, cBusy); bc->AddFunction("factory", rFactory, cFactory); bc->AddFunction("research", rResearch, cClassOneFloat);