Moving CBotVarInt class in its own header and source files.

dev-time-step
Grunaka 2015-11-15 21:52:05 +01:00
parent 44021e91f7
commit a4f14650c6
6 changed files with 552 additions and 306 deletions

View File

@ -222,60 +222,6 @@ public:
#define MAX(a,b) ((a>b) ? a : b)
// class for the management of integer numbers (int)
class CBotVarInt : public CBotVar
{
private:
int m_val; // the value
CBotString m_defnum; // the name if given by DefineNum
friend class CBotVar;
public:
CBotVarInt( const CBotToken* name );
// ~CBotVarInt();
void SetValInt(int val, const char* s = nullptr) override;
void SetValFloat(float val) override;
int GetValInt() override;
float GetValFloat() override;
CBotString GetValString() override;
void Copy(CBotVar* pSrc, bool bName=true) override;
void Add(CBotVar* left, CBotVar* right) override; // addition
void Sub(CBotVar* left, CBotVar* right) override; // substraction
void Mul(CBotVar* left, CBotVar* right) override; // multiplication
int Div(CBotVar* left, CBotVar* right) override; // division
int Modulo(CBotVar* left, CBotVar* right) override; // remainder of division
void Power(CBotVar* left, CBotVar* right) override; // power
bool Lo(CBotVar* left, CBotVar* right) override;
bool Hi(CBotVar* left, CBotVar* right) override;
bool Ls(CBotVar* left, CBotVar* right) override;
bool Hs(CBotVar* left, CBotVar* right) override;
bool Eq(CBotVar* left, CBotVar* right) override;
bool Ne(CBotVar* left, CBotVar* right) override;
void XOr(CBotVar* left, CBotVar* right) override;
void Or(CBotVar* left, CBotVar* right) override;
void And(CBotVar* left, CBotVar* right) override;
void SL(CBotVar* left, CBotVar* right) override;
void SR(CBotVar* left, CBotVar* right) override;
void ASR(CBotVar* left, CBotVar* right) override;
void Neg() override;
void Not() override;
void Inc() override;
void Dec() override;
bool Save0State(FILE* pf) override;
bool Save1State(FILE* pf) override;
};
extern CBotInstr* CompileParams(CBotToken* &p, CBotCStack* pStack, CBotVar** ppVars);
extern bool TypeCompatible( CBotTypResult& type1, CBotTypResult& type2, int op = 0 );

View File

@ -972,24 +972,6 @@ bool CBotVar::Save0State(FILE* pf)
return WriteString(pf, m_token->GetString()); // and variable name
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Save0State(FILE* pf)
{
if ( !m_defnum.IsEmpty() )
{
if(!WriteWord(pf, 200 )) return false; // special marker
if(!WriteString(pf, m_defnum)) return false; // name of the value
}
return CBotVar::Save0State(pf);
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Save1State(FILE* pf)
{
return WriteWord(pf, m_val); // the value of the variable
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarClass::Save1State(FILE* pf)
{

View File

@ -33,6 +33,7 @@
#include "CBotVar/CBotVarBoolean.h"
#include "CBotVar/CBotVarString.h"
#include "CBotVar/CBotVarFloat.h"
#include "CBotVar/CBotVarInt.h"
#include "CBotDefines.h"
#include "CBotClass.h"
@ -57,22 +58,6 @@ CBotVar::CBotVar( )
m_mPrivate = 0;
}
CBotVarInt::CBotVarInt( const CBotToken* name )
{
m_token = new CBotToken(name);
m_next = nullptr;
m_pMyThis = nullptr;
m_pUserPtr = nullptr;
m_InitExpr = nullptr;
m_LimExpr = nullptr;
m_type = CBotTypInt;
m_binit = InitType::UNDEF;
m_bStatic = false;
m_mPrivate = 0;
m_val = 0;
}
CBotVar::~CBotVar( )
{
delete m_token;
@ -711,224 +696,6 @@ CBotClass* CBotVar::GetClass()
return nullptr;
}
/*
void CBotVar::SetIndirection(CBotVar* pVar)
{
// nop, only CBotVarPointer::SetIndirection
}
*/
//////////////////////////////////////////////////////////////////////////////////////
// copy a variable in to another
void CBotVarInt::Copy(CBotVar* pSrc, bool bName)
{
CBotVarInt* p = static_cast<CBotVarInt*>(pSrc);
if ( bName) *m_token = *p->m_token;
m_type = p->m_type;
m_val = p->m_val;
m_binit = p->m_binit;
m_pMyThis = nullptr;
m_pUserPtr = p->m_pUserPtr;
// identificator is the same (by défaut)
if (m_ident == 0 ) m_ident = p->m_ident;
m_defnum = p->m_defnum;
}
void CBotVarInt::SetValInt(int val, const char* defnum)
{
m_val = val;
m_binit = CBotVar::InitType::DEF;
m_defnum = defnum;
}
void CBotVarInt::SetValFloat(float val)
{
m_val = static_cast<int>(val);
m_binit = CBotVar::InitType::DEF;
}
int CBotVarInt::GetValInt()
{
return m_val;
}
float CBotVarInt::GetValFloat()
{
return static_cast<float>(m_val);
}
CBotString CBotVarInt::GetValString()
{
if ( !m_defnum.IsEmpty() ) return m_defnum;
CBotString res;
if ( m_binit == CBotVar::InitType::UNDEF )
{
res.LoadString(TX_UNDEF);
return res;
}
if ( m_binit == CBotVar::InitType::IS_NAN )
{
res.LoadString(TX_NAN);
return res;
}
char buffer[300];
sprintf(buffer, "%d", m_val);
res = buffer;
return res;
}
void CBotVarInt::Mul(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() * right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::Power(CBotVar* left, CBotVar* right)
{
m_val = static_cast<int>( pow( static_cast<double>( left->GetValInt()) , static_cast<double>( left->GetValInt()) ));
m_binit = CBotVar::InitType::DEF;
}
int CBotVarInt::Div(CBotVar* left, CBotVar* right)
{
int r = right->GetValInt();
if ( r != 0 )
{
m_val = left->GetValInt() / r;
m_binit = CBotVar::InitType::DEF;
}
return ( r == 0 ? TX_DIVZERO : 0 );
}
int CBotVarInt::Modulo(CBotVar* left, CBotVar* right)
{
int r = right->GetValInt();
if ( r != 0 )
{
m_val = left->GetValInt() % r;
m_binit = CBotVar::InitType::DEF;
}
return ( r == 0 ? TX_DIVZERO : 0 );
}
void CBotVarInt::Add(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() + right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::Sub(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() - right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::XOr(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() ^ right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::And(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() & right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::Or(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() | right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::SL(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() << right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::ASR(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() >> right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::SR(CBotVar* left, CBotVar* right)
{
int source = left->GetValInt();
int shift = right->GetValInt();
if (shift>=1) source &= 0x7fffffff;
m_val = source >> shift;
m_binit = CBotVar::InitType::DEF;
}
void CBotVarInt::Neg()
{
m_val = -m_val;
}
void CBotVarInt::Not()
{
m_val = ~m_val;
}
void CBotVarInt::Inc()
{
m_val++;
m_defnum.Empty();
}
void CBotVarInt::Dec()
{
m_val--;
m_defnum.Empty();
}
bool CBotVarInt::Lo(CBotVar* left, CBotVar* right)
{
return left->GetValInt() < right->GetValInt();
}
bool CBotVarInt::Hi(CBotVar* left, CBotVar* right)
{
return left->GetValInt() > right->GetValInt();
}
bool CBotVarInt::Ls(CBotVar* left, CBotVar* right)
{
return left->GetValInt() <= right->GetValInt();
}
bool CBotVarInt::Hs(CBotVar* left, CBotVar* right)
{
return left->GetValInt() >= right->GetValInt();
}
bool CBotVarInt::Eq(CBotVar* left, CBotVar* right)
{
return left->GetValInt() == right->GetValInt();
}
bool CBotVarInt::Ne(CBotVar* left, CBotVar* right)
{
return left->GetValInt() != right->GetValInt();
}
////////////////////////////////////////////////////////////////
// copy a variable into another

View File

@ -0,0 +1,295 @@
/*
* 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 "CBotVarInt.h"
#include "CBotToken.h"
#include "CBotUtils.h"
// Local include
// Global include
#include <cmath>
////////////////////////////////////////////////////////////////////////////////
CBotVarInt::CBotVarInt( const CBotToken* name )
{
m_token = new CBotToken(name);
m_next = nullptr;
m_pMyThis = nullptr;
m_pUserPtr = nullptr;
m_InitExpr = nullptr;
m_LimExpr = nullptr;
m_type = CBotTypInt;
m_binit = InitType::UNDEF;
m_bStatic = false;
m_mPrivate = 0;
m_val = 0;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Copy(CBotVar* pSrc, bool bName)
{
CBotVarInt* p = static_cast<CBotVarInt*>(pSrc);
if ( bName) *m_token = *p->m_token;
m_type = p->m_type;
m_val = p->m_val;
m_binit = p->m_binit;
m_pMyThis = nullptr;
m_pUserPtr = p->m_pUserPtr;
// identificator is the same (by défaut)
if (m_ident == 0 ) m_ident = p->m_ident;
m_defnum = p->m_defnum;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::SetValInt(int val, const char* defnum)
{
m_val = val;
m_binit = CBotVar::InitType::DEF;
m_defnum = defnum;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::SetValFloat(float val)
{
m_val = static_cast<int>(val);
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
int CBotVarInt::GetValInt()
{
return m_val;
}
////////////////////////////////////////////////////////////////////////////////
float CBotVarInt::GetValFloat()
{
return static_cast<float>(m_val);
}
////////////////////////////////////////////////////////////////////////////////
CBotString CBotVarInt::GetValString()
{
if ( !m_defnum.IsEmpty() ) return m_defnum;
CBotString res;
if ( m_binit == CBotVar::InitType::UNDEF )
{
res.LoadString(TX_UNDEF);
return res;
}
if ( m_binit == CBotVar::InitType::IS_NAN )
{
res.LoadString(TX_NAN);
return res;
}
char buffer[300];
sprintf(buffer, "%d", m_val);
res = buffer;
return res;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Mul(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() * right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Power(CBotVar* left, CBotVar* right)
{
m_val = static_cast<int>( pow( static_cast<double>( left->GetValInt()) , static_cast<double>( left->GetValInt()) ));
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
int CBotVarInt::Div(CBotVar* left, CBotVar* right)
{
int r = right->GetValInt();
if ( r != 0 )
{
m_val = left->GetValInt() / r;
m_binit = CBotVar::InitType::DEF;
}
return ( r == 0 ? TX_DIVZERO : 0 );
}
////////////////////////////////////////////////////////////////////////////////
int CBotVarInt::Modulo(CBotVar* left, CBotVar* right)
{
int r = right->GetValInt();
if ( r != 0 )
{
m_val = left->GetValInt() % r;
m_binit = CBotVar::InitType::DEF;
}
return ( r == 0 ? TX_DIVZERO : 0 );
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Add(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() + right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Sub(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() - right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::XOr(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() ^ right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::And(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() & right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Or(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() | right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::SL(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() << right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::ASR(CBotVar* left, CBotVar* right)
{
m_val = left->GetValInt() >> right->GetValInt();
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::SR(CBotVar* left, CBotVar* right)
{
int source = left->GetValInt();
int shift = right->GetValInt();
if (shift>=1) source &= 0x7fffffff;
m_val = source >> shift;
m_binit = CBotVar::InitType::DEF;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Neg()
{
m_val = -m_val;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Not()
{
m_val = ~m_val;
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Inc()
{
m_val++;
m_defnum.Empty();
}
////////////////////////////////////////////////////////////////////////////////
void CBotVarInt::Dec()
{
m_val--;
m_defnum.Empty();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Lo(CBotVar* left, CBotVar* right)
{
return left->GetValInt() < right->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Hi(CBotVar* left, CBotVar* right)
{
return left->GetValInt() > right->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Ls(CBotVar* left, CBotVar* right)
{
return left->GetValInt() <= right->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Hs(CBotVar* left, CBotVar* right)
{
return left->GetValInt() >= right->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Eq(CBotVar* left, CBotVar* right)
{
return left->GetValInt() == right->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Ne(CBotVar* left, CBotVar* right)
{
return left->GetValInt() != right->GetValInt();
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Save0State(FILE* pf)
{
if ( !m_defnum.IsEmpty() )
{
if(!WriteWord(pf, 200 )) return false; // special marker
if(!WriteString(pf, m_defnum)) return false; // name of the value
}
return CBotVar::Save0State(pf);
}
////////////////////////////////////////////////////////////////////////////////
bool CBotVarInt::Save1State(FILE* pf)
{
return WriteWord(pf, m_val); // the value of the variable
}

View File

@ -0,0 +1,255 @@
/*
* 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 CBotVarInt class Class for the management of integer numbers (int).
*/
class CBotVarInt : public CBotVar
{
public:
/*!
* \brief CBotVarInt
* \param name
*/
CBotVarInt( const CBotToken* name );
/*!
* \brief SetValInt
* \param val
* \param s
*/
void SetValInt(int val, const char* s = nullptr) override;
/*!
* \brief SetValFloat
* \param val
*/
void SetValFloat(float val) override;
/*!
* \brief GetValInt
* \return
*/
int GetValInt() override;
/*!
* \brief GetValFloat
* \return
*/
float GetValFloat() override;
/*!
* \brief GetValString
* \return
*/
CBotString GetValString() override;
/*!
* \brief Copy Copy a variable in to another.
* \param pSrc
* \param bName
*/
void Copy(CBotVar* pSrc, bool bName=true) override;
/*!
* \brief Add
* \param left
* \param right
*/
void Add(CBotVar* left, CBotVar* right) override;
/*!
* \brief Sub
* \param left
* \param right
*/
void Sub(CBotVar* left, CBotVar* right) override;
/*!
* \brief Mul
* \param left
* \param right
*/
void Mul(CBotVar* left, CBotVar* right) override;
/*!
* \brief Div
* \param left
* \param right
* \return
*/
int Div(CBotVar* left, CBotVar* right) override;
/*!
* \brief Modulo
* \param left
* \param right
* \return
*/
int Modulo(CBotVar* left, CBotVar* right) override;
/*!
* \brief Power
* \param left
* \param right
*/
void Power(CBotVar* left, CBotVar* right) override;
/*!
* \brief Lo
* \param left
* \param right
* \return
*/
bool Lo(CBotVar* left, CBotVar* right) override;
/*!
* \brief Hi
* \param left
* \param right
* \return
*/
bool Hi(CBotVar* left, CBotVar* right) override;
/*!
* \brief Ls
* \param left
* \param right
* \return
*/
bool Ls(CBotVar* left, CBotVar* right) override;
/*!
* \brief Hs
* \param left
* \param right
* \return
*/
bool Hs(CBotVar* left, CBotVar* right) override;
/*!
* \brief Eq
* \param left
* \param right
* \return
*/
bool Eq(CBotVar* left, CBotVar* right) override;
/*!
* \brief Ne
* \param left
* \param right
* \return
*/
bool Ne(CBotVar* left, CBotVar* right) override;
/*!
* \brief XOr
* \param left
* \param right
*/
void XOr(CBotVar* left, CBotVar* right) override;
/*!
* \brief Or
* \param left
* \param right
*/
void Or(CBotVar* left, CBotVar* right) override;
/*!
* \brief And
* \param left
* \param right
*/
void And(CBotVar* left, CBotVar* right) override;
/*!
* \brief SL
* \param left
* \param right
*/
void SL(CBotVar* left, CBotVar* right) override;
/*!
* \brief SR
* \param left
* \param right
*/
void SR(CBotVar* left, CBotVar* right) override;
/*!
* \brief ASR
* \param left
* \param right
*/
void ASR(CBotVar* left, CBotVar* right) override;
/*!
* \brief Neg
*/
void Neg() override;
/*!
* \brief Not
*/
void Not() override;
/*!
* \brief Inc
*/
void Inc() override;
/*!
* \brief Dec
*/
void Dec() override;
/*!
* \brief Save0State
* \param pf
* \return
*/
bool Save0State(FILE* pf) override;
/*!
* \brief Save1State
* \param pf
* \return
*/
bool Save1State(FILE* pf) override;
private:
//! The value.
int m_val;
//! The name if given by DefineNum.
CBotString m_defnum;
friend class CBotVar;
};

View File

@ -61,6 +61,7 @@ set(SOURCES
CBotVar/CBotVarBoolean.cpp
CBotVar/CBotVarString.cpp
CBotVar/CBotVarFloat.cpp
CBotVar/CBotVarInt.cpp
)
# Includes