Moving CBotVarArray class in its own header and source files.

dev-time-step
Grunaka 2015-11-15 17:04:27 +01:00
parent 394a49f5aa
commit 63ab9d7301
10 changed files with 283 additions and 144 deletions

View File

@ -468,39 +468,6 @@ public:
bool Ne(CBotVar* left, CBotVar* right) override;
};
// classe pour les tableaux
#define MAXARRAYSIZE 9999
class CBotVarArray : public CBotVar
{
private:
CBotVarClass*
m_pInstance; // instance manager of table
friend class CBotVar; // my daddy is a buddy
public:
CBotVarArray( const CBotToken* name, CBotTypResult& type );
~CBotVarArray();
void SetPointer(CBotVar* p) override;
CBotVarClass*
GetPointer() override;
void Copy(CBotVar* pSrc, bool bName=true) override;
CBotVar* GetItem(int n, bool bGrow=false) override; // makes an element according to its numeric index
// enlarged the table if necessary if bExtend
// CBotVar* GetItem(const char* name); // makes a element by literal index
CBotVar* GetItemList() override; // gives the first item in the list
CBotString GetValString() override; // gets the contents of the array into a string
bool Save1State(FILE* pf) override;
};
extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars);
extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 );

28
src/CBot/CBotDefines.h Normal file
View File

@ -0,0 +1,28 @@
/*
* 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
// Local include
// Global include
#define MAXARRAYSIZE 9999

View File

@ -26,6 +26,8 @@
#include "CBotStack.h"
#include "CBotVar/CBotVarArray.h"
// Local include
// Global include

View File

@ -22,6 +22,8 @@
#include "CBotStack.h"
#include "CBotVar/CBotVarArray.h"
// Local include
// Global include

View File

@ -27,6 +27,8 @@
#include "CBotStack.h"
#include "CBotDefines.h"
// Local include
// Global include

View File

@ -25,6 +25,8 @@
#include "CBotStack.h"
#include "CBotVar/CBotVarArray.h"
// Local include
// Global include

View File

@ -27,6 +27,10 @@
#include "CBotStack.h"
#include "CBotVar/CBotVarArray.h"
#include "CBotDefines.h"
#include <cassert>
#include <cmath>
#include <cstdio>
@ -1774,117 +1778,6 @@ bool CBotVarClass::Ne(CBotVar* left, CBotVar* right)
return l != r;
}
/////////////////////////////////////////////////////////////////////////////
// management of arrays
CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type )
{
if ( !type.Eq(CBotTypArrayPointer) &&
!type.Eq(CBotTypArrayBody)) assert(0);
m_token = new CBotToken(name);
m_next = nullptr;
m_pMyThis = nullptr;
m_pUserPtr = nullptr;
m_type = type;
m_type.SetType(CBotTypArrayPointer);
m_binit = CBotVar::InitType::UNDEF;
m_pInstance = nullptr; // the list of the array elements
}
CBotVarArray::~CBotVarArray()
{
if ( m_pInstance != nullptr ) m_pInstance->DecrementUse(); // the lowest reference
}
// copy a variable into another
void CBotVarArray::Copy(CBotVar* pSrc, bool bName)
{
if ( pSrc->GetType() != CBotTypArrayPointer )
assert(0);
CBotVarArray* p = static_cast<CBotVarArray*>(pSrc);
if ( bName) *m_token = *p->m_token;
m_type = p->m_type;
m_pInstance = p->GetPointer();
if ( m_pInstance != nullptr )
m_pInstance->IncrementUse(); // a reference increase
m_binit = p->m_binit;
//- m_bStatic = p->m_bStatic;
m_pMyThis = nullptr;//p->m_pMyThis;
m_pUserPtr = p->m_pUserPtr;
// keeps indentificator the same (by default)
if (m_ident == 0 ) m_ident = p->m_ident;
}
void CBotVarArray::SetPointer(CBotVar* pVarClass)
{
m_binit = CBotVar::InitType::DEF; // init, even on a null pointer
if ( m_pInstance == pVarClass) return; // Special, not decrement and reincrement
// because the decrement can destroy the object
if ( pVarClass != nullptr )
{
if ( pVarClass->GetType() == CBotTypArrayPointer )
pVarClass = pVarClass->GetPointer(); // the real pointer to the object
if ( !pVarClass->m_type.Eq(CBotTypClass) &&
!pVarClass->m_type.Eq(CBotTypArrayBody))
assert(0);
(static_cast<CBotVarClass*>(pVarClass))->IncrementUse(); // incement the reference
}
if ( m_pInstance != nullptr ) m_pInstance->DecrementUse();
m_pInstance = static_cast<CBotVarClass*>(pVarClass);
}
CBotVarClass* CBotVarArray::GetPointer()
{
if ( m_pInstance == nullptr ) return nullptr;
return m_pInstance->GetPointer();
}
CBotVar* CBotVarArray::GetItem(int n, bool bExtend)
{
if ( m_pInstance == nullptr )
{
if ( !bExtend ) return nullptr;
// creates an instance of the table
CBotVarClass* instance = new CBotVarClass(nullptr, m_type);
SetPointer( instance );
}
return m_pInstance->GetItem(n, bExtend);
}
CBotVar* CBotVarArray::GetItemList()
{
if ( m_pInstance == nullptr) return nullptr;
return m_pInstance->GetItemList();
}
CBotString CBotVarArray::GetValString()
{
if ( m_pInstance == nullptr ) return ( CBotString( "Null pointer" ) ) ;
return m_pInstance->GetValString();
}
bool CBotVarArray::Save1State(FILE* pf)
{
if ( !WriteType(pf, m_type) ) return false;
return SaveVar(pf, m_pInstance); // saves the instance that manages the table
}
/////////////////////////////////////////////////////////////////////////////
// gestion des pointeurs à une instance donnée
// TODO management of pointers to a given instance

View File

@ -0,0 +1,140 @@
/*
* 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
*/
// Modules inlcude
#include "CBotVarArray.h"
// Local include
// Global include
#include <cassert>
////////////////////////////////////////////////////////////////////////////////
CBotVarArray::CBotVarArray(const CBotToken* name, CBotTypResult& type )
{
if ( !type.Eq(CBotTypArrayPointer) &&
!type.Eq(CBotTypArrayBody)) assert(0);
m_token = new CBotToken(name);
m_next = nullptr;
m_pMyThis = nullptr;
m_pUserPtr = nullptr;
m_type = type;
m_type.SetType(CBotTypArrayPointer);
m_binit = CBotVar::InitType::UNDEF;
m_pInstance = nullptr; // the list of the array elements
}
////////////////////////////////////////////////////////////////////////////////
CBotVarArray::~CBotVarArray()
{
if ( m_pInstance != nullptr ) m_pInstance->DecrementUse(); // the lowest reference
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarArray::Copy(CBotVar* pSrc, bool bName)
{
if ( pSrc->GetType() != CBotTypArrayPointer )
assert(0);
CBotVarArray* p = static_cast<CBotVarArray*>(pSrc);
if ( bName) *m_token = *p->m_token;
m_type = p->m_type;
m_pInstance = p->GetPointer();
if ( m_pInstance != nullptr )
m_pInstance->IncrementUse(); // a reference increase
m_binit = p->m_binit;
//- m_bStatic = p->m_bStatic;
m_pMyThis = nullptr;//p->m_pMyThis;
m_pUserPtr = p->m_pUserPtr;
// keeps indentificator the same (by default)
if (m_ident == 0 ) m_ident = p->m_ident;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarArray::SetPointer(CBotVar* pVarClass)
{
m_binit = CBotVar::InitType::DEF; // init, even on a null pointer
if ( m_pInstance == pVarClass) return; // Special, not decrement and reincrement
// because the decrement can destroy the object
if ( pVarClass != nullptr )
{
if ( pVarClass->GetType() == CBotTypArrayPointer )
pVarClass = pVarClass->GetPointer(); // the real pointer to the object
if ( !pVarClass->m_type.Eq(CBotTypClass) &&
!pVarClass->m_type.Eq(CBotTypArrayBody))
assert(0);
(static_cast<CBotVarClass*>(pVarClass))->IncrementUse(); // incement the reference
}
if ( m_pInstance != nullptr ) m_pInstance->DecrementUse();
m_pInstance = static_cast<CBotVarClass*>(pVarClass);
}
////////////////////////////////////////////////////////////////////////////////
CBotVarClass* CBotVarArray::GetPointer()
{
if ( m_pInstance == nullptr ) return nullptr;
return m_pInstance->GetPointer();
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotVarArray::GetItem(int n, bool bExtend)
{
if ( m_pInstance == nullptr )
{
if ( !bExtend ) return nullptr;
// creates an instance of the table
CBotVarClass* instance = new CBotVarClass(nullptr, m_type);
SetPointer( instance );
}
return m_pInstance->GetItem(n, bExtend);
}
////////////////////////////////////////////////////////////////////////////////
CBotVar* CBotVarArray::GetItemList()
{
if ( m_pInstance == nullptr) return nullptr;
return m_pInstance->GetItemList();
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotVarArray::GetValString()
{
if ( m_pInstance == nullptr ) return ( CBotString( "Null pointer" ) ) ;
return m_pInstance->GetValString();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarArray::Save1State(FILE* pf)
{
if ( !WriteType(pf, m_type) ) return false;
return SaveVar(pf, m_pInstance); // saves the instance that manages the table
}

View File

@ -0,0 +1,102 @@
/*
* 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 "CBot.h"
#include "CBotDefines.h"
// Local include
// Global include
/*!
* \brief The CBotVarArray class Classe pour les tableaux.
*/
class CBotVarArray : public CBotVar
{
public:
/*!
* \brief CBotVarArray
* \param name
* \param type
*/
CBotVarArray( const CBotToken* name, CBotTypResult& type );
/*!
* \brief ~CBotVarArray
*/
~CBotVarArray();
/*!
* \brief SetPointer
* \param p
*/
void SetPointer(CBotVar* p) override;
/*!
* \brief GetPointer
* \return
*/
CBotVarClass* GetPointer() override;
/*!
* \brief Copy Copy a variable into another.
* \param pSrc
* \param bName
*/
void Copy(CBotVar* pSrc, bool bName=true) override;
/*!
* \brief GetItem Makes an element according to its numeric index enlarged
* the table if necessary if bExtend.
* \param n
* \param bGrow
* \return
*/
CBotVar* GetItem(int n, bool bGrow=false) override;
/*!
* \brief GetItemList Gives the first item in the list.
* \return
*/
CBotVar* GetItemList() override;
/*!
* \brief GetValString Gets the contents of the array into a string.
* \return
*/
CBotString GetValString() override;
/*!
* \brief Save1State
* \param pf
* \return
*/
bool Save1State(FILE* pf) override;
private:
//! Instance manager of table.
CBotVarClass* m_pInstance;
//! My daddy is a buddy.
friend class CBotVar;
};

View File

@ -53,6 +53,7 @@ set(SOURCES
CBotInstr/CBotListArray.cpp
CBotInstr/CBotInstArray.cpp
CBotInstr/CBotInt.cpp
CBotVar/CBotVarArray.cpp
)
# Includes