[colobot] 178/377: More random CBotToken refactoring, removed CBotToken::Delete

Didier Raboud odyx at moszumanska.debian.org
Wed Mar 30 13:34:13 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 6ef14617a0eb53aa451f029bf36cb6698795f5aa
Author: krzys-h <krzys_h at interia.pl>
Date:   Wed Dec 23 16:46:41 2015 +0100

    More random CBotToken refactoring, removed CBotToken::Delete
---
 src/CBot/CBotCStack.cpp             |  4 ++--
 src/CBot/CBotCStack.h               |  8 ++++----
 src/CBot/CBotClass.cpp              |  2 +-
 src/CBot/CBotInstr/CBotExprVar.cpp  |  4 ++--
 src/CBot/CBotInstr/CBotLeftExpr.cpp |  4 ++--
 src/CBot/CBotProgram.cpp            | 24 ++++++++++------------
 src/CBot/CBotProgram.h              | 14 ++++++-------
 src/CBot/CBotToken.cpp              | 11 ++--------
 src/CBot/CBotToken.h                | 10 ++--------
 src/script/script.cpp               | 40 +++++++++++--------------------------
 10 files changed, 44 insertions(+), 77 deletions(-)

diff --git a/src/CBot/CBotCStack.cpp b/src/CBot/CBotCStack.cpp
index 9058003..a524260 100644
--- a/src/CBot/CBotCStack.cpp
+++ b/src/CBot/CBotCStack.cpp
@@ -262,13 +262,13 @@ bool CBotCStack::NextToken(CBotToken* &p)
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void CBotCStack::SetBotCall(CBotProgram* p)
+void CBotCStack::SetProgram(CBotProgram* p)
 {
     m_prog = p;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-CBotProgram* CBotCStack::GetBotCall()
+CBotProgram* CBotCStack::GetProgram()
 {
     return m_prog;
 }
diff --git a/src/CBot/CBotCStack.h b/src/CBot/CBotCStack.h
index ff18378..02c78f3 100644
--- a/src/CBot/CBotCStack.h
+++ b/src/CBot/CBotCStack.h
@@ -213,16 +213,16 @@ public:
     CBotTypResult GetRetType();
 
     /*!
-     * \brief SetBotCall
+     * \brief SetProgram
      * \param p
      */
-    void SetBotCall(CBotProgram* p);
+    void SetProgram(CBotProgram* p);
 
     /*!
-     * \brief GetBotCall
+     * \brief GetProgram
      * \return
      */
-    CBotProgram* GetBotCall();
+    CBotProgram* GetProgram();
 
     /*!
      * \brief CompileCall
diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp
index 9817a7b..1f8a307 100644
--- a/src/CBot/CBotClass.cpp
+++ b/src/CBot/CBotClass.cpp
@@ -723,7 +723,7 @@ bool CBotClass::CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond)
 
                     if ( f != nullptr )
                     {
-                        f->m_pProg = pStack->GetBotCall();
+                        f->m_pProg = pStack->GetProgram();
                         f->m_bSynchro = bSynchro;
                         // replaces the element in the chain
                         f->m_next = pf->m_next;
diff --git a/src/CBot/CBotInstr/CBotExprVar.cpp b/src/CBot/CBotInstr/CBotExprVar.cpp
index 9740521..bca8171 100644
--- a/src/CBot/CBotInstr/CBotExprVar.cpp
+++ b/src/CBot/CBotInstr/CBotExprVar.cpp
@@ -70,7 +70,7 @@ CBotInstr* CBotExprVar::Compile(CBotToken*& p, CBotCStack* pStack, CBotVar::Prot
             if (ident > 0 && ident < 9000)
             {
                 if ( var->IsPrivate(privat) &&
-                     !pStk->GetBotCall()->m_bCompileClass)
+                     !pStk->GetProgram()->m_bCompileClass)
                 {
                     pStk->SetError(CBotErrPrivate, p);
                     goto err;
@@ -140,7 +140,7 @@ CBotInstr* CBotExprVar::Compile(CBotToken*& p, CBotCStack* pStack, CBotVar::Prot
                                 {
                                     i->SetUniqNum(var->GetUniqNum());
                                     if ( var->IsPrivate() &&
-                                     !pStk->GetBotCall()->m_bCompileClass)
+                                     !pStk->GetProgram()->m_bCompileClass)
                                     {
                                         pStk->SetError(CBotErrPrivate, pp);
                                         goto err;
diff --git a/src/CBot/CBotInstr/CBotLeftExpr.cpp b/src/CBot/CBotInstr/CBotLeftExpr.cpp
index aef6bbe..d87f74a 100644
--- a/src/CBot/CBotInstr/CBotLeftExpr.cpp
+++ b/src/CBot/CBotInstr/CBotLeftExpr.cpp
@@ -68,7 +68,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack)
             if (inst->m_nIdent > 0 && inst->m_nIdent < 9000)
             {
                 if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) &&
-                     !pStk->GetBotCall()->m_bCompileClass)
+                     !pStk->GetProgram()->m_bCompileClass)
                 {
                     pStk->SetError(CBotErrPrivate, p);
                     goto err;
@@ -132,7 +132,7 @@ CBotLeftExpr* CBotLeftExpr::Compile(CBotToken* &p, CBotCStack* pStack)
                             if (var != nullptr)
                             {
                                 if ( var->IsPrivate(CBotVar::ProtectionLevel::ReadOnly) &&
-                                     !pStk->GetBotCall()->m_bCompileClass)
+                                     !pStk->GetProgram()->m_bCompileClass)
                                 {
                                     pStk->SetError(CBotErrPrivate, pp);
                                     goto err;
diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp
index 66697d7..8d54af3 100644
--- a/src/CBot/CBotProgram.cpp
+++ b/src/CBot/CBotProgram.cpp
@@ -70,13 +70,13 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
     m_error = CBotNoErr;
 
     // Step 1. Process the code into tokens
-    CBotToken*  pBaseToken = CBotToken::CompileTokens(program);
-    if ( pBaseToken == nullptr ) return false;
+    auto tokens = CBotToken::CompileTokens(program);
+    if (tokens == nullptr) return false;
 
-    CBotCStack* pStack      = new CBotCStack(nullptr);
-    CBotToken*  p  = pBaseToken->GetNext();                 // skips the first token (separator)
+    auto pStack = std::unique_ptr<CBotCStack>(new CBotCStack(nullptr));
+    CBotToken* p = tokens.get()->GetNext();                 // skips the first token (separator)
 
-    pStack->SetBotCall(this);                               // defined used routines
+    pStack->SetProgram(this);                               // defined used routines
     CBotCall::SetPUser(pUser);
 
     // Step 2. Find all function and class definitions
@@ -87,13 +87,13 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
         if ( p->GetType() == ID_CLASS ||
             ( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS ))
         {
-            CBotClass*  nxt = CBotClass::Compile1(p, pStack);
+            CBotClass*  nxt = CBotClass::Compile1(p, pStack.get());
             if (m_classes == nullptr ) m_classes = nxt;
             else m_classes->AddNext(nxt);
         }
         else
         {
-            CBotFunction*   next = CBotFunction::Compile1(p, pStack, nullptr);
+            CBotFunction*   next = CBotFunction::Compile1(p, pStack.get(), nullptr);
             if (m_functions == nullptr ) m_functions = next;
             else m_functions->AddNext(next);
         }
@@ -103,7 +103,6 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
         m_error = pStack->GetError(m_errorStart, m_errorEnd);
         delete m_functions;
         m_functions = nullptr;
-        delete pBaseToken;
         return false;
     }
 
@@ -111,7 +110,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
 //  CBotFunction*   temp = nullptr;
     CBotFunction*   next = m_functions;      // rewind the list
 
-    p  = pBaseToken->GetNext();                             // returns to the beginning
+    p  = tokens.get()->GetNext();                             // returns to the beginning
 
     while ( pStack->IsOk() && p != nullptr && p->GetType() != 0 )
     {
@@ -121,12 +120,12 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
             ( p->GetType() == ID_PUBLIC && p->GetNext()->GetType() == ID_CLASS ))
         {
             m_bCompileClass = true;
-            CBotClass::Compile(p, pStack);                  // completes the definition of the class
+            CBotClass::Compile(p, pStack.get());                  // completes the definition of the class
         }
         else
         {
             m_bCompileClass = false;
-            CBotFunction::Compile(p, pStack, next);
+            CBotFunction::Compile(p, pStack.get(), next);
             if (next->IsExtern()) functions.push_back(next->GetName()/* + next->GetParams()*/);
             next->m_pProg = this;                           // keeps pointers to the module
             next = next->Next();
@@ -143,9 +142,6 @@ bool CBotProgram::Compile(const std::string& program, std::vector<std::string>&
         m_functions = nullptr;
     }
 
-    delete pBaseToken;
-    delete pStack;
-
     return (m_functions != nullptr);
 }
 
diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h
index 5d27f92..f8bc93c 100644
--- a/src/CBot/CBotProgram.h
+++ b/src/CBot/CBotProgram.h
@@ -277,7 +277,7 @@ public:
      */
     static bool DefineNum(const std::string& name, long val);
 
-    /*!
+    /**
      * \brief Save the current execution status into a file
      * \param pf
      * \parblock
@@ -291,7 +291,7 @@ public:
      */
     bool SaveState(FILE* pf);
 
-    /*!
+    /**
      * \brief Restore the execution state from a file
      *
      * The previous program code must already have been recompiled with Compile() before calling this function
@@ -301,7 +301,7 @@ public:
      */
     bool RestoreState(FILE* pf);
 
-    /*!
+    /**
      * \brief GetPosition Gives the position of a routine in the original text
      * the user can select the item to find from the beginning to the end
      * see the above modes in CBotGet.
@@ -321,7 +321,7 @@ public:
                      CBotGet modestart = GetPosExtern,
                      CBotGet modestop = GetPosBloc);
 
-    /*!
+    /**
      * \brief Returns the list of all user-defined functions in this program as instances of CBotFunction
      *
      * This list includes all the functions (not only extern)
@@ -330,10 +330,10 @@ public:
      */
     CBotFunction* GetFunctions();
 
-    /*!
-     * \brief m_bCompileClass
+    /**
+     * \brief true while compiling class
      *
-     * TODO: document this
+     * TODO: refactor this
      */
     bool m_bCompileClass;
 
diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp
index 89f87e9..16a3c22 100644
--- a/src/CBot/CBotToken.cpp
+++ b/src/CBot/CBotToken.cpp
@@ -20,7 +20,6 @@
 #include "CBot/CBotToken.h"
 
 #include <cstdarg>
-#include <map>
 
 //! \brief Keeps the string corresponding to keyword ID
 //! Map is filled with id-string pars that are needed for CBot language parsing
@@ -449,7 +448,7 @@ bis:
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-CBotToken* CBotToken::CompileTokens(const std::string& program)
+std::unique_ptr<CBotToken> CBotToken::CompileTokens(const std::string& program)
 {
     CBotToken       *nxt, *prv, *tokenbase;
     const char*     p = program.c_str();
@@ -477,13 +476,7 @@ CBotToken* CBotToken::CompileTokens(const std::string& program)
         pp = p;
     }
 
-    return tokenbase;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-void CBotToken::Delete(CBotToken* pToken)
-{
-    delete pToken;
+    return std::unique_ptr<CBotToken>(tokenbase);
 }
 
 ////////////////////////////////////////////////////////////////////////////////
diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h
index 6955ed6..93274b7 100644
--- a/src/CBot/CBotToken.h
+++ b/src/CBot/CBotToken.h
@@ -22,6 +22,7 @@
 #include <vector>
 #include <string>
 #include <map>
+#include <memory>
 
 #include "CBot/CBotEnums.h"
 
@@ -165,14 +166,7 @@ public:
      * \return The first token of the linked liste.
      * \todo Replace the error code by an enum.
      */
-    static CBotToken* CompileTokens(const std::string& p);
-
-    /**
-     * \brief Delete Releases the CBotToken linked list.
-     * \deprecated This function is deprecated because it only delete a pointer.
-     * \todo Remove this function.
-     */
-    static void Delete(CBotToken* pToken);
+    static std::unique_ptr<CBotToken> CompileTokens(const std::string& p);
 
     /*!
      * \brief DefineNum This function define a language keyword with an associated
diff --git a/src/script/script.cpp b/src/script/script.cpp
index 3537b97..b7d24be 100644
--- a/src/script/script.cpp
+++ b/src/script/script.cpp
@@ -156,13 +156,6 @@ bool CScript::IsEmpty()
 
 bool CScript::CheckToken()
 {
-    CBotToken*  bt;
-    CBotToken*  allBt;
-    std::string bs;
-    std::string token;
-    int         cursor1, cursor2, i;
-    char        used[100];
-
     if ( !m_object->GetCheckToken() )  return true;
 
     m_error = CBotNoErr;
@@ -171,25 +164,20 @@ bool CScript::CheckToken()
     m_token[0] = 0;
     m_bCompile = false;
 
-    for ( i=0 ; i<m_main->GetObligatoryToken() ; i++ )
-    {
-        used[i] = 0;  // token not used
-    }
+    std::vector<bool> used(m_main->GetObligatoryToken(), false);
 
-    allBt = CBotToken::CompileTokens(m_script.get());
-    bt = allBt;
+    auto tokens = CBotToken::CompileTokens(m_script.get());
+    CBotToken* bt = tokens.get();
     while ( bt != nullptr )
     {
-        bs = bt->GetString();
-        token = bs;
-
-        cursor1 = bt->GetStart();
-        cursor2 = bt->GetEnd();
+        std::string token = bt->GetString();
+        int cursor1 = bt->GetStart();
+        int cursor2 = bt->GetEnd();
 
-        i = m_main->IsObligatoryToken(token.c_str());
+        int i = m_main->IsObligatoryToken(token.c_str());
         if ( i != -1 )
         {
-            used[i] = 1;  // token used
+            used[i] = true;  // token used
         }
 
         if ( !m_main->IsProhibitedToken(token.c_str()) )
@@ -199,7 +187,6 @@ bool CScript::CheckToken()
             m_cursor2 = cursor2;
             strcpy(m_title, "<prohibited>");
             m_mainFunction[0] = 0;
-            CBotToken::Delete(allBt);
             return false;
         }
 
@@ -207,20 +194,18 @@ bool CScript::CheckToken()
     }
 
     // At least once every obligatory instruction?
-    for ( i=0 ; i<m_main->GetObligatoryToken() ; i++ )
+    for (unsigned int i = 0; i < used.size(); i++)
     {
-        if ( used[i] == 0 )  // token not used?
+        if (!used[i])  // token not used?
         {
             strcpy(m_token, m_main->GetObligatoryToken(i));
             m_error = static_cast<CBotError>(ERR_OBLIGATORYTOKEN);
             strcpy(m_title, "<obligatory>");
             m_mainFunction[0] = 0;
-            CBotToken::Delete(allBt);
             return false;
         }
     }
 
-    CBotToken::Delete(allBt);
     return true;
 }
 
@@ -635,7 +620,8 @@ void CScript::ColorizeScript(Ui::CEdit* edit, int rangeStart, int rangeEnd)
     std::string text = std::string(edit->GetText(), edit->GetMaxChar());
     text = text.substr(rangeStart, rangeEnd-rangeStart);
 
-    CBotToken* bt = CBotToken::CompileTokens(text.c_str());
+    auto tokens = CBotToken::CompileTokens(text.c_str());
+    CBotToken* bt = tokens.get();
     while ( bt != nullptr )
     {
         std::string token = bt->GetString();
@@ -684,8 +670,6 @@ void CScript::ColorizeScript(Ui::CEdit* edit, int rangeStart, int rangeEnd)
 
         bt = bt->GetNext();
     }
-
-    CBotToken::Delete(bt);
 }
 
 

-- 
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