From df111dbf98eb76395e7d0c1b385120bb16620487 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sat, 28 May 2016 22:59:34 +0200 Subject: [PATCH] Refactored CBotVar to templates --- src/CBot/CBotVar/CBotVar.cpp | 17 +- src/CBot/CBotVar/CBotVar.h | 5 + src/CBot/CBotVar/CBotVarBoolean.cpp | 118 +----------- src/CBot/CBotVar/CBotVarBoolean.h | 24 +-- src/CBot/CBotVar/CBotVarFloat.cpp | 196 ------------------- src/CBot/CBotVar/CBotVarFloat.h | 39 +--- src/CBot/CBotVar/CBotVarInt.cpp | 285 +++++----------------------- src/CBot/CBotVar/CBotVarInt.h | 36 +--- src/CBot/CBotVar/CBotVarString.cpp | 98 +--------- src/CBot/CBotVar/CBotVarString.h | 52 +++-- src/CBot/CBotVar/CBotVarValue.h | 177 +++++++++++++++++ src/CBot/CMakeLists.txt | 1 + 12 files changed, 308 insertions(+), 740 deletions(-) create mode 100644 src/CBot/CBotVar/CBotVarValue.h diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index 8876ded2..fc4503ea 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -61,6 +61,11 @@ CBotVar::CBotVar( ) m_mPrivate = ProtectionLevel::Public; } +CBotVar::CBotVar(const CBotToken &name) : CBotVar() +{ + m_token = new CBotToken(name); +} + //////////////////////////////////////////////////////////////////////////////// CBotVar::~CBotVar( ) { @@ -698,7 +703,16 @@ void CBotVar::Dec() //////////////////////////////////////////////////////////////////////////////// void CBotVar::Copy(CBotVar* pSrc, bool bName) { - assert(0); + if (bName) *m_token = *pSrc->m_token; + m_type = pSrc->m_type; + m_binit = pSrc->m_binit; +//- m_bStatic = pSrc->m_bStatic; + m_next = nullptr; + m_pMyThis = nullptr;//p->m_pMyThis; + m_pUserPtr = pSrc->m_pUserPtr; + + // keeps indentificator the same (by default) + if (m_ident == 0) m_ident = pSrc->m_ident; } //////////////////////////////////////////////////////////////////////////////// @@ -727,4 +741,5 @@ CBotClass* CBotVar::GetClass() return nullptr; } + } // namespace CBot diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index a4563b4b..be171783 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -50,6 +50,11 @@ public: */ CBotVar(); + /** + * \brief Constructor. Do not call directly, use CBotVar::Create() + */ + CBotVar(const CBotToken& name); + /** * \brief Destructor. Do not call directly, use CBotVar::Destroy() */ diff --git a/src/CBot/CBotVar/CBotVarBoolean.cpp b/src/CBot/CBotVar/CBotVarBoolean.cpp index 3a40772a..c9a9b52e 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.cpp +++ b/src/CBot/CBotVar/CBotVarBoolean.cpp @@ -19,137 +19,27 @@ #include "CBot/CBotVar/CBotVarBoolean.h" -#include "CBot/CBotEnums.h" -#include "CBot/CBotUtils.h" - -#include "CBot/CBotToken.h" - namespace CBot { -//////////////////////////////////////////////////////////////////////////////// -CBotVarBoolean::CBotVarBoolean(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 = CBotTypBoolean; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = ProtectionLevel::Public; - m_val = 0; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarBoolean::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarBoolean* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - 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 CBotVarBoolean::SetValInt(int val, const std::string& s) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarBoolean::SetValFloat(float val) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotVarBoolean::GetValInt() -{ - return m_val; -} - -//////////////////////////////////////////////////////////////////////////////// -float CBotVarBoolean::GetValFloat() -{ - return static_cast(m_val); -} - -//////////////////////////////////////////////////////////////////////////////// -std::string CBotVarBoolean::GetValString() -{ - std::string ret; - - std::string 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; - } - - ret = LoadString( m_val > 0 ? ID_TRUE : ID_FALSE ); - return ret; -} - -//////////////////////////////////////////////////////////////////////////////// void CBotVarBoolean::And(CBotVar* left, CBotVar* right) { - m_val = left->GetValInt() && right->GetValInt(); - m_binit = CBotVar::InitType::DEF; + SetValInt(left->GetValInt() && right->GetValInt()); } - -//////////////////////////////////////////////////////////////////////////////// void CBotVarBoolean::Or(CBotVar* left, CBotVar* right) { - m_val = left->GetValInt() || right->GetValInt(); - m_binit = CBotVar::InitType::DEF; + SetValInt(left->GetValInt() || right->GetValInt()); } - -//////////////////////////////////////////////////////////////////////////////// void CBotVarBoolean::XOr(CBotVar* left, CBotVar* right) { - m_val = left->GetValInt() ^ right->GetValInt(); - m_binit = CBotVar::InitType::DEF; + SetValInt(left->GetValInt() ^ right->GetValInt()); } - -//////////////////////////////////////////////////////////////////////////////// void CBotVarBoolean::Not() { - m_val = m_val ? false : true ; + SetValInt(!GetValInt()); } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarBoolean::Eq(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() == right->GetValInt(); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarBoolean::Ne(CBotVar* left, CBotVar* right) -{ - return left->GetValInt() != right->GetValInt(); -} - -//////////////////////////////////////////////////////////////////////////////// bool CBotVarBoolean::Save1State(FILE* pf) { return WriteWord(pf, m_val); // the value of the variable diff --git a/src/CBot/CBotVar/CBotVarBoolean.h b/src/CBot/CBotVar/CBotVarBoolean.h index 6fee80ae..34f627bc 100644 --- a/src/CBot/CBotVar/CBotVarBoolean.h +++ b/src/CBot/CBotVar/CBotVarBoolean.h @@ -19,7 +19,7 @@ #pragma once -#include "CBot/CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVarValue.h" namespace CBot { @@ -27,35 +27,17 @@ namespace CBot /** * \brief CBotVar subclass for managing boolean values (::CBotTypBoolean) */ -class CBotVarBoolean : public CBotVar +class CBotVarBoolean : public CBotVarNumberBase { public: - /** - * \brief Constructor. Do not call directly, use CBotVar::Create() - */ - CBotVarBoolean(const CBotToken& name); - - void SetValInt(int val, const std::string& s = nullptr) override; - void SetValFloat(float val) override; - int GetValInt() override; - float GetValFloat() override; - std::string GetValString() override; - - void Copy(CBotVar* pSrc, bool bName = true) override; + CBotVarBoolean(const CBotToken &name) : CBotVarNumberBase(name) {} void And(CBotVar* left, CBotVar* right) override; void Or(CBotVar* left, CBotVar* right) override; void XOr(CBotVar* left, CBotVar* right) override; void Not() override; - bool Eq(CBotVar* left, CBotVar* right) override; - bool Ne(CBotVar* left, CBotVar* right) override; - bool Save1State(FILE* pf) override; - -private: - //! The value. - bool m_val; }; } // namespace CBot diff --git a/src/CBot/CBotVar/CBotVarFloat.cpp b/src/CBot/CBotVar/CBotVarFloat.cpp index ca178a3a..ae90966a 100644 --- a/src/CBot/CBotVar/CBotVarFloat.cpp +++ b/src/CBot/CBotVar/CBotVarFloat.cpp @@ -19,205 +19,9 @@ #include "CBot/CBotVar/CBotVarFloat.h" -#include "CBot/CBotEnums.h" -#include "CBot/CBotToken.h" - -#include "CBot/CBotUtils.h" - -#include - namespace CBot { -//////////////////////////////////////////////////////////////////////////////// -CBotVarFloat::CBotVarFloat(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 = CBotTypFloat; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = ProtectionLevel::Public; - - m_val = 0; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarFloat* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - 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 CBotVarFloat::SetValInt(int val, const std::string& s) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::SetValFloat(float val) -{ - m_val = val; - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotVarFloat::GetValInt() -{ - return static_cast(m_val); -} - -//////////////////////////////////////////////////////////////////////////////// -float CBotVarFloat::GetValFloat() -{ - return m_val; -} - -//////////////////////////////////////////////////////////////////////////////// -std::string CBotVarFloat::GetValString() -{ - std::string res; - - if ( m_binit == CBotVar::InitType::UNDEF ) - { - return LoadString(TX_UNDEF); - } - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - return LoadString(TX_NAN); - } - - char buffer[300]; - sprintf(buffer, "%.2f", m_val); - res = buffer; - - return res; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Mul(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValFloat() * right->GetValFloat(); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Power(CBotVar* left, CBotVar* right) -{ - m_val = static_cast(pow( left->GetValFloat() , right->GetValFloat() )); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotError CBotVarFloat::Div(CBotVar* left, CBotVar* right) -{ - float r = right->GetValFloat(); - if ( r != 0 ) - { - m_val = left->GetValFloat() / r; - m_binit = CBotVar::InitType::DEF; - } - return ( r == 0 ? CBotErrZeroDiv : CBotNoErr ); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotError CBotVarFloat::Modulo(CBotVar* left, CBotVar* right) -{ - float r = right->GetValFloat(); - if ( r != 0 ) - { - m_val = static_cast(fmod( left->GetValFloat() , r )); - m_binit = CBotVar::InitType::DEF; - } - return ( r == 0 ? CBotErrZeroDiv : CBotNoErr ); -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Add(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValFloat() + right->GetValFloat(); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Sub(CBotVar* left, CBotVar* right) -{ - m_val = left->GetValFloat() - right->GetValFloat(); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Neg() -{ - m_val = -m_val; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Inc() -{ - m_val++; -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarFloat::Dec() -{ - m_val--; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Lo(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() < right->GetValFloat(); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Hi(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() > right->GetValFloat(); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Ls(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() <= right->GetValFloat(); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Hs(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() >= right->GetValFloat(); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Eq(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() == right->GetValFloat(); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarFloat::Ne(CBotVar* left, CBotVar* right) -{ - return left->GetValFloat() != right->GetValFloat(); -} - -//////////////////////////////////////////////////////////////////////////////// bool CBotVarFloat::Save1State(FILE* pf) { return WriteFloat(pf, m_val); // the value of the variable diff --git a/src/CBot/CBotVar/CBotVarFloat.h b/src/CBot/CBotVar/CBotVarFloat.h index 0e294867..84324e60 100644 --- a/src/CBot/CBotVar/CBotVarFloat.h +++ b/src/CBot/CBotVar/CBotVarFloat.h @@ -19,7 +19,7 @@ #pragma once -#include "CBot/CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVarValue.h" namespace CBot { @@ -27,45 +27,12 @@ namespace CBot /** * \brief CBotVar subclass for managing float values (::CBotTypFloat) */ -class CBotVarFloat : public CBotVar +class CBotVarFloat : public CBotVarNumber { public: - /** - * \brief Constructor. Do not call directly, use CBotVar::Create() - */ - CBotVarFloat(const CBotToken& name); - - void SetValInt(int val, const std::string& s = nullptr) override; - void SetValFloat(float val) override; - int GetValInt() override; - float GetValFloat() override; - std::string GetValString() override; - - void Copy(CBotVar* pSrc, bool bName = true) override; - - void Add(CBotVar* left, CBotVar* right) override; - void Sub(CBotVar* left, CBotVar* right) override; - void Mul(CBotVar* left, CBotVar* right) override; - CBotError Div(CBotVar* left, CBotVar* right) override; - CBotError Modulo(CBotVar* left, CBotVar* right) override; - void Power(CBotVar* left, CBotVar* right) override; - - 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 Neg() override; - void Inc() override; - void Dec() override; + CBotVarFloat(const CBotToken &name) : CBotVarNumber(name) {} bool Save1State(FILE* pf) override; - -private: - //! The value. - float m_val; }; } // namespace CBot diff --git a/src/CBot/CBotVar/CBotVarInt.cpp b/src/CBot/CBotVar/CBotVarInt.cpp index 00704903..29715c9b 100644 --- a/src/CBot/CBotVar/CBotVarInt.cpp +++ b/src/CBot/CBotVar/CBotVarInt.cpp @@ -19,276 +19,93 @@ #include "CBot/CBotVar/CBotVarInt.h" -#include "CBot/CBotEnums.h" -#include "CBot/CBotToken.h" -#include "CBot/CBotUtils.h" - -#include - namespace CBot { -//////////////////////////////////////////////////////////////////////////////// -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 = ProtectionLevel::Public; - - m_val = 0; -} - -//////////////////////////////////////////////////////////////////////////////// void CBotVarInt::Copy(CBotVar* pSrc, bool bName) { - CBotVarInt* p = static_cast(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; + CBotVarNumber::Copy(pSrc, bName); + CBotVarInt* p = static_cast(pSrc); + m_defnum = p->m_defnum; } -//////////////////////////////////////////////////////////////////////////////// void CBotVarInt::SetValInt(int val, const std::string& defnum) { - m_val = val; - m_binit = CBotVar::InitType::DEF; + CBotVarNumber::SetValInt(val, defnum); m_defnum = defnum; } -//////////////////////////////////////////////////////////////////////////////// -void CBotVarInt::SetValFloat(float val) -{ - m_val = static_cast(val); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -int CBotVarInt::GetValInt() -{ - return m_val; -} - -//////////////////////////////////////////////////////////////////////////////// -float CBotVarInt::GetValFloat() -{ - return static_cast(m_val); -} - -//////////////////////////////////////////////////////////////////////////////// std::string CBotVarInt::GetValString() { - if ( !m_defnum.empty() ) return m_defnum; - - std::string res; - - if ( m_binit == CBotVar::InitType::UNDEF ) - { - return LoadString(TX_UNDEF); - } - - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - return LoadString(TX_NAN); - } - - char buffer[300]; - sprintf(buffer, "%d", m_val); - res = buffer; - - return res; + if (!m_defnum.empty()) return m_defnum; + return CBotVarValue::GetValString(); } -//////////////////////////////////////////////////////////////////////////////// -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( pow( static_cast(left->GetValInt()) , static_cast(right->GetValInt()) )); - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -CBotError 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 ? CBotErrZeroDiv : CBotNoErr ); -} - -//////////////////////////////////////////////////////////////////////////////// -CBotError 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 ? CBotErrZeroDiv : CBotNoErr ); -} - -//////////////////////////////////////////////////////////////////////////////// -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; + CBotVarNumber::Neg(); + m_defnum.empty(); +} +void CBotVarInt::Inc() +{ + CBotVarNumber::Inc(); + m_defnum.empty(); +} +void CBotVarInt::Dec() +{ + CBotVarNumber::Dec(); + m_defnum.empty(); +} + +void CBotVarInt::XOr(CBotVar* left, CBotVar* right) +{ + SetValInt(left->GetValInt() ^ right->GetValInt()); +} +void CBotVarInt::And(CBotVar* left, CBotVar* right) +{ + SetValInt(left->GetValInt() & right->GetValInt()); +} +void CBotVarInt::Or(CBotVar* left, CBotVar* right) +{ + SetValInt(left->GetValInt() | right->GetValInt()); +} + +void CBotVarInt::SL(CBotVar* left, CBotVar* right) +{ + SetValInt(left->GetValInt() << right->GetValInt()); +} +void CBotVarInt::ASR(CBotVar* left, CBotVar* right) +{ + SetValInt(left->GetValInt() >> right->GetValInt()); +} +void CBotVarInt::SR(CBotVar* left, CBotVar* right) +{ + int source = left->GetValInt(); + int shift = right->GetValInt(); + if (shift >= 1) source &= 0x7fffffff; + SetValInt(source >> shift); } -//////////////////////////////////////////////////////////////////////////////// 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.empty() ) + if (!m_defnum.empty()) { - if(!WriteWord(pf, 200 )) return false; // special marker - if(!WriteString(pf, m_defnum)) return false; // name of the value + if(!WriteWord(pf, 200)) return false; // special marker + if(!WriteString(pf, m_defnum)) return false; } return CBotVar::Save0State(pf); } -//////////////////////////////////////////////////////////////////////////////// bool CBotVarInt::Save1State(FILE* pf) { - return WriteWord(pf, m_val); // the value of the variable + return WriteWord(pf, m_val); } } // namespace CBot diff --git a/src/CBot/CBotVar/CBotVarInt.h b/src/CBot/CBotVar/CBotVarInt.h index 8d7d4f97..32d0af0b 100644 --- a/src/CBot/CBotVar/CBotVarInt.h +++ b/src/CBot/CBotVar/CBotVarInt.h @@ -19,7 +19,7 @@ #pragma once -#include "CBot/CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVarValue.h" namespace CBot { @@ -27,35 +27,19 @@ namespace CBot /** * \brief CBotVar subclass for managing integer values (::CBotTypInt) */ -class CBotVarInt : public CBotVar +class CBotVarInt : public CBotVarNumber { public: - /** - * \brief Constructor. Do not call directly, use CBotVar::Create() - */ - CBotVarInt(const CBotToken& name); + CBotVarInt(const CBotToken &name) : CBotVarNumber(name) {} void SetValInt(int val, const std::string& s = "") override; - void SetValFloat(float val) override; - int GetValInt() override; - float GetValFloat() override; std::string GetValString() override; void Copy(CBotVar* pSrc, bool bName = true) override; - void Add(CBotVar* left, CBotVar* right) override; - void Sub(CBotVar* left, CBotVar* right) override; - void Mul(CBotVar* left, CBotVar* right) override; - CBotError Div(CBotVar* left, CBotVar* right) override; - CBotError Modulo(CBotVar* left, CBotVar* right) override; - void Power(CBotVar* left, CBotVar* right) override; - - 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 Neg() override; + void Inc() override; + void Dec() override; void XOr(CBotVar* left, CBotVar* right) override; void Or(CBotVar* left, CBotVar* right) override; @@ -66,16 +50,10 @@ public: void SR(CBotVar* left, CBotVar* right) override; void ASR(CBotVar* left, CBotVar* right) override; - void Neg() override; - void Inc() override; - void Dec() override; - bool Save0State(FILE* pf) override; bool Save1State(FILE* pf) override; -private: - //! The value. - int m_val; +protected: //! The name if given by DefineNum. std::string m_defnum; friend class CBotVar; diff --git a/src/CBot/CBotVar/CBotVarString.cpp b/src/CBot/CBotVar/CBotVarString.cpp index 653badf6..a5f3bba6 100644 --- a/src/CBot/CBotVar/CBotVarString.cpp +++ b/src/CBot/CBotVar/CBotVarString.cpp @@ -19,117 +19,27 @@ #include "CBot/CBotVar/CBotVarString.h" -#include "CBot/CBotEnums.h" -#include "CBot/CBotToken.h" -#include "CBot/CBotUtils.h" - namespace CBot { -//////////////////////////////////////////////////////////////////////////////// -CBotVarString::CBotVarString(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 = CBotTypString; - m_binit = InitType::UNDEF; - m_bStatic = false; - m_mPrivate = ProtectionLevel::Public; - - m_val.clear(); -} - -//////////////////////////////////////////////////////////////////////////////// -void CBotVarString::Copy(CBotVar* pSrc, bool bName) -{ - CBotVarString* p = static_cast(pSrc); - - if (bName) *m_token = *p->m_token; - m_type = p->m_type; - m_val = p->m_val; - m_binit = p->m_binit; -//- m_bStatic = p->m_bStatic; - m_next = nullptr; - 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 CBotVarString::SetValString(const std::string& val) -{ - m_val = val; - m_binit = CBotVar::InitType::DEF; -} - -//////////////////////////////////////////////////////////////////////////////// -std::string CBotVarString::GetValString() -{ - if ( m_binit == CBotVar::InitType::UNDEF ) - { - return LoadString(TX_UNDEF); - } - if ( m_binit == CBotVar::InitType::IS_NAN ) - { - return LoadString(TX_NAN); - } - - return m_val; -} - -//////////////////////////////////////////////////////////////////////////////// void CBotVarString::Add(CBotVar* left, CBotVar* right) { - m_val = left->GetValString() + right->GetValString(); - m_binit = CBotVar::InitType::DEF; + SetValString(left->GetValString() + right->GetValString()); } -//////////////////////////////////////////////////////////////////////////////// bool CBotVarString::Eq(CBotVar* left, CBotVar* right) { - return (left->GetValString() == right->GetValString()); + return left->GetValString() == right->GetValString(); } -//////////////////////////////////////////////////////////////////////////////// bool CBotVarString::Ne(CBotVar* left, CBotVar* right) { - return (left->GetValString() != right->GetValString()); + return left->GetValString() != right->GetValString(); } -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarString::Lo(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarString::Hi(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarString::Ls(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -//////////////////////////////////////////////////////////////////////////////// -bool CBotVarString::Hs(CBotVar* left, CBotVar* right) -{ - return (left->GetValString() == right->GetValString()); -} - -//////////////////////////////////////////////////////////////////////////////// bool CBotVarString::Save1State(FILE* pf) { - return WriteString(pf, m_val); // the value of the variable + return WriteString(pf, m_val); } } // namespace CBot diff --git a/src/CBot/CBotVar/CBotVarString.h b/src/CBot/CBotVar/CBotVarString.h index 136294e5..5ff29d15 100644 --- a/src/CBot/CBotVar/CBotVarString.h +++ b/src/CBot/CBotVar/CBotVarString.h @@ -19,7 +19,7 @@ #pragma once -#include "CBot/CBotVar/CBotVar.h" +#include "CBot/CBotVar/CBotVarValue.h" namespace CBot { @@ -27,33 +27,55 @@ namespace CBot /** * \brief CBotVar subclass for managing string values (::CBotTypString) */ -class CBotVarString : public CBotVar +class CBotVarString : public CBotVarValue { public: - /** - * \brief Constructor. Do not call directly, use CBotVar::Create() - */ - CBotVarString(const CBotToken& name); + CBotVarString(const CBotToken &name) : CBotVarValue(name) {} - void SetValString(const std::string& val) override; - std::string GetValString() override; + void SetValInt(int val, const std::string& s = "") override + { + SetValString(ToString(val)); + } - void Copy(CBotVar* pSrc, bool bName = true) override; + void SetValFloat(float val) override + { + SetValString(ToString(val)); + } + + int GetValInt() + { + return FromString(GetValString()); + } + + float GetValFloat() + { + return FromString(GetValString()); + } void Add(CBotVar* left, CBotVar* right) override; - 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; bool Save1State(FILE* pf) override; private: - //! The value. - std::string m_val; + template + static std::string ToString(T val) + { + std::ostringstream ss; + ss << val; + return ss.str(); + } + + template + static T FromString(std::string val) + { + std::istringstream ss(val); + T v; + ss >> v; + return v; + } }; } // namespace CBot diff --git a/src/CBot/CBotVar/CBotVarValue.h b/src/CBot/CBotVar/CBotVarValue.h new file mode 100644 index 00000000..3e184d7b --- /dev/null +++ b/src/CBot/CBotVar/CBotVarValue.h @@ -0,0 +1,177 @@ +#pragma once + +#include "CBot/CBotVar/CBotVar.h" + +#include "CBot/CBotEnums.h" +#include "CBot/CBotToken.h" + +#include +#include + + +namespace CBot +{ + +/** + * \brief A variable holding a simple value (bool, int, float, string) + */ +template +class CBotVarValue : public CBotVar +{ +public: + /** + * \brief Constructor. Do not call directly, use CBotVar::Create() + */ + CBotVarValue(const CBotToken& name) : CBotVar(name) + { + m_type = type; + } + + void Copy(CBotVar* pSrc, bool bName = true) override + { + CBotVar::Copy(pSrc, bName); + + CBotVarValue* p = static_cast(pSrc); + m_val = p->m_val; + } + + + void SetValString(const std::string& val) override + { + std::istringstream s(val); + s >> m_val; + m_binit = CBotVar::InitType::DEF; + } + + std::string GetValString() override + { + if (m_binit == CBotVar::InitType::UNDEF) + return LoadString(TX_UNDEF); + if (m_binit == CBotVar::InitType::IS_NAN) + return LoadString(TX_NAN); + + std::ostringstream s; + s << m_val; + return s.str(); + } + +protected: + //! The value + T m_val; +}; + +/** + * \brief A number based variable (bool, int, float) + */ +template +class CBotVarNumberBase : public CBotVarValue +{ +public: + CBotVarNumberBase(const CBotToken &name) : CBotVarValue(name) {} + + void SetValInt(int val, const std::string &s = "") override + { + this->m_val = static_cast(val); + this->m_binit = CBotVar::InitType::DEF; + } + + void SetValFloat(float val) override + { + this->m_val = static_cast(val); + this->m_binit = CBotVar::InitType::DEF; + } + + int GetValInt() override + { + return static_cast(this->m_val); + } + + float GetValFloat() override + { + return static_cast(this->m_val); + } + + + bool Eq(CBotVar* left, CBotVar* right) override + { + return left->GetValFloat() == right->GetValFloat(); + } + bool Ne(CBotVar* left, CBotVar* right) override + { + return left->GetValFloat() != right->GetValFloat(); + } +}; + +/** + * \brief A number variable (int, float) + */ +template +class CBotVarNumber : public CBotVarNumberBase +{ +public: + CBotVarNumber(const CBotToken &name) : CBotVarNumberBase(name) {} + + void Mul(CBotVar* left, CBotVar* right) override + { + this->SetValFloat(left->GetValFloat() * right->GetValFloat()); + } + void Power(CBotVar* left, CBotVar* right) override + { + this->SetValFloat(pow(left->GetValFloat(), right->GetValFloat())); + } + CBotError Div(CBotVar* left, CBotVar* right) override + { + float r = right->GetValFloat(); + if (r == 0) return CBotErrZeroDiv; + this->SetValFloat(left->GetValFloat() / r); + return CBotNoErr; + } + CBotError Modulo(CBotVar* left, CBotVar* right) override + { + float r = right->GetValFloat(); + if (r == 0) return CBotErrZeroDiv; + this->SetValFloat(fmod(left->GetValFloat(), r)); + return CBotNoErr; + } + void Add(CBotVar* left, CBotVar* right) override + { + this->SetValFloat(left->GetValFloat() + right->GetValFloat()); + } + void Sub(CBotVar* left, CBotVar* right) override + { + this->SetValFloat(left->GetValFloat() - right->GetValFloat()); + } + + void Neg() override + { + this->m_val = - this->m_val; + } + void Inc() override + { + this->m_val++; + } + void Dec() override + { + this->m_val--; + } + + bool Lo(CBotVar* left, CBotVar* right) override + { + return left->GetValFloat() < right->GetValFloat(); + } + bool Hi(CBotVar* left, CBotVar* right) override + { + return left->GetValFloat() > right->GetValFloat(); + } + bool Ls(CBotVar* left, CBotVar* right) override + { + return left->GetValFloat() <= right->GetValFloat(); + } + bool Hs(CBotVar* left, CBotVar* right) override + { + return left->GetValFloat() >= right->GetValFloat(); + } +}; + +} + diff --git a/src/CBot/CMakeLists.txt b/src/CBot/CMakeLists.txt index dbd3d02e..b77d0bbc 100644 --- a/src/CBot/CMakeLists.txt +++ b/src/CBot/CMakeLists.txt @@ -122,6 +122,7 @@ set(SOURCES CBotUtils.h CBotVar/CBotVar.cpp CBotVar/CBotVar.h + CBotVar/CBotVarValue.h CBotVar/CBotVarArray.cpp CBotVar/CBotVarArray.h CBotVar/CBotVarBoolean.cpp