[colobot] 190/377: Extracted most of linked list logic into a template class
Didier Raboud
odyx at moszumanska.debian.org
Wed Mar 30 13:34:14 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 3008e18fc680af81f9f02928b2a84f25d0894c81
Author: krzys-h <krzys_h at interia.pl>
Date: Wed Dec 23 23:33:40 2015 +0100
Extracted most of linked list logic into a template class
---
src/CBot/CBotCall.cpp | 9 ----
src/CBot/CBotCall.h | 15 +-----
src/CBot/CBotCallMethode.cpp | 18 -------
src/CBot/CBotCallMethode.h | 20 +-------
src/CBot/CBotClass.cpp | 12 -----
src/CBot/CBotClass.h | 10 +---
src/CBot/CBotDefParam.cpp | 17 -------
src/CBot/CBotDefParam.h | 22 +--------
src/CBot/CBotInstr/CBotFunction.cpp | 2 +-
src/CBot/CBotToken.cpp | 47 ++++---------------
src/CBot/CBotToken.h | 24 ++--------
src/CBot/CBotUtils.h | 94 +++++++++++++++++++++++++++++++++++--
src/CBot/CBotVar/CBotVar.cpp | 17 -------
src/CBot/CBotVar/CBotVar.h | 24 +---------
14 files changed, 114 insertions(+), 217 deletions(-)
diff --git a/src/CBot/CBotCall.cpp b/src/CBot/CBotCall.cpp
index d1e7232..2d1ffe7 100644
--- a/src/CBot/CBotCall.cpp
+++ b/src/CBot/CBotCall.cpp
@@ -47,15 +47,12 @@ CBotCall::CBotCall(const std::string& name,
m_name = name;
m_rExec = rExec;
m_rComp = rCompile;
- m_next = nullptr;
m_nFuncIdent = CBotVar::NextUniqNum();
}
////////////////////////////////////////////////////////////////////////////////
CBotCall::~CBotCall()
{
- if (m_next) delete m_next;
- m_next = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
@@ -156,12 +153,6 @@ std::string CBotCall::GetName()
}
////////////////////////////////////////////////////////////////////////////////
-CBotCall* CBotCall::Next()
-{
- return m_next;
-}
-
-////////////////////////////////////////////////////////////////////////////////
int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype)
{
CBotCall* pt = m_ListCalls;
diff --git a/src/CBot/CBotCall.h b/src/CBot/CBotCall.h
index 3dcb3aa..26789aa 100644
--- a/src/CBot/CBotCall.h
+++ b/src/CBot/CBotCall.h
@@ -19,14 +19,10 @@
#pragma once
-// Modules inlcude
+#include "CBot/CBotUtils.h"
-// Local include
-
-// Global include
#include <string>
-// Forward declaration
class CBotStack;
class CBotCStack;
class CBotVar;
@@ -38,7 +34,7 @@ class CBotToken;
/*!
* \brief The CBotCall class. Class for routine calls (external).
*/
-class CBotCall
+class CBotCall : public CBotLinkedList<CBotCall>
{
public:
@@ -124,12 +120,6 @@ public:
std::string GetName();
/*!
- * \brief Next
- * \return
- */
- CBotCall* Next();
-
- /*!
* \brief SetPUser
* \param pUser
*/
@@ -149,5 +139,4 @@ private:
std::string m_name;
bool (*m_rExec) (CBotVar* pVar, CBotVar* pResult, int& Exception, void* pUser);
CBotTypResult (*m_rComp) (CBotVar* &pVar, void* pUser);
- CBotCall* m_next;
};
diff --git a/src/CBot/CBotCallMethode.cpp b/src/CBot/CBotCallMethode.cpp
index 39b6084..a947dee 100644
--- a/src/CBot/CBotCallMethode.cpp
+++ b/src/CBot/CBotCallMethode.cpp
@@ -39,15 +39,12 @@ CBotCallMethode::CBotCallMethode(const std::string& name,
m_name = name;
m_rExec = rExec;
m_rComp = rCompile;
- m_next = nullptr;
m_nFuncIdent = CBotVar::NextUniqNum();
}
////////////////////////////////////////////////////////////////////////////////
CBotCallMethode::~CBotCallMethode()
{
- delete m_next;
- m_next = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
@@ -88,21 +85,6 @@ std::string CBotCallMethode::GetName()
}
////////////////////////////////////////////////////////////////////////////////
-CBotCallMethode* CBotCallMethode::Next()
-{
- return m_next;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-void CBotCallMethode::AddNext(CBotCallMethode* pt)
-{
- CBotCallMethode* p = this;
- while ( p->m_next != nullptr ) p = p->m_next;
-
- p->m_next = pt;
-}
-
-////////////////////////////////////////////////////////////////////////////////
int CBotCallMethode::DoCall(long& nIdent,
const std::string& name,
CBotVar* pThis,
diff --git a/src/CBot/CBotCallMethode.h b/src/CBot/CBotCallMethode.h
index bdccbb9..6eefd3f 100644
--- a/src/CBot/CBotCallMethode.h
+++ b/src/CBot/CBotCallMethode.h
@@ -19,12 +19,9 @@
#pragma once
-// Modules inlcude
#include "CBot/CBotTypResult.h"
+#include "CBot/CBotUtils.h"
-// Local include
-
-// Global include
class CBotVar;
class CBotCStack;
class CBotStack;
@@ -34,7 +31,7 @@ class CBotToken;
* \brief The CBotCallMethode class Class managing the methods declared by
* AddFunction on a class.
*/
-class CBotCallMethode
+class CBotCallMethode : public CBotLinkedList<CBotCallMethode>
{
public:
@@ -94,23 +91,10 @@ public:
*/
std::string GetName();
- /*!
- * \brief Next
- * \return
- */
- CBotCallMethode* Next();
-
- /*!
- * \brief AddNext
- * \param p
- */
- void AddNext(CBotCallMethode* p);
-
private:
std::string m_name;
bool (*m_rExec) (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user);
CBotTypResult (*m_rComp) (CBotVar* pThis, CBotVar* &pVar);
- CBotCallMethode* m_next;
friend class CBotClass;
long m_nFuncIdent;
diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp
index 07d8f35..7694742 100644
--- a/src/CBot/CBotClass.cpp
+++ b/src/CBot/CBotClass.cpp
@@ -53,7 +53,6 @@ CBotClass::CBotClass(const std::string& name,
m_pParent = pPapa;
m_name = name;
m_pVar = nullptr;
- m_next = nullptr;
m_pCalls = nullptr;
m_pMethod = nullptr;
m_rMaj = nullptr;
@@ -91,8 +90,6 @@ CBotClass::~CBotClass()
delete m_pVar;
delete m_pCalls;
delete m_pMethod;
-
- delete m_next; // releases all of them on this level
}
////////////////////////////////////////////////////////////////////////////////
@@ -245,15 +242,6 @@ bool CBotClass::AddItem(CBotVar* pVar)
}
////////////////////////////////////////////////////////////////////////////////
-void CBotClass::AddNext(CBotClass* pClass)
-{
- CBotClass* p = this;
- while (p->m_next != nullptr) p = p->m_next;
-
- p->m_next = pClass;
-}
-
-////////////////////////////////////////////////////////////////////////////////
std::string CBotClass::GetName()
{
return m_name;
diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h
index c924281..262369a 100644
--- a/src/CBot/CBotClass.h
+++ b/src/CBot/CBotClass.h
@@ -97,7 +97,7 @@ class CBotCStack;
* float y = var->GetValFloat();
* \endcode
*/
-class CBotClass
+class CBotClass : public CBotLinkedList<CBotClass>
{
public:
//! Mark if is set or not
@@ -171,12 +171,6 @@ public:
bool AddItem(CBotVar* pVar);
/*!
- * \brief AddNext
- * \param pClass
- */
- void AddNext(CBotClass* pClass);
-
- /*!
* \brief GetName Gives the name of the class.
* \return
*/
@@ -380,8 +374,6 @@ private:
CBotVar* m_pVar;
//! Intrinsic class.
bool m_bIntrinsic;
- //! The string class.
- CBotClass* m_next;
//! List of methods defined in external.
CBotCallMethode* m_pCalls;
//! Compiled list of methods.
diff --git a/src/CBot/CBotDefParam.cpp b/src/CBot/CBotDefParam.cpp
index df0ff96..6718326 100644
--- a/src/CBot/CBotDefParam.cpp
+++ b/src/CBot/CBotDefParam.cpp
@@ -33,14 +33,12 @@
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::CBotDefParam()
{
- m_next = nullptr;
m_nIdent = 0;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::~CBotDefParam()
{
- delete m_next;
}
////////////////////////////////////////////////////////////////////////////////
@@ -104,15 +102,6 @@ CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack)
}
////////////////////////////////////////////////////////////////////////////////
-void CBotDefParam::AddNext(CBotDefParam* p)
-{
- CBotDefParam* pp = this;
- while (pp->m_next != nullptr) pp = pp->m_next;
-
- pp->m_next = p;
-}
-
-////////////////////////////////////////////////////////////////////////////////
bool CBotDefParam::Execute(CBotVar** ppVars, CBotStack* &pj)
{
int i = 0;
@@ -190,12 +179,6 @@ CBotTypResult CBotDefParam::GetTypResult()
}
////////////////////////////////////////////////////////////////////////////////
-CBotDefParam* CBotDefParam::GetNext()
-{
- return m_next;
-}
-
-////////////////////////////////////////////////////////////////////////////////
std::string CBotDefParam::GetParamString()
{
std::string param;
diff --git a/src/CBot/CBotDefParam.h b/src/CBot/CBotDefParam.h
index 7006964..61ca27c 100644
--- a/src/CBot/CBotDefParam.h
+++ b/src/CBot/CBotDefParam.h
@@ -19,13 +19,9 @@
#pragma once
-// Modules inlcude
#include "CBot/CBotToken.h"
#include "CBot/CBotStack.h"
-
-// Local include
-
-// Global include
+#include "CBot/CBotUtils.h"
class CBotCStack;
class CBotStack;
@@ -34,7 +30,7 @@ class CBotVar;
/*!
* \brief The CBotDefParam class A list of parameters.
*/
-class CBotDefParam
+class CBotDefParam : public CBotLinkedList<CBotDefParam>
{
public:
@@ -72,12 +68,6 @@ public:
void RestoreState(CBotStack* &pj, bool bMain);
/*!
- * \brief AddNext
- * \param p
- */
- void AddNext(CBotDefParam* p);
-
- /*!
* \brief GetType
* \return
*/
@@ -90,12 +80,6 @@ public:
CBotTypResult GetTypResult();
/*!
- * \brief GetNext
- * \return
- */
- CBotDefParam* GetNext();
-
- /*!
* \brief GetParamString
* \return
*/
@@ -108,7 +92,5 @@ private:
std::string m_typename;
//! Type of paramteter.
CBotTypResult m_type;
- //! Next parameter.
- CBotDefParam* m_next;
long m_nIdent;
};
diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp
index fb74930..5df2212 100644
--- a/src/CBot/CBotInstr/CBotFunction.cpp
+++ b/src/CBot/CBotInstr/CBotFunction.cpp
@@ -236,7 +236,7 @@ CBotFunction* CBotFunction::Compile(CBotToken* &p, CBotCStack* pStack, CBotFunct
// and compiles the following instruction block
func->m_openblk = *p;
func->m_Block = CBotBlock::Compile(p, pStk, false);
- func->m_closeblk = p->GetPrev() != nullptr ? *(p->GetPrev()) : CBotToken();
+ func->m_closeblk = (p != nullptr && p->GetPrev() != nullptr) ? *(p->GetPrev()) : CBotToken();
if ( pStk->IsOk() )
{
if ( func->m_bPublic ) // public function, return known for all
diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp
index 4e1c31d..cec1bed 100644
--- a/src/CBot/CBotToken.cpp
+++ b/src/CBot/CBotToken.cpp
@@ -137,6 +137,16 @@ CBotToken::CBotToken()
}
////////////////////////////////////////////////////////////////////////////////
+CBotToken::CBotToken(const std::string& text, const std::string& sep, int start, int end)
+{
+ m_text = text;
+ m_sep = sep;
+
+ m_start = start;
+ m_end = end;
+}
+
+////////////////////////////////////////////////////////////////////////////////
CBotToken::CBotToken(const CBotToken& pSrc)
{
m_type = pSrc.m_type;
@@ -150,31 +160,8 @@ CBotToken::CBotToken(const CBotToken& pSrc)
}
////////////////////////////////////////////////////////////////////////////////
-CBotToken::CBotToken(const std::string& text, const std::string& sep, int start, int end)
-{
- m_text = text; // word (mot) found as token
- m_sep = sep; // separator
- m_next = nullptr;
- m_prev = nullptr;
- m_start = start;
- m_end = end;
-
- m_type = TokenTypVar; // at the beginning a default variable type
- m_keywordId = -1;
-}
-
-////////////////////////////////////////////////////////////////////////////////
CBotToken::~CBotToken()
{
- assert(m_prev == nullptr);
-
- if (m_next != nullptr)
- {
- m_next->m_prev = nullptr;
-
- delete m_next; // recursive
- m_next = nullptr;
- }
}
////////////////////////////////////////////////////////////////////////////////
@@ -220,20 +207,6 @@ long CBotToken::GetKeywordId()
}
////////////////////////////////////////////////////////////////////////////////
-CBotToken* CBotToken::GetNext()
-{
- if (this == nullptr) return nullptr;
- return m_next;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-CBotToken* CBotToken::GetPrev()
-{
- if (this == nullptr) return nullptr;
- return m_prev;
-}
-
-////////////////////////////////////////////////////////////////////////////////
std::string CBotToken::GetString()
{
return m_text;
diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h
index a2b836f..cb3c6a9 100644
--- a/src/CBot/CBotToken.h
+++ b/src/CBot/CBotToken.h
@@ -19,13 +19,14 @@
#pragma once
+#include "CBot/CBotEnums.h"
+#include "CBot/CBotUtils.h"
+
#include <vector>
#include <string>
#include <map>
#include <memory>
-#include "CBot/CBotEnums.h"
-
/**
* \brief Class representing one token of a program.
*
@@ -73,7 +74,7 @@
* \endcode
*/
-class CBotToken
+class CBotToken : public CBotDoublyLinkedList<CBotToken>
{
public:
/**
@@ -135,18 +136,6 @@ public:
int GetEnd();
/**
- * \brief Return the next token in the linked list
- * \return The next CBotToken in the list, of nullptr if this is the last one
- */
- CBotToken* GetNext();
-
- /**
- * \brief Return the previous token in the linked list
- * \return The previous CBotToken in the list, of nullptr if this is the first one
- */
- CBotToken* GetPrev();
-
- /**
* \brief SetPos Set the token position in the CBot program
* \param start The start position of the token
* \param end The end position of the token
@@ -198,11 +187,6 @@ private:
static CBotToken* NextToken(const char*& program, bool first);
private:
-
- //! The next token in the linked list
- CBotToken* m_next = nullptr;
- //! The previous token in the linked list
- CBotToken* m_prev = nullptr;
//! The token type
TokenType m_type = TokenTypVar;
//! The id of the keyword
diff --git a/src/CBot/CBotUtils.h b/src/CBot/CBotUtils.h
index 74914ca..259b034 100644
--- a/src/CBot/CBotUtils.h
+++ b/src/CBot/CBotUtils.h
@@ -19,14 +19,11 @@
#pragma once
-// Modules inlcude
#include "CBot/CBotTypResult.h"
-// Local include
-
-// Global include
#include <cstdio>
#include <string>
+#include <cassert>
// Forward declaration
class CBotVar;
@@ -97,3 +94,92 @@ long GetNumInt(const std::string& p);
*/
float GetNumFloat(const std::string& str);
+template<typename T> class CBotLinkedList {
+public:
+ /**
+ * \brief Destructor. Be careful, destroys the whole linked list!
+ */
+ virtual ~CBotLinkedList()
+ {
+ if (m_next != nullptr)
+ {
+ delete m_next;
+ m_next = nullptr;
+ }
+ }
+
+ /**
+ * \brief Returns the next variable in the linked list
+ * \return Next element in the list, or nullptr if this was the last element
+ */
+ T* GetNext()
+ {
+ return m_next;
+ }
+
+ /**
+ * \brief Appends a new element at the end of the linked list
+ * \param elem Element to add
+ */
+ void AddNext(T* elem)
+ {
+ CBotLinkedList<T>* p = this;
+ while (p->m_next != nullptr) p = p->m_next;
+ p->m_next = elem;
+ }
+
+protected:
+ T* m_next = nullptr;
+};
+
+template<typename T> class CBotDoublyLinkedList {
+public:
+ /**
+ * \brief Destructor. Be careful, destroys the whole linked list!
+ */
+ virtual ~CBotDoublyLinkedList()
+ {
+ assert(m_prev == nullptr);
+
+ if (m_next != nullptr)
+ {
+ m_next->m_prev = nullptr;
+ delete m_next;
+ m_next = nullptr;
+ }
+ }
+
+ /**
+ * \brief Returns the next variable in the linked list
+ * \return Next element in the list, or nullptr if this was the last element
+ */
+ T* GetNext()
+ {
+ return m_next;
+ }
+
+ /**
+ * \brief Returns the previous variable in the linked list
+ * \return Previous element in the list, or nullptr if this was the last element
+ */
+ T* GetPrev()
+ {
+ return m_prev;
+ }
+
+ /**
+ * \brief Appends a new element at the end of the linked list
+ * \param elem Element to add
+ */
+ void AddNext(T* elem)
+ {
+ CBotDoublyLinkedList<T>* p = this;
+ while (p->m_next != nullptr) p = p->m_next;
+ p->m_next = elem;
+ elem->m_prev = p;
+ }
+
+protected:
+ T* m_next = nullptr;
+ T* m_prev = nullptr;
+};
\ No newline at end of file
diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp
index 847509f..8499f11 100644
--- a/src/CBot/CBotVar/CBotVar.cpp
+++ b/src/CBot/CBotVar/CBotVar.cpp
@@ -50,7 +50,6 @@ long CBotVar::m_identcpt = 0;
////////////////////////////////////////////////////////////////////////////////
CBotVar::CBotVar( )
{
- m_next = nullptr;
m_pMyThis = nullptr;
m_pUserPtr = nullptr;
m_InitExpr = nullptr;
@@ -66,7 +65,6 @@ CBotVar::CBotVar( )
CBotVar::~CBotVar( )
{
delete m_token;
- delete m_next;
}
////////////////////////////////////////////////////////////////////////////////
@@ -453,21 +451,6 @@ CBotVar* CBotVar::GetStaticVar()
}
////////////////////////////////////////////////////////////////////////////////
-CBotVar* CBotVar::GetNext()
-{
- return m_next;
-}
-
-////////////////////////////////////////////////////////////////////////////////
-void CBotVar::AddNext(CBotVar* pVar)
-{
- CBotVar* p = this;
- while (p->m_next != nullptr) p = p->m_next;
-
- p->m_next = pVar;
-}
-
-////////////////////////////////////////////////////////////////////////////////
void CBotVar::SetVal(CBotVar* var)
{
switch (var->GetType())
diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h
index c2616ff..22cc222 100644
--- a/src/CBot/CBotVar/CBotVar.h
+++ b/src/CBot/CBotVar/CBotVar.h
@@ -22,6 +22,7 @@
#include "CBot/CBotDefines.h"
#include "CBot/CBotTypResult.h"
#include "CBot/CBotEnums.h"
+#include "CBot/CBotUtils.h"
#include <string>
@@ -35,7 +36,7 @@ class CBotToken;
*
* \nosubgrouping
*/
-class CBotVar
+class CBotVar : public CBotLinkedList<CBotVar>
{
public:
//! \name Creation / destruction
@@ -411,24 +412,6 @@ public:
//@}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //! \name Linked list
- //@{
-
- /**
- * \brief Returns the next variable if this CBotVar is used as a linked list
- * \return Next element in the list, or nullptr if this was the last element
- */
- CBotVar* GetNext();
-
- /**
- * \brief Appends a new element at the end of the linked list
- * \param pVar Element to add
- */
- void AddNext(CBotVar* pVar);
-
- //@}
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* \name Value management
*
@@ -637,13 +620,10 @@ public:
//@}
protected:
-
//! The corresponding token, defines the variable name
CBotToken* m_token;
//! Type of value.
CBotTypResult m_type;
- //! Next variable in a linked list
- CBotVar* m_next;
//! Initialization status
InitType m_binit;
//! Corresponding this element (TODO: ?)
--
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