Moving CBotClass class in its own header and source files.

dev-time-step
Grunaka 2015-11-15 17:37:53 +01:00
parent 63ab9d7301
commit 77d738634c
14 changed files with 408 additions and 155 deletions

View File

@ -73,6 +73,7 @@
#include "CBotInstr/CBotInt.h"
#include "CBotStack.h"
#include "CBotClass.h"
// Local include

View File

@ -17,21 +17,27 @@
* along with this program. If not, see http://gnu.org/licenses
*/
/////////////////////////////////////////////////////////////////////
// Management of variables of class type
//
#include "CBot.h"
#include "CBotCall.h"
// Modules inlcude
#include "CBotClass.h"
#include "CBotInstr/CBotNew.h"
#include "CBotInstr/CBotLeftExprVar.h"
#include "CBotInstr/CBotTwoOpExpr.h"
#include "CBotCall.h"
// Local include
// Global include
////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::m_ExClass = nullptr;
CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic)
////////////////////////////////////////////////////////////////////////////////
CBotClass::CBotClass(const char* name,
CBotClass* pPapa,
bool bIntrinsic)
{
m_pParent = pPapa;
m_name = name;
@ -60,6 +66,7 @@ CBotClass::CBotClass(const char* name, CBotClass* pPapa, bool bIntrinsic)
}
////////////////////////////////////////////////////////////////////////////////
CBotClass::~CBotClass()
{
// removes the list of class
@ -77,11 +84,15 @@ CBotClass::~CBotClass()
delete m_next; // releases all of them on this level
}
CBotClass* CBotClass::Create(const char* name, CBotClass* parent, bool intrinsic)
////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::Create(const char* name,
CBotClass* parent,
bool intrinsic)
{
return new CBotClass(name, parent, intrinsic);
}
////////////////////////////////////////////////////////////////////////////////
void CBotClass::Free()
{
while ( m_ExClass != nullptr )
@ -90,6 +101,7 @@ void CBotClass::Free()
}
}
////////////////////////////////////////////////////////////////////////////////
void CBotClass::Purge()
{
if ( this == nullptr ) return;
@ -108,6 +120,7 @@ void CBotClass::Purge()
m_next = nullptr; // no longer belongs to this chain
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::Lock(CBotProgram* p)
{
int i = m_cptLock++;
@ -144,6 +157,7 @@ bool CBotClass::Lock(CBotProgram* p)
return false;
}
////////////////////////////////////////////////////////////////////////////////
void CBotClass::Unlock()
{
if ( --m_cptOne > 0 ) return ;
@ -162,6 +176,7 @@ void CBotClass::Unlock()
m_ProgInLock[i] = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
void CBotClass::FreeLock(CBotProgram* p)
{
CBotClass* pClass = m_ExClass;
@ -182,9 +197,10 @@ void CBotClass::FreeLock(CBotProgram* p)
}
}
bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate)
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::AddItem(CBotString name,
CBotTypResult type,
int mPrivate)
{
CBotToken token(name, CBotString());
CBotClass* pClass = type.GetClass();
@ -207,7 +223,7 @@ bool CBotClass::AddItem(CBotString name, CBotTypResult type, int mPrivate)
return AddItem( pVar );
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::AddItem(CBotVar* pVar)
{
pVar->SetUniqNum(++m_nbVar);
@ -218,6 +234,7 @@ bool CBotClass::AddItem(CBotVar* pVar)
return true;
}
////////////////////////////////////////////////////////////////////////////////
void CBotClass::AddNext(CBotClass* pClass)
{
CBotClass* p = this;
@ -226,17 +243,20 @@ void CBotClass::AddNext(CBotClass* pClass)
p->m_next = pClass;
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotClass::GetName()
{
return m_name;
}
////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::GetParent()
{
if ( this == nullptr ) return nullptr;
return m_pParent;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::IsChildOf(CBotClass* pClass)
{
CBotClass* p = this;
@ -248,12 +268,13 @@ bool CBotClass::IsChildOf(CBotClass* pClass)
return false;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotClass::GetVar()
{
return m_pVar;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotClass::GetItem(const char* name)
{
CBotVar* p = m_pVar;
@ -267,6 +288,7 @@ CBotVar* CBotClass::GetItem(const char* name)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotClass::GetItemRef(int nIdent)
{
CBotVar* p = m_pVar;
@ -280,16 +302,19 @@ CBotVar* CBotClass::GetItemRef(int nIdent)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::IsIntrinsic()
{
return m_bIntrinsic;
}
////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::Find(CBotToken* &pToken)
{
return Find(pToken->GetString());
}
////////////////////////////////////////////////////////////////////////////////
CBotClass* CBotClass::Find(const char* name)
{
CBotClass* p = m_ExClass;
@ -303,9 +328,10 @@ CBotClass* CBotClass::Find(const char* name)
return nullptr;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::AddFunction(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar))
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar))
{
// stores pointers to the two functions
CBotCallMethode* p = m_pCalls;
@ -332,18 +358,19 @@ bool CBotClass::AddFunction(const char* name,
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) )
{
m_rMaj = rMaj;
return true;
}
// compiles a method associated with an instance of class
// the method can be declared by the user or AddFunction
////////////////////////////////////////////////////////////////////////////////
CBotTypResult CBotClass::CompileMethode(const char* name,
CBotVar* pThis, CBotVar** ppParams,
CBotCStack* pStack, long& nIdent)
CBotVar* pThis,
CBotVar** ppParams,
CBotCStack* pStack,
long& nIdent)
{
nIdent = 0; // forget the previous one if necessary
@ -360,11 +387,13 @@ CBotTypResult CBotClass::CompileMethode(const char* name,
return r;
}
// executes a method
bool CBotClass::ExecuteMethode(long& nIdent, const char* name,
CBotVar* pThis, CBotVar** ppParams,
CBotVar* &pResult, CBotStack* &pStack,
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::ExecuteMethode(long& nIdent,
const char* name,
CBotVar* pThis,
CBotVar** ppParams,
CBotVar* &pResult,
CBotStack* &pStack,
CBotToken* pToken)
{
int ret = m_pCalls->DoCall(nIdent, name, pThis, ppParams, pResult, pStack, pToken);
@ -382,17 +411,17 @@ bool CBotClass::ExecuteMethode(long& nIdent, const char* name,
return ret;
}
// restored the execution stack
void CBotClass::RestoreMethode(long& nIdent, const char* name, CBotVar* pThis,
CBotVar** ppParams, CBotStack* &pStack)
////////////////////////////////////////////////////////////////////////////////
void CBotClass::RestoreMethode(long& nIdent,
const char* name,
CBotVar* pThis,
CBotVar** ppParams,
CBotStack* &pStack)
{
m_pMethod->RestoreCall(nIdent, name, pThis, ppParams, pStack, this);
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::SaveStaticState(FILE* pf)
{
if (!WriteWord( pf, CBOTVERSION*2)) return false;
@ -429,6 +458,7 @@ bool CBotClass::SaveStaticState(FILE* pf)
return true;
}
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::RestoreStaticState(FILE* pf)
{
CBotString ClassName, VarName;
@ -466,9 +496,9 @@ bool CBotClass::RestoreStaticState(FILE* pf)
return true;
}
// test if a procedure name is already defined somewhere
bool CBotClass::CheckCall(CBotToken* &pToken, CBotDefParam* pParam)
////////////////////////////////////////////////////////////////////////////////
bool CBotClass::CheckCall(CBotToken* &pToken,
CBotDefParam* pParam)
{
CBotString name = pToken->GetString();

328
src/CBot/CBotClass.h Normal file
View File

@ -0,0 +1,328 @@
/*
* This file is part of the Colobot: Gold Edition source code
* Copyright (C) 2001-2015, Daniel Roux, EPSITEC SA & TerranovaTeam
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://gnu.org/licenses
*/
#pragma once
// Modules inlcude
#include "CBotDll.h"
// Local include
// Global include
/*!
* \brief The CBotClass class Class to define new classes in the language CBOT
* for example to define the class CPoint (x, y).
*/
class CBotClass
{
public:
//! Mark if is set or not
bool m_IsDef;
/*!
* \brief CBotClass Constructor. Once a class is created, it is known around
* CBot intrinsic mode gives a class that is not managed by pointers.
* \param name
* \param pParent
* \param bIntrinsic
*/
CBotClass( const char* name,
CBotClass* pParent,
bool bIntrinsic = false );
/*!
* \brief CBotClass Destructor.
*/
~CBotClass( );
/*!
* \brief Create
* \param name
* \param parent
* \param intrinsic
* \return
*/
static CBotClass* Create(const char* name,
CBotClass* parent,
bool intrinsic = false);
/*!
* \brief AddFunction This call allows to add as external (**) new method
* used by the objects of this class.
* \param name
* \param rExec
* \param rCompile
* \return
*/
bool AddFunction(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar));
/*!
* \brief AddUpdateFunc Defines routine to be called to update the elements
* of the class.
* \param rMaj
* \return
*/
bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) );
//
/*!
* \brief AddItem Adds an element to the class.
* \param name
* \param type
* \param mPrivate
* \return
*/
bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC);
/*!
* \brief AddItem Adds an item by passing the pointer to an instance of a
* variable the object is taken as is, so do not destroyed.
* \param pVar
* \return
*/
bool AddItem(CBotVar* pVar);
/*!
* \brief AddNext
* \param pClass
*/
void AddNext(CBotClass* pClass);
/*!
* \brief GetName Gives the name of the class.
* \return
*/
CBotString GetName();
/*!
* \brief GetParent Gives the parent class (or nullptr).
* \return
*/
CBotClass* GetParent();
/*!
* \brief IsChildOf True if a class is derived (Extends) of another.
* \param pClass
* \return true also if the classes are identical
*/
bool IsChildOf(CBotClass* pClass);
/*!
* \brief Find Trouve une classe d'après son nom
* \param pToken
* \return A class by it's its name.
*/
static CBotClass* Find(CBotToken* &pToken);
/*!
* \brief Find
* \param name
* \return
*/
static CBotClass* Find(const char* name);
/*!
* \brief GetVar Return the list of variables.
* \return
*/
CBotVar* GetVar();
/*!
* \brief GetItem One of the variables according to its name.
* \param name
* \return
*/
CBotVar* GetItem(const char* name);
/*!
* \brief GetItemRef
* \param nIdent
* \return
*/
CBotVar* GetItemRef(int nIdent);
/*!
* \brief CompileMethode Compiles a method associated with an instance of
* class the method can be declared by the user or AddFunction.
* \param name
* \param pThis
* \param ppParams
* \param pStack
* \param nIdent
* \return
*/
CBotTypResult CompileMethode(const char* name,
CBotVar* pThis,
CBotVar** ppParams,
CBotCStack* pStack,
long& nIdent);
/*!
* \brief ExecuteMethode Executes a method.
* \param nIdent
* \param name
* \param pThis
* \param ppParams
* \param pResult
* \param pStack
* \param pToken
* \return
*/
bool ExecuteMethode(long& nIdent,
const char* name,
CBotVar* pThis,
CBotVar** ppParams,
CBotVar* &pResult,
CBotStack* &pStack,
CBotToken* pToken);
/*!
* \brief RestoreMethode Restored the execution stack.
* \param nIdent
* \param name
* \param pThis
* \param ppParams
* \param pStack
*/
void RestoreMethode(long& nIdent,
const char* name,
CBotVar* pThis,
CBotVar** ppParams,
CBotStack* &pStack);
/*!
* \brief Compile Compiles a class declared by the user.
* \param p
* \param pStack
* \return
*/
static CBotClass* Compile(CBotToken* &p,
CBotCStack* pStack);
/*!
* \brief Compile1
* \param p
* \param pStack
* \return
*/
static CBotClass* Compile1(CBotToken* &p,
CBotCStack* pStack);
/*!
* \brief CompileDefItem
* \param p
* \param pStack
* \param bSecond
* \return
*/
bool CompileDefItem(CBotToken* &p,
CBotCStack* pStack,
bool bSecond);
/*!
* \brief IsIntrinsic
* \return
*/
bool IsIntrinsic();
/*!
* \brief Purge
*/
void Purge();
/*!
* \brief Free
*/
static void Free();
/*!
* \brief SaveStaticState
* \param pf
* \return
*/
static bool SaveStaticState(FILE* pf);
/*!
* \brief RestoreStaticState
* \param pf
* \return
*/
static bool RestoreStaticState(FILE* pf);
/*!
* \brief Lock
* \param p
* \return
*/
bool Lock(CBotProgram* p);
/*!
* \brief Unlock
*/
void Unlock();
/*!
* \brief FreeLock
* \param p
*/
static void FreeLock(CBotProgram* p);
/*!
* \brief CheckCall Test if a procedure name is already defined somewhere.
* \param pToken
* \param pParam
* \return
*/
bool CheckCall(CBotToken* &pToken,
CBotDefParam* pParam);
private:
//! List of classes existing at a given time.
static CBotClass* m_ExClass;
//! For this general list.
CBotClass* m_ExNext;
//! For this general list.
CBotClass* m_ExPrev;
//! Parent class.
CBotClass* m_pParent;
//! Name of this class.
CBotString m_name;
//! Number of variables in the chain.
int m_nbVar;
//! Content of the class.
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.
CBotFunction* m_pMethod;
void (*m_rMaj) ( CBotVar* pThis, void* pUser );
friend class CBotVarClass;
//! For Lock / UnLock.
int m_cptLock;
//! Lock for reentrancy.
int m_cptOne;
//! Processes waiting for sync.
CBotProgram* m_ProgInLock[5];
};

View File

@ -847,124 +847,6 @@ virtual ~CBotVar( ); // destructor
*/
////////////////////////////////////////////////////////////////////////
// management of classes
////////////////////////////////////////////////////////////////////////
// class to define new classes in the language CBOT
// for example to define the class CPoint (x, y)
class CBotClass
{
private:
static
CBotClass* m_ExClass; // list of classes existing at a given time
CBotClass* m_ExNext; // for this general list
CBotClass* m_ExPrev; // for this general list
private:
CBotClass* m_pParent; // parent class
CBotString m_name; // name of this class
int m_nbVar; // number of variables in the chain
CBotVar* m_pVar; // content of the class
bool m_bIntrinsic; // intrinsic class
CBotClass* m_next; // the string class
CBotCallMethode* m_pCalls; // list of methods defined in external
CBotFunction* m_pMethod; // compiled list of methods
void (*m_rMaj) ( CBotVar* pThis, void* pUser );
friend class CBotVarClass;
int m_cptLock; // for Lock / UnLock
int m_cptOne; // Lock for reentrancy
CBotProgram* m_ProgInLock[5];// processes waiting for sync
public:
bool m_IsDef; // mark if is set or not
CBotClass( const char* name,
CBotClass* pParent, bool bIntrinsic = false ); // constructor
// Once a class is created, it is known
// around CBoT
// intrinsic mode gives a class that is not managed by pointers
~CBotClass( ); // destructor
static CBotClass* Create(const char* name, CBotClass* parent, bool intrinsic = false);
bool AddFunction(const char* name,
bool rExec (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception, void* user),
CBotTypResult rCompile (CBotVar* pThis, CBotVar* &pVar));
// this call allows to add as external (**)
// new method used by the objects of this class
bool AddUpdateFunc( void rMaj ( CBotVar* pThis, void* pUser ) );
// defines routine to be called to update the elements of the class
bool AddItem(CBotString name, CBotTypResult type, int mPrivate = PR_PUBLIC);
// adds an element to the class
// bool AddItem(CBotString name, CBotClass* pClass);
// the same for elements belonging to pClass
bool AddItem(CBotVar* pVar);
// adds an item by passing the pointer to an instance of a variable
// the object is taken as is, so do not destroyed
// adds an element by giving an element of type CBotVar
void AddNext(CBotClass* pClass);
CBotString GetName(); // gives the name of the class
CBotClass* GetParent(); // gives the parent class (or nullptr)
// true if a class is derived (Extends) of another
// return true also if the classes are identical
bool IsChildOf(CBotClass* pClass);
static
CBotClass* Find(CBotToken* &pToken); // trouve une classe d'après son nom
// return a class by it's its name
static
CBotClass* Find(const char* name);
CBotVar* GetVar(); // return the list of variables
CBotVar* GetItem(const char* name); // one of the variables according to its name
CBotVar* GetItemRef(int nIdent);
CBotTypResult CompileMethode(const char* name, CBotVar* pThis, CBotVar** ppParams,
CBotCStack* pStack, long& nIdent);
bool ExecuteMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotVar* &pResult, CBotStack* &pStack, CBotToken* pToken);
void RestoreMethode(long& nIdent, const char* name, CBotVar* pThis, CBotVar** ppParams, CBotStack* &pStack);
// compiles a class declared by the user
static
CBotClass* Compile(CBotToken* &p, CBotCStack* pStack);
static
CBotClass* Compile1(CBotToken* &p, CBotCStack* pStack);
bool CompileDefItem(CBotToken* &p, CBotCStack* pStack, bool bSecond);
bool IsIntrinsic();
void Purge();
static
void Free();
static
bool SaveStaticState(FILE* pf);
static
bool RestoreStaticState(FILE* pf);
bool Lock(CBotProgram* p);
void Unlock();
static
void FreeLock(CBotProgram* p);
bool CheckCall(CBotToken* &pToken, CBotDefParam* pParam);
};
/*
////////////////////////////////////////////////////////////////////////
// Examples of use

View File

@ -30,6 +30,7 @@
#include "CBotInstr/CBotListArray.h"
#include "CBotStack.h"
#include "CBotClass.h"
#include <cassert>

View File

@ -24,6 +24,7 @@
#include "CBotInstArray.h"
#include "CBotStack.h"
#include "CBotClass.h"
// Local include

View File

@ -21,6 +21,7 @@
#include "CBotFieldExpr.h"
#include "CBotStack.h"
#include "CBotClass.h"
// Local include

View File

@ -21,6 +21,7 @@
#include "CBotInstrMethode.h"
#include "CBotStack.h"
#include "CBotClass.h"
// Local include

View File

@ -24,6 +24,7 @@
#include "CBotExpression.h"
#include "CBotStack.h"
#include "CBotClass.h"
#include "CBotVar/CBotVarArray.h"

View File

@ -21,6 +21,7 @@
#include "CBotNew.h"
#include "CBotStack.h"
#include "CBotClass.h"
// Local include

View File

@ -24,7 +24,7 @@
#include "CBotCall.h"
#include "CBotStack.h"
#include "CBotClass.h"
#include "CBotUtils.h"
#include <stdio.h>

View File

@ -30,6 +30,7 @@
#include "CBotVar/CBotVarArray.h"
#include "CBotDefines.h"
#include "CBotClass.h"
#include <cassert>
#include <cmath>

View File

@ -20,6 +20,8 @@
#include "level/robotmain.h"
#include "CBot/CBotDll.h"
// TODO must be replaced by CBot.h
#include "CBot/CBotClass.h"
#include "app/app.h"
#include "app/input.h"

View File

@ -19,6 +19,9 @@
#include "script/scriptfunc.h"
// TODO must be replaced by CBot.h
#include "CBot/CBotClass.h"
#include "app/app.h"
#include "common/global.h"