Extracted most of linked list logic into a template class

dev-time-step
krzys-h 2015-12-23 23:33:40 +01:00
parent d82b5ef746
commit 3008e18fc6
14 changed files with 114 additions and 217 deletions

View File

@ -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;
}
////////////////////////////////////////////////////////////////////////////////
@ -155,12 +152,6 @@ std::string CBotCall::GetName()
return m_name;
}
////////////////////////////////////////////////////////////////////////////////
CBotCall* CBotCall::Next()
{
return m_next;
}
////////////////////////////////////////////////////////////////////////////////
int CBotCall::DoCall(long& nIdent, CBotToken* token, CBotVar** ppVar, CBotStack* pStack, CBotTypResult& rettype)
{

View File

@ -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:
@ -123,12 +119,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;
};

View File

@ -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;
}
////////////////////////////////////////////////////////////////////////////////
@ -87,21 +84,6 @@ std::string CBotCallMethode::GetName()
return m_name;
}
////////////////////////////////////////////////////////////////////////////////
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,

View File

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

View File

@ -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
}
////////////////////////////////////////////////////////////////////////////////
@ -244,15 +241,6 @@ bool CBotClass::AddItem(CBotVar* pVar)
return true;
}
////////////////////////////////////////////////////////////////////////////////
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()
{

View File

@ -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
@ -170,12 +170,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.

View File

@ -33,14 +33,12 @@
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::CBotDefParam()
{
m_next = nullptr;
m_nIdent = 0;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam::~CBotDefParam()
{
delete m_next;
}
////////////////////////////////////////////////////////////////////////////////
@ -103,15 +101,6 @@ CBotDefParam* CBotDefParam::Compile(CBotToken* &p, CBotCStack* pStack)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
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)
{
@ -189,12 +178,6 @@ CBotTypResult CBotDefParam::GetTypResult()
return m_type;
}
////////////////////////////////////////////////////////////////////////////////
CBotDefParam* CBotDefParam::GetNext()
{
return m_next;
}
////////////////////////////////////////////////////////////////////////////////
std::string CBotDefParam::GetParamString()
{

View File

@ -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:
@ -71,12 +67,6 @@ public:
*/
void RestoreState(CBotStack* &pj, bool bMain);
/*!
* \brief AddNext
* \param p
*/
void AddNext(CBotDefParam* p);
/*!
* \brief GetType
* \return
@ -89,12 +79,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;
};

View File

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

View File

@ -136,6 +136,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)
{
@ -149,32 +159,9 @@ CBotToken::CBotToken(const CBotToken& pSrc)
m_end = pSrc.m_end;
}
////////////////////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////////////////////
@ -219,20 +206,6 @@ long CBotToken::GetKeywordId()
return m_keywordId;
}
////////////////////////////////////////////////////////////////////////////////
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()
{

View File

@ -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:
/**
@ -134,18 +135,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
@ -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

View File

@ -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;
};

View File

@ -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;
}
////////////////////////////////////////////////////////////////////////////////
@ -452,21 +450,6 @@ CBotVar* CBotVar::GetStaticVar()
return pClass->GetItem( m_token->GetString() );
}
////////////////////////////////////////////////////////////////////////////////
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)
{

View File

@ -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
@ -410,24 +411,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: ?)